Skip to content

Commit 352ba29

Browse files
committed
Add unit tests
1 parent b37f452 commit 352ba29

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { Tokenizer } from '../Tokenizer';
5+
import { Parser } from '../Parser';
6+
import { AstScript } from '../AstNode';
7+
8+
function escape(s: string): string {
9+
return s.replace(/\n/g, '[n]')
10+
.replace(/\r/g, '[r]')
11+
.replace(/\t/g, '[t]')
12+
.replace(/\\/g, '[b]');
13+
}
14+
15+
function matchSnapshot(input: string): void {
16+
const tokenizer: Tokenizer = new Tokenizer(input);
17+
const parser: Parser = new Parser(tokenizer);
18+
const result: AstScript = parser.parse();
19+
expect({
20+
input: escape(tokenizer.input.toString()),
21+
tree: result.getDump()
22+
}).toMatchSnapshot();
23+
}
24+
25+
function matchErrorSnapshot(input: string): void {
26+
const tokenizer: Tokenizer = new Tokenizer(input);
27+
const parser: Parser = new Parser(tokenizer);
28+
let error: Error | undefined = undefined;
29+
try {
30+
const result: AstScript = parser.parse();
31+
} catch (e) {
32+
error = e;
33+
}
34+
expect({
35+
input: escape(tokenizer.input.toString()),
36+
reportedError: error
37+
}).toMatchSnapshot();
38+
}
39+
40+
test('00: basic inputs', () => {
41+
matchSnapshot('command arg1 arg2');
42+
});
43+
44+
test('01: basic errors', () => {
45+
matchErrorSnapshot('@bad');
46+
matchErrorSnapshot('command @bad');
47+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`00: basic inputs 1`] = `
4+
Object {
5+
"input": "command arg1 arg2",
6+
"tree": "Script:
7+
Command:
8+
CompoundWord:
9+
Text=\\"command\\"
10+
CompoundWord:
11+
Text=\\"arg1\\"
12+
CompoundWord:
13+
Text=\\"arg2\\"
14+
",
15+
}
16+
`;
17+
18+
exports[`01: basic errors 1`] = `
19+
Object {
20+
"input": "@bad",
21+
"reportedError": [Error: (1,1): Expecting a command path],
22+
}
23+
`;
24+
25+
exports[`01: basic errors 2`] = `
26+
Object {
27+
"input": "command @bad",
28+
"reportedError": [Error: (1,9): Unexpected token: Other],
29+
}
30+
`;

0 commit comments

Comments
 (0)