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