Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat support decimaljs #190

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
563 changes: 439 additions & 124 deletions libraries/analysis-javascript/lib/target/TargetVisitor.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export function extractExportsFromRightAssignmentExpression(
});
}

console.log(exports);
return exports;
}

Expand Down
2 changes: 1 addition & 1 deletion libraries/analysis-javascript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"format:check": "prettier --config ../../.prettierrc.json --ignore-path ../../.prettierignore --check .",
"lint": "eslint --config ../../.eslintrc.json --ignore-path ../../.eslintignore .",
"lint:fix": "eslint --config ../../.eslintrc.json --ignore-path ../../.eslintignore . --fix",
"test": "mocha --config ../../.mocharc.json",
"test": "mocha --config ../../.mocharc.json -g 'Prototyped TargetVisitor test'",
"test:coverage": "nyc --reporter=text --reporter=html --reporter=lcov mocha --config ../../.mocharc.json",
"test:coverage:ci": "nyc --reporter=lcovonly mocha --config ../../.mocharc.json --reporter json --reporter-option output=test-results.json",
"test:watch": "mocha --config ../../.mocharc.json --watch"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
* Copyright 2020-2023 Delft University of Technology and SynTest contributors
*
* This file is part of SynTest Framework - SynTest JavaScript.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { traverse } from "@babel/core";
import { TargetType } from "@syntest/analysis";
import * as chai from "chai";

import { AbstractSyntaxTreeFactory } from "../../lib/ast/AbstractSyntaxTreeFactory";
import { ExportVisitor } from "../../lib/target/export/ExportVisitor";
import {
ClassTarget,
MethodTarget,
ObjectFunctionTarget,
ObjectTarget,
SubTarget,
} from "../../lib/target/Target";
import { TargetVisitor } from "../../lib/target/TargetVisitor";

const expect = chai.expect;

function targetHelper(source: string) {
const generator = new AbstractSyntaxTreeFactory();
const ast = generator.convert("", source);

const exportVisitor = new ExportVisitor("", true);
traverse(ast, exportVisitor);
const exports = exportVisitor.exports;

const visitor = new TargetVisitor("", true, exports);
traverse(ast, visitor);

return visitor.subTargets;
}
// function checkFunction(
// target: SubTarget,
// name: string,
// exported: boolean,
// isAsync: boolean
// ): void {
// expect(target.type).to.equal(TargetType.FUNCTION);

// const functionTarget = <FunctionTarget>target;

// expect(functionTarget.name).to.equal(name);
// expect(functionTarget.exported).to.equal(exported);
// expect(functionTarget.isAsync).to.equal(isAsync);
// }

function checkObject(target: SubTarget, name: string, exported: boolean): void {
expect(target.type).to.equal(TargetType.OBJECT);

const objectTarget = <ObjectTarget>target;

expect(objectTarget.name).to.equal(name);
expect(objectTarget.exported).to.equal(exported);
}

function checkObjectFunction(
target: SubTarget,
name: string,
objectId: string,
isAsync: boolean
): void {
expect(target.type).to.equal(TargetType.OBJECT_FUNCTION);

const functionTarget = <ObjectFunctionTarget>target;

expect(functionTarget.name).to.equal(name);
expect(functionTarget.objectId).to.equal(objectId);
expect(functionTarget.isAsync).to.equal(isAsync);
}

function checkClass(target: SubTarget, name: string, exported: boolean): void {
expect(target.type).to.equal(TargetType.CLASS);

const classTarget = <ClassTarget>target;

expect(classTarget.name).to.equal(name);
expect(classTarget.exported).to.equal(exported);
}

function checkClassMethod(
target: SubTarget,
name: string,
classId: string,
methodType: string,
visibility: string,
isStatic: boolean,
isAsync: boolean
): void {
expect(target.type).to.equal(TargetType.METHOD);

const methodTarget = <MethodTarget>target;

expect(methodTarget.name).to.equal(name);
expect(methodTarget.classId).to.equal(classId);
expect(methodTarget.methodType).to.equal(methodType);
expect(methodTarget.visibility).to.equal(visibility);
expect(methodTarget.isStatic).to.equal(isStatic);
expect(methodTarget.isAsync).to.equal(isAsync);
}

describe("Prototyped TargetVisitor test", () => {
it("Test 1", () => {
const source = `
const x = {}
x.prototype.y = function () {}
x.prototype.z = function () {}

module.exports = x
`;

const targets = targetHelper(source);

expect(targets.length).to.equal(3);

checkClass(targets[0], "x", true);
checkClassMethod(
targets[1],
"y",
targets[0].id,
"method",
"public",
false,
false
);
checkClassMethod(
targets[2],
"z",
targets[0].id,
"method",
"public",
false,
false
);
});

it("Test 2", () => {
const source = `
const x = {}
const y = {}
y.f = function () {}
x.prototype = y

module.exports = x
`;

const targets = targetHelper(source);

expect(targets.length).to.equal(4);

checkClass(targets[0], "x", true);
checkObject(targets[1], "y", false);

checkObjectFunction(targets[2], "f", targets[1].id, false);
checkClassMethod(
targets[3],
"f",
targets[0].id,
"method",
"public",
false,
false
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ export class JavaScriptDecoder implements Decoder<JavaScriptTestCase, string> {
decodings = decodings.slice(0, index);
}

if (decodings.length === 0) {
throw new Error("No statements in test case after error reduction");
}

const metaCommentBlock = this.generateMetaComments(testCase);

const testLines: string[] = this.generateTestLines(
Expand Down Expand Up @@ -125,7 +121,6 @@ export class JavaScriptDecoder implements Decoder<JavaScriptTestCase, string> {

const lines = [
"// Imports",
"require = require('esm')(module)",
...imports,
gatherAssertionData ? assertionFunction : "",
`describe('SynTest Test Suite', function() {`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export class JavaScriptRunner implements EncodingRunner<JavaScriptTestCase> {
childProcess.send({
message: "run",
silent: this.silenceTestOutput,
esm: true,
paths: paths,
timeout: this.testTimeout,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export type Message = RunMessage | DoneMessage;
export type RunMessage = {
message: "run";
silent: boolean;
esm: boolean;
paths: string[];
timeout: number;
};
Expand Down Expand Up @@ -71,11 +72,16 @@ process.on("message", async (data: Message) => {
throw new TypeError("Invalid data received from child process");
}
if (data.message === "run") {
await runMocha(data.silent, data.paths, data.timeout);
await runMocha(data.silent, data.esm, data.paths, data.timeout);
}
});

async function runMocha(silent: boolean, paths: string[], timeout: number) {
async function runMocha(
silent: boolean,
esm: boolean,
paths: string[],
timeout: number
) {
const argv: Mocha.MochaOptions = <Mocha.MochaOptions>(<unknown>{
reporter: silent ? SilentMochaReporter : undefined,
// diff: false,
Expand All @@ -90,13 +96,16 @@ async function runMocha(silent: boolean, paths: string[], timeout: number) {
});

const mocha = new Mocha(argv); // require('ts-node/register')
// eslint-disable-next-line unicorn/prefer-module
require("regenerator-runtime/runtime");
// eslint-disable-next-line unicorn/prefer-module, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires
require("@babel/register")({

if (esm) {
// eslint-disable-next-line unicorn/prefer-module
presets: [require.resolve("@babel/preset-env")],
});
require("regenerator-runtime/runtime");
// eslint-disable-next-line unicorn/prefer-module, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-var-requires
require("@babel/register")({
// eslint-disable-next-line unicorn/prefer-module
presets: [require.resolve("@babel/preset-env")],
});
}

for (const _path of paths) {
// eslint-disable-next-line unicorn/prefer-module
Expand Down Expand Up @@ -126,7 +135,7 @@ async function runMocha(silent: boolean, paths: string[], timeout: number) {
return {
status: status,
error:
status === JavaScriptExecutionStatus.FAILED
test && test.err
? {
name: test.err.name,
message: test.err.message,
Expand Down
2 changes: 1 addition & 1 deletion tools/javascript/lib/JavaScriptLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,6 @@ export class JavaScriptLauncher extends Launcher {

finalEncodings = new Map<Target, JavaScriptTestCase[]>(
[...newArchives.entries()].map(([target, archive]) => {
console.log("archive size", archive.size);
return [target, archive.getEncodings()];
})
);
Expand Down Expand Up @@ -744,6 +743,7 @@ export class JavaScriptLauncher extends Launcher {
.getActionableTargets()
.filter((target) => isExported(target));

console.log(currentSubject.getActionableTargets());
if (rootTargets.length === 0) {
JavaScriptLauncher.LOGGER.info(
`No actionable exported root targets found for ${target.name} in ${target.path}`
Expand Down