yukigo 0.2.3 → 0.3.2
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 +105 -40
- package/README.md +193 -193
- package/dist/analyzer/GraphBuilder.d.ts +1 -0
- package/dist/analyzer/GraphBuilder.js +2 -1
- package/dist/analyzer/index.d.ts +0 -7
- package/dist/analyzer/index.js +3 -3
- package/dist/analyzer/inspections/functional/functional.d.ts +1 -1
- package/dist/analyzer/inspections/functional/functional.js +1 -1
- package/dist/analyzer/inspections/functional/smells.d.ts +3 -3
- package/dist/analyzer/inspections/functional/smells.js +27 -35
- package/dist/analyzer/inspections/generic/generic.d.ts +22 -14
- package/dist/analyzer/inspections/generic/generic.js +82 -60
- package/dist/analyzer/inspections/generic/smells.d.ts +2 -0
- package/dist/analyzer/inspections/generic/smells.js +55 -64
- package/dist/analyzer/inspections/imperative/imperative.d.ts +3 -3
- package/dist/analyzer/inspections/imperative/imperative.js +3 -3
- package/dist/analyzer/inspections/imperative/smells.d.ts +1 -0
- package/dist/analyzer/inspections/imperative/smells.js +8 -5
- package/dist/analyzer/inspections/logic/logic.d.ts +8 -8
- package/dist/analyzer/inspections/logic/logic.js +5 -5
- package/dist/analyzer/inspections/logic/smells.js +2 -2
- package/dist/analyzer/inspections/object/object.d.ts +5 -5
- package/dist/analyzer/inspections/object/object.js +22 -21
- package/dist/analyzer/inspections/object/smells.js +11 -23
- package/dist/analyzer/utils.d.ts +6 -2
- package/dist/analyzer/utils.js +6 -2
- package/dist/interpreter/components/EnvBuilder.d.ts +20 -1
- package/dist/interpreter/components/EnvBuilder.js +211 -66
- package/dist/interpreter/components/EqualityComparer.d.ts +15 -0
- package/dist/interpreter/components/EqualityComparer.js +69 -0
- package/dist/interpreter/components/Operations.d.ts +17 -14
- package/dist/interpreter/components/Operations.js +61 -62
- package/dist/interpreter/components/PatternMatcher.d.ts +19 -58
- package/dist/interpreter/components/PatternMatcher.js +185 -286
- package/dist/interpreter/components/RuntimeContext.d.ts +24 -15
- package/dist/interpreter/components/RuntimeContext.js +43 -4
- package/dist/interpreter/components/TestRunner.d.ts +15 -13
- package/dist/interpreter/components/TestRunner.js +86 -53
- package/dist/interpreter/components/evaluators/BaseEvaluator.d.ts +19 -0
- package/dist/interpreter/components/evaluators/BaseEvaluator.js +20 -0
- package/dist/interpreter/components/evaluators/ExpressionEvaluator.d.ts +34 -0
- package/dist/interpreter/components/evaluators/ExpressionEvaluator.js +298 -0
- package/dist/interpreter/components/evaluators/OperationEvaluator.d.ts +25 -0
- package/dist/interpreter/components/evaluators/OperationEvaluator.js +226 -0
- package/dist/interpreter/components/evaluators/PrimitiveEvaluator.d.ts +19 -0
- package/dist/interpreter/components/evaluators/PrimitiveEvaluator.js +50 -0
- package/dist/interpreter/components/evaluators/StatementEvaluator.d.ts +28 -0
- package/dist/interpreter/components/evaluators/StatementEvaluator.js +240 -0
- package/dist/interpreter/components/evaluators/TestingEvaluator.d.ts +15 -0
- package/dist/interpreter/components/evaluators/TestingEvaluator.js +14 -0
- package/dist/interpreter/components/evaluators/index.d.ts +110 -0
- package/dist/interpreter/components/evaluators/index.js +21 -0
- package/dist/interpreter/components/evaluators/utils.d.ts +7 -0
- package/dist/interpreter/components/evaluators/utils.js +50 -0
- package/dist/interpreter/components/hooks/YukigoHook.d.ts +27 -0
- package/dist/interpreter/components/hooks/YukigoHook.js +1 -0
- package/dist/interpreter/components/kernel/commands.d.ts +122 -0
- package/dist/interpreter/components/kernel/commands.js +203 -0
- package/dist/interpreter/components/kernel/index.d.ts +51 -0
- package/dist/interpreter/components/kernel/index.js +176 -0
- package/dist/interpreter/components/logic/LogicEngine.d.ts +26 -23
- package/dist/interpreter/components/logic/LogicEngine.js +234 -221
- package/dist/interpreter/components/logic/LogicResolver.d.ts +22 -51
- package/dist/interpreter/components/logic/LogicResolver.js +73 -440
- package/dist/interpreter/components/logic/LogicTerm.d.ts +104 -0
- package/dist/interpreter/components/logic/LogicTerm.js +423 -0
- package/dist/interpreter/components/logic/LogicTranslator.d.ts +25 -9
- package/dist/interpreter/components/logic/LogicTranslator.js +211 -74
- package/dist/interpreter/components/runtimes/FunctionRuntime.d.ts +6 -4
- package/dist/interpreter/components/runtimes/FunctionRuntime.js +82 -84
- package/dist/interpreter/components/runtimes/LazyRuntime.d.ts +13 -14
- package/dist/interpreter/components/runtimes/LazyRuntime.js +112 -239
- package/dist/interpreter/components/runtimes/ObjectRuntime.d.ts +17 -14
- package/dist/interpreter/components/runtimes/ObjectRuntime.js +105 -74
- package/dist/interpreter/errors.d.ts +10 -0
- package/dist/interpreter/errors.js +17 -0
- package/dist/interpreter/index.d.ts +9 -5
- package/dist/interpreter/index.js +11 -13
- package/dist/interpreter/primitives/YuValue.d.ts +21 -0
- package/dist/interpreter/primitives/YuValue.js +40 -0
- package/dist/interpreter/primitives/capabilities.d.ts +90 -0
- package/dist/interpreter/primitives/capabilities.js +1 -0
- package/dist/interpreter/primitives/entities/LazyList.d.ts +48 -0
- package/dist/interpreter/primitives/entities/LazyList.js +164 -0
- package/dist/interpreter/primitives/entities/LogicResult.d.ts +51 -0
- package/dist/interpreter/primitives/entities/LogicResult.js +101 -0
- package/dist/interpreter/primitives/entities/RuntimeClass.d.ts +24 -0
- package/dist/interpreter/primitives/entities/RuntimeClass.js +57 -0
- package/dist/interpreter/primitives/entities/RuntimeFunction.d.ts +40 -0
- package/dist/interpreter/primitives/entities/RuntimeFunction.js +73 -0
- package/dist/interpreter/primitives/entities/RuntimeObject.d.ts +27 -0
- package/dist/interpreter/primitives/entities/RuntimeObject.js +82 -0
- package/dist/interpreter/primitives/entities/RuntimePredicate.d.ts +19 -0
- package/dist/interpreter/primitives/entities/RuntimePredicate.js +59 -0
- package/dist/interpreter/primitives/index.d.ts +14 -0
- package/dist/interpreter/primitives/index.js +14 -0
- package/dist/interpreter/primitives/scalars/YuBoolean.d.ts +24 -0
- package/dist/interpreter/primitives/scalars/YuBoolean.js +69 -0
- package/dist/interpreter/primitives/scalars/YuChar.d.ts +28 -0
- package/dist/interpreter/primitives/scalars/YuChar.js +78 -0
- package/dist/interpreter/primitives/scalars/YuNil.d.ts +25 -0
- package/dist/interpreter/primitives/scalars/YuNil.js +73 -0
- package/dist/interpreter/primitives/scalars/YuNumber.d.ts +49 -0
- package/dist/interpreter/primitives/scalars/YuNumber.js +165 -0
- package/dist/interpreter/primitives/sequences/YuArray.d.ts +33 -0
- package/dist/interpreter/primitives/sequences/YuArray.js +150 -0
- package/dist/interpreter/primitives/sequences/YuString.d.ts +40 -0
- package/dist/interpreter/primitives/sequences/YuString.js +130 -0
- package/dist/interpreter/utils.d.ts +23 -10
- package/dist/interpreter/utils.js +25 -30
- package/dist/tester/index.d.ts +3 -3
- package/dist/tester/index.js +39 -28
- package/dist/utils/helpers.d.ts +13 -5
- package/dist/utils/helpers.js +38 -85
- package/package.json +3 -16
- package/src/analyzer/GraphBuilder.ts +143 -142
- package/src/analyzer/index.ts +185 -194
- package/src/analyzer/inspections/functional/functional.ts +121 -121
- package/src/analyzer/inspections/functional/smells.ts +88 -102
- package/src/analyzer/inspections/generic/generic.ts +610 -577
- package/src/analyzer/inspections/generic/smells.ts +355 -365
- package/src/analyzer/inspections/imperative/imperative.ts +104 -101
- package/src/analyzer/inspections/imperative/smells.ts +58 -54
- package/src/analyzer/inspections/logic/logic.ts +84 -90
- package/src/analyzer/inspections/logic/smells.ts +54 -54
- package/src/analyzer/inspections/object/object.ts +295 -264
- package/src/analyzer/inspections/object/smells.ts +130 -144
- package/src/analyzer/utils.ts +115 -109
- package/src/index.ts +3 -3
- package/src/interpreter/components/EnvBuilder.ts +405 -202
- package/src/interpreter/components/EqualityComparer.ts +107 -0
- package/src/interpreter/components/Operations.ts +126 -99
- package/src/interpreter/components/PatternMatcher.ts +375 -475
- package/src/interpreter/components/RuntimeContext.ts +181 -119
- package/src/interpreter/components/TestRunner.ts +223 -151
- package/src/interpreter/components/evaluators/BaseEvaluator.ts +36 -0
- package/src/interpreter/components/evaluators/ExpressionEvaluator.ts +475 -0
- package/src/interpreter/components/evaluators/OperationEvaluator.ts +477 -0
- package/src/interpreter/components/evaluators/PrimitiveEvaluator.ts +80 -0
- package/src/interpreter/components/evaluators/StatementEvaluator.ts +372 -0
- package/src/interpreter/components/evaluators/TestingEvaluator.ts +26 -0
- package/src/interpreter/components/evaluators/index.ts +34 -0
- package/src/interpreter/components/evaluators/utils.ts +64 -0
- package/src/interpreter/components/hooks/YukigoHook.ts +45 -0
- package/src/interpreter/components/kernel/commands.ts +255 -0
- package/src/interpreter/components/kernel/index.ts +222 -0
- package/src/interpreter/components/logic/LogicEngine.ts +509 -519
- package/src/interpreter/components/logic/LogicResolver.ts +209 -858
- package/src/interpreter/components/logic/LogicTerm.ts +477 -0
- package/src/interpreter/components/logic/LogicTranslator.ts +362 -149
- package/src/interpreter/components/runtimes/FunctionRuntime.ts +249 -227
- package/src/interpreter/components/runtimes/LazyRuntime.ts +226 -334
- package/src/interpreter/components/runtimes/ObjectRuntime.ts +262 -224
- package/src/interpreter/errors.ts +68 -47
- package/src/interpreter/index.ts +50 -52
- package/src/interpreter/primitives/YuValue.ts +64 -0
- package/src/interpreter/primitives/capabilities.ts +111 -0
- package/src/interpreter/primitives/entities/LazyList.ts +225 -0
- package/src/interpreter/primitives/entities/LogicResult.ts +133 -0
- package/src/interpreter/primitives/entities/RuntimeClass.ts +70 -0
- package/src/interpreter/primitives/entities/RuntimeFunction.ts +108 -0
- package/src/interpreter/primitives/entities/RuntimeObject.ts +110 -0
- package/src/interpreter/primitives/entities/RuntimePredicate.ts +77 -0
- package/src/interpreter/primitives/index.ts +17 -0
- package/src/interpreter/primitives/scalars/YuBoolean.ts +79 -0
- package/src/interpreter/primitives/scalars/YuChar.ts +94 -0
- package/src/interpreter/primitives/scalars/YuNil.ts +84 -0
- package/src/interpreter/primitives/scalars/YuNumber.ts +187 -0
- package/src/interpreter/primitives/sequences/YuArray.ts +178 -0
- package/src/interpreter/primitives/sequences/YuString.ts +165 -0
- package/src/interpreter/utils.ts +105 -84
- package/src/tester/index.ts +157 -128
- package/src/utils/helpers.ts +113 -169
- package/tests/analyzer/functional.spec.ts +221 -207
- package/tests/analyzer/generic.spec.ts +178 -178
- package/tests/analyzer/helpers.spec.ts +83 -89
- package/tests/analyzer/logic.spec.ts +237 -237
- package/tests/analyzer/oop.spec.ts +320 -323
- package/tests/analyzer/transitive.spec.ts +166 -166
- package/tests/interpreter/EnvBuilder.spec.ts +181 -183
- package/tests/interpreter/FunctionRuntime.spec.ts +224 -223
- package/tests/interpreter/LazyRuntime.spec.ts +227 -225
- package/tests/interpreter/LogicEngine.spec.ts +371 -327
- package/tests/interpreter/LogicSubstitution.spec.ts +90 -80
- package/tests/interpreter/ObjectRuntime.spec.ts +431 -606
- package/tests/interpreter/Operations.spec.ts +312 -220
- package/tests/interpreter/PatternSystem.spec.ts +254 -213
- package/tests/interpreter/Tests.spec.ts +122 -122
- package/tests/interpreter/interpreter.spec.ts +1143 -991
- package/tests/kernel/YukigoKernel.spec.ts +112 -0
- package/tests/tester/Tester.spec.ts +156 -152
- package/tsconfig.build.json +16 -15
- package/tsconfig.json +27 -25
- package/dist/interpreter/components/Visitor.d.ts +0 -70
- package/dist/interpreter/components/Visitor.js +0 -634
- package/dist/interpreter/entities.d.ts +0 -105
- package/dist/interpreter/entities.js +0 -96
- package/dist/interpreter/trampoline.d.ts +0 -17
- package/dist/interpreter/trampoline.js +0 -38
- package/dist/src/analyzer/GraphBuilder.d.ts +0 -30
- package/dist/src/analyzer/GraphBuilder.js +0 -100
- package/dist/src/analyzer/index.d.ts +0 -59
- package/dist/src/analyzer/index.js +0 -152
- package/dist/src/analyzer/inspections/functional/functional.d.ts +0 -44
- package/dist/src/analyzer/inspections/functional/functional.js +0 -149
- package/dist/src/analyzer/inspections/functional/smells.d.ts +0 -16
- package/dist/src/analyzer/inspections/functional/smells.js +0 -98
- package/dist/src/analyzer/inspections/generic/generic.d.ts +0 -178
- package/dist/src/analyzer/inspections/generic/generic.js +0 -604
- package/dist/src/analyzer/inspections/generic/smells.d.ts +0 -61
- package/dist/src/analyzer/inspections/generic/smells.js +0 -349
- package/dist/src/analyzer/inspections/imperative/imperative.d.ts +0 -35
- package/dist/src/analyzer/inspections/imperative/imperative.js +0 -109
- package/dist/src/analyzer/inspections/imperative/smells.d.ts +0 -16
- package/dist/src/analyzer/inspections/imperative/smells.js +0 -58
- package/dist/src/analyzer/inspections/logic/logic.d.ts +0 -32
- package/dist/src/analyzer/inspections/logic/logic.js +0 -96
- package/dist/src/analyzer/inspections/logic/smells.d.ts +0 -15
- package/dist/src/analyzer/inspections/logic/smells.js +0 -60
- package/dist/src/analyzer/inspections/object/object.d.ts +0 -90
- package/dist/src/analyzer/inspections/object/object.js +0 -321
- package/dist/src/analyzer/inspections/object/smells.d.ts +0 -30
- package/dist/src/analyzer/inspections/object/smells.js +0 -135
- package/dist/src/analyzer/utils.d.ts +0 -30
- package/dist/src/analyzer/utils.js +0 -78
- package/dist/src/index.d.ts +0 -4
- package/dist/src/index.js +0 -4
- package/dist/src/interpreter/components/EnvBuilder.d.ts +0 -21
- package/dist/src/interpreter/components/EnvBuilder.js +0 -155
- package/dist/src/interpreter/components/Operations.d.ts +0 -14
- package/dist/src/interpreter/components/Operations.js +0 -84
- package/dist/src/interpreter/components/PatternMatcher.d.ts +0 -73
- package/dist/src/interpreter/components/PatternMatcher.js +0 -358
- package/dist/src/interpreter/components/RuntimeContext.d.ts +0 -35
- package/dist/src/interpreter/components/RuntimeContext.js +0 -93
- package/dist/src/interpreter/components/TestRunner.d.ts +0 -19
- package/dist/src/interpreter/components/TestRunner.js +0 -110
- package/dist/src/interpreter/components/Visitor.d.ts +0 -71
- package/dist/src/interpreter/components/Visitor.js +0 -638
- package/dist/src/interpreter/components/logic/LogicEngine.d.ts +0 -29
- package/dist/src/interpreter/components/logic/LogicEngine.js +0 -259
- package/dist/src/interpreter/components/logic/LogicResolver.d.ts +0 -53
- package/dist/src/interpreter/components/logic/LogicResolver.js +0 -484
- package/dist/src/interpreter/components/logic/LogicTranslator.d.ts +0 -14
- package/dist/src/interpreter/components/logic/LogicTranslator.js +0 -99
- package/dist/src/interpreter/components/runtimes/FunctionRuntime.d.ts +0 -12
- package/dist/src/interpreter/components/runtimes/FunctionRuntime.js +0 -149
- package/dist/src/interpreter/components/runtimes/LazyRuntime.d.ts +0 -19
- package/dist/src/interpreter/components/runtimes/LazyRuntime.js +0 -269
- package/dist/src/interpreter/components/runtimes/ObjectRuntime.d.ts +0 -35
- package/dist/src/interpreter/components/runtimes/ObjectRuntime.js +0 -126
- package/dist/src/interpreter/errors.d.ts +0 -19
- package/dist/src/interpreter/errors.js +0 -36
- package/dist/src/interpreter/index.d.ts +0 -24
- package/dist/src/interpreter/index.js +0 -41
- package/dist/src/interpreter/trampoline.d.ts +0 -17
- package/dist/src/interpreter/trampoline.js +0 -38
- package/dist/src/interpreter/utils.d.ts +0 -11
- package/dist/src/interpreter/utils.js +0 -65
- package/dist/src/tester/index.d.ts +0 -25
- package/dist/src/tester/index.js +0 -113
- package/dist/src/utils/helpers.d.ts +0 -13
- package/dist/src/utils/helpers.js +0 -52
- package/src/interpreter/components/Visitor.ts +0 -1065
- package/src/interpreter/trampoline.ts +0 -71
|
@@ -1,323 +1,320 @@
|
|
|
1
|
-
import { expect } from "chai";
|
|
2
|
-
import {
|
|
3
|
-
Class,
|
|
4
|
-
Method,
|
|
5
|
-
Send,
|
|
6
|
-
Self,
|
|
7
|
-
SymbolPrimitive,
|
|
8
|
-
Attribute,
|
|
9
|
-
New,
|
|
10
|
-
Interface,
|
|
11
|
-
Object as AstObject,
|
|
12
|
-
PrimitiveMethod,
|
|
13
|
-
Implement,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
} from "yukigo-ast";
|
|
24
|
-
import { Analyzer, InspectionRule } from "../../src/analyzer/index.js";
|
|
25
|
-
|
|
26
|
-
describe("OOP Inspections", () => {
|
|
27
|
-
const createSymbol = (name: string) => new SymbolPrimitive(name);
|
|
28
|
-
const analyzer = new Analyzer();
|
|
29
|
-
|
|
30
|
-
const runSingleRule = (
|
|
31
|
-
ast: any[],
|
|
32
|
-
inspection: string,
|
|
33
|
-
expected: boolean,
|
|
34
|
-
binding?: string,
|
|
35
|
-
args: string[] = []
|
|
36
|
-
) => {
|
|
37
|
-
const rule: InspectionRule = { inspection, binding, args, expected };
|
|
38
|
-
const results = analyzer.analyze(ast, [rule]);
|
|
39
|
-
const result = results[0];
|
|
40
|
-
return result.passed;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const createMethod = (
|
|
44
|
-
name: string,
|
|
45
|
-
equationsCount: number = 1,
|
|
46
|
-
isAbstract: boolean = false,
|
|
47
|
-
bodyNode?: Statement
|
|
48
|
-
): Method => {
|
|
49
|
-
const identifier = createSymbol(name);
|
|
50
|
-
const stmts = bodyNode ? [bodyNode] : [];
|
|
51
|
-
const equations = new Array(equationsCount).fill(
|
|
52
|
-
new Equation([], new UnguardedBody(new Sequence(stmts)))
|
|
53
|
-
);
|
|
54
|
-
const method = new Method(identifier, equations);
|
|
55
|
-
method
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const
|
|
78
|
-
const
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
const
|
|
200
|
-
const
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
const
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
);
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
"
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
);
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
});
|
|
322
|
-
});
|
|
323
|
-
});
|
|
1
|
+
import { expect } from "chai";
|
|
2
|
+
import {
|
|
3
|
+
Class,
|
|
4
|
+
Method,
|
|
5
|
+
Send,
|
|
6
|
+
Self,
|
|
7
|
+
SymbolPrimitive,
|
|
8
|
+
Attribute,
|
|
9
|
+
New,
|
|
10
|
+
Interface,
|
|
11
|
+
Object as AstObject,
|
|
12
|
+
PrimitiveMethod,
|
|
13
|
+
Implement,
|
|
14
|
+
Equation,
|
|
15
|
+
UnguardedBody,
|
|
16
|
+
Sequence,
|
|
17
|
+
NilPrimitive,
|
|
18
|
+
Statement,
|
|
19
|
+
Print,
|
|
20
|
+
StringPrimitive,
|
|
21
|
+
Variable,
|
|
22
|
+
SourceLocation,
|
|
23
|
+
} from "yukigo-ast";
|
|
24
|
+
import { Analyzer, InspectionRule } from "../../src/analyzer/index.js";
|
|
25
|
+
|
|
26
|
+
describe("OOP Inspections", () => {
|
|
27
|
+
const createSymbol = (name: string) => new SymbolPrimitive(name);
|
|
28
|
+
const analyzer = new Analyzer();
|
|
29
|
+
|
|
30
|
+
const runSingleRule = (
|
|
31
|
+
ast: any[],
|
|
32
|
+
inspection: string,
|
|
33
|
+
expected: boolean,
|
|
34
|
+
binding?: string,
|
|
35
|
+
args: string[] = []
|
|
36
|
+
) => {
|
|
37
|
+
const rule: InspectionRule = { inspection, binding, args, expected };
|
|
38
|
+
const results = analyzer.analyze(ast, [rule]);
|
|
39
|
+
const result = results[0];
|
|
40
|
+
return result.passed;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const createMethod = (
|
|
44
|
+
name: string,
|
|
45
|
+
equationsCount: number = 1,
|
|
46
|
+
isAbstract: boolean = false,
|
|
47
|
+
bodyNode?: Statement
|
|
48
|
+
): Method => {
|
|
49
|
+
const identifier = createSymbol(name);
|
|
50
|
+
const stmts = bodyNode ? [bodyNode] : [];
|
|
51
|
+
const equations = new Array(equationsCount).fill(
|
|
52
|
+
new Equation([], new UnguardedBody(new Sequence(stmts)))
|
|
53
|
+
);
|
|
54
|
+
const method = new Method(identifier, equations, undefined, isAbstract);
|
|
55
|
+
return method;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const createAttribute = (
|
|
59
|
+
name: string,
|
|
60
|
+
expression: any = new NilPrimitive(null)
|
|
61
|
+
) => {
|
|
62
|
+
return new Attribute(createSymbol(name), expression);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
const createNew = (className: string) => {
|
|
66
|
+
return new New(createSymbol(className), []);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const createClass = (
|
|
70
|
+
name: string,
|
|
71
|
+
stmts: Statement[] = [],
|
|
72
|
+
extendsName?: string,
|
|
73
|
+
implementsName?: string,
|
|
74
|
+
includes?: string[]
|
|
75
|
+
): Class => {
|
|
76
|
+
const identifier = createSymbol(name);
|
|
77
|
+
const extendsSymbol = extendsName ? createSymbol(extendsName) : undefined;
|
|
78
|
+
const implementsNode = implementsName
|
|
79
|
+
? new Implement(createSymbol(implementsName))
|
|
80
|
+
: undefined;
|
|
81
|
+
const includesMixin = includes
|
|
82
|
+
? includes.map((sym) => createSymbol(sym))
|
|
83
|
+
: [];
|
|
84
|
+
const expression = new Sequence(stmts);
|
|
85
|
+
|
|
86
|
+
return new Class(
|
|
87
|
+
identifier,
|
|
88
|
+
extendsSymbol,
|
|
89
|
+
implementsNode,
|
|
90
|
+
includesMixin,
|
|
91
|
+
expression
|
|
92
|
+
);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
const createObject = (name: string, children: Statement[] = []) => {
|
|
96
|
+
const identifier = createSymbol(name);
|
|
97
|
+
const expression = new Sequence(children);
|
|
98
|
+
return new AstObject(identifier, expression);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
describe("DeclaresAttribute", () => {
|
|
102
|
+
it("should detect if an attribute with the specific name is declared", () => {
|
|
103
|
+
const attr = createAttribute("energy");
|
|
104
|
+
const ast = [createClass("A", [attr])];
|
|
105
|
+
expect(
|
|
106
|
+
runSingleRule(ast, "DeclaresAttribute", true, undefined, ["energy"])
|
|
107
|
+
).to.be.true;
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should ignore attributes with different names", () => {
|
|
111
|
+
const attr = createAttribute("life");
|
|
112
|
+
const ast = [createClass("A", [attr])];
|
|
113
|
+
expect(
|
|
114
|
+
runSingleRule(ast, "DeclaresAttribute", false, undefined, ["energy"])
|
|
115
|
+
).to.be.true;
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
describe("DeclaresClass", () => {
|
|
120
|
+
it("should detect specific class declaration", () => {
|
|
121
|
+
const ast = [createClass("Bird")];
|
|
122
|
+
expect(runSingleRule(ast, "DeclaresClass", true, "Bird")).to.be.true;
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
describe("DeclaresInterface", () => {
|
|
127
|
+
it("should detect specific interface declaration", () => {
|
|
128
|
+
const ast = [
|
|
129
|
+
new Interface(createSymbol("Flyable"), [], new Sequence([])),
|
|
130
|
+
];
|
|
131
|
+
expect(runSingleRule(ast, "DeclaresInterface", true, "Flyable")).to.be
|
|
132
|
+
.true;
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("DeclaresMethod", () => {
|
|
137
|
+
it("should detect specific method declaration", () => {
|
|
138
|
+
const ast = [createMethod("fly")];
|
|
139
|
+
expect(runSingleRule(ast, "DeclaresMethod", true, "fly")).to.be.true;
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe("DeclaresObject", () => {
|
|
144
|
+
it("should detect specific object declaration", () => {
|
|
145
|
+
const ast = [createObject("pepita")];
|
|
146
|
+
expect(runSingleRule(ast, "DeclaresObject", true, "pepita")).to.be.true;
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
describe("DeclaresPrimitive", () => {
|
|
151
|
+
it("should detect primitive operator override", () => {
|
|
152
|
+
const op: any = "==";
|
|
153
|
+
const ast = [new PrimitiveMethod(op, [], new SourceLocation(0,0))];
|
|
154
|
+
expect(runSingleRule(ast, "DeclaresPrimitive", true, undefined, ["=="]))
|
|
155
|
+
.to.be.true;
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe("DeclaresSuperclass", () => {
|
|
160
|
+
it("should detect if a class extends a specific superclass", () => {
|
|
161
|
+
const ast = [createClass("Sparrow", [], "Bird")];
|
|
162
|
+
expect(
|
|
163
|
+
runSingleRule(ast, "DeclaresSuperclass", true, undefined, ["Bird"])
|
|
164
|
+
).to.be.true;
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
describe("Implements", () => {
|
|
169
|
+
it("should detect if a class implements a specific interface", () => {
|
|
170
|
+
const ast = [createClass("Pigeon", [], undefined, "Messenger")];
|
|
171
|
+
expect(runSingleRule(ast, "Implements", true, undefined, ["Messenger"]))
|
|
172
|
+
.to.be.true;
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
describe("IncludeMixin", () => {
|
|
177
|
+
it("should detect usage of a specific mixin", () => {
|
|
178
|
+
const ast = [
|
|
179
|
+
createClass("ClassWithMixin", [], undefined, undefined, ["Walking"]),
|
|
180
|
+
];
|
|
181
|
+
expect(runSingleRule(ast, "IncludeMixin", true, undefined, ["Walking"]))
|
|
182
|
+
.to.be.true;
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
describe("Instantiates", () => {
|
|
187
|
+
it("should detect instantiation of a specific class", () => {
|
|
188
|
+
const ast = [new Variable(new SymbolPrimitive("a"), createNew("Engine"))];
|
|
189
|
+
expect(runSingleRule(ast, "Instantiates", true, undefined, ["Engine"])).to
|
|
190
|
+
.be.true;
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
describe("UsesDynamicPolymorphism", () => {
|
|
195
|
+
it("should detect when a method name is used in at least two different places (polymorphism)", () => {
|
|
196
|
+
const method1 = createMethod("fly");
|
|
197
|
+
const method2 = createMethod("fly");
|
|
198
|
+
const class1 = createClass("Bird", [method1]);
|
|
199
|
+
const class2 = createClass("Airplane", [method2]);
|
|
200
|
+
const ast = [class1, class2];
|
|
201
|
+
expect(runSingleRule(ast, "UsesDynamicPolymorphism", true, undefined, ["fly"]))
|
|
202
|
+
.to.be.true;
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
it("should not throw if only one method is found", () => {
|
|
206
|
+
const method1 = createMethod("fly");
|
|
207
|
+
const class1 = createClass("Bird", [method1]);
|
|
208
|
+
const ast = [class1];
|
|
209
|
+
expect(runSingleRule(ast, "UsesDynamicPolymorphism", false, "fly")).to.be
|
|
210
|
+
.true;
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
describe("UsesInheritance", () => {
|
|
215
|
+
it("should detect inheritance in classes", () => {
|
|
216
|
+
const ast = [createClass("Child", [], "Parent")];
|
|
217
|
+
expect(runSingleRule(ast, "UsesInheritance", true)).to.be.true;
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("should detect inheritance in interfaces", () => {
|
|
221
|
+
const ast = [
|
|
222
|
+
new Interface(
|
|
223
|
+
createSymbol("IChild"),
|
|
224
|
+
[createSymbol("IParent")],
|
|
225
|
+
new Sequence([])
|
|
226
|
+
),
|
|
227
|
+
];
|
|
228
|
+
expect(runSingleRule(ast, "UsesInheritance", true)).to.be.true;
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("should ignore classes without parent", () => {
|
|
232
|
+
const ast = [createClass("Orphan")];
|
|
233
|
+
expect(runSingleRule(ast, "UsesInheritance", false)).to.be.true;
|
|
234
|
+
});
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
describe("UsesMixins", () => {
|
|
238
|
+
it("should detect any mixin usage", () => {
|
|
239
|
+
const ast = [
|
|
240
|
+
createClass("ClassWithMixin", [], undefined, undefined, ["AnyMixin"]),
|
|
241
|
+
];
|
|
242
|
+
expect(runSingleRule(ast, "UsesMixins", true)).to.be.true;
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe("UsesObjectComposition", () => {
|
|
247
|
+
it("should detect composition when an attribute is initialized with New", () => {
|
|
248
|
+
const instantiation = createNew("Engine");
|
|
249
|
+
const attr = createAttribute("myEngine", instantiation);
|
|
250
|
+
const ast = [createClass("A", [attr])];
|
|
251
|
+
expect(runSingleRule(ast, "UsesObjectComposition", true)).to.be.true;
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it("should ignore attributes initialized with literals", () => {
|
|
255
|
+
const attr = createAttribute(
|
|
256
|
+
"energy",
|
|
257
|
+
new StringPrimitive("Initialized with string!")
|
|
258
|
+
);
|
|
259
|
+
const ast = [createClass("A", [attr])];
|
|
260
|
+
expect(runSingleRule(ast, "UsesObjectComposition", false)).to.be.true;
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
describe("UsesStaticMethodOverload", () => {
|
|
265
|
+
it("should detect two methods with the same name in the same class", () => {
|
|
266
|
+
const m1 = createMethod("calculate");
|
|
267
|
+
const m2 = createMethod("calculate");
|
|
268
|
+
const ast = [createClass("Calculator", [m1, m2])];
|
|
269
|
+
expect(runSingleRule(ast, "UsesStaticMethodOverload", true)).to.be.true;
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it("should NOT detect overload if methods are in different classes", () => {
|
|
273
|
+
const m1 = createMethod("calculate");
|
|
274
|
+
const m2 = createMethod("calculate");
|
|
275
|
+
const classA = createClass("A", [m1]);
|
|
276
|
+
const classB = createClass("B", [m2]);
|
|
277
|
+
const ast = [classA, classB];
|
|
278
|
+
expect(runSingleRule(ast, "UsesStaticMethodOverload", false)).to.be.true;
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe("UsesDynamicMethodOverload", () => {
|
|
283
|
+
it("should detect a method with multiple equations (pattern matching)", () => {
|
|
284
|
+
const ast = [createMethod("fibonacci", 2)];
|
|
285
|
+
expect(runSingleRule(ast, "UsesDynamicMethodOverload", true)).to.be.true;
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
it("should ignore methods with a single equation", () => {
|
|
289
|
+
const ast = [createMethod("fibonacci", 1)];
|
|
290
|
+
expect(runSingleRule(ast, "UsesDynamicMethodOverload", false)).to.be.true;
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
describe("UsesTemplateMethod", () => {
|
|
295
|
+
it("should detect template method usage (call to undeclared method on self)", () => {
|
|
296
|
+
const declaredMethod = createMethod("abstractMethod", 1, true);
|
|
297
|
+
const sendToSelf = new Send(
|
|
298
|
+
new Self(),
|
|
299
|
+
createSymbol("abstractMethod"),
|
|
300
|
+
[]
|
|
301
|
+
);
|
|
302
|
+
const templateMethod = createMethod("process", 1, false, sendToSelf);
|
|
303
|
+
const ast = [createClass("MyClass", [declaredMethod, templateMethod])];
|
|
304
|
+
expect(runSingleRule(ast, "UsesTemplateMethod", true)).to.be.true;
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("should NOT detect if calling a declared method on self", () => {
|
|
308
|
+
const declaredMethod = createMethod(
|
|
309
|
+
"declared",
|
|
310
|
+
1,
|
|
311
|
+
false,
|
|
312
|
+
new Print(new StringPrimitive("This method is Declared"))
|
|
313
|
+
);
|
|
314
|
+
const sendToSelf = new Send(new Self(), createSymbol("declared"), []);
|
|
315
|
+
const method = createMethod("process", 1, false, sendToSelf);
|
|
316
|
+
const ast = [createClass("MyClass", [declaredMethod, method])];
|
|
317
|
+
expect(runSingleRule(ast, "UsesTemplateMethod", false)).to.be.true;
|
|
318
|
+
});
|
|
319
|
+
});
|
|
320
|
+
});
|