lawspec 0.20.1 → 0.22.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.
- package/CHANGELOG.md +361 -2
- package/README.md +3 -3
- package/beam-coverage.mjs +160 -0
- package/beam-doctor.mjs +306 -0
- package/bin/lawspec.mjs +306 -38
- package/build.json +492 -106
- package/compatibility.json +17 -0
- package/core.wasm +0 -0
- package/core_jsffi.js +2 -2
- package/doctor.mjs +7 -1
- package/examples/native-payments/elixir/lawspec.json +149 -0
- package/examples/native-payments/elixir/lib/payments_domain.ex +54 -0
- package/examples/native-payments/elixir/test/support/payment_generators.ex +9 -0
- package/examples/native-payments/erlang/include/payments_domain.hrl +2 -0
- package/examples/native-payments/erlang/lawspec.json +148 -0
- package/examples/native-payments/erlang/src/payments_domain.erl +21 -0
- package/examples/native-payments/erlang/test/payment_generators.erl +10 -0
- package/examples/native-payments/gleam/lawspec.json +149 -0
- package/examples/native-payments/gleam/prepare.mjs +48 -0
- package/examples/native-payments/gleam/src/payments_domain.gleam +32 -0
- package/examples/native-payments/gleam/test/payment_generators.gleam +13 -0
- package/examples/specs/abilities.lawspec +103 -0
- package/examples/specs/ability_imports.lawspec +107 -0
- package/examples/specs/builtins.lawspec +121 -0
- package/examples/specs/crypto.lawspec +51 -0
- package/examples/specs/distribution.lawspec +29 -0
- package/examples/specs/failures.lawspec +96 -0
- package/examples/specs/handler_bindings.lawspec +65 -0
- package/examples/specs/harness.lawspec +75 -0
- package/examples/specs/harness_testing.lawspec +45 -0
- package/examples/specs/matchers.lawspec +112 -0
- package/examples/specs/resources.lawspec +116 -0
- package/examples/specs/scheduling_order.lawspec +18 -0
- package/examples/specs/scheduling_overlap.lawspec +14 -0
- package/examples/specs/tables.lawspec +64 -0
- package/failure-database.mjs +46 -0
- package/index.d.ts +12 -1
- package/native-examples.mjs +7 -2
- package/package.json +1 -1
- package/templates.mjs +105 -16
- package/test-command.mjs +316 -35
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
unit example.harness
|
|
2
|
+
|
|
3
|
+
-- The laws of a small shop. How they are tested (which inputs, which
|
|
4
|
+
-- handlers, how many cases are enough) is not said here: that is the
|
|
5
|
+
-- harness's business, in harness_testing.lawspec.
|
|
6
|
+
|
|
7
|
+
-- An order: how many items it has, and its total in cents.
|
|
8
|
+
type Order is Order items :: Int32 total :: Int32 end
|
|
9
|
+
|
|
10
|
+
definition itemsOf (order :: Order) :: Int32 is match order with | Order items total -> items end end
|
|
11
|
+
definition totalOf (order :: Order) :: Int32 is match order with | Order items total -> total end end
|
|
12
|
+
definition isEmpty (order :: Order) :: Bool is itemsOf order == 0 end
|
|
13
|
+
|
|
14
|
+
-- The discount an order gets, and a price rounded to tens of cents.
|
|
15
|
+
discount :: Order -> Int32
|
|
16
|
+
roundCents :: Int32 -> Int32
|
|
17
|
+
|
|
18
|
+
law `a discount is never more than the total` is
|
|
19
|
+
definition is
|
|
20
|
+
`for all` (order :: Order where itemsOf order >= 0 && totalOf order >= 0) .
|
|
21
|
+
(discount order <= totalOf order) = true
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
law `rounding is idempotent` is
|
|
26
|
+
definition is
|
|
27
|
+
`for all` (cents :: Int32 where cents >= 0 && cents <= 1000000) .
|
|
28
|
+
roundCents (roundCents cents) = roundCents cents
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
law `rounding never lowers a price` is
|
|
33
|
+
definition is
|
|
34
|
+
`for all` (cents :: Int32 where cents >= 0 && cents <= 1000000) .
|
|
35
|
+
(roundCents cents >= cents) = true
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
-- A ledger records payments. Every law about booking holds for every
|
|
40
|
+
-- lawful ledger; the harness chooses which ones the tests use.
|
|
41
|
+
ability Ledger is
|
|
42
|
+
accept :: Int32 -> Bool
|
|
43
|
+
laws
|
|
44
|
+
law `positive amounts are accepted` is
|
|
45
|
+
definition is `for all` (cents :: Int32 where cents > 0) . accept cents = true end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
handler fakeLedger for Ledger is
|
|
50
|
+
accept cents is cents > 0 end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
book :: Int32 -> Bool uses Ledger
|
|
54
|
+
|
|
55
|
+
law `booking a positive amount succeeds` is
|
|
56
|
+
definition is
|
|
57
|
+
`for all` (cents :: Int32 where cents > 0) . book cents = true
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
law `a refund is never booked twice` is
|
|
62
|
+
definition is
|
|
63
|
+
`for all` (cents :: Int32 where cents > 0) . book cents = book cents
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
-- A scratch area the laws may use; it can be emptied between laws, and
|
|
68
|
+
-- laws running at the same time cannot interfere through it.
|
|
69
|
+
type Scratch is Scratch used :: Int32 end
|
|
70
|
+
|
|
71
|
+
resource Scratch is concurrent
|
|
72
|
+
acquire is Scratch 0 end
|
|
73
|
+
release scratch is unitValue end
|
|
74
|
+
reset scratch is unitValue end
|
|
75
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
-- The harness of example.harness: how its laws are tested. A harness
|
|
2
|
+
-- cannot change what a law means; it chooses inputs, handlers, adequacy and
|
|
3
|
+
-- how the tests run.
|
|
4
|
+
harness example.harness.testing for example.harness is
|
|
5
|
+
-- Small orders: up to three items at a dollar each.
|
|
6
|
+
strategy smallOrder :: Order is
|
|
7
|
+
bind items :: Int32 from (one of 0, 1, 2, 3) in one of Order items (items * 100)
|
|
8
|
+
end
|
|
9
|
+
-- Bulk orders: the default generator, kept to large ones.
|
|
10
|
+
strategy bulkOrder :: Order is
|
|
11
|
+
any such that itemsOf it > 10 && totalOf it >= 0 at most 1000 discards
|
|
12
|
+
end
|
|
13
|
+
strategy orders :: Order is frequency 9 smallOrder, 1 bulkOrder end
|
|
14
|
+
|
|
15
|
+
tags pricing
|
|
16
|
+
test with fakeLedger
|
|
17
|
+
order random
|
|
18
|
+
parallel
|
|
19
|
+
|
|
20
|
+
for law `a discount is never more than the total`
|
|
21
|
+
use orders for order
|
|
22
|
+
cover 5% "empty order" when isEmpty order
|
|
23
|
+
classify totalOf order > 1000 as "large"
|
|
24
|
+
label if isEmpty order then "empty" else "some items"
|
|
25
|
+
timeout 10 s
|
|
26
|
+
|
|
27
|
+
for law `rounding is idempotent`
|
|
28
|
+
target maximize cents
|
|
29
|
+
repeat 2
|
|
30
|
+
retry flaky 1
|
|
31
|
+
|
|
32
|
+
for law `rounding never lowers a price`
|
|
33
|
+
known failing "rounds half down; see PRICE-12"
|
|
34
|
+
|
|
35
|
+
for law `a refund is never booked twice`
|
|
36
|
+
skip "refunds are not booked yet"
|
|
37
|
+
|
|
38
|
+
for laws `booking a positive amount succeeds`, `positive amounts are accepted`
|
|
39
|
+
tags ledger
|
|
40
|
+
|
|
41
|
+
share Scratch per unit
|
|
42
|
+
benchmark `discount of a bulk order` is discount (Order 30 50000) end
|
|
43
|
+
-- book uses Ledger: the benchmark runs under its production handler.
|
|
44
|
+
benchmark `booking a payment` is book 500 end
|
|
45
|
+
end
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
unit example.matchers
|
|
2
|
+
|
|
3
|
+
-- Matchers are typed predicates that read like sentences. Each one is a
|
|
4
|
+
-- Bool, so a law may combine them with && and ||, and an example may expect
|
|
5
|
+
-- one to hold.
|
|
6
|
+
|
|
7
|
+
type Order is
|
|
8
|
+
| Pending id :: Int32
|
|
9
|
+
| Shipped id :: Int32 carrier :: Text
|
|
10
|
+
| Cancelled id :: Int32 reason :: Maybe Text
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
-- Native adapters, written on each target.
|
|
14
|
+
sortItems :: List Int32 -> List Int32
|
|
15
|
+
uniqueTags :: List Text -> List Text
|
|
16
|
+
average :: Int32 -> Int32 -> Float64
|
|
17
|
+
slug :: Text -> Text
|
|
18
|
+
ship :: Int32 -> Order
|
|
19
|
+
|
|
20
|
+
law `sorting keeps every item` is
|
|
21
|
+
definition is
|
|
22
|
+
`for all` (xs :: List Int32) . sortItems xs has same items as xs
|
|
23
|
+
end
|
|
24
|
+
example `duplicates stay` is
|
|
25
|
+
xs = [3, 1, 3]
|
|
26
|
+
expect sortItems xs = [1, 3, 3]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
law `unique tags come from the input` is
|
|
31
|
+
definition is
|
|
32
|
+
`for all` (tags :: List Text) .
|
|
33
|
+
uniqueTags tags is subset of tags && tags contains all of uniqueTags tags
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
law `unique tags keep each tag` is
|
|
38
|
+
definition is
|
|
39
|
+
`for all` (tags :: List Text) (tag :: Text) .
|
|
40
|
+
tags contains tag implies uniqueTags tags contains tag
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
law `the average lies between the two` is
|
|
45
|
+
definition is
|
|
46
|
+
`for all` (a :: Int32) (b :: Int32) .
|
|
47
|
+
average a b is within 0.5 of prelude.Float64 (prelude.quot (a + b) 2)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
law `a slug is lowercase words joined by dashes` is
|
|
52
|
+
definition is
|
|
53
|
+
`for all` (title :: Text) .
|
|
54
|
+
slug title matches regex "([a-z0-9]+(-[a-z0-9]+)*)?"
|
|
55
|
+
end
|
|
56
|
+
example `a title` is
|
|
57
|
+
title = "Hello World"
|
|
58
|
+
expect slug title = "hello-world"
|
|
59
|
+
expect slug title starts with "hello"
|
|
60
|
+
expect slug title ends with "world"
|
|
61
|
+
expect slug title contains "o-w"
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
law `shipping gives a shipped order` is
|
|
66
|
+
definition is
|
|
67
|
+
`for all` (id :: Int32) . ship id matches Shipped _ _
|
|
68
|
+
end
|
|
69
|
+
example `the carrier is never empty` is
|
|
70
|
+
id = 7
|
|
71
|
+
expect ship id matches Shipped 7 _
|
|
72
|
+
expect !(ship id matches Shipped _ "")
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
-- Laws of the matchers themselves.
|
|
77
|
+
law `having the same items is symmetric` is
|
|
78
|
+
definition is
|
|
79
|
+
`for all` (xs :: List Int8) (ys :: List Int8) .
|
|
80
|
+
(xs has same items as ys) = (ys has same items as xs)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
law `a list contains each of its items` is
|
|
85
|
+
definition is
|
|
86
|
+
`for all` (x :: Int8) (xs :: List Int8) . Cons x xs contains x
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
law `a list is a subset of itself` is
|
|
91
|
+
definition is
|
|
92
|
+
`for all` (xs :: List Int8) . xs is subset of xs && xs contains all of xs
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
law `a text starts and ends with itself` is
|
|
97
|
+
definition is
|
|
98
|
+
`for all` (t :: Text) . t starts with t && t ends with t && t contains t && t contains ""
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
law `a number is within any tolerance of itself` is
|
|
103
|
+
definition is
|
|
104
|
+
`for all` (x :: Int32) (d :: Int32 where d >= 0) . x is within d of x
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
law `a regex value matches like a literal` is
|
|
109
|
+
definition is
|
|
110
|
+
`for all` (t :: Text) . (t matches regex "[a-c]*") = (t matches regex "[a-c]*")
|
|
111
|
+
end
|
|
112
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
unit example.resources
|
|
2
|
+
|
|
3
|
+
-- A resource is something a law needs that lives outside the program: a
|
|
4
|
+
-- store, a directory, a port. Each case of a law acquires its resources
|
|
5
|
+
-- first and releases them after, even when the case fails, so every case
|
|
6
|
+
-- starts afresh and nothing leaks.
|
|
7
|
+
|
|
8
|
+
-- A store the tests open, use and close. Only adapters make one.
|
|
9
|
+
handle Store
|
|
10
|
+
|
|
11
|
+
openStore :: Unit -> Store
|
|
12
|
+
closeStore :: Store -> Unit
|
|
13
|
+
clearStore :: Store -> Unit
|
|
14
|
+
put :: Store -> Int32 -> Int32 -> Unit
|
|
15
|
+
get :: Store -> Int32 -> Maybe Int32
|
|
16
|
+
isOpen :: Store -> Bool
|
|
17
|
+
size :: Store -> Int32
|
|
18
|
+
|
|
19
|
+
resource Store is
|
|
20
|
+
acquire is openStore unitValue end
|
|
21
|
+
release store is closeStore store end
|
|
22
|
+
reset store is clearStore store end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
law `a stored value can be read back` for store :: Store is
|
|
26
|
+
definition is
|
|
27
|
+
`for all` (k :: Int32) (v :: Int32) . put store k v = unitValue and get store k = Just v
|
|
28
|
+
end
|
|
29
|
+
example `the store is open while the case runs` is
|
|
30
|
+
k = 1
|
|
31
|
+
v = 2
|
|
32
|
+
expect isOpen store
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
law `every case gets an empty store` for store :: Store is
|
|
37
|
+
definition is
|
|
38
|
+
`for all` (k :: Int32) (v :: Int32) .
|
|
39
|
+
size store = 0 and put store k v = unitValue and size store = 1
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
-- Built-in resources: a temporary directory, a free port, and the
|
|
44
|
+
-- environment, saved and restored.
|
|
45
|
+
writeNote :: Text -> Int32 -> Unit
|
|
46
|
+
readNote :: Text -> Maybe Int32
|
|
47
|
+
canListen :: Int32 -> Bool
|
|
48
|
+
setGreeting :: Int32 -> Unit
|
|
49
|
+
greeting :: Unit -> Maybe Int32
|
|
50
|
+
|
|
51
|
+
law `a note reads back` for dir :: TemporaryDirectory is
|
|
52
|
+
definition is
|
|
53
|
+
`for all` (n :: Int32) .
|
|
54
|
+
writeNote (directoryPath dir) n = unitValue and readNote (directoryPath dir) = Just n
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
law `a new directory has no note` for dir :: TemporaryDirectory is
|
|
59
|
+
definition is
|
|
60
|
+
readNote (directoryPath dir) = Nothing
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
law `a free port can be listened on` for port :: FreePort is
|
|
65
|
+
definition is
|
|
66
|
+
canListen (portNumber port) = true
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
law `the greeting is the one set` for saved :: SavedEnvironment is
|
|
71
|
+
definition is
|
|
72
|
+
`for all` (n :: Int32) . setGreeting n = unitValue and greeting unitValue = Just n
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
law `the environment is restored after each case` for saved :: SavedEnvironment is
|
|
77
|
+
definition is
|
|
78
|
+
greeting unitValue = Nothing
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
-- A pool is costly to open, so the harness shares one between the cases of
|
|
83
|
+
-- this unit's laws. Sharing is allowed only because Pool declares reset:
|
|
84
|
+
-- every case after the first gets the pool drained, so no case sees what
|
|
85
|
+
-- another left in it, and the law means the same as with a fresh pool.
|
|
86
|
+
handle Pool
|
|
87
|
+
|
|
88
|
+
openPool :: Unit -> Pool
|
|
89
|
+
drainPool :: Pool -> Unit
|
|
90
|
+
closePool :: Pool -> Unit
|
|
91
|
+
fill :: Pool -> Int32 -> Unit
|
|
92
|
+
level :: Pool -> Int32
|
|
93
|
+
|
|
94
|
+
resource Pool is
|
|
95
|
+
acquire is openPool unitValue end
|
|
96
|
+
release pool is closePool pool end
|
|
97
|
+
reset pool is drainPool pool end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
law `a pool starts each case empty` for pool :: Pool is
|
|
101
|
+
definition is
|
|
102
|
+
`for all` (n :: Int32 where n > 0 && n <= 100) .
|
|
103
|
+
level pool = 0 and fill pool n = unitValue and level pool = n
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
law `filling twice adds up` for pool :: Pool is
|
|
108
|
+
definition is
|
|
109
|
+
`for all` (a :: Int32 where a > 0 && a <= 100) (b :: Int32 where b > 0 && b <= 100) .
|
|
110
|
+
fill pool a = unitValue and fill pool b = unitValue and level pool = a + b
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
harness example.resources.testing for example.resources is
|
|
115
|
+
share Pool per unit
|
|
116
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
unit example.sequencing
|
|
2
|
+
|
|
3
|
+
-- How a harness orders a unit's laws never changes what they mean. The
|
|
4
|
+
-- scheduling acceptance suite checks that `order random` follows the run's
|
|
5
|
+
-- seed: the same seed gives the same order, and other seeds other orders.
|
|
6
|
+
-- pause notes when it is called (when the suite asks it to) and gives true.
|
|
7
|
+
pause :: Int32 -> Bool
|
|
8
|
+
|
|
9
|
+
law `the first pause ends` is definition is pause 1 = true end end
|
|
10
|
+
law `the second pause ends` is definition is pause 2 = true end end
|
|
11
|
+
law `the third pause ends` is definition is pause 3 = true end end
|
|
12
|
+
law `the fourth pause ends` is definition is pause 4 = true end end
|
|
13
|
+
law `the fifth pause ends` is definition is pause 5 = true end end
|
|
14
|
+
law `the sixth pause ends` is definition is pause 6 = true end end
|
|
15
|
+
|
|
16
|
+
harness example.sequencing.testing for example.sequencing is
|
|
17
|
+
order random
|
|
18
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
unit example.overlap
|
|
2
|
+
|
|
3
|
+
-- How a harness runs a unit's laws never changes what they mean. The
|
|
4
|
+
-- scheduling acceptance suite checks that `parallel` runs them at the same
|
|
5
|
+
-- time: each nap sleeps, and their sleeps overlap.
|
|
6
|
+
async nap :: Int32 -> Bool
|
|
7
|
+
|
|
8
|
+
law `the first nap ends` is definition is nap 101 = true end end
|
|
9
|
+
law `the second nap ends` is definition is nap 102 = true end end
|
|
10
|
+
law `the third nap ends` is definition is nap 103 = true end end
|
|
11
|
+
|
|
12
|
+
harness example.overlap.testing for example.overlap is
|
|
13
|
+
parallel
|
|
14
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
unit example.tables
|
|
2
|
+
|
|
3
|
+
-- Examples three ways: as rows of a table, as examples written in a law's
|
|
4
|
+
-- description, and as values recorded in the project.
|
|
5
|
+
|
|
6
|
+
shippingCost :: Int32 -> Int32 -> Int32
|
|
7
|
+
label :: Int32 -> Text
|
|
8
|
+
|
|
9
|
+
-- Each row is its own example. The column on the right of an expect's =
|
|
10
|
+
-- holds the expected values; the other columns bind the law's inputs.
|
|
11
|
+
law `shipping cost grows with weight and distance` is
|
|
12
|
+
definition is
|
|
13
|
+
`for all` (kilograms :: Int32 where kilograms >= 0 && kilograms <= 1000)
|
|
14
|
+
(kilometres :: Int32 where kilometres >= 0 && kilometres <= 1000) .
|
|
15
|
+
shippingCost kilograms kilometres >= 0
|
|
16
|
+
end
|
|
17
|
+
table (kilograms, kilometres, cost) is
|
|
18
|
+
row 0, 0, 0
|
|
19
|
+
row 1, 10, 15
|
|
20
|
+
row 2, 10, 30
|
|
21
|
+
row 10, 100, 1050
|
|
22
|
+
expect shippingCost kilograms kilometres = cost
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
-- Examples in a description are fenced, and become examples of the law.
|
|
27
|
+
law `a label names the parcel` is
|
|
28
|
+
definition is
|
|
29
|
+
`for all` (parcel :: Int32 where parcel >= 0) . label parcel starts with "parcel "
|
|
30
|
+
end
|
|
31
|
+
description is
|
|
32
|
+
"Every label starts with the word parcel and the parcel's number.
|
|
33
|
+
|
|
34
|
+
```example
|
|
35
|
+
parcel = 7
|
|
36
|
+
expect label parcel = \"parcel 7\"
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
```example `a large number`
|
|
40
|
+
parcel = 123456
|
|
41
|
+
expect label parcel = \"parcel 123456\"
|
|
42
|
+
```"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
-- A recorded value is stored in the project, under
|
|
47
|
+
-- recorded/example.tables/<name>, in the portable rendering every target
|
|
48
|
+
-- shares. lawspec test --update-recorded records it again; until then, a
|
|
49
|
+
-- changed value fails.
|
|
50
|
+
law `the first label is recorded` is
|
|
51
|
+
definition is
|
|
52
|
+
label 1 = recorded "first label"
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
law `labels are short` is
|
|
57
|
+
definition is
|
|
58
|
+
`for all` (parcel :: Int32 where parcel >= 0) . prelude.length (label parcel) <= 20
|
|
59
|
+
end
|
|
60
|
+
example `parcel 42` is
|
|
61
|
+
parcel = 42
|
|
62
|
+
expect label parcel = recorded "parcel 42"
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// Generated from templates/npm/failure-database.mjs by lawspec-dev generate. Do not edit.
|
|
2
|
+
// Failed seeds and framework counterexamples belong to one project root.
|
|
3
|
+
import {createHash} from 'node:crypto';
|
|
4
|
+
import {cp, mkdir, mkdtemp, readFile, rename, rm, writeFile} from 'node:fs/promises';
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import {readOptional} from './files.mjs';
|
|
7
|
+
|
|
8
|
+
export const projectKey = root => createHash('sha256').update(path.resolve(root)).digest('hex').slice(0, 12);
|
|
9
|
+
|
|
10
|
+
function parseDatabase(text) {
|
|
11
|
+
const database = JSON.parse(text);
|
|
12
|
+
if (!database || typeof database !== 'object' || Array.isArray(database))
|
|
13
|
+
throw new Error('The failure database must contain a map of law identities to failures');
|
|
14
|
+
return database;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function failureDatabase(configRoot, language, root) {
|
|
18
|
+
const legacy = path.join(configRoot, '.lawspec/failures', language);
|
|
19
|
+
const directory = path.join(legacy, projectKey(root));
|
|
20
|
+
const file = path.join(directory, 'laws.json');
|
|
21
|
+
const current = await readOptional(file);
|
|
22
|
+
if (current !== null) return {directory, file, database: parseDatabase(current)};
|
|
23
|
+
|
|
24
|
+
// Old databases identify only the language. Preserve them and copy their
|
|
25
|
+
// failures once into each project's initial state so no counterexample is
|
|
26
|
+
// lost. Subsequent passes and failures change only that project's copy.
|
|
27
|
+
const database = parseDatabase((await readOptional(path.join(legacy, 'laws.json'))) ?? '{}');
|
|
28
|
+
await mkdir(legacy, {recursive: true});
|
|
29
|
+
const temporary = await mkdtemp(path.join(legacy, '.migrate-'));
|
|
30
|
+
try {
|
|
31
|
+
for (const name of ['inputs', 'hypothesis', 'proptest-regressions.txt']) {
|
|
32
|
+
try { await cp(path.join(legacy, name), path.join(temporary, name), {recursive: true}); }
|
|
33
|
+
catch (error) { if (error.code !== 'ENOENT') throw error; }
|
|
34
|
+
}
|
|
35
|
+
await writeFile(path.join(temporary, 'laws.json'), JSON.stringify(database, null, 2) + '\n');
|
|
36
|
+
try { await rename(temporary, directory); }
|
|
37
|
+
catch (error) {
|
|
38
|
+
// Another invocation may have initialized the same project while this
|
|
39
|
+
// one copied the legacy data. Its complete database takes precedence.
|
|
40
|
+
if (!['EEXIST', 'ENOTEMPTY'].includes(error.code) || await readOptional(file) === null) throw error;
|
|
41
|
+
}
|
|
42
|
+
} finally {
|
|
43
|
+
await rm(temporary, {recursive: true, force: true});
|
|
44
|
+
}
|
|
45
|
+
return {directory, file, database: parseDatabase(await readFile(file, 'utf8'))};
|
|
46
|
+
}
|
package/index.d.ts
CHANGED
|
@@ -7,7 +7,10 @@ export type Target =
|
|
|
7
7
|
'go' |
|
|
8
8
|
'haskell' |
|
|
9
9
|
'kotlin' |
|
|
10
|
-
'rust'
|
|
10
|
+
'rust' |
|
|
11
|
+
'erlang' |
|
|
12
|
+
'elixir' |
|
|
13
|
+
'gleam';
|
|
11
14
|
|
|
12
15
|
export interface Source {
|
|
13
16
|
path: string;
|
|
@@ -207,19 +210,27 @@ export interface NativeGoImport {
|
|
|
207
210
|
path: string;
|
|
208
211
|
}
|
|
209
212
|
|
|
213
|
+
export interface NativeErlangInclude {
|
|
214
|
+
path: string;
|
|
215
|
+
library?: boolean;
|
|
216
|
+
}
|
|
217
|
+
|
|
210
218
|
export interface NativeBindings {
|
|
211
219
|
types?: NativeTypeBinding[];
|
|
212
220
|
generators?: NativeGeneratorBinding[];
|
|
213
221
|
functions?: NativeFunctionBinding[];
|
|
214
222
|
rustCrate?: string;
|
|
215
223
|
goImports?: NativeGoImport[];
|
|
224
|
+
erlangIncludes?: NativeErlangInclude[];
|
|
216
225
|
}
|
|
217
226
|
|
|
218
227
|
export type DischargeStatus =
|
|
219
228
|
'proved' |
|
|
220
229
|
'exhaustively-checked' |
|
|
221
230
|
'property-tested' |
|
|
231
|
+
'measured' |
|
|
222
232
|
'runtime-checked' |
|
|
233
|
+
'default-handler' |
|
|
223
234
|
'assumed';
|
|
224
235
|
|
|
225
236
|
export interface ObligationEvidence {
|
package/native-examples.mjs
CHANGED
|
@@ -33,6 +33,9 @@ export async function exportNativePayments(options, selected) {
|
|
|
33
33
|
const config = JSON.parse(files['lawspec.json']);
|
|
34
34
|
config.machineBits = options.machineBits ?? 64;
|
|
35
35
|
files['lawspec.json'] = JSON.stringify(config, null, options.minify ? undefined : 2) + '\n';
|
|
36
|
+
const instructions = target === 'gleam' ? setup[target].replace(
|
|
37
|
+
'Prepare dependencies with gleam build, then run gleam test.',
|
|
38
|
+
'Prepare dependencies with node prepare.mjs before generation, then run gleam test.') : setup[target];
|
|
36
39
|
files['README.md'] = `# Native payments: ${target}
|
|
37
40
|
|
|
38
41
|
This project checks application-owned payment types against the shared LawSpec
|
|
@@ -45,11 +48,13 @@ Install LawSpec and the native dependencies described below, then run:
|
|
|
45
48
|
|
|
46
49
|
\`\`\`sh
|
|
47
50
|
lawspec check
|
|
48
|
-
lawspec generate
|
|
51
|
+
${target === 'gleam' ? 'node prepare.mjs\n' : ''}lawspec generate
|
|
49
52
|
${commands[target]}
|
|
50
53
|
\`\`\`
|
|
51
54
|
|
|
52
|
-
${
|
|
55
|
+
${instructions}
|
|
56
|
+
|
|
57
|
+
${target === 'gleam' ? 'The preparation command compiles dependencies in a temporary project using\nyour dependency requirements and lock file. It leaves application sources in\nplace, so their imports can be resolved after LawSpec generates the runtime.\n' : ''}
|
|
53
58
|
|
|
54
59
|
JavaScript and TypeScript projects require \`npm install\` before testing.
|
|
55
60
|
The specification includes exact decimal arithmetic and explicit USD/GBP examples.
|