lawspec 0.23.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.
- package/CHANGELOG.md +101 -1
- package/README.md +3 -3
- package/bin/lawspec.mjs +15 -3
- package/build.json +170 -40
- package/compatibility.json +21 -0
- package/core.wasm +2 -2
- package/doctor.mjs +98 -1
- package/examples/native-payments/clojure/lawspec.json +51 -0
- package/examples/native-payments/clojure/src/domain/payments.clj +11 -0
- package/examples/native-payments/clojure/test/domain/payment_generators.clj +9 -0
- package/examples/native-payments/groovy/lawspec.json +165 -0
- package/examples/native-payments/groovy/src/main/groovy/domain/PaymentsDomain.groovy +49 -0
- package/examples/native-payments/groovy/src/test/groovy/domain/PaymentGenerators.groovy +15 -0
- package/examples/native-payments/scala/lawspec.json +165 -0
- package/examples/native-payments/scala/src/main/scala/domain/PaymentsDomain.scala +18 -0
- package/examples/native-payments/scala/src/test/scala/domain/PaymentGenerators.scala +8 -0
- package/examples/native-payments/swift/lawspec.json +154 -0
- package/examples/native-payments/swift/src/Native/PaymentsDomain.swift +35 -0
- package/examples/native-payments/swift/tests/Native/PaymentGenerators.swift +12 -0
- package/index.d.ts +5 -1
- package/package.json +1 -1
- package/scala-coverage.mjs +56 -0
- package/templates.mjs +65 -3
- package/test-command.mjs +78 -2
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
|
-
:
|
|
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,86 @@ 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
|
+
}
|
|
466
552
|
} else if (name === "fsharp" || name === "csharp") {
|
|
467
553
|
// .NET: the SDK, xUnit v3 on Microsoft.Testing.Platform (selected in
|
|
468
554
|
// global.json), and projects that import the generated props.
|
|
@@ -484,6 +570,17 @@ export async function doctor(target, root, plannedArtifacts = []) {
|
|
|
484
570
|
requireThat(test.includes("LawSpec.Tests.props"), `${testDir}/${tests[0]} must import LawSpec.Tests.props`);
|
|
485
571
|
versions["xunit.v3"] = test.match(/<PackageReference\s+Include="xunit\.v3"\s+Version="([^"]+)"/)?.[1];
|
|
486
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`);
|
|
487
584
|
} else if (["erlang", "elixir", "gleam"].includes(name)) {
|
|
488
585
|
const beam = await beamDoctor(target, root, plannedArtifacts, run);
|
|
489
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,165 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"sources": [
|
|
4
|
+
"laws"
|
|
5
|
+
],
|
|
6
|
+
"machineBits": 64,
|
|
7
|
+
"targets": [
|
|
8
|
+
{
|
|
9
|
+
"language": "groovy",
|
|
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
|
+
"Settled"
|
|
99
|
+
],
|
|
100
|
+
"style": "variant",
|
|
101
|
+
"fields": [
|
|
102
|
+
{
|
|
103
|
+
"field": "value",
|
|
104
|
+
"native": "price"
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"constructor": "Declined",
|
|
110
|
+
"native": [
|
|
111
|
+
"domain",
|
|
112
|
+
"PaymentsDomain",
|
|
113
|
+
"Rejected"
|
|
114
|
+
],
|
|
115
|
+
"style": "variant",
|
|
116
|
+
"fields": [
|
|
117
|
+
{
|
|
118
|
+
"field": "reason",
|
|
119
|
+
"native": "explanation"
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
"functions": [
|
|
127
|
+
{
|
|
128
|
+
"declaration": "example.payments::addFee",
|
|
129
|
+
"native": [
|
|
130
|
+
"domain",
|
|
131
|
+
"PaymentsDomain",
|
|
132
|
+
"apply_fee"
|
|
133
|
+
]
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"declaration": "example.payments::roundTrip",
|
|
137
|
+
"native": [
|
|
138
|
+
"domain",
|
|
139
|
+
"PaymentsDomain",
|
|
140
|
+
"restore"
|
|
141
|
+
]
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
"declaration": "example.payments::archive",
|
|
145
|
+
"native": [
|
|
146
|
+
"domain",
|
|
147
|
+
"PaymentsDomain",
|
|
148
|
+
"store"
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
"generators": [
|
|
153
|
+
{
|
|
154
|
+
"type": "example.payments::type::Money",
|
|
155
|
+
"factory": [
|
|
156
|
+
"domain",
|
|
157
|
+
"PaymentGenerators",
|
|
158
|
+
"prices"
|
|
159
|
+
]
|
|
160
|
+
}
|
|
161
|
+
]
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
package domain
|
|
2
|
+
|
|
3
|
+
import groovy.transform.CompileStatic
|
|
4
|
+
import lawspec.runtime.LawSpecRuntime.Maybe
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Application types, independent of generated LawSpec domain declarations.
|
|
8
|
+
* The records write out their constructors and accessors, which the Java
|
|
9
|
+
* model reads through Groovy's joint-compilation stubs.
|
|
10
|
+
*/
|
|
11
|
+
@CompileStatic
|
|
12
|
+
final class PaymentsDomain {
|
|
13
|
+
private PaymentsDomain() {}
|
|
14
|
+
|
|
15
|
+
enum CurrencyCode { Dollars, Euros, Pounds }
|
|
16
|
+
|
|
17
|
+
static record Price(BigDecimal major, CurrencyCode unit) {
|
|
18
|
+
Price(BigDecimal major, CurrencyCode unit) {
|
|
19
|
+
this.major = major
|
|
20
|
+
this.unit = unit
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
BigDecimal major() { major }
|
|
24
|
+
|
|
25
|
+
CurrencyCode unit() { unit }
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
static interface PaymentStatus {}
|
|
29
|
+
|
|
30
|
+
static record Settled(Price price) implements PaymentStatus {
|
|
31
|
+
Settled(Price price) { this.price = price }
|
|
32
|
+
|
|
33
|
+
Price price() { price }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
static record Rejected(String explanation) implements PaymentStatus {
|
|
37
|
+
Rejected(String explanation) { this.explanation = explanation }
|
|
38
|
+
|
|
39
|
+
String explanation() { explanation }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
static Price apply_fee(Price price) {
|
|
43
|
+
new Price(price.major() + new BigDecimal('0.2'), price.unit())
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static PaymentStatus restore(PaymentStatus payment) { payment }
|
|
47
|
+
|
|
48
|
+
static List<Maybe<PaymentStatus>> store(List<Maybe<PaymentStatus>> payments) { payments }
|
|
49
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package domain
|
|
2
|
+
|
|
3
|
+
import groovy.transform.CompileStatic
|
|
4
|
+
import org.jetbrains.jetCheck.Generator
|
|
5
|
+
|
|
6
|
+
@CompileStatic
|
|
7
|
+
final class PaymentGenerators {
|
|
8
|
+
private PaymentGenerators() {}
|
|
9
|
+
|
|
10
|
+
static Generator<PaymentsDomain.Price> prices() {
|
|
11
|
+
Generator.integers(100, 200).map { Integer cents ->
|
|
12
|
+
new PaymentsDomain.Price(new BigDecimal(BigInteger.valueOf(cents), 2), PaymentsDomain.CurrencyCode.Euros)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"sources": [
|
|
4
|
+
"laws"
|
|
5
|
+
],
|
|
6
|
+
"machineBits": 64,
|
|
7
|
+
"targets": [
|
|
8
|
+
{
|
|
9
|
+
"language": "scala",
|
|
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
|
+
"Settled"
|
|
99
|
+
],
|
|
100
|
+
"style": "variant",
|
|
101
|
+
"fields": [
|
|
102
|
+
{
|
|
103
|
+
"field": "value",
|
|
104
|
+
"native": "price"
|
|
105
|
+
}
|
|
106
|
+
]
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
"constructor": "Declined",
|
|
110
|
+
"native": [
|
|
111
|
+
"domain",
|
|
112
|
+
"PaymentsDomain",
|
|
113
|
+
"Rejected"
|
|
114
|
+
],
|
|
115
|
+
"style": "variant",
|
|
116
|
+
"fields": [
|
|
117
|
+
{
|
|
118
|
+
"field": "reason",
|
|
119
|
+
"native": "explanation"
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
}
|
|
123
|
+
]
|
|
124
|
+
}
|
|
125
|
+
],
|
|
126
|
+
"functions": [
|
|
127
|
+
{
|
|
128
|
+
"declaration": "example.payments::addFee",
|
|
129
|
+
"native": [
|
|
130
|
+
"domain",
|
|
131
|
+
"PaymentsDomain",
|
|
132
|
+
"apply_fee"
|
|
133
|
+
]
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
"declaration": "example.payments::roundTrip",
|
|
137
|
+
"native": [
|
|
138
|
+
"domain",
|
|
139
|
+
"PaymentsDomain",
|
|
140
|
+
"restore"
|
|
141
|
+
]
|
|
142
|
+
},
|
|
143
|
+
{
|
|
144
|
+
"declaration": "example.payments::archive",
|
|
145
|
+
"native": [
|
|
146
|
+
"domain",
|
|
147
|
+
"PaymentsDomain",
|
|
148
|
+
"store"
|
|
149
|
+
]
|
|
150
|
+
}
|
|
151
|
+
],
|
|
152
|
+
"generators": [
|
|
153
|
+
{
|
|
154
|
+
"type": "example.payments::type::Money",
|
|
155
|
+
"factory": [
|
|
156
|
+
"domain",
|
|
157
|
+
"PaymentGenerators",
|
|
158
|
+
"prices"
|
|
159
|
+
]
|
|
160
|
+
}
|
|
161
|
+
]
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
package domain
|
|
2
|
+
|
|
3
|
+
// Application-owned types; no generated LawSpec domain declarations.
|
|
4
|
+
object PaymentsDomain {
|
|
5
|
+
enum CurrencyCode {
|
|
6
|
+
case Dollars, Euros, Pounds
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
final case class Price(major: BigDecimal, unit: CurrencyCode)
|
|
10
|
+
|
|
11
|
+
sealed trait PaymentStatus
|
|
12
|
+
final case class Settled(price: Price) extends PaymentStatus
|
|
13
|
+
final case class Rejected(explanation: String) extends PaymentStatus
|
|
14
|
+
|
|
15
|
+
def apply_fee(price: Price): Price = Price(price.major + BigDecimal("0.2"), price.unit)
|
|
16
|
+
def restore(payment: PaymentStatus): PaymentStatus = payment
|
|
17
|
+
def store(payments: List[Option[PaymentStatus]]): List[Option[PaymentStatus]] = payments
|
|
18
|
+
}
|