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
@@ -91,6 +91,24 @@
91
91
  "2.3.22"
92
92
  ]
93
93
  },
94
+ "scala": {
95
+ "java": ["25", "27"],
96
+ "scala-cli": ["1.16", "1.17"],
97
+ "scala": ["3.9", "3.11"],
98
+ "scalacheck": ["1.20.0", "1.21"],
99
+ "scalatest": ["3.2.20", "3.3"]
100
+ },
101
+ "clojure": {
102
+ "java": ["25", "26"],
103
+ "clojure": ["1.12", "1.13"],
104
+ "test.check": ["1.1.3", "1.2"]
105
+ },
106
+ "groovy": {
107
+ "java": ["25", "26"],
108
+ "gradle": ["9.1", "9.4"],
109
+ "groovy": ["5.0", "5.1"],
110
+ "spock": ["2.5", "2.6"]
111
+ },
94
112
  "erlang": {
95
113
  "otp": ["29", "30"],
96
114
  "rebar3": ["3.27.1", "3.28"],
@@ -133,6 +151,17 @@
133
151
  "0.2.19",
134
152
  "0.2.20"
135
153
  ]
154
+ },
155
+ "fsharp": {
156
+ "dotnet": ["10", "11"],
157
+ "xunit.v3": ["4.0.2", "4.1"]
158
+ },
159
+ "csharp": {
160
+ "dotnet": ["10", "11"],
161
+ "xunit.v3": ["4.0.2", "4.1"]
162
+ },
163
+ "swift": {
164
+ "swift": ["6.2", "6.4"]
136
165
  }
137
166
  }
138
167
  }
package/core.wasm CHANGED
@@ -1,4 +1,4 @@
1
1
  [diffend] Oversized file quarantined before diffing.
2
2
  name: package/core.wasm
3
- size: 40449445 bytes
4
- sha256: 9858bc34a22fc2dcada374c624232b1718b861558656cb4bdc66210976e651aa
3
+ size: 57941480 bytes
4
+ sha256: a5297e71e959bce14e302c98c107c570bb57ba11a60ff9cfd41c6a7876b2bfed
package/doctor.mjs CHANGED
@@ -76,7 +76,13 @@ export async function doctor(target, root, plannedArtifacts = []) {
76
76
  ? ["src/main/java", "src/test/java"]
77
77
  : name === "kotlin"
78
78
  ? ["src/main/kotlin", "src/test/kotlin"]
79
- : ["src", ["python", "rust"].includes(name) ? "tests" : "test"];
79
+ : name === "scala"
80
+ ? ["src/main/scala", "src/test/scala"]
81
+ : name === "groovy"
82
+ ? ["src/main/groovy", "src/test/groovy"]
83
+ : name === "clojure"
84
+ ? ["src", "test"]
85
+ : ["src", ["python", "rust", "fsharp", "csharp", "swift"].includes(name) ? "tests" : "test"];
80
86
  const sourceDir = target.sourceDir ?? defaults[0];
81
87
  const testDir = target.testDir ?? defaults[1];
82
88
  try {
@@ -463,6 +469,118 @@ export async function doctor(target, root, plannedArtifacts = []) {
463
469
  } finally {
464
470
  await rm(tmp, { recursive: true, force: true });
465
471
  }
472
+ } else if (name === "scala") {
473
+ // Scala CLI reads the build from project.scala's directives; the
474
+ // sources keep sbt's layout, which Scala CLI compiles as main and test.
475
+ const command = target.scala || "scala";
476
+ const reported = await run(command, ["version"], root);
477
+ versions["scala-cli"] = reported.match(/Scala code runner version:\s*(\S+)/)?.[1] ?? reported.match(/Scala CLI version:\s*(\S+)/)?.[1];
478
+ // java --version prints to standard output (-version prints to standard error).
479
+ const java = await run("java", ["--version"], root).catch((error) => String(error.message));
480
+ versions.java = java.match(/^(?:openjdk|java)\s+(\S+)/m)?.[1];
481
+ requireThat(major(versions.java ?? "0") >= 25, "Scala requires JDK 25+");
482
+ const project = await readFile(path.join(root, "project.scala"), "utf8").catch(() => "");
483
+ const directive = (pattern) => project.match(pattern)?.[1];
484
+ versions.scala = directive(/\/\/>\s*using\s+scala\s+"?([0-9][^\s"]*)/);
485
+ versions.scalacheck = directive(/\/\/>\s*using\s+test\.deps?\s+"?org\.scalacheck::scalacheck:([^\s"]+)/);
486
+ versions.scalatest = directive(/\/\/>\s*using\s+test\.deps?\s+"?org\.scalatest::scalatest-funsuite:([^\s"]+)/);
487
+ requireThat(versions.scala, "project.scala must name Scala 3 (//> using scala 3.9.0)");
488
+ requireThat(versions.scalacheck, "project.scala must add ScalaCheck (//> using test.dep org.scalacheck::scalacheck:1.20.0)");
489
+ requireThat(versions.scalatest, "project.scala must add ScalaTest (//> using test.dep org.scalatest::scalatest-funsuite:3.2.20)");
490
+ requireThat(sourceDir.split("/").includes("main") || !sourceDir.split("/").includes("test"),
491
+ "The Scala source directory must not be under a directory named test, which Scala CLI compiles as tests");
492
+ requireThat(testDir.split("/").includes("test"),
493
+ "The Scala test directory must be under a directory named test, which Scala CLI compiles as tests");
494
+ } else if (name === "clojure") {
495
+ // Clojure over the Java model: deps.edn's :test alias runs LawSpec's
496
+ // runner, which compiles java/ into target/classes first.
497
+ const deps = await readFile(path.join(root, "deps.edn"), "utf8");
498
+ requireThat(deps.includes("\"target/classes\"") && deps.includes("\"target/test-classes\""),
499
+ "deps.edn must put target/classes on :paths and target/test-classes on the :test alias's :extra-paths");
500
+ requireThat(deps.includes("lawspec.test-runner"), "deps.edn's :test alias must run lawspec.test-runner (:main-opts [\"-m\" \"lawspec.test-runner\"])");
501
+ const command = target.clojure || "clojure";
502
+ const classpath = await run(command, ["-Spath", "-M:test"], root);
503
+ versions.clojure = classpath.match(/clojure-(\d+\.\d+\.\d+)\.jar/)?.[1];
504
+ versions["test.check"] = classpath.match(/test\.check-(\d+\.\d+\.\d+)\.jar/)?.[1];
505
+ requireThat(versions.clojure, "Missing org.clojure/clojure in deps.edn");
506
+ requireThat(versions["test.check"], "Missing org.clojure/test.check in deps.edn's :test alias");
507
+ versions.java = (await run("java", ["--version"], root)).match(/\b(\d+)(\.\d+)*/)?.[0];
508
+ requireThat(major(versions.java) >= 25, "Clojure needs a JDK 25+ (its compiler builds the Java model)");
509
+ } else if (name === "groovy") {
510
+ // Groovy over the Java model: Gradle's groovy plugin compiles both
511
+ // jointly, Spock runs the specifications and JetCheck the properties.
512
+ const command =
513
+ target.gradle ||
514
+ (await access(path.join(root, "gradlew")).then(
515
+ () => path.join(root, "gradlew"),
516
+ () => "gradle",
517
+ ));
518
+ const tmp = await mkdtemp(path.join(os.tmpdir(), "lawspec-gradle-"));
519
+ try {
520
+ const report = path.join(tmp, "report.json");
521
+ const init = path.join(tmp, "inspect.gradle");
522
+ await writeFile(
523
+ init,
524
+ `import groovy.json.JsonOutput\ngradle.projectsEvaluated {\n def p = gradle.rootProject\n p.tasks.register('lawspecEnvironmentReport') { doLast {\n def j = p.tasks.findByName('test')\n def deps = p.configurations.testRuntimeClasspath.resolvedConfiguration.resolvedArtifacts.collect { [name: it.moduleVersion.id.group + ':' + it.name, version: it.moduleVersion.id.version] }\n new File(System.getenv('LAWSPEC_REPORT')).text = JsonOutput.toJson([gradle: gradle.gradleVersion, java: System.getProperty('java.version'), testJava: j?.javaLauncher?.get()?.metadata?.languageVersion?.asInt(), runner: j?.options?.class?.name, enabled: j.enabled, includes: j.filter.includePatterns, excludes: j.filter.excludePatterns, sourceDirs: p.sourceSets.main.groovy.srcDirs.collect { it.canonicalPath }, testDirs: p.sourceSets.test.groovy.srcDirs.collect { it.canonicalPath }, dependencies: deps])\n } }\n}\n`,
525
+ );
526
+ await run(command, ["--no-daemon", "--console=plain", "-I", init, "lawspecEnvironmentReport"], root,
527
+ { env: { ...process.env, LAWSPEC_REPORT: report } });
528
+ const data = JSON.parse(await readFile(report, "utf8"));
529
+ requireThat(/^9\.[1-3]\./.test(data.gradle) && major(data.java) >= 25 && data.testJava >= 25,
530
+ "Groovy requires Gradle 9.1–9.3 and a Java 25+ toolchain");
531
+ requireThat(data.runner?.includes("JUnitPlatform") && data.enabled && !data.includes.length && !data.excludes.length,
532
+ "Gradle test must enable unfiltered JUnit Platform execution");
533
+ // The Java model compiles jointly with the Groovy sources.
534
+ // A custom sourceDir or testDir holds the Java model too.
535
+ const sources = target.sourceDir ? [sourceDir] : [sourceDir, "src/main/java"];
536
+ const tests = target.testDir ? [testDir] : [testDir, "src/test/java"];
537
+ requireThat(
538
+ sources.every((d) => data.sourceDirs.includes(path.join(root, d))) &&
539
+ tests.every((d) => data.testDirs.includes(path.join(root, d))),
540
+ `Gradle's Groovy source sets must include ${sources.join(" and ")}, and ${tests.join(" and ")}`,
541
+ );
542
+ const version = (dep) => data.dependencies.find((d) => d.name === dep)?.version;
543
+ versions.groovy = version("org.apache.groovy:groovy");
544
+ versions.spock = version("org.spockframework:spock-core")?.replace(/-groovy-.*/, "");
545
+ requireThat(versions.groovy, "Missing org.apache.groovy:groovy");
546
+ requireThat(versions.spock, "Missing org.spockframework:spock-core");
547
+ requireThat(version("org.jetbrains:jetCheck") === "0.3.0", "Missing org.jetbrains:jetCheck:0.3.0");
548
+ Object.assign(versions, { gradle: data.gradle, java: data.java });
549
+ } finally {
550
+ await rm(tmp, { recursive: true, force: true });
551
+ }
552
+ } else if (name === "fsharp" || name === "csharp") {
553
+ // .NET: the SDK, xUnit v3 on Microsoft.Testing.Platform (selected in
554
+ // global.json), and projects that import the generated props.
555
+ const extension = name === "fsharp" ? ".fsproj" : ".csproj";
556
+ versions.dotnet = (await run(target.dotnet || "dotnet", ["--version"], root)).trim();
557
+ requireThat(major(versions.dotnet) >= 10, ".NET SDK 10+ is required");
558
+ const global = JSON.parse(await readFile(path.join(root, "global.json"), "utf8").catch(() => "{}"));
559
+ requireThat(global?.test?.runner === "Microsoft.Testing.Platform",
560
+ "global.json must select the Microsoft.Testing.Platform test runner: { \"test\": { \"runner\": \"Microsoft.Testing.Platform\" } }");
561
+ const projects = async (folder) =>
562
+ (await readdir(path.join(root, folder)).catch(() => [])).filter((file) => file.endsWith(extension));
563
+ const sources = await projects(sourceDir);
564
+ const tests = await projects(testDir);
565
+ requireThat(sources.length === 1, `Expected one ${extension} project in ${sourceDir}`);
566
+ requireThat(tests.length === 1, `Expected one ${extension} project in ${testDir}`);
567
+ const source = await readFile(path.join(root, sourceDir, sources[0]), "utf8");
568
+ const test = await readFile(path.join(root, testDir, tests[0]), "utf8");
569
+ requireThat(source.includes("LawSpec.Sources.props"), `${sourceDir}/${sources[0]} must import LawSpec.Sources.props`);
570
+ requireThat(test.includes("LawSpec.Tests.props"), `${testDir}/${tests[0]} must import LawSpec.Tests.props`);
571
+ versions["xunit.v3"] = test.match(/<PackageReference\s+Include="xunit\.v3"\s+Version="([^"]+)"/)?.[1];
572
+ requireThat(versions["xunit.v3"], `${testDir}/${tests[0]} must reference the xunit.v3 package`);
573
+ } else if (name === "swift") {
574
+ // Swift: the toolchain (SwiftPM and Swift Testing come with it) and a
575
+ // package that declares the generated runtime targets under lawspec/.
576
+ const reported = await run(target.swift || "swift", ["--version"], root);
577
+ versions.swift = reported.match(/Swift version (\d+\.\d+(?:\.\d+)?)/)?.[1];
578
+ requireThat(versions.swift, "Cannot determine the Swift version (swift --version)");
579
+ const manifest = await readFile(path.join(root, "Package.swift"), "utf8").catch(() => "");
580
+ requireThat(manifest, "Missing Package.swift");
581
+ for (const module of ["LawSpecRuntime", "LawSpecTesting"])
582
+ requireThat(manifest.includes(`"lawspec/${module}"`),
583
+ `Package.swift must declare the target ${module} at lawspec/${module}, as lawspec init does`);
466
584
  } else if (["erlang", "elixir", "gleam"].includes(name)) {
467
585
  const beam = await beamDoctor(target, root, plannedArtifacts, run);
468
586
  Object.assign(versions, beam.versions);
@@ -0,0 +1,51 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "clojure",
10
+ "root": ".",
11
+ "nativeBindings": {
12
+ "functions": [
13
+ {
14
+ "declaration": "example.payments::addFee",
15
+ "native": [
16
+ "domain",
17
+ "payments",
18
+ "apply_fee"
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
+ "payment_generators",
44
+ "prices"
45
+ ]
46
+ }
47
+ ]
48
+ }
49
+ }
50
+ ]
51
+ }
@@ -0,0 +1,11 @@
1
+ ;; Application code over the model's payment data: Clojure binds functions,
2
+ ;; not types, so money is {:lawspec/tag :money/money :amount 1.20M
3
+ ;; :currency :currency/eur}.
4
+ (ns domain.payments
5
+ "Payments: the functions lawspec.json binds.")
6
+
7
+ (defn apply-fee [money] (update money :amount + 0.2M))
8
+
9
+ (defn restore [payment] payment)
10
+
11
+ (defn store [payments] payments)
@@ -0,0 +1,9 @@
1
+ ;; Application generators lawspec.json binds: test.check generators of the
2
+ ;; model's tagged values.
3
+ (ns domain.payment-generators
4
+ "Prices between 1.00 and 2.00 euros."
5
+ (:require [clojure.test.check.generators :as gen]))
6
+
7
+ (defn prices []
8
+ (gen/fmap (fn [cents] {:lawspec/tag :money/money :amount (BigDecimal/valueOf (long cents) 2) :currency :currency/eur})
9
+ (gen/choose 100 200)))
@@ -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,167 @@
1
+ {
2
+ "version": 1,
3
+ "sources": [
4
+ "laws"
5
+ ],
6
+ "machineBits": 64,
7
+ "targets": [
8
+ {
9
+ "language": "fsharp",
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
+ "PaymentStatus",
99
+ "Settled"
100
+ ],
101
+ "style": "variant",
102
+ "fields": [
103
+ {
104
+ "field": "value",
105
+ "native": "price"
106
+ }
107
+ ]
108
+ },
109
+ {
110
+ "constructor": "Declined",
111
+ "native": [
112
+ "Domain",
113
+ "PaymentsDomain",
114
+ "PaymentStatus",
115
+ "Rejected"
116
+ ],
117
+ "style": "variant",
118
+ "fields": [
119
+ {
120
+ "field": "reason",
121
+ "native": "explanation"
122
+ }
123
+ ]
124
+ }
125
+ ]
126
+ }
127
+ ],
128
+ "functions": [
129
+ {
130
+ "declaration": "example.payments::addFee",
131
+ "native": [
132
+ "Domain",
133
+ "PaymentsDomain",
134
+ "apply_fee"
135
+ ]
136
+ },
137
+ {
138
+ "declaration": "example.payments::roundTrip",
139
+ "native": [
140
+ "Domain",
141
+ "PaymentsDomain",
142
+ "restore"
143
+ ]
144
+ },
145
+ {
146
+ "declaration": "example.payments::archive",
147
+ "native": [
148
+ "Domain",
149
+ "PaymentsDomain",
150
+ "store"
151
+ ]
152
+ }
153
+ ],
154
+ "generators": [
155
+ {
156
+ "type": "example.payments::type::Money",
157
+ "factory": [
158
+ "Domain",
159
+ "PaymentGenerators",
160
+ "prices"
161
+ ]
162
+ }
163
+ ]
164
+ }
165
+ }
166
+ ]
167
+ }
@@ -0,0 +1,23 @@
1
+ /// Application types, independent of generated LawSpec domain declarations.
2
+ module Domain.PaymentsDomain
3
+
4
+ open LawSpec
5
+
6
+ type CurrencyCode =
7
+ | Dollars
8
+ | Euros
9
+ | Pounds
10
+
11
+ type Price = { major: ExactDecimal; unit: CurrencyCode }
12
+
13
+ type PaymentStatus =
14
+ | Settled of price: Price
15
+ | Rejected of explanation: string
16
+
17
+ let private fee: ExactDecimal = { Coefficient = 2I; Exponent = -1 }
18
+
19
+ let apply_fee (price: Price) : Price = { price with major = price.major + fee }
20
+
21
+ let restore (payment: PaymentStatus) : PaymentStatus = payment
22
+
23
+ let store (payments: PaymentStatus option list) : PaymentStatus option list = payments
@@ -0,0 +1,11 @@
1
+ /// Prices the application considers representative: 1.00 to 2.00 euros.
2
+ module Domain.PaymentGenerators
3
+
4
+ open FsCheck
5
+ open FsCheck.FSharp
6
+ open Domain.PaymentsDomain
7
+
8
+ let prices () : Arbitrary<Price> =
9
+ Gen.choose (100, 200)
10
+ |> Gen.map (fun cents -> { major = { Coefficient = bigint cents; Exponent = -2 }; unit = Euros })
11
+ |> Arb.fromGen