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,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
@@ -0,0 +1,191 @@
1
+ unit example.records
2
+
3
+ import example.records.inner as inner
4
+
5
+ -- Records: a type with one constructor has fields that code reads by name
6
+ -- (account.balance) and replaces by name (account with balance = 0).
7
+
8
+ type Account is
9
+ Account
10
+ owner :: Text
11
+ balance :: Int64
12
+ holds :: List Int64
13
+ end
14
+
15
+ type Ledger is
16
+ Ledger
17
+ account :: Account
18
+ entries :: Int32
19
+ end
20
+
21
+ definition deposit (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) :: Account is
22
+ if a.balance >= 0 && a.balance <= 1000000000 then a with balance = prelude.Int64 (a.balance + amount) else a
23
+ end
24
+
25
+ definition rename (l :: Ledger) (name :: Text) :: Ledger is
26
+ l with account = (l.account with owner = name), entries = 0
27
+ end
28
+
29
+ law `a deposit adds to the balance` is
30
+ definition is
31
+ `for all` (owner :: Text) (start :: Int64 where start >= 0 && start <= 1000000000) (amount :: Int64 where amount >= 0 && amount <= 1000000) .
32
+ (deposit (Account owner start []) amount).balance = start + amount
33
+ end
34
+ end
35
+
36
+ law `a deposit keeps the owner and holds` is
37
+ definition is
38
+ `for all` (a :: Account) (amount :: Int64 where amount >= 0 && amount <= 1000000) .
39
+ (deposit a amount).owner = a.owner and (deposit a amount).holds = a.holds
40
+ end
41
+ end
42
+
43
+ law `renaming reads back` is
44
+ definition is
45
+ `for all` (l :: Ledger) (name :: Text) . (rename l name).account.owner = name and (rename l name).entries = 0
46
+ end
47
+ end
48
+
49
+ -- A definition may fail without any ability, and another may attempt it.
50
+ type Refusal is | Overdrawn | Unfunded end
51
+
52
+ definition withdraw (a :: Account) (amount :: Int64) :: Account fails with Refusal is
53
+ if amount < 0 then raise Unfunded else
54
+ if amount > a.balance then raise Overdrawn else a with balance = prelude.Int64 (a.balance - amount)
55
+ end
56
+
57
+ definition settled (a :: Account) (amount :: Int64) :: Either Refusal Int64 is
58
+ match prelude.attempt (withdraw a amount) with
59
+ | Left refusal -> Left refusal
60
+ | Right after -> Right after.balance
61
+ end
62
+ end
63
+
64
+ law `a withdrawal within the balance leaves the rest` is
65
+ definition is
66
+ `for all` (owner :: Text) (start :: Int64 where start >= 0) (amount :: Int64 where amount >= 0 && amount <= start) .
67
+ settled (Account owner start []) amount = Right (prelude.Int64 (start - amount))
68
+ end
69
+ end
70
+
71
+ law `a withdrawal beyond the balance is refused` is
72
+ definition is
73
+ `for all` (owner :: Text) (start :: Int64 where start >= 0 && start < 1000) (amount :: Int64 where amount >= 1000) .
74
+ settled (Account owner start []) amount = Left Overdrawn and settled (Account owner start []) (0 - amount) = Left Unfunded
75
+ end
76
+ end
77
+
78
+ -- A refinement through a definition still guides generation: sample
79
+ -- names come from the pattern, so the law does not starve its filter.
80
+ definition validOwner (name :: Text) :: Bool is
81
+ prelude.length name == 9 && name matches regex "acct-[0-9]{4}"
82
+ end
83
+
84
+ law `a renamed ledger keeps a valid owner` is
85
+ definition is
86
+ `for all` (l :: Ledger) (name :: Text where validOwner name) . validOwner (rename l name).account.owner
87
+ end
88
+ end
89
+
90
+ -- A literal takes its type from where it is used: 0 is an Int64 here.
91
+ definition opening (a :: Maybe Account) :: Int64 is
92
+ let start = (match a with | Nothing -> 0 | Just account -> account.balance end) in
93
+ if start < 0 then 0 else start
94
+ end
95
+
96
+ law `an absent account opens at zero` is
97
+ definition is opening Nothing = 0 end
98
+ end
99
+
100
+ -- Recursion on an integer that moves toward a bound is total.
101
+ definition countdown (n :: Natural) :: List Natural is
102
+ if n == 0 then [] else Cons n (countdown (n - 1))
103
+ end
104
+
105
+ definition evensBelow (i :: Int32) (n :: Int32) :: List Int32 is
106
+ if i >= n || i < 0 then [] else
107
+ (if prelude.rem i 2 == 0 then Cons i (evensBelow (prelude.Int32 (i + 1)) n) else evensBelow (prelude.Int32 (i + 1)) n)
108
+ end
109
+
110
+ law `a countdown has n items` is
111
+ definition is `for all` (n :: Natural where n <= 50) . prelude.length (countdown n) = n end
112
+ end
113
+
114
+ law `the evens below n are half of the range` is
115
+ definition is
116
+ `for all` (n :: Int32 where n >= 0 && n <= 40) .
117
+ prelude.length (evensBelow 0 n) = prelude.quot (n + 1) 2
118
+ end
119
+ end
120
+
121
+ law `range counts from the first up to the last` is
122
+ definition is
123
+ `for all` (n :: Int32 where n >= 0 && n <= 40) . prelude.length (prelude.range 0 n) = n
124
+ end
125
+ example `three` is
126
+ n = 3
127
+ expect prelude.range 0 n = [0, 1, 2]
128
+ end
129
+ end
130
+
131
+ -- The prover keeps what guards say about calls, lengths and remainders.
132
+ definition clip (xs :: List Int32) :: Int32 is
133
+ if prelude.length xs <= 100 then prelude.Int32 (prelude.length xs) else 100
134
+ end
135
+
136
+ definition owner (p :: Int32) (k :: Int32) :: Int32 is
137
+ if k >= 1 && p >= 0 then prelude.Int32 (prelude.rem p k) else 0
138
+ end
139
+
140
+ law `a clipped length is at most 100` is
141
+ definition is `for all` (xs :: List Int32) . (clip xs <= 100 && clip xs >= 0) = true end
142
+ end
143
+
144
+ law `an owner is one of the k` is
145
+ definition is
146
+ `for all` (p :: Int32 where p >= 0) (k :: Int32 where k >= 1 && k <= 64) . (owner p k >= 0 && owner p k < k) = true
147
+ end
148
+ end
149
+
150
+ -- Arithmetic into a bounded type, by name.
151
+ definition nextEpoch (e :: Int16) :: Int16 is prelude.wrapping (e + 1) end
152
+ definition topUp (level :: UInt8) :: UInt8 is prelude.saturating (level + 10) end
153
+ definition total (a :: Int32) (b :: Int32) :: Maybe Int32 is prelude.bounded (a + b) end
154
+
155
+ law `epochs wrap around` is
156
+ definition is
157
+ `for all` (e :: Int16) . nextEpoch e = (if e == Int16.max then Int16.min else prelude.Int16 (e + 1))
158
+ end
159
+ end
160
+
161
+ law `a top up stops at the top` is
162
+ definition is
163
+ `for all` (level :: UInt8) . topUp level = (if level >= 245 then 255 else prelude.UInt8 (level + 10))
164
+ end
165
+ end
166
+
167
+ law `a total that overflows is Nothing` is
168
+ definition is
169
+ `for all` (a :: Int32) (b :: Int32) .
170
+ total a b = (if a + b > Int32.max || a + b < Int32.min then Nothing else Just (prelude.Int32 (a + b)))
171
+ end
172
+ end
173
+
174
+ -- A refinement shapes generation: texts a regex must match are drawn from
175
+ -- the regex, through the definition that says so, not filtered from any
176
+ -- text.
177
+ definition validTopic (name :: Text) :: Bool is
178
+ let n = prelude.length name in
179
+ n >= 1 && n <= 249 && name != "." && name != ".." && name matches regex "[a-zA-Z0-9._-]+"
180
+ end
181
+
182
+ law `a valid topic name keeps to its characters` is
183
+ definition is
184
+ `for all` (t :: Text where validTopic t) (n :: Int32 where n >= 1 && n <= 64) .
185
+ (prelude.length t >= 1 && prelude.length t <= 249 && n <= 64) = true
186
+ end
187
+ end
188
+
189
+ law `a unit may sit above its package's other units` is
190
+ definition is `for all` (x :: Int32) . inner.doubled x = x + x end
191
+ end
@@ -0,0 +1,10 @@
1
+ unit example.records.inner
2
+
3
+ -- A unit inside another unit's name: on Python, example.records is then its
4
+ -- package's __init__.py, and this unit is example/records/inner.py.
5
+
6
+ definition doubled (x :: Int32) :: Integer is x * 2 end
7
+
8
+ law `doubling is even` is
9
+ definition is `for all` (x :: Int32) . prelude.rem (doubled x) 2 = 0 end
10
+ end
@@ -0,0 +1,86 @@
1
+ unit example.sequences
2
+
3
+ -- Text, Bytes and List are sequences: joined with ++, sliced, and read by
4
+ -- index. Bytes give their octets, and Text its UTF-8 encoding.
5
+
6
+ -- A record key's hash: FNV-1a over the key's octets, in 32 bits.
7
+ definition fnv (octets :: List UInt8) (hash :: UInt32) :: UInt32 is
8
+ match octets with
9
+ | Nil -> hash
10
+ | Cons o rest -> fnv rest (prelude.wrapping ((xorByte hash o) * 16777619))
11
+ end
12
+ end
13
+
14
+ -- hash xor o, for an octet o, by its low eight bits.
15
+ definition xorByte (hash :: UInt32) (o :: UInt8) :: Integer is
16
+ let low = prelude.rem hash 256 in
17
+ hash - low + xorBits low o 128
18
+ end
19
+
20
+ definition xorBits (a :: Integer) (b :: Integer) (bit :: Integer) :: Integer is
21
+ if bit <= 0 || a < 0 || b < 0 then 0 else
22
+ (if prelude.quot a bit != prelude.quot b bit then bit else 0)
23
+ + xorBits (prelude.rem a bit) (prelude.rem b bit) (prelude.quot bit 2)
24
+ end
25
+
26
+ definition keyHash (key :: Bytes) :: UInt32 is fnv (prelude.octets key) 2166136261 end
27
+
28
+ definition partitionFor (key :: Bytes) (n :: Int32) :: Int32 is
29
+ if n >= 1 then prelude.Int32 (prelude.rem (keyHash key) n) else 0
30
+ end
31
+
32
+ law `a key's partition exists` is
33
+ definition is
34
+ `for all` (key :: Bytes) (n :: Int32 where n >= 1 && n <= 64) .
35
+ (partitionFor key n >= 0 && partitionFor key n < n) = true
36
+ end
37
+ end
38
+
39
+ law `the hash follows FNV-1a` is
40
+ definition is keyHash (prelude.utf8 "a") = 3826002220 and keyHash (prelude.utf8 "") = 2166136261 end
41
+ end
42
+
43
+ -- Member ids, made by the coordinator from a client id and a counter.
44
+ definition memberId (client :: Text) (n :: Int32) :: Text is client ++ "-" ++ prelude.textOf n end
45
+
46
+ law `a member id starts with its client` is
47
+ definition is
48
+ `for all` (client :: Text) (n :: Int32) . memberId client n starts with client
49
+ end
50
+ example `the third` is
51
+ client = "consumer"
52
+ n = 3
53
+ expect memberId client n = "consumer-3"
54
+ end
55
+ end
56
+
57
+ law `joining text adds lengths` is
58
+ definition is
59
+ `for all` (a :: Text) (b :: Text) . prelude.length (a ++ b) = prelude.length a + prelude.length b
60
+ end
61
+ end
62
+
63
+ law `octets and bytes round trip` is
64
+ definition is `for all` (b :: Bytes) . prelude.bytesOf (prelude.octets b) = b end
65
+ end
66
+
67
+ law `a slice is a part` is
68
+ definition is
69
+ `for all` (xs :: List Int32) (k :: Int32 where k >= 0 && k <= 10) .
70
+ prelude.take k xs ++ prelude.drop k xs = xs
71
+ end
72
+ end
73
+
74
+ law `at reads the item an index names` is
75
+ definition is
76
+ `for all` (x :: Int32) (xs :: List Int32) . prelude.at 0 (Cons x xs) = Just x and prelude.at (prelude.length xs + 1) (Cons x xs) = Nothing
77
+ end
78
+ end
79
+
80
+ law `texts join with a separator` is
81
+ definition is prelude.join ", " ["a", "b", "c"] = "a, b, c" and prelude.slice 1 3 "kafka" = "af" end
82
+ end
83
+
84
+ law `utf8 encodes code points` is
85
+ definition is prelude.utf8 "é" = prelude.bytesOf [195, 169] and prelude.length (prelude.utf8 "é") = 2 end
86
+ end
package/index.d.ts CHANGED
@@ -10,7 +10,13 @@ export type Target =
10
10
  'rust' |
11
11
  'erlang' |
12
12
  'elixir' |
13
- 'gleam';
13
+ 'gleam' |
14
+ 'fsharp' |
15
+ 'csharp' |
16
+ 'scala' |
17
+ 'groovy' |
18
+ 'clojure' |
19
+ 'swift';
14
20
 
15
21
  export interface Source {
16
22
  path: string;
@@ -240,6 +246,37 @@ export interface ObligationEvidence {
240
246
  status: DischargeStatus;
241
247
  reason: string;
242
248
  claim: Expr | null;
249
+ environment?: string;
250
+ environmentRun?: AttestedRun;
251
+ against?: AttestedDependency[];
252
+ }
253
+
254
+ export interface AttestedRun {
255
+ id: string;
256
+ startedAt: string;
257
+ recordedAt: string;
258
+ }
259
+
260
+ export interface AttestedStandIn {
261
+ identity: string;
262
+ version: string;
263
+ conformance: string;
264
+ historyDigest?: string;
265
+ }
266
+
267
+ export interface AttestedDependency {
268
+ dependency: string;
269
+ endpoint: string;
270
+ reached: boolean;
271
+ kind: 'real' | 'stand-in';
272
+ standIn?: AttestedStandIn;
273
+ }
274
+
275
+ export interface AttestationView {
276
+ environment: string;
277
+ run: AttestedRun;
278
+ producer?: PackageVersion;
279
+ dependencies: (AttestedDependency & {obligations: string[]})[];
243
280
  }
244
281
 
245
282
  export interface PackageVersion {
@@ -271,6 +308,7 @@ export interface CheckRequest {
271
308
  dependencies?: Record<string, string>;
272
309
  packages?: Package[];
273
310
  cacheDirectory?: string;
311
+ attestation?: unknown;
274
312
  }
275
313
 
276
314
  export interface GenerationRequest extends CheckRequest {
@@ -294,6 +332,7 @@ export interface Result {
294
332
  expansions?: string[];
295
333
  files?: Artifact[];
296
334
  evidence?: ObligationEvidence[];
335
+ attestation?: AttestationView;
297
336
  packages?: PackageView[];
298
337
  project?: {package?: PackageVersion; dependencies: Record<string, string>;};
299
338
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lawspec",
3
- "version": "0.22.0",
3
+ "version": "0.24.0",
4
4
  "description": "State the law once. Check it everywhere.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -35,7 +35,7 @@
35
35
  "examples"
36
36
  ],
37
37
  "scripts": {
38
- "prepack": "cp ../README.md ../CHANGELOG.md .",
38
+ "prepack": "for f in README.md CHANGELOG.md; do cmp -s ../$f $f || { cp ../$f .$f.$$ && mv -f .$f.$$ $f; }; done",
39
39
  "test": "node --test test/*.test.mjs"
40
40
  }
41
41
  }
@@ -0,0 +1,56 @@
1
+ // Generated from templates/npm/scala-coverage.mjs by lawspec-dev generate. Do not edit.
2
+ // lawspec test --coverage on Scala: Scala 3 instruments the code itself
3
+ // (-coverage-out, in scoverage's format) and the scoverage reporter, which
4
+ // Scala CLI fetches, writes Cobertura, scoverage XML and HTML reports.
5
+ import {mkdir, readdir, writeFile} from "node:fs/promises";
6
+ import {spawn} from "node:child_process";
7
+ import path from "node:path";
8
+
9
+ export const scoverageReporter = "2.5.2";
10
+
11
+ // The report script, run by Scala CLI on its own (not as part of the project).
12
+ const reportScript = [
13
+ "//> using scala 3.9.0",
14
+ `//> using dep org.scoverage::scalac-scoverage-reporter:${scoverageReporter}`,
15
+ "//> using javaOpt --sun-misc-unsafe-memory-access=allow",
16
+ "import java.io.File",
17
+ "import scoverage.reporter.{CoberturaXmlWriter, IOUtils, ScoverageHtmlWriter, ScoverageXmlWriter}",
18
+ "import scoverage.serialize.Serializer",
19
+ "",
20
+ "@main def report(data: String, sources: String, output: String): Unit = {",
21
+ " val dataDir = File(data)",
22
+ " val root = File(sources)",
23
+ " val coverage = Serializer.deserialize(Serializer.coverageFile(dataDir), root)",
24
+ " coverage.apply(IOUtils.invoked(IOUtils.findMeasurementFiles(dataDir).toIndexedSeq))",
25
+ " val out = File(output)",
26
+ " out.mkdirs()",
27
+ " CoberturaXmlWriter(Seq(root), out, None).write(coverage)",
28
+ " ScoverageXmlWriter(Seq(root), out, false, None).write(coverage)",
29
+ " ScoverageHtmlWriter(Seq(root), out, None).write(coverage)",
30
+ " println(f\"Statement coverage: ${coverage.statementCoveragePercent}%.2f%%\")",
31
+ "}",
32
+ ""].join("\n");
33
+
34
+ // Where the instrumented runs write their data, under the coverage directory.
35
+ export function scalaCoverageData(directory) {
36
+ return path.join(directory, "data");
37
+ }
38
+
39
+ // Writes the reports into directory from the runs' data; false when no run
40
+ // was measured.
41
+ export async function collectScalaCoverage({root, directory, command = "scala", offline = false}) {
42
+ const data = scalaCoverageData(directory);
43
+ const files = await readdir(data).catch(() => []);
44
+ if (!files.includes("scoverage.coverage")) return false;
45
+ const script = path.join(directory, "reporter", "Report.scala");
46
+ await mkdir(path.dirname(script), {recursive: true});
47
+ await writeFile(script, reportScript);
48
+ const args = ["run", ...(offline ? ["--offline"] : []), script, "--", data, root, path.join(directory, "report")];
49
+ const status = await new Promise((resolve, reject) => {
50
+ const child = spawn(command, args, {cwd: path.dirname(script), stdio: ["ignore", "inherit", "inherit"]});
51
+ child.on("error", reject);
52
+ child.on("close", resolve);
53
+ });
54
+ if (status !== 0) throw new Error(`the scoverage reporter failed (${command} ${args.join(" ")})`);
55
+ return true;
56
+ }