forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.test.ts
More file actions
47 lines (41 loc) · 1.26 KB
/
Parser.test.ts
File metadata and controls
47 lines (41 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Tokenizer } from '../Tokenizer';
import { Parser } from '../Parser';
import { AstScript } from '../AstNode';
function escape(s: string): string {
return s.replace(/\n/g, '[n]')
.replace(/\r/g, '[r]')
.replace(/\t/g, '[t]')
.replace(/\\/g, '[b]');
}
function matchSnapshot(input: string): void {
const tokenizer: Tokenizer = new Tokenizer(input);
const parser: Parser = new Parser(tokenizer);
const result: AstScript = parser.parse();
expect({
input: escape(tokenizer.input.toString()),
tree: '\n' + result.getDump()
}).toMatchSnapshot();
}
function matchErrorSnapshot(input: string): void {
const tokenizer: Tokenizer = new Tokenizer(input);
const parser: Parser = new Parser(tokenizer);
let error: Error | undefined = undefined;
try {
parser.parse();
} catch (e) {
error = e;
}
expect({
input: escape(tokenizer.input.toString()),
reportedError: error
}).toMatchSnapshot();
}
test('00: basic inputs', () => {
matchSnapshot('command arg1 arg2');
});
test('01: basic errors', () => {
matchErrorSnapshot('@bad');
matchErrorSnapshot('command @bad');
});