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
@@ -91,6 +91,23 @@
91
91
  "2.3.22"
92
92
  ]
93
93
  },
94
+ "erlang": {
95
+ "otp": ["29", "30"],
96
+ "rebar3": ["3.27.1", "3.28"],
97
+ "proper": ["1.5.0", "1.5.1"]
98
+ },
99
+ "elixir": {
100
+ "otp": ["29", "30"],
101
+ "elixir": ["1.20", "1.21"],
102
+ "stream_data": ["1.4.0", "1.4.1"]
103
+ },
104
+ "gleam": {
105
+ "otp": ["29", "30"],
106
+ "gleam": ["1.18", "1.20"],
107
+ "gleam_stdlib": ["1.0.5", "1.0.6"],
108
+ "gleeunit": ["1.11.0", "1.11.1"],
109
+ "qcheck": ["1.0.5", "1.0.6"]
110
+ },
94
111
  "rust": {
95
112
  "rust": [
96
113
  "1.85",
@@ -116,6 +133,14 @@
116
133
  "0.2.19",
117
134
  "0.2.20"
118
135
  ]
136
+ },
137
+ "fsharp": {
138
+ "dotnet": ["10", "11"],
139
+ "xunit.v3": ["4.0.2", "4.1"]
140
+ },
141
+ "csharp": {
142
+ "dotnet": ["10", "11"],
143
+ "xunit.v3": ["4.0.2", "4.1"]
119
144
  }
120
145
  }
121
146
  }
package/core.wasm CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/core.wasm
3
- size: 37314915 bytes
4
- sha256: d5ef65c89a28298039c024cef7b5b61e9885feeacad8990099161a35a4ecfed5
3
+ size: 51506170 bytes
4
+ sha256: ecc1e0486be653a1ff48db04c5a6f144434e57483328d10ab351fb1072b87e06
package/doctor.mjs CHANGED
@@ -14,6 +14,7 @@ import path from "node:path";
14
14
  import os from "node:os";
15
15
  import { fileURLToPath } from "node:url";
16
16
  import { setup } from "./templates.mjs";
17
+ import { beamDoctor } from "./beam-doctor.mjs";
17
18
  const exec = promisify(execFile);
18
19
  const profiles = JSON.parse(
19
20
  await readFile(new URL("./compatibility.json", import.meta.url), "utf8"),
@@ -75,12 +76,13 @@ export async function doctor(target, root, plannedArtifacts = []) {
75
76
  ? ["src/main/java", "src/test/java"]
76
77
  : name === "kotlin"
77
78
  ? ["src/main/kotlin", "src/test/kotlin"]
78
- : ["src", ["python", "rust"].includes(name) ? "tests" : "test"];
79
+ : ["src", ["python", "rust", "fsharp", "csharp"].includes(name) ? "tests" : "test"];
79
80
  const sourceDir = target.sourceDir ?? defaults[0];
80
81
  const testDir = target.testDir ?? defaults[1];
81
82
  try {
82
83
  requireThat(major(process.versions.node) >= 22, "Node 22+ is required");
83
84
  const versions = {};
85
+ let coverage;
84
86
  if (["javascript", "typescript"].includes(name)) {
85
87
  const pkg = JSON.parse(
86
88
  await readFile(path.join(root, "package.json"), "utf8"),
@@ -461,13 +463,38 @@ export async function doctor(target, root, plannedArtifacts = []) {
461
463
  } finally {
462
464
  await rm(tmp, { recursive: true, force: true });
463
465
  }
466
+ } else if (name === "fsharp" || name === "csharp") {
467
+ // .NET: the SDK, xUnit v3 on Microsoft.Testing.Platform (selected in
468
+ // global.json), and projects that import the generated props.
469
+ const extension = name === "fsharp" ? ".fsproj" : ".csproj";
470
+ versions.dotnet = (await run(target.dotnet || "dotnet", ["--version"], root)).trim();
471
+ requireThat(major(versions.dotnet) >= 10, ".NET SDK 10+ is required");
472
+ const global = JSON.parse(await readFile(path.join(root, "global.json"), "utf8").catch(() => "{}"));
473
+ requireThat(global?.test?.runner === "Microsoft.Testing.Platform",
474
+ "global.json must select the Microsoft.Testing.Platform test runner: { \"test\": { \"runner\": \"Microsoft.Testing.Platform\" } }");
475
+ const projects = async (folder) =>
476
+ (await readdir(path.join(root, folder)).catch(() => [])).filter((file) => file.endsWith(extension));
477
+ const sources = await projects(sourceDir);
478
+ const tests = await projects(testDir);
479
+ requireThat(sources.length === 1, `Expected one ${extension} project in ${sourceDir}`);
480
+ requireThat(tests.length === 1, `Expected one ${extension} project in ${testDir}`);
481
+ const source = await readFile(path.join(root, sourceDir, sources[0]), "utf8");
482
+ const test = await readFile(path.join(root, testDir, tests[0]), "utf8");
483
+ requireThat(source.includes("LawSpec.Sources.props"), `${sourceDir}/${sources[0]} must import LawSpec.Sources.props`);
484
+ requireThat(test.includes("LawSpec.Tests.props"), `${testDir}/${tests[0]} must import LawSpec.Tests.props`);
485
+ versions["xunit.v3"] = test.match(/<PackageReference\s+Include="xunit\.v3"\s+Version="([^"]+)"/)?.[1];
486
+ requireThat(versions["xunit.v3"], `${testDir}/${tests[0]} must reference the xunit.v3 package`);
487
+ } else if (["erlang", "elixir", "gleam"].includes(name)) {
488
+ const beam = await beamDoctor(target, root, plannedArtifacts, run);
489
+ Object.assign(versions, beam.versions);
490
+ coverage = beam.coverage;
464
491
  } else throw new Error(`Unsupported target ${name}`);
465
492
  for (const dependency of Object.keys(profiles[name] || {}))
466
493
  requireThat(
467
494
  supported(name, dependency, versions[dependency]),
468
495
  `${dependency} ${versions[dependency] || "(unknown)"} is outside the verified ${name} profile [${profiles[name][dependency].join(", ")})`,
469
496
  );
470
- return { target: name, ok: true, versions };
497
+ return { target: name, ok: true, versions, ...(coverage ? {coverage} : {}) };
471
498
  } catch (error) {
472
499
  return {
473
500
  target: name,
@@ -0,0 +1,51 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "csharp",
10
+ "root": ".",
11
+ "nativeBindings": {
12
+ "functions": [
13
+ {
14
+ "declaration": "example.payments::addFee",
15
+ "native": [
16
+ "Domain",
17
+ "Payments",
18
+ "ApplyFee"
19
+ ]
20
+ },
21
+ {
22
+ "declaration": "example.payments::roundTrip",
23
+ "native": [
24
+ "Domain",
25
+ "Payments",
26
+ "Restore"
27
+ ]
28
+ },
29
+ {
30
+ "declaration": "example.payments::archive",
31
+ "native": [
32
+ "Domain",
33
+ "Payments",
34
+ "Store"
35
+ ]
36
+ }
37
+ ],
38
+ "generators": [
39
+ {
40
+ "type": "example.payments::type::Money",
41
+ "factory": [
42
+ "Domain",
43
+ "PaymentGenerators",
44
+ "Prices"
45
+ ]
46
+ }
47
+ ]
48
+ }
49
+ }
50
+ ]
51
+ }
@@ -0,0 +1,19 @@
1
+ // Application code over the generated payment types: C# binds functions,
2
+ // not types, so it uses LawSpec.Data's Money, Currency and Payment.
3
+ namespace Domain;
4
+
5
+ using LawSpec;
6
+ using LawSpec.Data;
7
+ using Microsoft.FSharp.Collections;
8
+ using Microsoft.FSharp.Core;
9
+
10
+ public static class Payments
11
+ {
12
+ private static readonly ExactDecimal Fee = new(2, -1);
13
+
14
+ public static Money ApplyFee(Money money) => new(money.Amount + Fee, money.Currency);
15
+
16
+ public static Payment Restore(Payment payment) => payment;
17
+
18
+ public static FSharpList<FSharpOption<Payment>> Store(FSharpList<FSharpOption<Payment>> payments) => payments;
19
+ }
@@ -0,0 +1,14 @@
1
+ // Prices the application considers representative: 1.00 to 2.00 euros.
2
+ namespace Domain;
3
+
4
+ using System.Numerics;
5
+ using FsCheck;
6
+ using FsCheck.Fluent;
7
+ using LawSpec;
8
+ using LawSpec.Data;
9
+
10
+ public static class PaymentGenerators
11
+ {
12
+ public static Arbitrary<Money> Prices() =>
13
+ Arb.From(Gen.Choose(100, 200).Select(cents => new Money(new ExactDecimal(new BigInteger(cents), -2), Currency.EUR)));
14
+ }
@@ -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).