Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
Fix other tests
  • Loading branch information
elsassph committed Jun 7, 2021
1 parent 999069a commit bb258e4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/lexer/Token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ export interface Token {
/** The text found in the original BrightScript source, if any. */
text: string;
/** True if this token's `text` is a reserved word, otherwise `false`. */
isReserved: boolean;
isReserved?: boolean;
/** Where the token was found. */
range: Range;
/**
* Any leading whitespace found prior to this token. Excludes newline characters.
*/
leadingWhitespace: string;
leadingWhitespace?: string;
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,6 @@ export class Parser {
/**
* Static wrapper around creating a new parser and parsing a list of tokens
*/
public static parse(source: string, options?: ParseOptions): Parser;
public static parse(tokens: Token[], options?: ParseOptions): Parser;
public static parse(toParse: Token[] | string, options?: ParseOptions): Parser {
let tokens: Token[];
if (typeof toParse === 'string') {
Expand Down
21 changes: 13 additions & 8 deletions src/parser/tests/Parser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import type { Token } from '../../lexer';
import { TokenKind, ReservedWords } from '../../lexer';
import { interpolatedRange } from '../../astUtils/creators';
import type { Range } from '../../astUtils';

/* A set of utilities to be used while writing tests for the BRS parser. */

/**
* Creates a token with the given `kind` and (optional) `literal` value.
* @param {TokenKind} kind the tokenKind the produced token should represent.
* @param {string} text the text represented by this token.
* @param {*} [literal] the literal value that the produced token should contain, if any
* @returns {object} a token of `kind` representing `text` with value `literal`.
*/
export function token(kind: TokenKind, text?: string): Token {
return {
Expand All @@ -23,11 +20,19 @@ export function token(kind: TokenKind, text?: string): Token {

/**
* Creates an Identifier token with the given `text`.
* @param {string} text
* @returns {object} a token with the provided `text`.
*/
export function identifier(text) {
return exports.token(TokenKind.Identifier, text);
export function identifier(text: string) {
return token(TokenKind.Identifier, text);
}

/**
* Test whether a range matches a group of elements with a `range`
*/
export function rangeMatch(range: Range, elements: ({ range: Range })[]): boolean {
return range.start.line === elements[0].range.start.line &&
range.start.character === elements[0].range.start.character &&
range.end.line === elements[elements.length - 1].range.end.line &&
range.end.character === elements[elements.length - 1].range.end.character;
}

/** An end-of-file token. */
Expand Down
4 changes: 2 additions & 2 deletions src/parser/tests/controlFlow/For.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ describe('parser for loops', () => {
text: 'to',
isReserved: false,
range: {
start: { line: 0, column: 10 },
end: { start: 0, column: 12 }
start: { line: 0, character: 10 },
end: { line: 0, character: 12 }
}
},
{
Expand Down
23 changes: 22 additions & 1 deletion src/parser/tests/controlFlow/If.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import * as assert from 'assert';

import { Parser } from '../../Parser';
import { TokenKind, Lexer } from '../../../lexer';
import { EOF, identifier, token } from '../Parser.spec';
import { EOF, identifier, rangeMatch, token } from '../Parser.spec';
import { isBlock, isCommentStatement, isIfStatement } from '../../../astUtils';
import type { Block, IfStatement } from '../../Statement';

describe('parser if statements', () => {
it('allows empty if blocks', () => {
Expand Down Expand Up @@ -602,4 +603,24 @@ describe('parser if statements', () => {
expect(diagnostics).to.be.lengthOf(0);
expect(statements).to.be.length.greaterThan(0);
});

it('single-line if block statements have correct range', () => {
let { tokens } = Lexer.scan(`
if false then print "true"
if false then print "true": a = 10
if false then print "true" else print "false"
if false then print "true" else print "false": a = 20
`);
let { statements, diagnostics } = Parser.parse(tokens);
expect(diagnostics).to.be.lengthOf(0);

const then1 = (statements[0] as IfStatement).thenBranch;
expect(rangeMatch(then1.range, then1.statements)).to.be.true;
const then2 = (statements[1] as IfStatement).thenBranch;
expect(rangeMatch(then2.range, then2.statements)).to.be.true;
const else1 = (statements[2] as IfStatement).elseBranch as Block;
expect(rangeMatch(else1.range, else1.statements)).to.be.true;
const else2 = (statements[3] as IfStatement).elseBranch as Block;
expect(rangeMatch(else2.range, else2.statements)).to.be.true;
});
});
8 changes: 4 additions & 4 deletions src/parser/tests/expression/Call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('parser call expressions', () => {
it('parses named function calls', () => {
const { statements, diagnostics } = Parser.parse([
identifier('RebootSystem'),
{ kind: TokenKind.LeftParen, text: '(', line: 1 },
{ kind: TokenKind.LeftParen, text: '(', range: null },
token(TokenKind.RightParen, ')'),
EOF
]);
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('parser call expressions', () => {
it('allows closing parentheses on separate line', () => {
const { statements, diagnostics } = Parser.parse([
identifier('RebootSystem'),
{ kind: TokenKind.LeftParen, text: '(', line: 1 },
{ kind: TokenKind.LeftParen, text: '(', range: null },
token(TokenKind.Newline, '\\n'),
token(TokenKind.Newline, '\\n'),
token(TokenKind.RightParen, ')'),
Expand All @@ -71,9 +71,9 @@ describe('parser call expressions', () => {
it('accepts arguments', () => {
const { statements, diagnostics } = Parser.parse([
identifier('add'),
{ kind: TokenKind.LeftParen, text: '(', line: 1 },
{ kind: TokenKind.LeftParen, text: '(', range: null },
token(TokenKind.IntegerLiteral, '1'),
{ kind: TokenKind.Comma, text: ',', line: 1 },
{ kind: TokenKind.Comma, text: ',', range: null },
token(TokenKind.IntegerLiteral, '2'),
token(TokenKind.RightParen, ')'),
EOF
Expand Down
2 changes: 1 addition & 1 deletion src/parser/tests/statement/ReturnStatement.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('parser return statements', () => {
token(TokenKind.Newline, '\\n'),
token(TokenKind.Return, 'return'),
identifier('RebootSystem'),
{ kind: TokenKind.LeftParen, text: '(', line: 2 },
{ kind: TokenKind.LeftParen, text: '(', range: null },
token(TokenKind.RightParen, ')'),
token(TokenKind.Newline, '\\n'),
token(TokenKind.EndFunction, 'end function'),
Expand Down

0 comments on commit bb258e4

Please sign in to comment.