Skip to content

Commit

Permalink
test: Simplify pretty print expressions test by reducing cases and in…
Browse files Browse the repository at this point in the history
…creasing runs
  • Loading branch information
xpyctumo committed Jan 20, 2025
1 parent db495e8 commit 0402d0e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 77 deletions.
75 changes: 12 additions & 63 deletions src/test/prettyPrint/expressions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,72 +1,21 @@
import fc from "fast-check";
import { AstExpression, eqExpressions, getAstFactory } from "../../ast/ast";
import { 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";
import { randomAstExpression } 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 },
);
});
it(`should parse Ast`, () => {
fc.assert(
fc.property(randomAstExpression(maxDepth), (generatedAst) => {
const prettyBefore = prettyPrint(generatedAst);
const parser = getParser(getAstFactory(), "new");
const parsedAst = parser.parseExpression(prettyBefore);
expect(eqExpressions(generatedAst, parsedAst)).toBe(true);
}),
{ seed: 1, numRuns: 5000 },
);
});
});
28 changes: 14 additions & 14 deletions src/test/utils/expression/randomAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function dummyAstNode<T>(
}));
}

export function randomAstBoolean(): fc.Arbitrary<AstBoolean> {
function randomAstBoolean(): fc.Arbitrary<AstBoolean> {
return dummyAstNode(
fc.record({
kind: fc.constant("boolean"),
Expand All @@ -37,7 +37,7 @@ export function randomAstBoolean(): fc.Arbitrary<AstBoolean> {
);
}

export function randomAstString(): fc.Arbitrary<AstString> {
function randomAstString(): fc.Arbitrary<AstString> {
const escapeString = (s: string): string =>
s.replace(/\\/g, "\\\\").replace(/"/g, '\\"');

Expand All @@ -49,7 +49,7 @@ export function randomAstString(): fc.Arbitrary<AstString> {
);
}

export function randomAstNumber(): fc.Arbitrary<AstNumber> {
function randomAstNumber(): fc.Arbitrary<AstNumber> {
const values = [
...Array.from({ length: 10 }, (_, i) => [0n, BigInt(i)]).flat(),
...Array.from({ length: 256 }, (_, i) => 1n ** BigInt(i)),
Expand All @@ -64,7 +64,7 @@ export function randomAstNumber(): fc.Arbitrary<AstNumber> {
);
}

export function randomAstOpUnary(
function randomAstOpUnary(
operand: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstOpUnary> {
return dummyAstNode(
Expand All @@ -75,7 +75,7 @@ export function randomAstOpUnary(
}),
);
}
export function randomAstOpBinary(
function randomAstOpBinary(
leftExpression: fc.Arbitrary<AstExpression>,
rightExpression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstOpBinary> {
Expand Down Expand Up @@ -108,7 +108,7 @@ export function randomAstOpBinary(
);
}

export function randomAstConditional(
function randomAstConditional(
conditionExpression: fc.Arbitrary<AstExpression>,
thenBranchExpression: fc.Arbitrary<AstExpression>,
elseBranchExpression: fc.Arbitrary<AstExpression>,
Expand All @@ -123,7 +123,7 @@ export function randomAstConditional(
);
}

export function randomAstId(): fc.Arbitrary<AstId> {
function randomAstId(): fc.Arbitrary<AstId> {
return dummyAstNode(
fc.record({
kind: fc.constant("id"),
Expand All @@ -143,15 +143,15 @@ function randomAstCapitalizedId(): fc.Arbitrary<AstId> {
);
}

export function randomAstNull(): fc.Arbitrary<AstNull> {
function randomAstNull(): fc.Arbitrary<AstNull> {
return dummyAstNode(
fc.record({
kind: fc.constant("null"),
}),
);
}

export function randomAstInitOf(
function randomAstInitOf(
expression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstInitOf> {
return dummyAstNode(
Expand All @@ -163,7 +163,7 @@ export function randomAstInitOf(
);
}

export function randomAstStaticCall(
function randomAstStaticCall(
expression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstStaticCall> {
return dummyAstNode(
Expand All @@ -175,7 +175,7 @@ export function randomAstStaticCall(
);
}

export function randomAstStructFieldInitializer(
function randomAstStructFieldInitializer(
expression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstStructFieldInitializer> {
return dummyAstNode(
Expand All @@ -187,7 +187,7 @@ export function randomAstStructFieldInitializer(
);
}

export function randomAstStructInstance(
function randomAstStructInstance(
structFieldInitializer: fc.Arbitrary<AstStructFieldInitializer>,
): fc.Arbitrary<AstStructInstance> {
return dummyAstNode(
Expand All @@ -199,7 +199,7 @@ export function randomAstStructInstance(
);
}

export function randomAstFieldAccess(
function randomAstFieldAccess(
expression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstFieldAccess> {
return dummyAstNode(
Expand All @@ -211,7 +211,7 @@ export function randomAstFieldAccess(
);
}

export function randomAstMethodCall(
function randomAstMethodCall(
selfExpression: fc.Arbitrary<AstExpression>,
argsExpression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstMethodCall> {
Expand Down

0 comments on commit 0402d0e

Please sign in to comment.