|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +import "mocha"; |
| 5 | +import { expect } from "chai"; |
| 6 | + |
| 7 | +import { Ast, AstUtils } from "../../../powerquery-parser/language"; |
| 8 | +import { AssertTestUtils } from "../../testUtils"; |
| 9 | +import { DefaultSettings } from "../../../powerquery-parser"; |
| 10 | +import { ParseOk } from "../../../powerquery-parser/parser"; |
| 11 | + |
| 12 | +describe(`AstUtils`, () => { |
| 13 | + describe(`getIdentifierLiteral`, () => { |
| 14 | + async function runTest(params: { |
| 15 | + readonly text: string; |
| 16 | + readonly expectedIdentifierLiteral: string; |
| 17 | + readonly expectedIdentifierExpressionLiteral: string; |
| 18 | + }): Promise<void> { |
| 19 | + const parseOk: ParseOk = await AssertTestUtils.assertGetParseOk(DefaultSettings, params.text); |
| 20 | + |
| 21 | + // First, ensure the root node is an IdentifierExpression. |
| 22 | + const rootAsIdentifierExpression: Ast.IdentifierExpression = |
| 23 | + AstUtils.assertAsNodeKind<Ast.IdentifierExpression>(parseOk.root, Ast.NodeKind.IdentifierExpression); |
| 24 | + |
| 25 | + // Second, grab our actual literals. |
| 26 | + const actualIdentifierExpressionLiteral: string = AstUtils.getIdentifierLiteral(rootAsIdentifierExpression); |
| 27 | + |
| 28 | + const actualIdentifierLiteral: string = AstUtils.getIdentifierLiteral( |
| 29 | + rootAsIdentifierExpression.identifier, |
| 30 | + ); |
| 31 | + |
| 32 | + expect( |
| 33 | + actualIdentifierExpressionLiteral, |
| 34 | + `expected identifier expression literal to be ${params.expectedIdentifierExpressionLiteral}`, |
| 35 | + ).to.equal(params.expectedIdentifierExpressionLiteral); |
| 36 | + |
| 37 | + // Finally, assert that our actual literals match the expected literals. |
| 38 | + expect( |
| 39 | + actualIdentifierLiteral, |
| 40 | + `expected identifier literal to be ${params.expectedIdentifierLiteral}`, |
| 41 | + ).to.equal(params.expectedIdentifierLiteral); |
| 42 | + } |
| 43 | + |
| 44 | + it(`foo`, async () => { |
| 45 | + await runTest({ |
| 46 | + text: "foo", |
| 47 | + expectedIdentifierLiteral: "foo", |
| 48 | + expectedIdentifierExpressionLiteral: "foo", |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it(`@foo`, async () => { |
| 53 | + await runTest({ |
| 54 | + text: "@foo", |
| 55 | + expectedIdentifierExpressionLiteral: "@foo", |
| 56 | + expectedIdentifierLiteral: "foo", |
| 57 | + }); |
| 58 | + }); |
| 59 | + }); |
| 60 | +}); |
0 commit comments