lawspec 0.22.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ // Prices the application considers representative: 1.00 to 2.00 euros.
2
+ namespace Domain;
3
+
4
+ using System.Numerics;
5
+ using FsCheck;
6
+ using FsCheck.Fluent;
7
+ using LawSpec;
8
+ using LawSpec.Data;
9
+
10
+ public static class PaymentGenerators
11
+ {
12
+ public static Arbitrary<Money> Prices() =>
13
+ Arb.From(Gen.Choose(100, 200).Select(cents => new Money(new ExactDecimal(new BigInteger(cents), -2), Currency.EUR)));
14
+ }
@@ -0,0 +1,167 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "fsharp",
10
+ "root": ".",
11
+ "nativeBindings": {
12
+ "types": [
13
+ {
14
+ "type": "example.payments::type::Currency",
15
+ "native": [
16
+ "Domain",
17
+ "PaymentsDomain",
18
+ "CurrencyCode"
19
+ ],
20
+ "constructors": [
21
+ {
22
+ "constructor": "USD",
23
+ "native": [
24
+ "Domain",
25
+ "PaymentsDomain",
26
+ "CurrencyCode",
27
+ "Dollars"
28
+ ],
29
+ "style": "unit",
30
+ "fields": []
31
+ },
32
+ {
33
+ "constructor": "EUR",
34
+ "native": [
35
+ "Domain",
36
+ "PaymentsDomain",
37
+ "CurrencyCode",
38
+ "Euros"
39
+ ],
40
+ "style": "unit",
41
+ "fields": []
42
+ },
43
+ {
44
+ "constructor": "GBP",
45
+ "native": [
46
+ "Domain",
47
+ "PaymentsDomain",
48
+ "CurrencyCode",
49
+ "Pounds"
50
+ ],
51
+ "style": "unit",
52
+ "fields": []
53
+ }
54
+ ]
55
+ },
56
+ {
57
+ "type": "example.payments::type::Money",
58
+ "native": [
59
+ "Domain",
60
+ "PaymentsDomain",
61
+ "Price"
62
+ ],
63
+ "constructors": [
64
+ {
65
+ "constructor": "Money",
66
+ "native": [
67
+ "Domain",
68
+ "PaymentsDomain",
69
+ "Price"
70
+ ],
71
+ "style": "record",
72
+ "fields": [
73
+ {
74
+ "field": "amount",
75
+ "native": "major"
76
+ },
77
+ {
78
+ "field": "currency",
79
+ "native": "unit"
80
+ }
81
+ ]
82
+ }
83
+ ]
84
+ },
85
+ {
86
+ "type": "example.payments::type::Payment",
87
+ "native": [
88
+ "Domain",
89
+ "PaymentsDomain",
90
+ "PaymentStatus"
91
+ ],
92
+ "constructors": [
93
+ {
94
+ "constructor": "Paid",
95
+ "native": [
96
+ "Domain",
97
+ "PaymentsDomain",
98
+ "PaymentStatus",
99
+ "Settled"
100
+ ],
101
+ "style": "variant",
102
+ "fields": [
103
+ {
104
+ "field": "value",
105
+ "native": "price"
106
+ }
107
+ ]
108
+ },
109
+ {
110
+ "constructor": "Declined",
111
+ "native": [
112
+ "Domain",
113
+ "PaymentsDomain",
114
+ "PaymentStatus",
115
+ "Rejected"
116
+ ],
117
+ "style": "variant",
118
+ "fields": [
119
+ {
120
+ "field": "reason",
121
+ "native": "explanation"
122
+ }
123
+ ]
124
+ }
125
+ ]
126
+ }
127
+ ],
128
+ "functions": [
129
+ {
130
+ "declaration": "example.payments::addFee",
131
+ "native": [
132
+ "Domain",
133
+ "PaymentsDomain",
134
+ "apply_fee"
135
+ ]
136
+ },
137
+ {
138
+ "declaration": "example.payments::roundTrip",
139
+ "native": [
140
+ "Domain",
141
+ "PaymentsDomain",
142
+ "restore"
143
+ ]
144
+ },
145
+ {
146
+ "declaration": "example.payments::archive",
147
+ "native": [
148
+ "Domain",
149
+ "PaymentsDomain",
150
+ "store"
151
+ ]
152
+ }
153
+ ],
154
+ "generators": [
155
+ {
156
+ "type": "example.payments::type::Money",
157
+ "factory": [
158
+ "Domain",
159
+ "PaymentGenerators",
160
+ "prices"
161
+ ]
162
+ }
163
+ ]
164
+ }
165
+ }
166
+ ]
167
+ }
@@ -0,0 +1,23 @@
1
+ /// Application types, independent of generated LawSpec domain declarations.
2
+ module Domain.PaymentsDomain
3
+
4
+ open LawSpec
5
+
6
+ type CurrencyCode =
7
+ | Dollars
8
+ | Euros
9
+ | Pounds
10
+
11
+ type Price = { major: ExactDecimal; unit: CurrencyCode }
12
+
13
+ type PaymentStatus =
14
+ | Settled of price: Price
15
+ | Rejected of explanation: string
16
+
17
+ let private fee: ExactDecimal = { Coefficient = 2I; Exponent = -1 }
18
+
19
+ let apply_fee (price: Price) : Price = { price with major = price.major + fee }
20
+
21
+ let restore (payment: PaymentStatus) : PaymentStatus = payment
22
+
23
+ let store (payments: PaymentStatus option list) : PaymentStatus option list = payments
@@ -0,0 +1,11 @@
1
+ /// Prices the application considers representative: 1.00 to 2.00 euros.
2
+ module Domain.PaymentGenerators
3
+
4
+ open FsCheck
5
+ open FsCheck.FSharp
6
+ open Domain.PaymentsDomain
7
+
8
+ let prices () : Arbitrary<Price> =
9
+ Gen.choose (100, 200)
10
+ |> Gen.map (fun cents -> { major = { Coefficient = bigint cents; Exponent = -2 }; unit = Euros })
11
+ |> Arb.fromGen
@@ -144,3 +144,47 @@ law `a sized queue keeps arrival order` is
144
144
  `for all` (x :: Int8) (y :: Int8) . prelude.sizedFront (prelude.sizedEnqueue y (prelude.sizedEnqueue x SizedQueueEmpty)) = x
145
145
  end
146
146
  end
147
+
148
+ -- Text keys and items order by code point on every target. U+1F600 follows
149
+ -- U+FF21 by code point, though in UTF-16 its first surrogate (U+D83D)
150
+ -- precedes U+FF21, so a target ordering text by UTF-16 unit fails here.
151
+
152
+ -- The items of a set of words, in the set's order.
153
+ definition orderedItems (s :: Set Text) :: List Text is
154
+ match s with
155
+ | SetItems xs -> xs
156
+ end
157
+ end
158
+
159
+ -- The distinct words, in the order of the keys that count them.
160
+ definition countedKeys (words :: List Text) :: List Text is
161
+ prelude.keys (countWords words (prelude.keyValOf []))
162
+ end
163
+
164
+ law `text keys and items order by code point` is
165
+ definition is
166
+ `for all` (words :: List Text) . countedKeys words = orderedItems (prelude.setOf words)
167
+ end
168
+ example `a fullwidth letter precedes an emoji` is
169
+ words = ["😀", "A", "😀"]
170
+ expect countedKeys words = ["A", "😀"]
171
+ expect orderedItems (prelude.setOf words) = ["A", "😀"]
172
+ end
173
+ end
174
+
175
+ -- How one set of words orders against another.
176
+ definition compareSets (s :: Set Text) (t :: Set Text) :: Ordering is
177
+ prelude.compare s t
178
+ end
179
+
180
+ law `sets of text order as their items do` is
181
+ definition is
182
+ `for all` (xs :: List Text) (ys :: List Text) .
183
+ compareSets (prelude.setOf xs) (prelude.setOf ys) = prelude.compare (orderedItems (prelude.setOf xs)) (orderedItems (prelude.setOf ys))
184
+ end
185
+ example `a set led by a fullwidth letter precedes one led by a later letter` is
186
+ xs = ["😀", "A"]
187
+ ys = ["B"]
188
+ expect compareSets (prelude.setOf xs) (prelude.setOf ys) = Less
189
+ end
190
+ end
@@ -0,0 +1,110 @@
1
+ unit example.composition
2
+
3
+ -- A log store seen through two abilities, Logs and Reader. One spec handler
4
+ -- answers both from one state, and a set of handlers runs every law over
5
+ -- the spec handlers together. The native handlers share a Store: each case
6
+ -- opens one, and every native handler of the case is made with it.
7
+
8
+ type Problem is | UnknownLog | EmptyRecord end
9
+
10
+ type Log is
11
+ Log
12
+ records :: List Text
13
+ appends :: Int64
14
+ end
15
+
16
+ handle Store
17
+
18
+ openStore :: Unit -> Store
19
+ closeStore :: Store -> Unit
20
+
21
+ resource Store is
22
+ acquire is openStore unitValue end
23
+ release store is closeStore store end
24
+ end
25
+
26
+ ability Logs with Store is
27
+ create :: Text -> Unit
28
+ append :: Text -> Text -> Int64 fails with Problem
29
+ laws
30
+ law `a new log takes its first record at offset zero` is
31
+ definition is `for all` (t :: Text) (r :: Text where r != "") . (create t; append t r) = 0 end
32
+ end
33
+ law `an unknown log refuses records` is
34
+ definition is `for all` (t :: Text) (r :: Text) . append t r fails with UnknownLog end
35
+ end
36
+ end
37
+
38
+ ability Reader with Store is
39
+ size :: Text -> Maybe Int64
40
+ laws
41
+ law `an unknown log has no size` is
42
+ definition is `for all` (t :: Text) . size t = Nothing end
43
+ end
44
+ end
45
+
46
+ -- A client over Logs: it keeps the offset of its last write.
47
+ ability Writer with Store is
48
+ write :: Text -> Text -> Bool
49
+ lastOffset :: Maybe Int64
50
+ laws
51
+ law `a write to a new log is read back` is
52
+ definition is
53
+ `for all` (t :: Text) (r :: Text where r != "") . (create t; write t r; size t) = Just 1
54
+ end
55
+ end
56
+ end
57
+
58
+ definition empty (u :: Unit) :: KeyVal Text Log is prelude.keyValOf [] end
59
+
60
+ -- One state for both abilities. It starts from a computed value, and the
61
+ -- clause for append updates it in one branch only.
62
+ handler store for Logs, Reader with state logs :: KeyVal Text Log start empty unitValue is
63
+ create t is ~logs := prelude.put t (Log [] 0) logs; unitValue end
64
+ append t r is
65
+ match prelude.lookup t logs with
66
+ | Nothing -> raise UnknownLog
67
+ | Just log ->
68
+ if r == "" then raise EmptyRecord else
69
+ (~logs := prelude.put t (log with records = Cons r log.records, appends = prelude.saturating (log.appends + 1)) logs;
70
+ log.appends)
71
+ end
72
+ end
73
+ size t is
74
+ match prelude.lookup t logs with
75
+ | Nothing -> Nothing
76
+ | Just log -> Just log.appends
77
+ end
78
+ end
79
+ end
80
+
81
+ -- A clause may be a definition that gives Pair result state: `op by f`.
82
+ definition writeTo (t :: Text) (r :: Text) (last :: Maybe Int64) :: Pair Bool (Maybe Int64) uses Logs is
83
+ match prelude.attempt (append t r) with
84
+ | Left problem -> Pair false last
85
+ | Right offset -> Pair true (Just offset)
86
+ end
87
+ end
88
+
89
+ definition lastOf (last :: Maybe Int64) :: Pair (Maybe Int64) (Maybe Int64) is Pair last last end
90
+
91
+ handler logWriter for Writer with state last :: Maybe Int64 start Nothing is
92
+ write by writeTo
93
+ lastOffset by lastOf
94
+ end
95
+
96
+ -- The reference composition: every ability answered by spec handlers.
97
+ handlers reference is store, logWriter end
98
+
99
+ law `sizes count records` is
100
+ definition is
101
+ `for all` (t :: Text) (a :: Text where a != "") (b :: Text where b != "") .
102
+ (create t; append t a; append t b; size t) = Just 2
103
+ end
104
+ end
105
+
106
+ law `a writer remembers where it wrote` is
107
+ definition is
108
+ `for all` (t :: Text) (r :: Text where r != "") . (create t; write t r; lastOffset) = Just 0
109
+ end
110
+ end
@@ -0,0 +1,124 @@
1
+ unit example.contracts
2
+
3
+ -- Shapes contract importers write: handlers whose clauses fail, handlers
4
+ -- over Bytes and Text, refined records, and handlers named like their
5
+ -- abilities.
6
+
7
+ type Refusal is | Negative | Empty end
8
+
9
+ definition bump (n :: Int64) (total :: Int64) :: Pair Int64 Int64 fails with Refusal is
10
+ if n < 0 then raise Negative else
11
+ if total > 1000000 || n > 1000000 then Pair total total else Pair (prelude.Int64 (total + n)) (prelude.Int64 (total + n))
12
+ end
13
+
14
+ -- A definition that may fail but does not.
15
+ definition peek (total :: Int64) :: Pair Int64 Int64 fails with Refusal is Pair total total end
16
+
17
+ ability Counter is
18
+ add :: Int64 -> Int64 fails with Refusal
19
+ current :: Int64 fails with Refusal
20
+ end
21
+
22
+ -- Named like its ability.
23
+ handler counter for Counter with state total :: Int64 start 0 is
24
+ add by bump
25
+ current by peek
26
+ end
27
+
28
+ -- A state with no typed form keeps this handler on the dynamic path.
29
+ type Scaled is Scaled count :: Int64 scale :: Float64 end
30
+
31
+ definition bumpScaled (n :: Int64) (s :: Scaled) :: Pair Int64 Scaled fails with Refusal is
32
+ if n < 0 then raise Negative else
33
+ if s.count > 1000000 || n > 1000000 then Pair s.count s else
34
+ Pair (prelude.Int64 (s.count + n)) (s with count = prelude.Int64 (s.count + n))
35
+ end
36
+
37
+ definition peekScaled (s :: Scaled) :: Pair Int64 Scaled fails with Refusal is Pair s.count s end
38
+
39
+ handler scaledCounter for Counter with state s :: Scaled start Scaled 0 1.0 is
40
+ add by bumpScaled
41
+ current by peekScaled
42
+ end
43
+
44
+ -- Bytes and Text inside handler clauses, on both paths.
45
+ ability Codec is
46
+ octetCount :: Text -> Int64
47
+ label :: Text -> Int32 -> Text
48
+ end
49
+
50
+ handler codec for Codec is
51
+ octetCount t is prelude.length (prelude.octets (prelude.utf8 t)) end
52
+ label t n is prelude.join "-" [t, prelude.textOf n] ++ prelude.slice 0 0 t end
53
+ end
54
+
55
+ handler scaledCodec for Codec with state s :: Float64 start 1.0 is
56
+ octetCount t is prelude.length (prelude.bytesOf (prelude.octets (prelude.utf8 t))) end
57
+ label t n is prelude.take (prelude.length t + 9) (t ++ "-" ++ prelude.textOf n) end
58
+ end
59
+
60
+ -- A failed operation leaves the handler's state as it was, on the typed
61
+ -- path (counter) and the dynamic one (scaledCounter).
62
+ law `a refused add changes nothing` using counter is
63
+ definition is
64
+ `for all` (n :: Int64 where n >= 0 && n <= 1000) .
65
+ (let refused = prelude.attempt (add (0 - 1)) in add n) = n
66
+ end
67
+ end
68
+
69
+ law `a refused add changes nothing, dynamically` using scaledCounter is
70
+ definition is
71
+ `for all` (n :: Int64 where n >= 0 && n <= 1000) .
72
+ (let refused = prelude.attempt (add (0 - 1)) in add n) = n
73
+ end
74
+ end
75
+
76
+ law `a text's octets count its UTF-8 bytes` using codec is
77
+ definition is
78
+ `for all` (n :: Int32 where n >= 0 && n <= 1000) .
79
+ octetCount (prelude.textOf n ++ "é") = prelude.length (prelude.textOf n) + 2
80
+ end
81
+ end
82
+
83
+ law `a text's octets count its UTF-8 bytes, dynamically` using scaledCodec is
84
+ definition is
85
+ `for all` (n :: Int32 where n >= 0 && n <= 1000) .
86
+ octetCount (prelude.textOf n ++ "é") = prelude.length (prelude.textOf n) + 2
87
+ end
88
+ end
89
+
90
+ law `a label joins its parts` using codec is
91
+ definition is label "member" 3 = "member-3" end
92
+ end
93
+
94
+ law `a label joins its parts, dynamically` using scaledCodec is
95
+ definition is label "member" 3 = "member-3" end
96
+ end
97
+
98
+ -- Refined records.
99
+ type Pet is
100
+ Pet
101
+ id :: Maybe Int64
102
+ name :: (v :: Text where v matches regex "[a-z]+")
103
+ end
104
+
105
+ definition validName (name :: Text) :: Bool is name matches regex "[a-z]+" end
106
+
107
+ type Badge is
108
+ Badge
109
+ label :: (v :: Text where validName v)
110
+ end
111
+
112
+ definition withId (body :: Pet) (key :: Int64) :: Pet is body with id = Just key end
113
+
114
+ definition validPet (p :: Pet) :: Bool is p.name matches regex "[a-z]{2,8}" end
115
+
116
+ law `an id is set by a record update` is
117
+ definition is
118
+ `for all` (p :: Pet where validPet p) (key :: Int64) . (withId p key).id = Just key and (withId p key).name = p.name
119
+ end
120
+ end
121
+
122
+ law `a badge keeps its label` is
123
+ definition is `for all` (t :: Badge) . validName t.label = true end
124
+ end
@@ -162,3 +162,14 @@ law `a share is at most the total` is
162
162
  expect share t n = 2
163
163
  end
164
164
  end
165
+
166
+ -- A text refined to be non-empty is drawn from its refinement, at every
167
+ -- size: four of them, one also unlike another, are met at the first case,
168
+ -- not filtered out of draws that are mostly empty while the size is small.
169
+ law `several non-empty texts are drawn` is
170
+ definition is
171
+ `for all` (a :: Text where a != "") (b :: Text where b != "") (c :: Text where c != "")
172
+ (d :: Text where d != "" && d != c) .
173
+ prelude.length a + prelude.length b + prelude.length c + prelude.length d >= 4
174
+ end
175
+ end