lawspec 0.21.0 → 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.
@@ -0,0 +1,149 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "elixir",
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
+ "Dollars"
25
+ ],
26
+ "style": "unit",
27
+ "fields": []
28
+ },
29
+ {
30
+ "constructor": "EUR",
31
+ "native": [
32
+ "PaymentsDomain",
33
+ "Euros"
34
+ ],
35
+ "style": "unit",
36
+ "fields": []
37
+ },
38
+ {
39
+ "constructor": "GBP",
40
+ "native": [
41
+ "PaymentsDomain",
42
+ "Pounds"
43
+ ],
44
+ "style": "unit",
45
+ "fields": []
46
+ }
47
+ ]
48
+ },
49
+ {
50
+ "type": "example.payments::type::Money",
51
+ "native": [
52
+ "PaymentsDomain",
53
+ "Price"
54
+ ],
55
+ "constructors": [
56
+ {
57
+ "constructor": "Money",
58
+ "native": [
59
+ "PaymentsDomain",
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
+ "PaymentsDomain",
80
+ "PaymentStatus"
81
+ ],
82
+ "constructors": [
83
+ {
84
+ "constructor": "Paid",
85
+ "native": [
86
+ "PaymentsDomain",
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
+ "PaymentsDomain",
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
+ "PaymentsDomain",
119
+ "apply_fee"
120
+ ]
121
+ },
122
+ {
123
+ "declaration": "example.payments::roundTrip",
124
+ "native": [
125
+ "PaymentsDomain",
126
+ "restore"
127
+ ]
128
+ },
129
+ {
130
+ "declaration": "example.payments::archive",
131
+ "native": [
132
+ "PaymentsDomain",
133
+ "store"
134
+ ]
135
+ }
136
+ ],
137
+ "generators": [
138
+ {
139
+ "type": "example.payments::type::Money",
140
+ "factory": [
141
+ "PaymentGenerators",
142
+ "prices"
143
+ ]
144
+ }
145
+ ]
146
+ }
147
+ }
148
+ ]
149
+ }
@@ -0,0 +1,54 @@
1
+ defmodule PaymentsDomain.Dollars do
2
+ defstruct []
3
+ @type t :: %__MODULE__{}
4
+ end
5
+
6
+ defmodule PaymentsDomain.Euros do
7
+ defstruct []
8
+ @type t :: %__MODULE__{}
9
+ end
10
+
11
+ defmodule PaymentsDomain.Pounds do
12
+ defstruct []
13
+ @type t :: %__MODULE__{}
14
+ end
15
+
16
+ defmodule PaymentsDomain.CurrencyCode do
17
+ @type t :: PaymentsDomain.Dollars.t() | PaymentsDomain.Euros.t() | PaymentsDomain.Pounds.t()
18
+ end
19
+
20
+ defmodule PaymentsDomain.Price do
21
+ # Application field names and order deliberately differ from the specification.
22
+ defstruct [:unit, :major]
23
+ @type t :: %__MODULE__{unit: PaymentsDomain.CurrencyCode.t(), major: :lawspec_beam_scalar.decimal()}
24
+ end
25
+
26
+ defmodule PaymentsDomain.Settled do
27
+ defstruct [:price]
28
+ @type t :: %__MODULE__{price: PaymentsDomain.Price.t()}
29
+ end
30
+
31
+ defmodule PaymentsDomain.Rejected do
32
+ defstruct [:explanation]
33
+ @type t :: %__MODULE__{explanation: String.t()}
34
+ end
35
+
36
+ defmodule PaymentsDomain.PaymentStatus do
37
+ @type t :: PaymentsDomain.Settled.t() | PaymentsDomain.Rejected.t()
38
+ end
39
+
40
+ defmodule PaymentsDomain do
41
+ alias PaymentsDomain.{Price, PaymentStatus}
42
+
43
+ @spec apply_fee(Price.t()) :: Price.t()
44
+ def apply_fee(%Price{major: amount} = price) do
45
+ fee = :lawspec_beam_scalar.decimal(2, -1)
46
+ %{price | major: :lawspec_beam_scalar.binary("+", amount, fee, "Decimal", "Decimal")}
47
+ end
48
+
49
+ @spec restore(PaymentStatus.t()) :: PaymentStatus.t()
50
+ def restore(payment), do: payment
51
+
52
+ @spec store([:nothing | {:just, PaymentStatus.t()}]) :: [:nothing | {:just, PaymentStatus.t()}]
53
+ def store(payments), do: payments
54
+ end
@@ -0,0 +1,9 @@
1
+ defmodule PaymentGenerators do
2
+ # StreamData.map preserves the integer generator's native shrink tree.
3
+ def prices do
4
+ StreamData.map(StreamData.integer(100..200), fn cents ->
5
+ %PaymentsDomain.Price{unit: %PaymentsDomain.Euros{},
6
+ major: :lawspec_beam_scalar.decimal(cents, -2)}
7
+ end)
8
+ end
9
+ end
@@ -0,0 +1,2 @@
1
+ %% Application field order deliberately differs from the specification.
2
+ -record(price, {unit, major}).
@@ -0,0 +1,148 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "erlang",
10
+ "root": ".",
11
+ "nativeBindings": {
12
+ "types": [
13
+ {
14
+ "type": "example.payments::type::Currency",
15
+ "native": [
16
+ "payments_domain",
17
+ "currency_code"
18
+ ],
19
+ "constructors": [
20
+ {
21
+ "constructor": "USD",
22
+ "native": [
23
+ "dollars"
24
+ ],
25
+ "style": "unit",
26
+ "fields": []
27
+ },
28
+ {
29
+ "constructor": "EUR",
30
+ "native": [
31
+ "euros"
32
+ ],
33
+ "style": "unit",
34
+ "fields": []
35
+ },
36
+ {
37
+ "constructor": "GBP",
38
+ "native": [
39
+ "pounds"
40
+ ],
41
+ "style": "unit",
42
+ "fields": []
43
+ }
44
+ ]
45
+ },
46
+ {
47
+ "type": "example.payments::type::Money",
48
+ "native": [
49
+ "payments_domain",
50
+ "price"
51
+ ],
52
+ "constructors": [
53
+ {
54
+ "constructor": "Money",
55
+ "native": [
56
+ "price"
57
+ ],
58
+ "style": "record",
59
+ "fields": [
60
+ {
61
+ "field": "amount",
62
+ "native": "major"
63
+ },
64
+ {
65
+ "field": "currency",
66
+ "native": "unit"
67
+ }
68
+ ]
69
+ }
70
+ ]
71
+ },
72
+ {
73
+ "type": "example.payments::type::Payment",
74
+ "native": [
75
+ "payments_domain",
76
+ "payment_status"
77
+ ],
78
+ "constructors": [
79
+ {
80
+ "constructor": "Paid",
81
+ "native": [
82
+ "settled"
83
+ ],
84
+ "style": "variant",
85
+ "fields": [
86
+ {
87
+ "field": "value",
88
+ "native": "price"
89
+ }
90
+ ]
91
+ },
92
+ {
93
+ "constructor": "Declined",
94
+ "native": [
95
+ "rejected"
96
+ ],
97
+ "style": "variant",
98
+ "fields": [
99
+ {
100
+ "field": "reason",
101
+ "native": "explanation"
102
+ }
103
+ ]
104
+ }
105
+ ]
106
+ }
107
+ ],
108
+ "functions": [
109
+ {
110
+ "declaration": "example.payments::addFee",
111
+ "native": [
112
+ "payments_domain",
113
+ "apply_fee"
114
+ ]
115
+ },
116
+ {
117
+ "declaration": "example.payments::roundTrip",
118
+ "native": [
119
+ "payments_domain",
120
+ "restore"
121
+ ]
122
+ },
123
+ {
124
+ "declaration": "example.payments::archive",
125
+ "native": [
126
+ "payments_domain",
127
+ "store"
128
+ ]
129
+ }
130
+ ],
131
+ "generators": [
132
+ {
133
+ "type": "example.payments::type::Money",
134
+ "factory": [
135
+ "payment_generators",
136
+ "prices"
137
+ ]
138
+ }
139
+ ],
140
+ "erlangIncludes": [
141
+ {
142
+ "path": "payments_domain.hrl"
143
+ }
144
+ ]
145
+ }
146
+ }
147
+ ]
148
+ }
@@ -0,0 +1,21 @@
1
+ %% Application-owned payment types; no generated domain declarations.
2
+ -module(payments_domain).
3
+ -export([apply_fee/1, restore/1, store/1]).
4
+ -export_type([currency_code/0, price/0, payment_status/0]).
5
+ -include("payments_domain.hrl").
6
+
7
+ -type currency_code() :: dollars | euros | pounds.
8
+ -type price() :: #price{unit :: currency_code(), major :: lawspec_beam_scalar:decimal()}.
9
+ -type payment_status() :: {settled, price()} | {rejected, binary()}.
10
+
11
+ -spec apply_fee(price()) -> price().
12
+ apply_fee(#price{major = Amount} = Price) ->
13
+ Fee = lawspec_beam_scalar:decimal(2, -1),
14
+ Price#price{major = lawspec_beam_scalar:binary(
15
+ <<"+">>, Amount, Fee, <<"Decimal">>, <<"Decimal">>)}.
16
+
17
+ -spec restore(payment_status()) -> payment_status().
18
+ restore(Payment) -> Payment.
19
+
20
+ -spec store([nothing | {just, payment_status()}]) -> [nothing | {just, payment_status()}].
21
+ store(Payments) -> Payments.
@@ -0,0 +1,10 @@
1
+ %% PropEr's native bind retains shrinking of the integer number of cents.
2
+ -module(payment_generators).
3
+ -export([prices/0]).
4
+ -include("payments_domain.hrl").
5
+
6
+ prices() ->
7
+ proper_types:bind(proper_types:integer(100, 200), fun(Cents) ->
8
+ proper_types:exactly(#price{unit = euros,
9
+ major = lawspec_beam_scalar:decimal(Cents, -2)})
10
+ end, false).
@@ -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
+ }
@@ -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
+ }