lawspec 0.21.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.
Files changed (39) hide show
  1. package/CHANGELOG.md +322 -2
  2. package/README.md +3 -3
  3. package/beam-coverage.mjs +160 -0
  4. package/beam-doctor.mjs +306 -0
  5. package/bin/lawspec.mjs +150 -41
  6. package/build.json +359 -101
  7. package/compatibility.json +25 -0
  8. package/core.wasm +2 -2
  9. package/doctor.mjs +29 -2
  10. package/examples/native-payments/csharp/lawspec.json +51 -0
  11. package/examples/native-payments/csharp/src/Domain/Payments.cs +19 -0
  12. package/examples/native-payments/csharp/tests/Native/PaymentGenerators.cs +14 -0
  13. package/examples/native-payments/elixir/lawspec.json +149 -0
  14. package/examples/native-payments/elixir/lib/payments_domain.ex +54 -0
  15. package/examples/native-payments/elixir/test/support/payment_generators.ex +9 -0
  16. package/examples/native-payments/erlang/include/payments_domain.hrl +2 -0
  17. package/examples/native-payments/erlang/lawspec.json +148 -0
  18. package/examples/native-payments/erlang/src/payments_domain.erl +21 -0
  19. package/examples/native-payments/erlang/test/payment_generators.erl +10 -0
  20. package/examples/native-payments/fsharp/lawspec.json +167 -0
  21. package/examples/native-payments/fsharp/src/Native/Types/PaymentsDomain.fs +23 -0
  22. package/examples/native-payments/fsharp/tests/Native/PaymentGenerators.fs +11 -0
  23. package/examples/native-payments/gleam/lawspec.json +149 -0
  24. package/examples/native-payments/gleam/prepare.mjs +48 -0
  25. package/examples/native-payments/gleam/src/payments_domain.gleam +32 -0
  26. package/examples/native-payments/gleam/test/payment_generators.gleam +13 -0
  27. package/examples/specs/collections.lawspec +44 -0
  28. package/examples/specs/composition.lawspec +110 -0
  29. package/examples/specs/contracts.lawspec +124 -0
  30. package/examples/specs/generation.lawspec +11 -0
  31. package/examples/specs/records.lawspec +191 -0
  32. package/examples/specs/records_inner.lawspec +10 -0
  33. package/examples/specs/sequences.lawspec +86 -0
  34. package/failure-database.mjs +46 -0
  35. package/index.d.ts +45 -1
  36. package/native-examples.mjs +7 -2
  37. package/package.json +2 -2
  38. package/templates.mjs +134 -3
  39. package/test-command.mjs +188 -17
@@ -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
@@ -0,0 +1,149 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "gleam",
10
+ "root": ".",
11
+ "nativeBindings": {
12
+ "types": [
13
+ {
14
+ "type": "example.payments::type::Currency",
15
+ "native": [
16
+ "payments_domain",
17
+ "CurrencyCode"
18
+ ],
19
+ "constructors": [
20
+ {
21
+ "constructor": "USD",
22
+ "native": [
23
+ "payments_domain",
24
+ "Dollars"
25
+ ],
26
+ "style": "unit",
27
+ "fields": []
28
+ },
29
+ {
30
+ "constructor": "EUR",
31
+ "native": [
32
+ "payments_domain",
33
+ "Euros"
34
+ ],
35
+ "style": "unit",
36
+ "fields": []
37
+ },
38
+ {
39
+ "constructor": "GBP",
40
+ "native": [
41
+ "payments_domain",
42
+ "Pounds"
43
+ ],
44
+ "style": "unit",
45
+ "fields": []
46
+ }
47
+ ]
48
+ },
49
+ {
50
+ "type": "example.payments::type::Money",
51
+ "native": [
52
+ "payments_domain",
53
+ "Price"
54
+ ],
55
+ "constructors": [
56
+ {
57
+ "constructor": "Money",
58
+ "native": [
59
+ "payments_domain",
60
+ "Price"
61
+ ],
62
+ "style": "record",
63
+ "fields": [
64
+ {
65
+ "field": "amount",
66
+ "native": "major"
67
+ },
68
+ {
69
+ "field": "currency",
70
+ "native": "unit"
71
+ }
72
+ ]
73
+ }
74
+ ]
75
+ },
76
+ {
77
+ "type": "example.payments::type::Payment",
78
+ "native": [
79
+ "payments_domain",
80
+ "PaymentStatus"
81
+ ],
82
+ "constructors": [
83
+ {
84
+ "constructor": "Paid",
85
+ "native": [
86
+ "payments_domain",
87
+ "Settled"
88
+ ],
89
+ "style": "variant",
90
+ "fields": [
91
+ {
92
+ "field": "value",
93
+ "native": "price"
94
+ }
95
+ ]
96
+ },
97
+ {
98
+ "constructor": "Declined",
99
+ "native": [
100
+ "payments_domain",
101
+ "Rejected"
102
+ ],
103
+ "style": "variant",
104
+ "fields": [
105
+ {
106
+ "field": "reason",
107
+ "native": "explanation"
108
+ }
109
+ ]
110
+ }
111
+ ]
112
+ }
113
+ ],
114
+ "functions": [
115
+ {
116
+ "declaration": "example.payments::addFee",
117
+ "native": [
118
+ "payments_domain",
119
+ "apply_fee"
120
+ ]
121
+ },
122
+ {
123
+ "declaration": "example.payments::roundTrip",
124
+ "native": [
125
+ "payments_domain",
126
+ "restore"
127
+ ]
128
+ },
129
+ {
130
+ "declaration": "example.payments::archive",
131
+ "native": [
132
+ "payments_domain",
133
+ "store"
134
+ ]
135
+ }
136
+ ],
137
+ "generators": [
138
+ {
139
+ "type": "example.payments::type::Money",
140
+ "factory": [
141
+ "payment_generators",
142
+ "prices"
143
+ ]
144
+ }
145
+ ]
146
+ }
147
+ }
148
+ ]
149
+ }
@@ -0,0 +1,48 @@
1
+ // Compile dependencies before LawSpec generates the runtime imported by the app.
2
+ import {execFile} from 'node:child_process';
3
+ import {promisify} from 'node:util';
4
+ import {cp, mkdir, mkdtemp, readFile, readdir, rm} from 'node:fs/promises';
5
+ import path from 'node:path';
6
+ import {fileURLToPath} from 'node:url';
7
+
8
+ const exec = promisify(execFile);
9
+ const root = path.dirname(fileURLToPath(import.meta.url));
10
+ await mkdir(path.join(root, '.lawspec'), {recursive: true});
11
+ const temporary = await mkdtemp(path.join(root, '.lawspec/dependencies-'));
12
+ try {
13
+ // Use the project's actual dependency requirements and lock file, with empty
14
+ // application roots. Application code remains in place throughout setup.
15
+ await mkdir(path.join(temporary, 'src'), {recursive: true});
16
+ await mkdir(path.join(temporary, 'test-support/src'), {recursive: true});
17
+ for (const file of ['gleam.toml', 'test-support/gleam.toml'])
18
+ await cp(path.join(root, file), path.join(temporary, file));
19
+ try { await cp(path.join(root, 'manifest.toml'), path.join(temporary, 'manifest.toml')); }
20
+ catch (error) { if (error.code !== 'ENOENT') throw error; }
21
+ // Reuse downloaded packages when available, including fully offline setups.
22
+ try { await cp(path.join(root, 'build/packages'), path.join(temporary, 'build/packages'), {recursive: true}); }
23
+ catch (error) { if (error.code !== 'ENOENT') throw error; }
24
+
25
+ const {stdout, stderr} = await exec('gleam', ['build'], {cwd: temporary,
26
+ timeout: 300000, maxBuffer: 8 * 1024 * 1024});
27
+ process.stdout.write(stdout);
28
+ process.stderr.write(stderr);
29
+ const information = path.join(temporary, 'package.json');
30
+ await exec('gleam', ['export', 'package-information', '--out', information], {cwd: temporary});
31
+ const application = JSON.parse(await readFile(information, 'utf8'))['gleam.toml'].name;
32
+
33
+ await mkdir(path.join(root, 'build/dev/erlang'), {recursive: true});
34
+ await cp(path.join(temporary, 'build/packages'), path.join(root, 'build/packages'), {recursive: true});
35
+ for (const entry of await readdir(path.join(temporary, 'build/dev/erlang'), {withFileTypes: true})) {
36
+ if (!entry.isDirectory() || [application, 'lawspec_test_support'].includes(entry.name)) continue;
37
+ await cp(path.join(temporary, 'build/dev/erlang', entry.name),
38
+ path.join(root, 'build/dev/erlang', entry.name), {recursive: true});
39
+ }
40
+ await cp(path.join(temporary, 'manifest.toml'), path.join(root, 'manifest.toml'));
41
+ console.log('Dependencies compiled. Run lawspec generate, then gleam test.');
42
+ } catch (error) {
43
+ process.stderr.write(error.stdout ?? '');
44
+ process.stderr.write(error.stderr ?? '');
45
+ throw error;
46
+ } finally {
47
+ await rm(temporary, {recursive: true, force: true});
48
+ }
@@ -0,0 +1,32 @@
1
+ //// Application-owned payment types; no generated domain declarations.
2
+ import gleam/option.{type Option}
3
+ import lawspec/scalar
4
+ import lawspec/types
5
+
6
+ pub type CurrencyCode {
7
+ Dollars
8
+ Euros
9
+ Pounds
10
+ }
11
+
12
+ // Field order deliberately differs from the specification and binding JSON.
13
+ pub type Price {
14
+ Price(unit: CurrencyCode, major: types.Decimal)
15
+ }
16
+
17
+ pub type PaymentStatus {
18
+ Settled(price: Price)
19
+ Rejected(explanation: String)
20
+ }
21
+
22
+ pub fn apply_fee(price: Price) -> Price {
23
+ Price(..price, major: scalar.decimal_add(price.major, scalar.decimal(2, -1)))
24
+ }
25
+
26
+ pub fn restore(payment: PaymentStatus) -> PaymentStatus {
27
+ payment
28
+ }
29
+
30
+ pub fn store(payments: List(Option(PaymentStatus))) -> List(Option(PaymentStatus)) {
31
+ payments
32
+ }
@@ -0,0 +1,13 @@
1
+ import lawspec/scalar
2
+ import payments_domain
3
+ import qcheck
4
+
5
+ // qcheck.map preserves the integer generator's native shrink tree.
6
+ pub fn prices() -> qcheck.Generator(payments_domain.Price) {
7
+ qcheck.map(qcheck.bounded_int(100, 200), fn(cents) {
8
+ payments_domain.Price(
9
+ unit: payments_domain.Euros,
10
+ major: scalar.decimal(cents, -2),
11
+ )
12
+ })
13
+ }
@@ -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