-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathoutline-lexer-test.js
More file actions
117 lines (99 loc) · 2.29 KB
/
outline-lexer-test.js
File metadata and controls
117 lines (99 loc) · 2.29 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import Scanner from './scanner.js';
import OutlineLexer from './outline-lexer.js';
import equals from 'pop-equals';
function test(input, output) {
const text = input.map(enline).join('');
const lister = new OutlineLexLister();
const lexer = new OutlineLexer(lister);
const scanner = new Scanner(lexer);
scanner.next(text);
scanner.return();
if (!equals(lister.list, output)) {
console.error('ERROR');
process.stderr.write(text);
console.error('expected', output);
console.error('actual ', lister.list);
process.exitCode |= 1;
}
}
class OutlineLexLister {
debug = process.env.DEBUG_OUTLINE_LEXER;
constructor() {
this.list = [];
}
next(type, text, scanner) {
if (this.debug) {
console.log('OLL', scanner.position(), type, JSON.stringify(text));
}
if (type !== 'text') {
this.list.push(type.toUpperCase());
}
if (text) {
this.list.push(text);
}
return this;
}
}
function enline(line) {
return `${line}\n`;
}
test(['a', 'b'], ['a', 'b', 'STOP']);
test(['a', ' b'], ['a', 'b', 'STOP']);
test(['a', ' b', ' c'], ['a', 'b', 'c', 'STOP']);
test(['a', ' b', ' c'], ['a', 'b', 'c', 'STOP']);
test(['a', ' b', ' ', ' c'], ['a', 'b', 'BREAK', 'c', 'STOP']);
test(['a', ' b', ' \tc'], ['a', 'b', 'c', 'STOP']);
test(['a', '- b', '- c'], ['a', 'START', '-', 'b', 'STOP', 'START', '-', 'c', 'STOP', 'STOP']);
test(
['a', '- b', 'c', '- d'],
['a', 'START', '-', 'b', 'STOP', 'c', 'START', '-', 'd', 'STOP', 'STOP']
);
test(['a', '- b', ' c'], ['a', 'START', '-', 'b', 'c', 'STOP', 'STOP']);
test(
['a', '- b', ' - * c', ' d'],
['a', 'START', '-', 'b', 'START', '-*', 'c', 'd', 'STOP', 'STOP', 'STOP']
);
test(['a', ' b', ' c', ' d', 'e'], ['a', 'b', 'c', 'd', 'e', 'STOP']);
test(
['Alpha', '+ Bravo', '', ' Charlie', '+ Delta', '', ' Echo', 'Foxtrot'],
[
'Alpha',
'START',
'+',
'Bravo',
'BREAK',
'Charlie',
'STOP',
'START',
'+',
'Delta',
'BREAK',
'Echo',
'STOP',
'Foxtrot',
'STOP',
]
);
test(
['+ A', ' + B', ' >', '>', 'B'],
[
'START',
'+',
'A',
'START',
'+',
'B',
'STOP',
'START',
'>',
'BREAK',
'STOP',
'STOP',
'START',
'>',
'BREAK',
'STOP',
'B',
'STOP',
]
);