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

Draft: pretty-printer tests with randomly generated AST Expressions #1248

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ src/func/funcfiftlib.js
**/grammar.ohm*.js
src/grammar/next/grammar.ts
jest.setup.js
jest.globalSetup.js
jest.teardown.js
/docs
3 changes: 2 additions & 1 deletion cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@
"Ints",
"workchain",
"xffff",
"привет"
"привет",
"letrec"
],
"ignoreRegExpList": [
"\\b[xB]\\{[a-fA-F0-9]*_?\\}", // binary literals in Fift-asm
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module.exports = {
testEnvironment: "node",
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
maxWorkers: "50%",
globalSetup: "./jest.setup.js",
globalSetup: "./jest.globalSetup.js",
setupFiles: ["./jest.setup.js"],
globalTeardown: "./jest.teardown.js",
snapshotSerializers: ["@tact-lang/ton-jest/serializers"],
};
8 changes: 8 additions & 0 deletions jest.globalSetup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const coverage = require("@tact-lang/coverage");

module.exports = async () => {
if (process.env.COVERAGE === "true") {
coverage.beginCoverage();
}
};
55 changes: 49 additions & 6 deletions jest.setup.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,51 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const coverage = require("@tact-lang/coverage");
const fc = require("fast-check");

module.exports = async () => {
if (process.env.COVERAGE === "true") {
coverage.beginCoverage();
function sanitizeObject(
obj,
options = {
excludeKeys: [],
valueTransformers: {},
},
) {
const { excludeKeys, valueTransformers } = options;

if (Array.isArray(obj)) {
return obj.map((item) => sanitizeObject(item, options));
} else if (obj !== null && typeof obj === "object") {
const newObj = {};
for (const [key, value] of Object.entries(obj)) {
if (!excludeKeys.includes(key)) {
const transformer = valueTransformers[key];
newObj[key] = transformer
? transformer(value)
: sanitizeObject(value, options);
}
}
return newObj;
}
};
return obj;
}

fc.configureGlobal({
reporter: (log) => {
if (log.failed) {
const sanitizedCounterexample = sanitizeObject(log.counterexample, {
excludeKeys: ["id", "loc"],
valueTransformers: {
value: (val) =>
typeof val === "bigint" ? val.toString() : val,
},
});

const errorMessage = `
Property failed after ${log.numRuns} tests
Seed: ${log.seed}
Path: ${log.counterexamplePath}
Counterexample: ${JSON.stringify(sanitizedCounterexample, null, 0)}
Errors: ${log.error ? log.error : "Unknown error"}
`;

throw new Error(errorMessage);
}
},
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"cross-env": "^7.0.3",
"cspell": "^8.8.3",
"eslint": "^8.56.0",
"fast-check": "^3.23.2",
"glob": "^8.1.0",
"husky": "^9.1.5",
"jest": "^29.3.1",
Expand Down
72 changes: 72 additions & 0 deletions src/test/prettyPrint/expressions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import fc from "fast-check";
import { AstExpression, eqExpressions, getAstFactory } from "../../ast/ast";
import { prettyPrint } from "../../prettyPrinter";
import { getParser } from "../../grammar";
import {
randomAstConditional,
randomAstOpBinary,
randomAstOpUnary,
randomAstExpression,
randomAstInitOf,
randomAstNull,
randomAstStaticCall,
randomAstStructInstance,
randomAstStructFieldInitializer,
randomAstId,
randomAstFieldAccess,
randomAstMethodCall,
randomAstBoolean,
randomAstNumber,
randomAstString,
} from "../utils/expression/randomAst";

describe("Pretty Print Expressions", () => {
const maxDepth = 3;
const expression = () => randomAstExpression(maxDepth);

const cases: [string, fc.Arbitrary<AstExpression>][] = [
//
// Primary expressions
//
["AstMethodCall", randomAstMethodCall(expression(), expression())],
["AstFieldAccess", randomAstFieldAccess(expression())],
["AstStaticCall", randomAstStaticCall(expression())],
[
"AstStructInstance",
randomAstStructInstance(
randomAstStructFieldInitializer(expression()),
),
],
["AstId", randomAstId()],
["AstNull", randomAstNull()],
["AstInitOf", randomAstInitOf(expression())],
["AstString", randomAstString()],

//
// Literals
//
["AstNumber", randomAstNumber()],
["AstBoolean", randomAstBoolean()],

[
"AstConditional",
randomAstConditional(expression(), expression(), expression()),
],
["AstOpBinary", randomAstOpBinary(expression(), expression())],
["AstOpUnary", randomAstOpUnary(expression())],
];

cases.forEach(([caseName, astGenerator]) => {
it(`should parse ${caseName}`, () => {
fc.assert(
fc.property(astGenerator, (generatedAst) => {
const prettyBefore = prettyPrint(generatedAst);
const parser = getParser(getAstFactory(), "new");
const parsedAst = parser.parseExpression(prettyBefore);
expect(eqExpressions(generatedAst, parsedAst)).toBe(true);
}),
{ seed: 1 },
);
});
});
});
Loading
Loading