yukigo 0.1.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 +4 -0
- package/CHANGELOG.md +6 -0
- package/README.md +199 -0
- package/dist/analyzer/index.d.ts +71 -0
- package/dist/analyzer/index.js +110 -0
- package/dist/analyzer/inspections/functional.d.ts +46 -0
- package/dist/analyzer/inspections/functional.js +123 -0
- package/dist/analyzer/inspections/generic.d.ts +151 -0
- package/dist/analyzer/inspections/generic.js +427 -0
- package/dist/analyzer/inspections/imperative.d.ts +37 -0
- package/dist/analyzer/inspections/imperative.js +105 -0
- package/dist/analyzer/inspections/logic.d.ts +49 -0
- package/dist/analyzer/inspections/logic.js +140 -0
- package/dist/analyzer/inspections/object.d.ts +83 -0
- package/dist/analyzer/inspections/object.js +235 -0
- package/dist/analyzer/utils.d.ts +4 -0
- package/dist/analyzer/utils.js +16 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -0
- package/dist/interpreter/components/EnvBuilder.d.ts +16 -0
- package/dist/interpreter/components/EnvBuilder.js +78 -0
- package/dist/interpreter/components/FunctionRuntime.d.ts +8 -0
- package/dist/interpreter/components/FunctionRuntime.js +52 -0
- package/dist/interpreter/components/LazyRuntime.d.ts +7 -0
- package/dist/interpreter/components/LazyRuntime.js +75 -0
- package/dist/interpreter/components/LogicEngine.d.ts +21 -0
- package/dist/interpreter/components/LogicEngine.js +152 -0
- package/dist/interpreter/components/LogicResolver.d.ts +11 -0
- package/dist/interpreter/components/LogicResolver.js +87 -0
- package/dist/interpreter/components/Operations.d.ts +14 -0
- package/dist/interpreter/components/Operations.js +69 -0
- package/dist/interpreter/components/PatternMatcher.d.ts +41 -0
- package/dist/interpreter/components/PatternMatcher.js +206 -0
- package/dist/interpreter/components/Visitor.d.ts +64 -0
- package/dist/interpreter/components/Visitor.js +299 -0
- package/dist/interpreter/errors.d.ts +19 -0
- package/dist/interpreter/errors.js +36 -0
- package/dist/interpreter/index.d.ts +32 -0
- package/dist/interpreter/index.js +44 -0
- package/dist/interpreter/utils.d.ts +14 -0
- package/dist/interpreter/utils.js +57 -0
- package/dist/utils/helpers.d.ts +14 -0
- package/dist/utils/helpers.js +51 -0
- package/package.json +30 -0
- package/src/analyzer/index.ts +132 -0
- package/src/analyzer/inspections/functional.ts +159 -0
- package/src/analyzer/inspections/generic.ts +499 -0
- package/src/analyzer/inspections/imperative.ts +129 -0
- package/src/analyzer/inspections/logic.ts +166 -0
- package/src/analyzer/inspections/object.ts +282 -0
- package/src/analyzer/utils.ts +26 -0
- package/src/index.ts +3 -0
- package/src/interpreter/components/EnvBuilder.ts +97 -0
- package/src/interpreter/components/FunctionRuntime.ts +79 -0
- package/src/interpreter/components/LazyRuntime.ts +97 -0
- package/src/interpreter/components/LogicEngine.ts +227 -0
- package/src/interpreter/components/LogicResolver.ts +130 -0
- package/src/interpreter/components/Operations.ts +81 -0
- package/src/interpreter/components/PatternMatcher.ts +254 -0
- package/src/interpreter/components/Visitor.ts +493 -0
- package/src/interpreter/errors.ts +47 -0
- package/src/interpreter/index.ts +59 -0
- package/src/interpreter/utils.ts +79 -0
- package/src/utils/helpers.ts +73 -0
- package/tests/analyzer/functional.spec.ts +221 -0
- package/tests/analyzer/generic.spec.ts +100 -0
- package/tests/analyzer/helpers.spec.ts +83 -0
- package/tests/analyzer/logic.spec.ts +292 -0
- package/tests/analyzer/oop.spec.ts +338 -0
- package/tests/interpreter/EnvBuilder.spec.ts +178 -0
- package/tests/interpreter/FunctionRuntime.spec.ts +234 -0
- package/tests/interpreter/LazyRuntime.spec.ts +190 -0
- package/tests/interpreter/LogicEngine.spec.ts +194 -0
- package/tests/interpreter/Operations.spec.ts +220 -0
- package/tests/interpreter/PatternSystem.spec.ts +189 -0
- package/tests/interpreter/interpreter.spec.ts +937 -0
- package/tsconfig.build.json +7 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { parseDocument } from "yaml";
|
|
2
|
+
import { InspectionRule } from "../analyzer/index.js";
|
|
3
|
+
import { YukigoPrimitive } from "yukigo-ast";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Translates Mulang inspections (YAML format) to an array of `InspectionRule` objects.
|
|
7
|
+
* @param mulangYamlString The Mulang inspection syntax as a YAML string.
|
|
8
|
+
* @returns An array of InspectionRule objects.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export class MulangAdapter {
|
|
12
|
+
public translateMulangInspection(mulangInspection: any): InspectionRule {
|
|
13
|
+
if (
|
|
14
|
+
!mulangInspection ||
|
|
15
|
+
typeof mulangInspection.inspection !== "string" ||
|
|
16
|
+
typeof mulangInspection.binding !== "string"
|
|
17
|
+
) {
|
|
18
|
+
throw Error(
|
|
19
|
+
`Skipping malformed Mulang inspection entry: ${mulangInspection}`
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const inspection: string[] = mulangInspection.inspection.split(":");
|
|
24
|
+
const expected: boolean = inspection[0] !== "Not";
|
|
25
|
+
const args: string[] = inspection.slice(expected ? 1 : 2);
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
inspection: expected ? inspection[0] : inspection[1],
|
|
29
|
+
expected,
|
|
30
|
+
args,
|
|
31
|
+
binding: mulangInspection.binding,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public translateMulangExpectations(
|
|
36
|
+
mulangYamlString: string
|
|
37
|
+
): InspectionRule[] {
|
|
38
|
+
const parsedYaml = parseDocument(mulangYamlString).toJS();
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
!parsedYaml ||
|
|
42
|
+
!parsedYaml.expectations ||
|
|
43
|
+
!Array.isArray(parsedYaml.expectations)
|
|
44
|
+
) {
|
|
45
|
+
throw Error(
|
|
46
|
+
"Invalid Mulang YAML structure. Expected 'expectations' to be an array."
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const inspectionRules: InspectionRule[] = [];
|
|
51
|
+
|
|
52
|
+
for (const mulangInspection of parsedYaml.expectations) {
|
|
53
|
+
const inspection = this.translateMulangInspection(mulangInspection);
|
|
54
|
+
inspectionRules.push(inspection);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return inspectionRules;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const yukigoTsMappings: { [key in YukigoPrimitive]: string } = {
|
|
62
|
+
YuNumber: "number",
|
|
63
|
+
YuString: "string",
|
|
64
|
+
YuChar: "char",
|
|
65
|
+
YuBoolean: "boolean",
|
|
66
|
+
YuNil: "null",
|
|
67
|
+
// Below some missing mappigs
|
|
68
|
+
YuList: "YuList",
|
|
69
|
+
YuObject: "YuObject",
|
|
70
|
+
YuDict: "YuDict",
|
|
71
|
+
YuTuple: "YuTuple",
|
|
72
|
+
YuSymbol: "YuSymbol",
|
|
73
|
+
};
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import {
|
|
3
|
+
Function,
|
|
4
|
+
Equation,
|
|
5
|
+
SymbolPrimitive,
|
|
6
|
+
UnguardedBody,
|
|
7
|
+
GuardedBody,
|
|
8
|
+
Sequence,
|
|
9
|
+
CompositionExpression,
|
|
10
|
+
Lambda,
|
|
11
|
+
Application,
|
|
12
|
+
ListComprehension,
|
|
13
|
+
Yield,
|
|
14
|
+
LiteralPattern,
|
|
15
|
+
ListPattern,
|
|
16
|
+
WildcardPattern,
|
|
17
|
+
BooleanPrimitive,
|
|
18
|
+
NumberPrimitive,
|
|
19
|
+
If,
|
|
20
|
+
} from "yukigo-ast";
|
|
21
|
+
import {
|
|
22
|
+
UsesComposition,
|
|
23
|
+
UsesAnonymousVariable,
|
|
24
|
+
UsesComprehension,
|
|
25
|
+
UsesGuards,
|
|
26
|
+
UsesLambda,
|
|
27
|
+
UsesYield,
|
|
28
|
+
UsesPatternMatching,
|
|
29
|
+
} from "../../src/analyzer/inspections/functional.js";
|
|
30
|
+
import { executeVisitor } from "../../src/analyzer/utils.js";
|
|
31
|
+
|
|
32
|
+
describe("Functional Spec", () => {
|
|
33
|
+
const createSymbol = (name: string) => new SymbolPrimitive(name);
|
|
34
|
+
const createSequence = (expr: any) => new Sequence([expr]);
|
|
35
|
+
const createEquation = (patterns: any[], bodyExpr: any) => {
|
|
36
|
+
const body = Array.isArray(bodyExpr)
|
|
37
|
+
? bodyExpr
|
|
38
|
+
: new UnguardedBody(createSequence(bodyExpr));
|
|
39
|
+
|
|
40
|
+
return new Equation(patterns, body, undefined);
|
|
41
|
+
};
|
|
42
|
+
const createFunction = (name: string, equations: Equation[]) => {
|
|
43
|
+
return new Function(createSymbol(name), equations);
|
|
44
|
+
};
|
|
45
|
+
const mockExpr = new NumberPrimitive(1);
|
|
46
|
+
|
|
47
|
+
describe("UsesGuards", () => {
|
|
48
|
+
it("detects guards when they are present", () => {
|
|
49
|
+
const guardedBody = new GuardedBody(mockExpr, mockExpr);
|
|
50
|
+
const equation = new Equation([], [guardedBody, guardedBody], undefined);
|
|
51
|
+
const func = createFunction("f", [equation]);
|
|
52
|
+
|
|
53
|
+
const visitor = new UsesGuards("f");
|
|
54
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("does not detect guards when using UnguardedBody", () => {
|
|
58
|
+
const equation = createEquation([], mockExpr);
|
|
59
|
+
const func = createFunction("f", [equation]);
|
|
60
|
+
|
|
61
|
+
const visitor = new UsesGuards("f");
|
|
62
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("should respect binding scope", () => {
|
|
66
|
+
const guardedBody = new GuardedBody(mockExpr, mockExpr);
|
|
67
|
+
const equation = new Equation([], [guardedBody], undefined);
|
|
68
|
+
const func = createFunction("g", [equation]);
|
|
69
|
+
|
|
70
|
+
const visitor = new UsesGuards("f");
|
|
71
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
describe("UsesComposition", () => {
|
|
76
|
+
const createComp = () => new CompositionExpression(mockExpr, mockExpr);
|
|
77
|
+
|
|
78
|
+
it("is True when composition is present on top level", () => {
|
|
79
|
+
const func = createFunction("x", [createEquation([], createComp())]);
|
|
80
|
+
|
|
81
|
+
const visitor = new UsesComposition("x");
|
|
82
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("is True when composition is present inside lambda", () => {
|
|
86
|
+
const lambda = new Lambda([], createComp());
|
|
87
|
+
const func = createFunction("x", [createEquation([], lambda)]);
|
|
88
|
+
|
|
89
|
+
const visitor = new UsesComposition("x");
|
|
90
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("is True when composition is present inside application", () => {
|
|
94
|
+
const app = new Application(mockExpr, createComp());
|
|
95
|
+
const func = createFunction("x", [createEquation([], app)]);
|
|
96
|
+
|
|
97
|
+
const visitor = new UsesComposition("x");
|
|
98
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("is True when composition is present within if", () => {
|
|
102
|
+
const ifExpr = new If(new BooleanPrimitive(true), createComp(), mockExpr);
|
|
103
|
+
const func = createFunction("f", [createEquation([], ifExpr)]);
|
|
104
|
+
|
|
105
|
+
const visitor = new UsesComposition("f");
|
|
106
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("is False when composition is not present", () => {
|
|
110
|
+
const func = createFunction("x", [createEquation([], mockExpr)]);
|
|
111
|
+
const visitor = new UsesComposition("x");
|
|
112
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe("UsesPatternMatching", () => {
|
|
117
|
+
it("is True when Pattern Matching with Literal", () => {
|
|
118
|
+
const pattern = new LiteralPattern(new NumberPrimitive(0));
|
|
119
|
+
const func = createFunction("factorial", [
|
|
120
|
+
createEquation([pattern], mockExpr),
|
|
121
|
+
]);
|
|
122
|
+
|
|
123
|
+
const visitor = new UsesPatternMatching("factorial");
|
|
124
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("is True when Pattern Matching on List", () => {
|
|
128
|
+
const pattern = new ListPattern([]);
|
|
129
|
+
const func = createFunction("foo", [createEquation([pattern], mockExpr)]);
|
|
130
|
+
|
|
131
|
+
const visitor = new UsesPatternMatching("foo");
|
|
132
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("is True when Pattern Matching on anonymous variable (Wildcard)", () => {
|
|
136
|
+
const pattern = new WildcardPattern();
|
|
137
|
+
const func = createFunction("baz", [createEquation([pattern], mockExpr)]);
|
|
138
|
+
|
|
139
|
+
const visitor = new UsesPatternMatching("baz");
|
|
140
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it("is False when no patterns (or ignored patterns) are used", () => {
|
|
144
|
+
const func = createFunction("foo", [createEquation([], mockExpr)]);
|
|
145
|
+
const visitor = new UsesPatternMatching("foo");
|
|
146
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe("UsesLambda", () => {
|
|
151
|
+
it("detects lambda when is present", () => {
|
|
152
|
+
const lambda = new Lambda([], mockExpr);
|
|
153
|
+
const func = createFunction("f", [createEquation([], lambda)]);
|
|
154
|
+
|
|
155
|
+
const visitor = new UsesLambda("f");
|
|
156
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("detects lambda when is not present", () => {
|
|
160
|
+
const func = createFunction("f", [createEquation([], mockExpr)]);
|
|
161
|
+
const visitor = new UsesLambda("f");
|
|
162
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe("UsesAnonymousVariable", () => {
|
|
167
|
+
it("is True if _ is present in parameters", () => {
|
|
168
|
+
const pattern = new WildcardPattern();
|
|
169
|
+
const func = createFunction("foo", [createEquation([pattern], mockExpr)]);
|
|
170
|
+
|
|
171
|
+
const visitor = new UsesAnonymousVariable("foo");
|
|
172
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it("is True if _ is present in nested structures (e.g. ListPattern)", () => {
|
|
176
|
+
const innerWildcard = new WildcardPattern();
|
|
177
|
+
|
|
178
|
+
const listPattern = new ListPattern([innerWildcard]);
|
|
179
|
+
|
|
180
|
+
const func = createFunction("foo", [
|
|
181
|
+
createEquation([listPattern], mockExpr),
|
|
182
|
+
]);
|
|
183
|
+
|
|
184
|
+
const visitor = new UsesAnonymousVariable("foo");
|
|
185
|
+
|
|
186
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
it("is False if _ is not present", () => {
|
|
190
|
+
const func = createFunction("foo", [createEquation([], mockExpr)]);
|
|
191
|
+
const visitor = new UsesAnonymousVariable("foo");
|
|
192
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe("UsesComprehension", () => {
|
|
197
|
+
it("is True when list comprehension exists", () => {
|
|
198
|
+
const comp = new ListComprehension(mockExpr, []);
|
|
199
|
+
const func = createFunction("x", [createEquation([], comp)]);
|
|
200
|
+
|
|
201
|
+
const visitor = new UsesComprehension(undefined);
|
|
202
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("is False when comprehension doesnt exists", () => {
|
|
206
|
+
const func = createFunction("x", [createEquation([], mockExpr)]);
|
|
207
|
+
const visitor = new UsesComprehension("x");
|
|
208
|
+
expect(executeVisitor(func, visitor)).to.eq(false);
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe("UsesYield", () => {
|
|
213
|
+
it("is True when yield exists", () => {
|
|
214
|
+
const yieldNode = new Yield(mockExpr);
|
|
215
|
+
const func = createFunction("gen", [createEquation([], yieldNode)]);
|
|
216
|
+
|
|
217
|
+
const visitor = new UsesYield("gen");
|
|
218
|
+
expect(executeVisitor(func, visitor)).to.eq(true);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import {
|
|
3
|
+
TypesParameterAs,
|
|
4
|
+
TypesReturnAs,
|
|
5
|
+
} from "../../src/analyzer/inspections/generic.js";
|
|
6
|
+
import {
|
|
7
|
+
TypeSignature,
|
|
8
|
+
ParameterizedType,
|
|
9
|
+
SymbolPrimitive,
|
|
10
|
+
SimpleType,
|
|
11
|
+
} from "yukigo-ast";
|
|
12
|
+
import { executeVisitor } from "../../src/analyzer/utils.js";
|
|
13
|
+
|
|
14
|
+
describe("Generic Inspections Type Checks", () => {
|
|
15
|
+
const createTypeSignature = (name: string, params: string[], ret: string) => {
|
|
16
|
+
const identifier = new SymbolPrimitive(name);
|
|
17
|
+
const inputTypes = params.map((p) => {
|
|
18
|
+
const t = new SimpleType(p, []);
|
|
19
|
+
t.toString = () => p;
|
|
20
|
+
return t;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
const returnType = new SimpleType(ret, []);
|
|
24
|
+
returnType.toString = () => ret;
|
|
25
|
+
|
|
26
|
+
const body = new ParameterizedType(inputTypes, returnType, []);
|
|
27
|
+
return new TypeSignature(identifier, body);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
describe("TypesParameterAs", () => {
|
|
31
|
+
it("should find a match when parameter type matches at correct index", () => {
|
|
32
|
+
// f :: Int -> String -> Bool
|
|
33
|
+
const node = createTypeSignature("f", ["Int", "String"], "Bool");
|
|
34
|
+
const visitor = new TypesParameterAs("f", 0, "Int");
|
|
35
|
+
|
|
36
|
+
expect(executeVisitor(node, visitor)).to.eq(true);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should find a match at a specific index (2nd parameter)", () => {
|
|
40
|
+
// f :: Int -> String -> Bool
|
|
41
|
+
const node = createTypeSignature("f", ["Int", "String"], "Bool");
|
|
42
|
+
const visitor = new TypesParameterAs("f", 1, "String");
|
|
43
|
+
|
|
44
|
+
expect(executeVisitor(node, visitor)).to.eq(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should NOT throw if parameter index is out of bounds", () => {
|
|
48
|
+
const node = createTypeSignature("f", ["Int"], "Bool");
|
|
49
|
+
const visitor = new TypesParameterAs("f", 2, "Int");
|
|
50
|
+
|
|
51
|
+
expect(executeVisitor(node, visitor)).to.eq(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("should NOT throw if type does not match", () => {
|
|
55
|
+
const node = createTypeSignature("f", ["Int"], "Bool");
|
|
56
|
+
const visitor = new TypesParameterAs("f", 1, "String");
|
|
57
|
+
|
|
58
|
+
expect(executeVisitor(node, visitor)).to.eq(false);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("should NOT throw if binding name does not match", () => {
|
|
62
|
+
const node = createTypeSignature("g", ["Int"], "Bool");
|
|
63
|
+
const visitor = new TypesParameterAs("f", 1, "Int");
|
|
64
|
+
|
|
65
|
+
expect(executeVisitor(node, visitor)).to.eq(false);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("TypesReturnAs", () => {
|
|
70
|
+
it("should find a match when return type matches", () => {
|
|
71
|
+
// f :: Int -> Bool
|
|
72
|
+
const node = createTypeSignature("f", ["Int"], "Bool");
|
|
73
|
+
const visitor = new TypesReturnAs("f", "Bool");
|
|
74
|
+
|
|
75
|
+
expect(executeVisitor(node, visitor)).to.eq(true);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("should handle complex return types", () => {
|
|
79
|
+
// f :: Int -> (Int, Int)
|
|
80
|
+
const node = createTypeSignature("f", ["Int"], "(Int, Int)");
|
|
81
|
+
const visitor = new TypesReturnAs("f", "(Int, Int)");
|
|
82
|
+
|
|
83
|
+
expect(executeVisitor(node, visitor)).to.eq(true);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("should NOT throw if return type does not match", () => {
|
|
87
|
+
const node = createTypeSignature("f", ["Int"], "Bool");
|
|
88
|
+
const visitor = new TypesReturnAs("f", "Int");
|
|
89
|
+
|
|
90
|
+
expect(executeVisitor(node, visitor)).to.eq(false);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("should NOT throw if binding name does not match", () => {
|
|
94
|
+
const node = createTypeSignature("g", ["Int"], "Bool");
|
|
95
|
+
const visitor = new TypesReturnAs("f", "Bool");
|
|
96
|
+
|
|
97
|
+
expect(executeVisitor(node, visitor)).to.eq(false);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { MulangAdapter } from "../../src/index.js";
|
|
2
|
+
import { isYukigoPrimitive, NumberPrimitive, Otherwise } from "yukigo-ast";
|
|
3
|
+
import { assert } from "chai";
|
|
4
|
+
|
|
5
|
+
describe("Helpers Spec", () => {
|
|
6
|
+
it("translates correctly mulang's expectations", () => {
|
|
7
|
+
const mulangAdapter = new MulangAdapter();
|
|
8
|
+
const mulangExpectations = `
|
|
9
|
+
expectations:
|
|
10
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
11
|
+
binding: squareList
|
|
12
|
+
inspection: HasBinding
|
|
13
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
14
|
+
binding: squareList
|
|
15
|
+
inspection: HasLambdaExpression
|
|
16
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
17
|
+
binding: square
|
|
18
|
+
inspection: HasArithmetic
|
|
19
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
20
|
+
binding: doble
|
|
21
|
+
inspection: Not:HasBinding
|
|
22
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
23
|
+
binding: square
|
|
24
|
+
inspection: Uses:x
|
|
25
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
26
|
+
binding: squareList
|
|
27
|
+
inspection: Uses:map
|
|
28
|
+
- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
|
|
29
|
+
binding: squareList
|
|
30
|
+
inspection: Not:Uses:map`;
|
|
31
|
+
|
|
32
|
+
const yukigoExpectations =
|
|
33
|
+
mulangAdapter.translateMulangExpectations(mulangExpectations);
|
|
34
|
+
assert.deepEqual(yukigoExpectations, [
|
|
35
|
+
{
|
|
36
|
+
inspection: "HasBinding",
|
|
37
|
+
binding: "squareList",
|
|
38
|
+
args: [],
|
|
39
|
+
expected: true,
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
inspection: "HasLambdaExpression",
|
|
43
|
+
binding: "squareList",
|
|
44
|
+
args: [],
|
|
45
|
+
expected: true,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
inspection: "HasArithmetic",
|
|
49
|
+
binding: "square",
|
|
50
|
+
args: [],
|
|
51
|
+
expected: true,
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
inspection: "HasBinding",
|
|
55
|
+
binding: "doble",
|
|
56
|
+
args: [],
|
|
57
|
+
expected: false,
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
inspection: "Uses",
|
|
61
|
+
binding: "square",
|
|
62
|
+
args: ["x"],
|
|
63
|
+
expected: true,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
inspection: "Uses",
|
|
67
|
+
binding: "squareList",
|
|
68
|
+
args: ["map"],
|
|
69
|
+
expected: true,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
inspection: "Uses",
|
|
73
|
+
binding: "squareList",
|
|
74
|
+
args: ["map"],
|
|
75
|
+
expected: false,
|
|
76
|
+
},
|
|
77
|
+
]);
|
|
78
|
+
});
|
|
79
|
+
it("detects correctly YukigoPrimitive", () => {
|
|
80
|
+
assert.isFalse(isYukigoPrimitive(new Otherwise()));
|
|
81
|
+
assert.isTrue(isYukigoPrimitive(new NumberPrimitive(4)));
|
|
82
|
+
});
|
|
83
|
+
});
|