diff --git a/src/test/prettyPrint/expressions.spec.ts b/src/test/prettyPrint/expressions.spec.ts index 8663ab68b..c13ad27f2 100644 --- a/src/test/prettyPrint/expressions.spec.ts +++ b/src/test/prettyPrint/expressions.spec.ts @@ -12,6 +12,12 @@ import { randomAstStaticCall, randomAstStructInstance, randomAstStructFieldInitializer, + randomAstId, + randomAstFieldAccess, + randomAstMethodCall, + randomAstBoolean, + randomAstNumber, + randomAstString, } from "../utils/expression/randomAst"; describe("Pretty Print Expressions", () => { @@ -20,14 +26,11 @@ describe("Pretty Print Expressions", () => { const expression = () => randomAstExpression(maxShrinks); const cases: [string, fc.Arbitrary][] = [ - [ - "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", @@ -35,6 +38,23 @@ describe("Pretty Print Expressions", () => { 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]) => { diff --git a/src/test/prettyPrint/primitives.spec.ts b/src/test/prettyPrint/primitives.spec.ts deleted file mode 100644 index 843eabd5c..000000000 --- a/src/test/prettyPrint/primitives.spec.ts +++ /dev/null @@ -1,33 +0,0 @@ -import fc from "fast-check"; -import { - randomAstBoolean, - randomAstNumber, - randomAstString, -} from "../utils/expression/randomAst"; -import { getParser } from "../../grammar"; -import { getAstFactory, eqExpressions, AstExpression } from "../../grammar/ast"; -import { prettyPrint } from "../../prettyPrinter"; - -describe("Pretty Print Primitives", () => { - const astFactory = getAstFactory(); - const parser = getParser(astFactory, "new"); - - const cases: [string, fc.Arbitrary][] = [ - ["AstBoolean", randomAstBoolean()], - ["AstNumber", randomAstNumber()], - ["AstString", randomAstString()], - ]; - - cases.forEach(([caseName, astGenerator]) => { - it(`should parse ${caseName}`, () => { - fc.assert( - fc.property(astGenerator, (generatedAst) => { - const prettyBefore = prettyPrint(generatedAst); - const parsedAst = parser.parseExpression(prettyBefore); - expect(eqExpressions(generatedAst, parsedAst)).toBe(true); - }), - { seed: 1 }, - ); - }); - }); -}); diff --git a/src/test/utils/expression/randomAst.ts b/src/test/utils/expression/randomAst.ts index 3af1c9e53..438ec8d09 100644 --- a/src/test/utils/expression/randomAst.ts +++ b/src/test/utils/expression/randomAst.ts @@ -3,8 +3,10 @@ import { AstBoolean, AstConditional, AstExpression, + AstFieldAccess, AstId, AstInitOf, + AstMethodCall, AstNull, AstNumber, AstOpBinary, @@ -121,17 +123,7 @@ export function randomAstConditional( ); } -function randomAstTypeId(): fc.Arbitrary { - 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 { +export function randomAstId(): fc.Arbitrary { return dummyAstNode( fc.record({ kind: fc.constant("id"), @@ -197,6 +189,32 @@ export function randomAstStructInstance( ); } +export function randomAstFieldAccess( + expression: fc.Arbitrary, +): fc.Arbitrary { + return dummyAstNode( + fc.record({ + kind: fc.constant("field_access"), + aggregate: expression, + field: randomAstId(), + }), + ); +} + +export function randomAstMethodCall( + selfExpression: fc.Arbitrary, + argsExpression: fc.Arbitrary, +): fc.Arbitrary { + return dummyAstNode( + fc.record({ + self: selfExpression, + kind: fc.constant("method_call"), + method: randomAstId(), + args: fc.array(argsExpression), + }), + ); +} + export function randomAstExpression( maxShrinks: number, ): fc.Arbitrary { @@ -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"),