Skip to content

Commit 2c981c9

Browse files
authored
feat: improved testcase readability (#176)
* Created assertion data object such that we do not have to rely on logs to file anymore * Used the context builder to create better variable names * Enable assertions again * Replace try catch for exceptions by proper "rejectedWith" function from chai I also found a bug: The require function caches contents of files. So when we require a "constant object" from a file multiple times we are actually using the same instance. During the search process this is no problem since all tests are ran in isolation. However, the final test suite puts all the tests in the same file so the results where not consistent. I fixed this by removing cached files in the beforeEach method of mocha.
1 parent 67df3cc commit 2c981c9

36 files changed

+854
-878
lines changed

libraries/analysis-javascript/lib/type/discovery/element/ElementVisitor.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,35 @@ export class ElementVisitor extends AbstractSyntaxTreeVisitor {
5454
const id = this._getNodeId(path);
5555
const bindingId = this._getBindingId(path);
5656

57+
// Here we check if the id is already registered (we do not allow this normally)
5758
if (this._elementMap.has(id)) {
59+
// Export specifiers can actually have the same exported and local object
60+
// e.g. export { x }
5861
if (
5962
path.parentPath.isExportSpecifier() &&
6063
path.parentPath.get("exported") === path
6164
) {
6265
return;
6366
}
67+
68+
// Import specifiers can actually have the same imported and local object
69+
// e.g. import { x } from '...'
70+
if (
71+
path.parentPath.isImportSpecifier() &&
72+
path.parentPath.get("imported") === path
73+
) {
74+
return;
75+
}
76+
77+
// Object properties can actually have the same value and key object
78+
// e.g. const obj = { x }
79+
if (
80+
path.parentPath.isObjectProperty() &&
81+
path.parentPath.get("value") === path
82+
) {
83+
return;
84+
}
85+
6486
throw new Error(`Overriding element with id: ${id}`);
6587
}
6688

libraries/instrumentation-javascript/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@
4646
"test:watch": "mocha --config ../../.mocharc.json --watch"
4747
},
4848
"dependencies": {
49+
"@syntest/analysis-javascript": "^0.1.0-beta.21",
4950
"@babel/core": "7.20.12",
5051
"@babel/traverse": "7.20.12",
5152
"@istanbuljs/schema": "^0.1.3",
52-
"@syntest/analysis-javascript": "^0.1.0-beta.21",
5353
"@syntest/ast-visitor-javascript": "*",
5454
"@syntest/storage": "*",
5555
"istanbul-lib-coverage": "^3.2.0"

libraries/search-javascript/lib/search/crossover/TreeCrossover.ts

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,51 @@ export class TreeCrossover extends Crossover<JavaScriptTestCase> {
6565

6666
for (const swapA of swapStatementsA) {
6767
for (const swapB of swapStatementsB) {
68-
if (!swapA.child.classType || !swapB.child.classType) {
69-
throw new Error("All statements require a classType!");
70-
}
71-
7268
if (swapA.child.variableIdentifier === swapB.child.variableIdentifier) {
7369
if (
74-
(swapA.child instanceof ConstructorCall ||
75-
swapB.child instanceof ConstructorCall ||
76-
swapA.child instanceof ConstantObject ||
77-
swapB.child instanceof ConstantObject) && // if one of the two is a constructorcall or constant object both need to be
78-
swapA.child.classType !== swapB.child.classType
70+
swapA.child instanceof ConstructorCall &&
71+
!(swapB.child instanceof ConstructorCall)
72+
) {
73+
continue;
74+
}
75+
76+
if (
77+
swapB.child instanceof ConstructorCall &&
78+
!(swapA.child instanceof ConstructorCall)
79+
) {
80+
continue;
81+
}
82+
83+
if (
84+
swapA.child instanceof ConstructorCall &&
85+
swapB.child instanceof ConstructorCall &&
86+
swapA.child.export.id !== swapB.child.export.id
87+
) {
88+
continue;
89+
}
90+
91+
if (
92+
swapA.child instanceof ConstantObject &&
93+
!(swapB.child instanceof ConstantObject)
94+
) {
95+
continue;
96+
}
97+
98+
if (
99+
swapB.child instanceof ConstantObject &&
100+
!(swapA.child instanceof ConstantObject)
79101
) {
80102
continue;
81103
}
104+
105+
if (
106+
swapA.child instanceof ConstantObject &&
107+
swapB.child instanceof ConstantObject &&
108+
swapA.child.export.id !== swapB.child.export.id
109+
) {
110+
continue;
111+
}
112+
82113
crossoverOptions.push({
83114
parentA: swapA,
84115
parentB: swapB,

0 commit comments

Comments
 (0)