lawspec 0.22.0 → 0.24.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 (37) hide show
  1. package/CHANGELOG.md +358 -2
  2. package/README.md +3 -3
  3. package/bin/lawspec.mjs +95 -14
  4. package/build.json +314 -120
  5. package/compatibility.json +29 -0
  6. package/core.wasm +2 -2
  7. package/doctor.mjs +119 -1
  8. package/examples/native-payments/clojure/lawspec.json +51 -0
  9. package/examples/native-payments/clojure/src/domain/payments.clj +11 -0
  10. package/examples/native-payments/clojure/test/domain/payment_generators.clj +9 -0
  11. package/examples/native-payments/csharp/lawspec.json +51 -0
  12. package/examples/native-payments/csharp/src/Domain/Payments.cs +19 -0
  13. package/examples/native-payments/csharp/tests/Native/PaymentGenerators.cs +14 -0
  14. package/examples/native-payments/fsharp/lawspec.json +167 -0
  15. package/examples/native-payments/fsharp/src/Native/Types/PaymentsDomain.fs +23 -0
  16. package/examples/native-payments/fsharp/tests/Native/PaymentGenerators.fs +11 -0
  17. package/examples/native-payments/groovy/lawspec.json +165 -0
  18. package/examples/native-payments/groovy/src/main/groovy/domain/PaymentsDomain.groovy +49 -0
  19. package/examples/native-payments/groovy/src/test/groovy/domain/PaymentGenerators.groovy +15 -0
  20. package/examples/native-payments/scala/lawspec.json +165 -0
  21. package/examples/native-payments/scala/src/main/scala/domain/PaymentsDomain.scala +18 -0
  22. package/examples/native-payments/scala/src/test/scala/domain/PaymentGenerators.scala +8 -0
  23. package/examples/native-payments/swift/lawspec.json +154 -0
  24. package/examples/native-payments/swift/src/Native/PaymentsDomain.swift +35 -0
  25. package/examples/native-payments/swift/tests/Native/PaymentGenerators.swift +12 -0
  26. package/examples/specs/collections.lawspec +44 -0
  27. package/examples/specs/composition.lawspec +110 -0
  28. package/examples/specs/contracts.lawspec +124 -0
  29. package/examples/specs/generation.lawspec +11 -0
  30. package/examples/specs/records.lawspec +191 -0
  31. package/examples/specs/records_inner.lawspec +10 -0
  32. package/examples/specs/sequences.lawspec +86 -0
  33. package/index.d.ts +40 -1
  34. package/package.json +2 -2
  35. package/scala-coverage.mjs +56 -0
  36. package/templates.mjs +107 -3
  37. package/test-command.mjs +105 -4
@@ -0,0 +1,165 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "groovy",
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
+ "Settled"
99
+ ],
100
+ "style": "variant",
101
+ "fields": [
102
+ {
103
+ "field": "value",
104
+ "native": "price"
105
+ }
106
+ ]
107
+ },
108
+ {
109
+ "constructor": "Declined",
110
+ "native": [
111
+ "domain",
112
+ "PaymentsDomain",
113
+ "Rejected"
114
+ ],
115
+ "style": "variant",
116
+ "fields": [
117
+ {
118
+ "field": "reason",
119
+ "native": "explanation"
120
+ }
121
+ ]
122
+ }
123
+ ]
124
+ }
125
+ ],
126
+ "functions": [
127
+ {
128
+ "declaration": "example.payments::addFee",
129
+ "native": [
130
+ "domain",
131
+ "PaymentsDomain",
132
+ "apply_fee"
133
+ ]
134
+ },
135
+ {
136
+ "declaration": "example.payments::roundTrip",
137
+ "native": [
138
+ "domain",
139
+ "PaymentsDomain",
140
+ "restore"
141
+ ]
142
+ },
143
+ {
144
+ "declaration": "example.payments::archive",
145
+ "native": [
146
+ "domain",
147
+ "PaymentsDomain",
148
+ "store"
149
+ ]
150
+ }
151
+ ],
152
+ "generators": [
153
+ {
154
+ "type": "example.payments::type::Money",
155
+ "factory": [
156
+ "domain",
157
+ "PaymentGenerators",
158
+ "prices"
159
+ ]
160
+ }
161
+ ]
162
+ }
163
+ }
164
+ ]
165
+ }
@@ -0,0 +1,49 @@
1
+ package domain
2
+
3
+ import groovy.transform.CompileStatic
4
+ import lawspec.runtime.LawSpecRuntime.Maybe
5
+
6
+ /**
7
+ * Application types, independent of generated LawSpec domain declarations.
8
+ * The records write out their constructors and accessors, which the Java
9
+ * model reads through Groovy's joint-compilation stubs.
10
+ */
11
+ @CompileStatic
12
+ final class PaymentsDomain {
13
+ private PaymentsDomain() {}
14
+
15
+ enum CurrencyCode { Dollars, Euros, Pounds }
16
+
17
+ static record Price(BigDecimal major, CurrencyCode unit) {
18
+ Price(BigDecimal major, CurrencyCode unit) {
19
+ this.major = major
20
+ this.unit = unit
21
+ }
22
+
23
+ BigDecimal major() { major }
24
+
25
+ CurrencyCode unit() { unit }
26
+ }
27
+
28
+ static interface PaymentStatus {}
29
+
30
+ static record Settled(Price price) implements PaymentStatus {
31
+ Settled(Price price) { this.price = price }
32
+
33
+ Price price() { price }
34
+ }
35
+
36
+ static record Rejected(String explanation) implements PaymentStatus {
37
+ Rejected(String explanation) { this.explanation = explanation }
38
+
39
+ String explanation() { explanation }
40
+ }
41
+
42
+ static Price apply_fee(Price price) {
43
+ new Price(price.major() + new BigDecimal('0.2'), price.unit())
44
+ }
45
+
46
+ static PaymentStatus restore(PaymentStatus payment) { payment }
47
+
48
+ static List<Maybe<PaymentStatus>> store(List<Maybe<PaymentStatus>> payments) { payments }
49
+ }
@@ -0,0 +1,15 @@
1
+ package domain
2
+
3
+ import groovy.transform.CompileStatic
4
+ import org.jetbrains.jetCheck.Generator
5
+
6
+ @CompileStatic
7
+ final class PaymentGenerators {
8
+ private PaymentGenerators() {}
9
+
10
+ static Generator<PaymentsDomain.Price> prices() {
11
+ Generator.integers(100, 200).map { Integer cents ->
12
+ new PaymentsDomain.Price(new BigDecimal(BigInteger.valueOf(cents), 2), PaymentsDomain.CurrencyCode.Euros)
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,165 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "scala",
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
+ "Settled"
99
+ ],
100
+ "style": "variant",
101
+ "fields": [
102
+ {
103
+ "field": "value",
104
+ "native": "price"
105
+ }
106
+ ]
107
+ },
108
+ {
109
+ "constructor": "Declined",
110
+ "native": [
111
+ "domain",
112
+ "PaymentsDomain",
113
+ "Rejected"
114
+ ],
115
+ "style": "variant",
116
+ "fields": [
117
+ {
118
+ "field": "reason",
119
+ "native": "explanation"
120
+ }
121
+ ]
122
+ }
123
+ ]
124
+ }
125
+ ],
126
+ "functions": [
127
+ {
128
+ "declaration": "example.payments::addFee",
129
+ "native": [
130
+ "domain",
131
+ "PaymentsDomain",
132
+ "apply_fee"
133
+ ]
134
+ },
135
+ {
136
+ "declaration": "example.payments::roundTrip",
137
+ "native": [
138
+ "domain",
139
+ "PaymentsDomain",
140
+ "restore"
141
+ ]
142
+ },
143
+ {
144
+ "declaration": "example.payments::archive",
145
+ "native": [
146
+ "domain",
147
+ "PaymentsDomain",
148
+ "store"
149
+ ]
150
+ }
151
+ ],
152
+ "generators": [
153
+ {
154
+ "type": "example.payments::type::Money",
155
+ "factory": [
156
+ "domain",
157
+ "PaymentGenerators",
158
+ "prices"
159
+ ]
160
+ }
161
+ ]
162
+ }
163
+ }
164
+ ]
165
+ }
@@ -0,0 +1,18 @@
1
+ package domain
2
+
3
+ // Application-owned types; no generated LawSpec domain declarations.
4
+ object PaymentsDomain {
5
+ enum CurrencyCode {
6
+ case Dollars, Euros, Pounds
7
+ }
8
+
9
+ final case class Price(major: BigDecimal, unit: CurrencyCode)
10
+
11
+ sealed trait PaymentStatus
12
+ final case class Settled(price: Price) extends PaymentStatus
13
+ final case class Rejected(explanation: String) extends PaymentStatus
14
+
15
+ def apply_fee(price: Price): Price = Price(price.major + BigDecimal("0.2"), price.unit)
16
+ def restore(payment: PaymentStatus): PaymentStatus = payment
17
+ def store(payments: List[Option[PaymentStatus]]): List[Option[PaymentStatus]] = payments
18
+ }
@@ -0,0 +1,8 @@
1
+ package domain
2
+
3
+ import lawspec.testing.Arb
4
+
5
+ object PaymentGenerators {
6
+ def prices(): Arb[PaymentsDomain.Price] = Arb.int(100 to 200).map(cents =>
7
+ PaymentsDomain.Price(BigDecimal(BigInt(cents), 2), PaymentsDomain.CurrencyCode.Euros))
8
+ }
@@ -0,0 +1,154 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "swift",
10
+ "root": ".",
11
+ "nativeBindings": {
12
+ "types": [
13
+ {
14
+ "type": "example.payments::type::Currency",
15
+ "native": [
16
+ "PaymentsDomain",
17
+ "CurrencyCode"
18
+ ],
19
+ "constructors": [
20
+ {
21
+ "constructor": "USD",
22
+ "native": [
23
+ "PaymentsDomain",
24
+ "CurrencyCode",
25
+ "dollars"
26
+ ],
27
+ "style": "unit",
28
+ "fields": []
29
+ },
30
+ {
31
+ "constructor": "EUR",
32
+ "native": [
33
+ "PaymentsDomain",
34
+ "CurrencyCode",
35
+ "euros"
36
+ ],
37
+ "style": "unit",
38
+ "fields": []
39
+ },
40
+ {
41
+ "constructor": "GBP",
42
+ "native": [
43
+ "PaymentsDomain",
44
+ "CurrencyCode",
45
+ "pounds"
46
+ ],
47
+ "style": "unit",
48
+ "fields": []
49
+ }
50
+ ]
51
+ },
52
+ {
53
+ "type": "example.payments::type::Money",
54
+ "native": [
55
+ "PaymentsDomain",
56
+ "Price"
57
+ ],
58
+ "constructors": [
59
+ {
60
+ "constructor": "Money",
61
+ "native": [
62
+ "PaymentsDomain",
63
+ "Price"
64
+ ],
65
+ "style": "record",
66
+ "fields": [
67
+ {
68
+ "field": "amount",
69
+ "native": "major"
70
+ },
71
+ {
72
+ "field": "currency",
73
+ "native": "unit"
74
+ }
75
+ ]
76
+ }
77
+ ]
78
+ },
79
+ {
80
+ "type": "example.payments::type::Payment",
81
+ "native": [
82
+ "PaymentsDomain",
83
+ "PaymentStatus"
84
+ ],
85
+ "constructors": [
86
+ {
87
+ "constructor": "Paid",
88
+ "native": [
89
+ "PaymentsDomain",
90
+ "PaymentStatus",
91
+ "settled"
92
+ ],
93
+ "style": "variant",
94
+ "fields": [
95
+ {
96
+ "field": "value",
97
+ "native": "price"
98
+ }
99
+ ]
100
+ },
101
+ {
102
+ "constructor": "Declined",
103
+ "native": [
104
+ "PaymentsDomain",
105
+ "PaymentStatus",
106
+ "rejected"
107
+ ],
108
+ "style": "variant",
109
+ "fields": [
110
+ {
111
+ "field": "reason",
112
+ "native": "explanation"
113
+ }
114
+ ]
115
+ }
116
+ ]
117
+ }
118
+ ],
119
+ "functions": [
120
+ {
121
+ "declaration": "example.payments::addFee",
122
+ "native": [
123
+ "PaymentsDomain",
124
+ "applyFee"
125
+ ]
126
+ },
127
+ {
128
+ "declaration": "example.payments::roundTrip",
129
+ "native": [
130
+ "PaymentsDomain",
131
+ "restore"
132
+ ]
133
+ },
134
+ {
135
+ "declaration": "example.payments::archive",
136
+ "native": [
137
+ "PaymentsDomain",
138
+ "store"
139
+ ]
140
+ }
141
+ ],
142
+ "generators": [
143
+ {
144
+ "type": "example.payments::type::Money",
145
+ "factory": [
146
+ "PaymentGenerators",
147
+ "prices"
148
+ ]
149
+ }
150
+ ]
151
+ }
152
+ }
153
+ ]
154
+ }
@@ -0,0 +1,35 @@
1
+ // Application types, independent of generated LawSpec domain declarations.
2
+ import LawSpecRuntime
3
+
4
+ public enum PaymentsDomain {
5
+ public enum CurrencyCode: Sendable {
6
+ case dollars
7
+ case euros
8
+ case pounds
9
+ }
10
+
11
+ public struct Price: Sendable {
12
+ public var major: ExactDecimal
13
+ public var unit: CurrencyCode
14
+
15
+ public init(major: ExactDecimal, unit: CurrencyCode) {
16
+ self.major = major
17
+ self.unit = unit
18
+ }
19
+ }
20
+
21
+ public enum PaymentStatus: Sendable {
22
+ case settled(price: Price)
23
+ case rejected(explanation: String)
24
+ }
25
+
26
+ private static let fee = ExactDecimal(coefficient: 2, exponent: -1)
27
+
28
+ public static func applyFee(_ price: Price) -> Price {
29
+ Price(major: price.major + fee, unit: price.unit)
30
+ }
31
+
32
+ public static func restore(_ payment: PaymentStatus) -> PaymentStatus { payment }
33
+
34
+ public static func store(_ payments: PersistentList<PaymentStatus?>) -> PersistentList<PaymentStatus?> { payments }
35
+ }
@@ -0,0 +1,12 @@
1
+ // Prices the application considers representative: 1.00 to 2.00 euros.
2
+ import App
3
+ import LawSpecRuntime
4
+ import LawSpecTesting
5
+
6
+ enum PaymentGenerators {
7
+ static func prices() -> Gen<PaymentsDomain.Price> {
8
+ Gens.range(100, 200).map { cents in
9
+ PaymentsDomain.Price(major: ExactDecimal(coefficient: BigInt(cents), exponent: -2), unit: .euros)
10
+ }
11
+ }
12
+ }
@@ -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