forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenizer.test.ts
More file actions
74 lines (62 loc) · 1.95 KB
/
Tokenizer.test.ts
File metadata and controls
74 lines (62 loc) · 1.95 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import { Tokenizer, TokenKind, Token } from '../Tokenizer';
function escape(s: string): string {
return s.replace(/\n/g, '[n]')
.replace(/\r/g, '[r]')
.replace(/\t/g, '[t]')
.replace(/\\/g, '[b]');
}
function tokenize(input: string): Token[] {
const tokenizer: Tokenizer = new Tokenizer(input);
return tokenizer.readTokens();
}
function matchSnapshot(input: string): void {
const tokenizer: Tokenizer = new Tokenizer(input);
const reportedTokens: { kind: string, value: string }[] = tokenizer.readTokens().map(
token => {
return {
kind: TokenKind[token.kind],
value: escape(token.toString())
};
}
);
expect(
{
input: escape(tokenizer.input.toString()),
tokens: reportedTokens
}
).toMatchSnapshot();
}
test('00: empty inputs', () => {
matchSnapshot('');
matchSnapshot('\r\n');
});
test('01: white space tokens', () => {
matchSnapshot(' \t abc \r\ndef \n ghi\n\r ');
});
test('02: text with escapes', () => {
matchSnapshot(' ab+56\\>qrst(abc\\))');
expect(() => tokenize('Unterminated: \\')).toThrowError();
});
test('03: The && operator', () => {
matchSnapshot('&&abc&&cde&&');
matchSnapshot('a&b');
matchSnapshot('&&');
matchSnapshot('&');
});
test('04: dollar variables', () => {
matchSnapshot('$abc123.456');
matchSnapshot('$ab$_90');
expect(() => tokenize('$')).toThrowError();
expect(() => tokenize('${abc}')).toThrowError();
});
test('05: double-quoted strings', () => {
matchSnapshot('what "is" is');
matchSnapshot('what"is"is');
matchSnapshot('what"is\\""is');
matchSnapshot('no C-style escapes: "\\t\\r\\n"');
expect(() => tokenize('Unterminated: "')).toThrowError();
expect(() => tokenize('Unterminated: "abc')).toThrowError();
expect(() => tokenize('Unterminated: "abc\\')).toThrowError();
});