Skip to content

Commit

Permalink
test: Fix 'maxDepth' in Ast expression generator
Browse files Browse the repository at this point in the history
  • Loading branch information
xpyctumo committed Jan 15, 2025
1 parent abaff6c commit 0d532ab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
10 changes: 4 additions & 6 deletions src/test/prettyPrint/expressions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fc from "fast-check";
import { AstExpression, eqExpressions, getAstFactory } from "../../grammar/ast";
import { AstExpression, eqExpressions, getAstFactory } from "../../ast/ast";
import { prettyPrint } from "../../prettyPrinter";
import { getParser } from "../../grammar";
import {
Expand All @@ -21,9 +21,8 @@ import {
} from "../utils/expression/randomAst";

describe("Pretty Print Expressions", () => {
// Max depth of the expression tree
const maxShrinks = 15;
const expression = () => randomAstExpression(maxShrinks);
const maxDepth = 3;
const expression = () => randomAstExpression(maxDepth);

const cases: [string, fc.Arbitrary<AstExpression>][] = [
//
Expand Down Expand Up @@ -62,8 +61,7 @@ describe("Pretty Print Expressions", () => {
fc.assert(
fc.property(astGenerator, (generatedAst) => {
const prettyBefore = prettyPrint(generatedAst);
const astFactory = getAstFactory();
const parser = getParser(astFactory, "new");
const parser = getParser(getAstFactory(), "new");
const parsedAst = parser.parseExpression(prettyBefore);
expect(eqExpressions(generatedAst, parsedAst)).toBe(true);
}),
Expand Down
86 changes: 42 additions & 44 deletions src/test/utils/expression/randomAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
AstString,
AstStructFieldInitializer,
AstStructInstance,
} from "../../../grammar/ast";
} from "../../../ast/ast";
import { dummySrcInfo } from "../../../grammar/src-info";

function dummyAstNode<T>(
Expand Down Expand Up @@ -133,6 +133,16 @@ export function randomAstId(): fc.Arbitrary<AstId> {
);
}

function randomAstCapitalizedId(): fc.Arbitrary<AstId> {
return dummyAstNode(
fc.record({
kind: fc.constant("id"),
text: fc.stringMatching(/^[A-Z]+$/),
// Rules for text value are in src/grammar/grammar.ohm
}),
);
}

export function randomAstNull(): fc.Arbitrary<AstNull> {
return dummyAstNode(
fc.record({
Expand All @@ -148,7 +158,7 @@ export function randomAstInitOf(
fc.record({
kind: fc.constant("init_of"),
contract: randomAstId(),
args: fc.array(expression),
args: fc.array(expression, { maxLength: 1 }),
}),
);
}
Expand Down Expand Up @@ -183,7 +193,7 @@ export function randomAstStructInstance(
return dummyAstNode(
fc.record({
kind: fc.constant("struct_instance"),
type: randomAstId(),
type: randomAstCapitalizedId(),
args: fc.array(structFieldInitializer),
}),
);
Expand Down Expand Up @@ -216,47 +226,35 @@ export function randomAstMethodCall(
}

export function randomAstExpression(
maxShrinks: number,
maxDepth: number,
): fc.Arbitrary<AstExpression> {
return fc.letrec<{
AstExpression: AstExpression;
AstOpUnary: AstOpUnary;
AstOpBinary: AstOpBinary;
AstConditional: AstConditional;
}>((tie) => ({
AstExpression: fc.oneof(
// Enabling will fail the tests by recursion
// No weighted items
const baseExpressions = [
randomAstNumber(),
randomAstBoolean(),
randomAstId(),
randomAstNull(),
randomAstString(),
];

// randomAstMethodCall(
// fc.limitShrink(tie("AstExpression"), 1),
// fc.limitShrink(tie("AstExpression"), 1)),
// randomAstFieldAccess(fc.limitShrink(tie("AstExpression"), 1)),
// randomAstStaticCall(fc.limitShrink(tie("AstExpression"), 1)),
randomAstNumber(),
randomAstBoolean(),
randomAstId(),
randomAstNull(),
// randomAstInitOf(tie("AstExpression")),
randomAstString(),
tie("AstOpUnary"),
tie("AstOpBinary"),
tie("AstConditional"),
),
AstOpUnary: fc.limitShrink(
randomAstOpUnary(tie("AstExpression")),
maxShrinks,
),
AstOpBinary: fc.limitShrink(
randomAstOpBinary(tie("AstExpression"), tie("AstExpression")),
maxShrinks,
),
AstConditional: fc.limitShrink(
randomAstConditional(
tie("AstExpression"),
tie("AstExpression"),
tie("AstExpression"),
),
maxShrinks,
),
})).AstExpression;
// More weighted items
return fc.memo((depth: number): fc.Arbitrary<AstExpression> => {
if (depth == 1) {
return fc.oneof(...baseExpressions);
}

const subExpr = () => randomAstExpression(depth - 1);

return fc.oneof(
...baseExpressions,
randomAstMethodCall(subExpr(), subExpr()),
randomAstFieldAccess(subExpr()),
randomAstStaticCall(subExpr()),
randomAstStructInstance(randomAstStructFieldInitializer(subExpr())),
randomAstInitOf(subExpr()),
randomAstOpUnary(subExpr()),
randomAstOpBinary(subExpr(), subExpr()),
randomAstConditional(subExpr(), subExpr(), subExpr()),
);
})(maxDepth);
}

0 comments on commit 0d532ab

Please sign in to comment.