Skip to content

Commit

Permalink
test: Added some expressions for fuzzy tester
Browse files Browse the repository at this point in the history
  • Loading branch information
xpyctumo committed Jan 13, 2025
1 parent 754a927 commit 629689f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 53 deletions.
36 changes: 28 additions & 8 deletions src/test/prettyPrint/expressions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ import {
randomAstStaticCall,
randomAstStructInstance,
randomAstStructFieldInitializer,
randomAstId,
randomAstFieldAccess,
randomAstMethodCall,
randomAstBoolean,
randomAstNumber,
randomAstString,
} from "../utils/expression/randomAst";

describe("Pretty Print Expressions", () => {
Expand All @@ -20,21 +26,35 @@ describe("Pretty Print Expressions", () => {
const expression = () => randomAstExpression(maxShrinks);

const cases: [string, fc.Arbitrary<AstExpression>][] = [
[
"AstConditional",
randomAstConditional(expression(), expression(), expression()),
],
["AstOpBinary", randomAstOpBinary(expression(), expression())],
["AstOpUnary", randomAstOpUnary(expression())],
["AstNull", randomAstNull()],
["AstInitOf", randomAstInitOf(expression())],
//
// 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]) => {
Expand Down
33 changes: 0 additions & 33 deletions src/test/prettyPrint/primitives.spec.ts

This file was deleted.

54 changes: 42 additions & 12 deletions src/test/utils/expression/randomAst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import {
AstBoolean,
AstConditional,
AstExpression,
AstFieldAccess,
AstId,
AstInitOf,
AstMethodCall,
AstNull,
AstNumber,
AstOpBinary,
Expand Down Expand Up @@ -121,17 +123,7 @@ export function randomAstConditional(
);
}

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

function randomAstId(): fc.Arbitrary<AstId> {
export function randomAstId(): fc.Arbitrary<AstId> {
return dummyAstNode(
fc.record({
kind: fc.constant("id"),
Expand Down Expand Up @@ -197,6 +189,32 @@ export function randomAstStructInstance(
);
}

export function randomAstFieldAccess(
expression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstFieldAccess> {
return dummyAstNode(
fc.record({
kind: fc.constant("field_access"),
aggregate: expression,
field: randomAstId(),
}),
);
}

export function randomAstMethodCall(
selfExpression: fc.Arbitrary<AstExpression>,
argsExpression: fc.Arbitrary<AstExpression>,
): fc.Arbitrary<AstMethodCall> {
return dummyAstNode(
fc.record({
self: selfExpression,
kind: fc.constant("method_call"),
method: randomAstId(),
args: fc.array(argsExpression),
}),
);
}

export function randomAstExpression(
maxShrinks: number,
): fc.Arbitrary<AstExpression> {
Expand All @@ -207,7 +225,19 @@ export function randomAstExpression(
AstConditional: AstConditional;
}>((tie) => ({
AstExpression: fc.oneof(
randomAstNumber(), // TODO: Expand this to include more expressions, look into AstExpressionPrimary
// Enabling will fail the tests by recursion

// 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"),
Expand Down

0 comments on commit 629689f

Please sign in to comment.