yukigo 0.1.0 → 0.2.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/.mocharc.json +3 -3
- package/CHANGELOG.md +26 -0
- package/README.md +193 -199
- package/dist/analyzer/GraphBuilder.d.ts +29 -0
- package/dist/analyzer/GraphBuilder.js +99 -0
- package/dist/analyzer/index.d.ts +11 -23
- package/dist/analyzer/index.js +100 -58
- package/dist/analyzer/inspections/functional/functional.d.ts +44 -0
- package/dist/analyzer/inspections/functional/functional.js +149 -0
- package/dist/analyzer/inspections/functional/smells.d.ts +16 -0
- package/dist/analyzer/inspections/functional/smells.js +98 -0
- package/dist/analyzer/inspections/{generic.d.ts → generic/generic.d.ts} +70 -43
- package/dist/analyzer/inspections/generic/generic.js +604 -0
- package/dist/analyzer/inspections/generic/smells.d.ts +61 -0
- package/dist/analyzer/inspections/generic/smells.js +349 -0
- package/dist/analyzer/inspections/imperative/imperative.d.ts +35 -0
- package/dist/analyzer/inspections/imperative/imperative.js +109 -0
- package/dist/analyzer/inspections/imperative/smells.d.ts +16 -0
- package/dist/analyzer/inspections/imperative/smells.js +58 -0
- package/dist/analyzer/inspections/logic/logic.d.ts +32 -0
- package/dist/analyzer/inspections/logic/logic.js +96 -0
- package/dist/analyzer/inspections/logic/smells.d.ts +15 -0
- package/dist/analyzer/inspections/logic/smells.js +60 -0
- package/dist/analyzer/inspections/object/object.d.ts +88 -0
- package/dist/analyzer/inspections/object/object.js +319 -0
- package/dist/analyzer/inspections/object/smells.d.ts +30 -0
- package/dist/analyzer/inspections/object/smells.js +135 -0
- package/dist/analyzer/utils.d.ts +26 -4
- package/dist/analyzer/utils.js +71 -13
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interpreter/components/EnvBuilder.d.ts +9 -5
- package/dist/interpreter/components/EnvBuilder.js +100 -30
- package/dist/interpreter/components/Operations.d.ts +4 -4
- package/dist/interpreter/components/Operations.js +17 -2
- package/dist/interpreter/components/PatternMatcher.d.ts +47 -17
- package/dist/interpreter/components/PatternMatcher.js +264 -119
- package/dist/interpreter/components/RuntimeContext.d.ts +35 -0
- package/dist/interpreter/components/RuntimeContext.js +93 -0
- package/dist/interpreter/components/TestRunner.d.ts +18 -0
- package/dist/interpreter/components/TestRunner.js +103 -0
- package/dist/interpreter/components/Visitor.d.ts +63 -57
- package/dist/interpreter/components/Visitor.js +508 -173
- package/dist/interpreter/components/logic/LogicEngine.d.ts +29 -0
- package/dist/interpreter/components/logic/LogicEngine.js +259 -0
- package/dist/interpreter/components/logic/LogicResolver.d.ts +53 -0
- package/dist/interpreter/components/logic/LogicResolver.js +471 -0
- package/dist/interpreter/components/logic/LogicTranslator.d.ts +14 -0
- package/dist/interpreter/components/logic/LogicTranslator.js +99 -0
- package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +12 -0
- package/dist/interpreter/components/runtimes/FunctionRuntime.js +147 -0
- package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +19 -0
- package/dist/interpreter/components/runtimes/LazyRuntime.js +269 -0
- package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +35 -0
- package/dist/interpreter/components/runtimes/ObjectRuntime.js +126 -0
- package/dist/interpreter/entities.d.ts +105 -0
- package/dist/interpreter/entities.js +96 -0
- package/dist/interpreter/errors.d.ts +1 -1
- package/dist/interpreter/index.d.ts +4 -12
- package/dist/interpreter/index.js +10 -13
- package/dist/interpreter/trampoline.d.ts +17 -0
- package/dist/interpreter/trampoline.js +38 -0
- package/dist/interpreter/utils.d.ts +4 -7
- package/dist/interpreter/utils.js +25 -17
- package/dist/tester/index.d.ts +25 -0
- package/dist/tester/index.js +108 -0
- package/dist/utils/helpers.d.ts +0 -4
- package/dist/utils/helpers.js +20 -24
- package/package.json +2 -2
- package/src/analyzer/GraphBuilder.ts +142 -0
- package/src/analyzer/index.ts +185 -132
- package/src/analyzer/inspections/functional/functional.ts +121 -0
- package/src/analyzer/inspections/functional/smells.ts +102 -0
- package/src/analyzer/inspections/{generic.ts → generic/generic.ts} +581 -499
- package/src/analyzer/inspections/generic/smells.ts +365 -0
- package/src/analyzer/inspections/imperative/imperative.ts +101 -0
- package/src/analyzer/inspections/imperative/smells.ts +54 -0
- package/src/analyzer/inspections/logic/logic.ts +90 -0
- package/src/analyzer/inspections/logic/smells.ts +54 -0
- package/src/analyzer/inspections/{object.ts → object/object.ts} +264 -282
- package/src/analyzer/inspections/object/smells.ts +144 -0
- package/src/analyzer/utils.ts +109 -26
- package/src/index.ts +3 -2
- package/src/interpreter/components/EnvBuilder.ts +202 -97
- package/src/interpreter/components/Operations.ts +99 -81
- package/src/interpreter/components/PatternMatcher.ts +475 -254
- package/src/interpreter/components/RuntimeContext.ts +119 -0
- package/src/interpreter/components/TestRunner.ts +151 -0
- package/src/interpreter/components/Visitor.ts +1065 -493
- package/src/interpreter/components/logic/LogicEngine.ts +519 -0
- package/src/interpreter/components/logic/LogicResolver.ts +858 -0
- package/src/interpreter/components/logic/LogicTranslator.ts +149 -0
- package/src/interpreter/components/runtimes/FunctionRuntime.ts +227 -0
- package/src/interpreter/components/runtimes/LazyRuntime.ts +334 -0
- package/src/interpreter/components/runtimes/ObjectRuntime.ts +224 -0
- package/src/interpreter/errors.ts +47 -47
- package/src/interpreter/index.ts +52 -59
- package/src/interpreter/trampoline.ts +71 -0
- package/src/interpreter/utils.ts +84 -79
- package/src/tester/index.ts +128 -0
- package/src/utils/helpers.ts +67 -73
- package/tests/analyzer/functional.spec.ts +207 -221
- package/tests/analyzer/generic.spec.ts +178 -100
- package/tests/analyzer/helpers.spec.ts +83 -83
- package/tests/analyzer/logic.spec.ts +237 -292
- package/tests/analyzer/oop.spec.ts +323 -338
- package/tests/analyzer/transitive.spec.ts +166 -0
- package/tests/interpreter/EnvBuilder.spec.ts +183 -178
- package/tests/interpreter/FunctionRuntime.spec.ts +223 -234
- package/tests/interpreter/LazyRuntime.spec.ts +225 -190
- package/tests/interpreter/LogicEngine.spec.ts +327 -194
- package/tests/interpreter/LogicSubstitution.spec.ts +80 -0
- package/tests/interpreter/ObjectRuntime.spec.ts +606 -0
- package/tests/interpreter/Operations.spec.ts +220 -220
- package/tests/interpreter/PatternSystem.spec.ts +213 -189
- package/tests/interpreter/Tests.spec.ts +122 -0
- package/tests/interpreter/interpreter.spec.ts +991 -937
- package/tests/tester/Tester.spec.ts +153 -0
- package/tsconfig.build.json +15 -7
- package/tsconfig.json +25 -17
- package/dist/analyzer/inspections/functional.d.ts +0 -46
- package/dist/analyzer/inspections/functional.js +0 -123
- package/dist/analyzer/inspections/generic.js +0 -427
- package/dist/analyzer/inspections/imperative.d.ts +0 -37
- package/dist/analyzer/inspections/imperative.js +0 -105
- package/dist/analyzer/inspections/logic.d.ts +0 -49
- package/dist/analyzer/inspections/logic.js +0 -140
- package/dist/analyzer/inspections/object.d.ts +0 -83
- package/dist/analyzer/inspections/object.js +0 -235
- package/dist/interpreter/components/FunctionRuntime.d.ts +0 -8
- package/dist/interpreter/components/FunctionRuntime.js +0 -52
- package/dist/interpreter/components/LazyRuntime.d.ts +0 -7
- package/dist/interpreter/components/LazyRuntime.js +0 -75
- package/dist/interpreter/components/LogicEngine.d.ts +0 -21
- package/dist/interpreter/components/LogicEngine.js +0 -152
- package/dist/interpreter/components/LogicResolver.d.ts +0 -11
- package/dist/interpreter/components/LogicResolver.js +0 -87
- package/src/analyzer/inspections/functional.ts +0 -159
- package/src/analyzer/inspections/imperative.ts +0 -129
- package/src/analyzer/inspections/logic.ts +0 -166
- package/src/interpreter/components/FunctionRuntime.ts +0 -79
- package/src/interpreter/components/LazyRuntime.ts +0 -97
- package/src/interpreter/components/LogicEngine.ts +0 -227
- package/src/interpreter/components/LogicResolver.ts +0 -130
|
@@ -1,189 +1,213 @@
|
|
|
1
|
-
import { expect } from "chai";
|
|
2
|
-
import {
|
|
3
|
-
VariablePattern,
|
|
4
|
-
LiteralPattern,
|
|
5
|
-
SymbolPrimitive,
|
|
6
|
-
NumberPrimitive,
|
|
7
|
-
WildcardPattern,
|
|
8
|
-
ConsPattern,
|
|
9
|
-
ListPattern,
|
|
10
|
-
ConstructorPattern,
|
|
11
|
-
UnionPattern,
|
|
12
|
-
AsPattern,
|
|
13
|
-
LazyList,
|
|
14
|
-
} from "yukigo-ast";
|
|
15
|
-
import {
|
|
16
|
-
PatternMatcher,
|
|
17
|
-
PatternResolver,
|
|
18
|
-
} from "../../src/interpreter/components/PatternMatcher.js";
|
|
19
|
-
import { createStream } from "../../src/interpreter/utils.js";
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
expect(match(p, [1,
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
expect(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
const
|
|
146
|
-
expect(
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
expect(match(p,
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
const { success, bindings } = match(p,
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
const map = new Map(bindings);
|
|
171
|
-
expect(map.get("
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
const
|
|
177
|
-
expect(
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import {
|
|
3
|
+
VariablePattern,
|
|
4
|
+
LiteralPattern,
|
|
5
|
+
SymbolPrimitive,
|
|
6
|
+
NumberPrimitive,
|
|
7
|
+
WildcardPattern,
|
|
8
|
+
ConsPattern,
|
|
9
|
+
ListPattern,
|
|
10
|
+
ConstructorPattern,
|
|
11
|
+
UnionPattern,
|
|
12
|
+
AsPattern,
|
|
13
|
+
LazyList,
|
|
14
|
+
} from "yukigo-ast";
|
|
15
|
+
import {
|
|
16
|
+
PatternMatcher,
|
|
17
|
+
PatternResolver,
|
|
18
|
+
} from "../../src/interpreter/components/PatternMatcher.js";
|
|
19
|
+
import { createStream } from "../../src/interpreter/utils.js";
|
|
20
|
+
import { idContinuation, trampoline } from "../../src/interpreter/trampoline.js";
|
|
21
|
+
import { RuntimeContext } from "../../src/interpreter/components/RuntimeContext.js";
|
|
22
|
+
|
|
23
|
+
const s = (v: string) => new SymbolPrimitive(v);
|
|
24
|
+
const n = (v: number) => new NumberPrimitive(v);
|
|
25
|
+
const lit = (v: string | number) =>
|
|
26
|
+
new LiteralPattern(typeof v === "string" ? s(v) : n(v));
|
|
27
|
+
const variable = (v: string) => new VariablePattern(s(v));
|
|
28
|
+
const wildcard = () => new WildcardPattern();
|
|
29
|
+
|
|
30
|
+
describe("Pattern System", () => {
|
|
31
|
+
describe("PatternResolver (Pretty Printing)", () => {
|
|
32
|
+
let resolver: PatternResolver;
|
|
33
|
+
|
|
34
|
+
beforeEach(() => {
|
|
35
|
+
resolver = new PatternResolver();
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("should resolve a variable pattern", () => {
|
|
39
|
+
const p = variable("X");
|
|
40
|
+
expect(p.accept(resolver)).to.equal("X");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("should resolve a wildcard pattern", () => {
|
|
44
|
+
const p = wildcard();
|
|
45
|
+
expect(p.accept(resolver)).to.equal("_");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("should resolve a literal pattern", () => {
|
|
49
|
+
const p = lit(42);
|
|
50
|
+
expect(p.accept(resolver)).to.equal("42");
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("should resolve a cons pattern", () => {
|
|
54
|
+
// (1:X)
|
|
55
|
+
const p = new ConsPattern(lit(1), variable("X"));
|
|
56
|
+
expect(p.accept(resolver)).to.equal("(1:X)");
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("should resolve a constructor pattern", () => {
|
|
60
|
+
// Just(X)
|
|
61
|
+
const p = new ConstructorPattern(new SymbolPrimitive("Just"), [variable("X")]);
|
|
62
|
+
expect(p.accept(resolver)).to.equal("Just X");
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should resolve nested patterns", () => {
|
|
66
|
+
// (1:(2:[]))
|
|
67
|
+
const p = new ConsPattern(
|
|
68
|
+
lit(1),
|
|
69
|
+
new ConsPattern(lit(2), new ListPattern([]))
|
|
70
|
+
);
|
|
71
|
+
expect(p.accept(resolver)).to.equal("(1:(2:[]))");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("PatternMatcher (Logic)", () => {
|
|
76
|
+
const match = (
|
|
77
|
+
pattern: any,
|
|
78
|
+
value: any
|
|
79
|
+
): { success: boolean; bindings: [string, any][] } => {
|
|
80
|
+
const bindings: [string, any][] = [];
|
|
81
|
+
const matcher = new PatternMatcher(value, bindings, new RuntimeContext({lazyLoading: false}));
|
|
82
|
+
|
|
83
|
+
const success = trampoline(pattern.accept(matcher)(idContinuation));
|
|
84
|
+
return { success, bindings };
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
it("should match a variable and bind the value", () => {
|
|
88
|
+
const p = variable("X");
|
|
89
|
+
const { success, bindings } = match(p, 100);
|
|
90
|
+
|
|
91
|
+
expect(success).to.be.true;
|
|
92
|
+
expect(bindings).to.deep.equal([["X", 100]]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it("should match a wildcard but not bind anything", () => {
|
|
96
|
+
const p = wildcard();
|
|
97
|
+
const { success, bindings } = match(p, 100);
|
|
98
|
+
|
|
99
|
+
expect(success).to.be.true;
|
|
100
|
+
expect(bindings).to.be.empty;
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it("should match equal literals", () => {
|
|
104
|
+
const p = lit(42);
|
|
105
|
+
expect(match(p, 42).success).to.be.true;
|
|
106
|
+
expect(match(p, 99).success).to.be.false;
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("should match a list pattern exactly", () => {
|
|
110
|
+
const p = new ListPattern([lit(1), lit(2)]);
|
|
111
|
+
expect(match(p, [1, 2]).success).to.be.true;
|
|
112
|
+
expect(match(p, [1]).success).to.be.false;
|
|
113
|
+
expect(match(p, [1, 3]).success).to.be.false;
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("should match a Cons pattern with array input", () => {
|
|
117
|
+
const p = new ConsPattern(variable("H"), variable("T"));
|
|
118
|
+
const { success, bindings } = match(p, [1, 2, 3]);
|
|
119
|
+
|
|
120
|
+
expect(success).to.be.true;
|
|
121
|
+
const map = new Map(bindings);
|
|
122
|
+
expect(map.get("H")).to.equal(1);
|
|
123
|
+
expect(map.get("T")).to.deep.equal([2, 3]);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it("should fail Cons pattern on empty array", () => {
|
|
127
|
+
const p = new ConsPattern(variable("H"), variable("T"));
|
|
128
|
+
expect(match(p, []).success).to.be.false;
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("should match a Constructor pattern", () => {
|
|
132
|
+
const p = new ConstructorPattern(new SymbolPrimitive("Just"), [variable("X")]);
|
|
133
|
+
const val = ["Just", 10];
|
|
134
|
+
|
|
135
|
+
const { success, bindings } = match(p, val);
|
|
136
|
+
expect(success).to.be.true;
|
|
137
|
+
expect(bindings).to.deep.equal([["X", 10]]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it("should match an AsPattern (@)", () => {
|
|
141
|
+
const p = new AsPattern(
|
|
142
|
+
variable("List"),
|
|
143
|
+
new ListPattern([variable("X"), wildcard()])
|
|
144
|
+
);
|
|
145
|
+
const { success, bindings } = match(p, [1, 2]);
|
|
146
|
+
expect(success).to.be.true;
|
|
147
|
+
const map = new Map(bindings);
|
|
148
|
+
expect(map.get("List")).to.deep.equal([1, 2]);
|
|
149
|
+
expect(map.get("X")).to.equal(1);
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("should match a UnionPattern", () => {
|
|
153
|
+
const p = new UnionPattern([lit(1), lit(2)]);
|
|
154
|
+
expect(match(p, 1).success).to.be.true;
|
|
155
|
+
expect(match(p, 2).success).to.be.true;
|
|
156
|
+
expect(match(p, 3).success).to.be.false;
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("String Matching as List", () => {
|
|
160
|
+
it("should match an empty string against an empty list pattern", () => {
|
|
161
|
+
const p = new ListPattern([]);
|
|
162
|
+
expect(match(p, "").success).to.be.true;
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it("should match a string against a cons pattern", () => {
|
|
166
|
+
const p = new ConsPattern(variable("H"), variable("T"));
|
|
167
|
+
const { success, bindings } = match(p, "abc");
|
|
168
|
+
|
|
169
|
+
expect(success).to.be.true;
|
|
170
|
+
const map = new Map(bindings);
|
|
171
|
+
expect(map.get("H")).to.equal("a");
|
|
172
|
+
expect(map.get("T")).to.equal("bc");
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("should fail to match an empty string against a cons pattern", () => {
|
|
176
|
+
const p = new ConsPattern(variable("H"), variable("T"));
|
|
177
|
+
expect(match(p, "").success).to.be.false;
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("LazyList Matching", () => {
|
|
182
|
+
const createLazy = (items: number[]): LazyList =>
|
|
183
|
+
createStream(function* () {
|
|
184
|
+
for (const i of items) yield i;
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it("should match Cons pattern against LazyList", () => {
|
|
188
|
+
const p = new ConsPattern(variable("X"), variable("Xs"));
|
|
189
|
+
const lazyVal = createLazy([1, 2, 3]);
|
|
190
|
+
|
|
191
|
+
const { success, bindings } = match(p, lazyVal);
|
|
192
|
+
expect(success).to.be.true;
|
|
193
|
+
|
|
194
|
+
const map = new Map(bindings);
|
|
195
|
+
expect(map.get("X")).to.equal(1);
|
|
196
|
+
|
|
197
|
+
const tail = map.get("Xs") as LazyList;
|
|
198
|
+
expect(tail.type).to.equal("LazyList");
|
|
199
|
+
|
|
200
|
+
const gen = tail.generator();
|
|
201
|
+
expect(gen.next().value).to.equal(2);
|
|
202
|
+
expect(gen.next().value).to.equal(3);
|
|
203
|
+
expect(gen.next().done).to.be.true;
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("should match ListPattern against LazyList (forcing realization)", () => {
|
|
207
|
+
const p = new ListPattern([lit(1), lit(2)]);
|
|
208
|
+
const lazyVal = createLazy([1, 2]);
|
|
209
|
+
expect(match(p, lazyVal).success).to.be.true;
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { describe, it, beforeEach } from "mocha";
|
|
2
|
+
import { expect } from "chai";
|
|
3
|
+
import {
|
|
4
|
+
Assert,
|
|
5
|
+
BooleanPrimitive,
|
|
6
|
+
Equality,
|
|
7
|
+
NumberPrimitive,
|
|
8
|
+
Sequence,
|
|
9
|
+
Test,
|
|
10
|
+
TestGroup,
|
|
11
|
+
Failure,
|
|
12
|
+
Raise,
|
|
13
|
+
StringPrimitive,
|
|
14
|
+
Truth,
|
|
15
|
+
} from "yukigo-ast";
|
|
16
|
+
import { Interpreter, Tester } from "../../src/index.js";
|
|
17
|
+
import { FailedAssert } from "../../src/interpreter/components/TestRunner.js";
|
|
18
|
+
|
|
19
|
+
describe("Testing Nodes", () => {
|
|
20
|
+
let interpreter: Interpreter;
|
|
21
|
+
|
|
22
|
+
beforeEach(() => {
|
|
23
|
+
interpreter = new Interpreter([]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("Assertions", () => {
|
|
27
|
+
it("should pass a valid Equality assertion", () => {
|
|
28
|
+
const assertion = new Assert(
|
|
29
|
+
new BooleanPrimitive(false), // negated = false
|
|
30
|
+
new Equality(new NumberPrimitive(1), new NumberPrimitive(1)),
|
|
31
|
+
);
|
|
32
|
+
// Should not throw
|
|
33
|
+
interpreter.evaluate(new Sequence([assertion]));
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("should fail an invalid Equality assertion", () => {
|
|
37
|
+
const assertion = new Assert(
|
|
38
|
+
new BooleanPrimitive(false),
|
|
39
|
+
new Equality(new NumberPrimitive(1), new NumberPrimitive(2)),
|
|
40
|
+
);
|
|
41
|
+
expect(() => interpreter.evaluate(new Sequence([assertion]))).to.throw(
|
|
42
|
+
FailedAssert,
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should pass a negated invalid Equality assertion", () => {
|
|
47
|
+
const assertion = new Assert(
|
|
48
|
+
new BooleanPrimitive(true), // negated = true
|
|
49
|
+
new Equality(new NumberPrimitive(1), new NumberPrimitive(2)),
|
|
50
|
+
);
|
|
51
|
+
interpreter.evaluate(new Sequence([assertion]));
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should pass a Truth assertion", () => {
|
|
55
|
+
const assertion = new Assert(
|
|
56
|
+
new BooleanPrimitive(false),
|
|
57
|
+
new Truth(new BooleanPrimitive(true)),
|
|
58
|
+
);
|
|
59
|
+
interpreter.evaluate(new Sequence([assertion]));
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should fail a Truth assertion with false", () => {
|
|
63
|
+
const assertion = new Assert(
|
|
64
|
+
new BooleanPrimitive(false),
|
|
65
|
+
new Truth(new BooleanPrimitive(false)),
|
|
66
|
+
);
|
|
67
|
+
expect(() => interpreter.evaluate(new Sequence([assertion]))).to.throw(
|
|
68
|
+
FailedAssert,
|
|
69
|
+
);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("should pass a Failure assertion when code raises error", () => {
|
|
73
|
+
const funcThatRaises = new Raise(new StringPrimitive("Boom"));
|
|
74
|
+
const assertion = new Assert(
|
|
75
|
+
new BooleanPrimitive(false),
|
|
76
|
+
new Failure(funcThatRaises, new StringPrimitive("[Raise] Boom")),
|
|
77
|
+
);
|
|
78
|
+
interpreter.evaluate(new Sequence([assertion]));
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("should fail a Failure assertion when code does NOT raise error", () => {
|
|
82
|
+
const funcThatReturns = new NumberPrimitive(1);
|
|
83
|
+
const assertion = new Assert(
|
|
84
|
+
new BooleanPrimitive(false),
|
|
85
|
+
new Failure(funcThatReturns, new StringPrimitive("[Raise] Boom")),
|
|
86
|
+
);
|
|
87
|
+
expect(() => interpreter.evaluate(new Sequence([assertion]))).to.throw(
|
|
88
|
+
FailedAssert,
|
|
89
|
+
);
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
describe("Tests and Groups", () => {
|
|
94
|
+
it("should execute a Test node containing assertions", () => {
|
|
95
|
+
const assertion = new Assert(
|
|
96
|
+
new BooleanPrimitive(false),
|
|
97
|
+
new Equality(new NumberPrimitive(5), new NumberPrimitive(5)),
|
|
98
|
+
);
|
|
99
|
+
const testNode = new Test(
|
|
100
|
+
new StringPrimitive("My Test"),
|
|
101
|
+
new Sequence([assertion]),
|
|
102
|
+
);
|
|
103
|
+
interpreter.evaluate(testNode);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("should execute a TestGroup node", () => {
|
|
107
|
+
const assertion = new Assert(
|
|
108
|
+
new BooleanPrimitive(false),
|
|
109
|
+
new Equality(new NumberPrimitive(10), new NumberPrimitive(10)),
|
|
110
|
+
);
|
|
111
|
+
const testNode = new Test(
|
|
112
|
+
new StringPrimitive("Sub Test"),
|
|
113
|
+
new Sequence([assertion]),
|
|
114
|
+
);
|
|
115
|
+
const groupNode = new TestGroup(
|
|
116
|
+
new StringPrimitive("My Group"),
|
|
117
|
+
new Sequence([testNode]),
|
|
118
|
+
);
|
|
119
|
+
interpreter.evaluate(groupNode);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|