-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-builder-test.js
More file actions
192 lines (169 loc) · 5.97 KB
/
parser-builder-test.js
File metadata and controls
192 lines (169 loc) · 5.97 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
console.log("parser-builder-test.js");
(function() {
var testsPassed = 0;
var testsFailed = 0;
function testEqual(title, executor, expectedValue) {
if (!title || !title.length) {
title = "Untitled Test";
}
var result;
try { result = executor(); } catch(e) { result = e; }
if (result !== expectedValue) {
console.warn("Failed Test:", title, "Expected: ", expectedValue, "but got:", result);
testsFailed++;
} else {
//console.log("Passed:", title);
testsPassed++;
}
};
function testThrows(title, executor) {
if (!title || !title.length) {
title = "Untitled Test";
}
var result = null;
try { executor(); } catch(e) { result = e; }
if (result === null) {
console.warn("Failed Test:", title, "Expected a thrown error, but executed without error.");
testsFailed++;
} else {
//console.log("Passed:", title);
testsPassed++;
}
};
// chr
testEqual(
"chr doesn't parse wrong char", function() {
return chr("b").parse("a"); }, null);
testEqual(
"chr parses correct input", function() {
return chr("a").parse("a"); }, "a");
testEqual(
"chr parses only what it's asked", function() {
return chr("a").parse("abc"); }, "a");
// digit
testEqual(
"digit doesn't parse wrong char", function() {
return digit.parse("a"); }, null);
testEqual(
"digit parses correct input", function() {
return digit.parse("5"); }, "5");
testEqual(
"digit parses only what it's asked", function() {
return digit.parse("5273"); }, "5");
// seq
testEqual(
"seq works with chr", function() {
return seq(chr("a"), chr("b")).parse("ab"); }, "ab");
testEqual(
"seq parses only what it's asked with chr", function() {
return seq(chr("a"), chr("b")).parse("abc"); }, "ab");
testEqual(
"seq parses only what it's asked with digit", function() {
return seq(digit, digit, digit).parse("6460"); }, "646");
testEqual(
"seq returns nothing when one of its elements fails", function() {
return seq(digit, digit, digit).parse("6a60"); }, null);
// or
testEqual(
"or works 1", function() {
return or(chr("a"), chr("b")).parse("a"); }, "a");
testEqual(
"or works 2", function() {
return or(chr("a"), chr("b")).parse("b"); }, "b");
testEqual(
"or fails correctly", function() {
return or(chr("a"), chr("b")).parse("c"); }, null);
testEqual(
"or parses only what it's asked", function() {
return or(chr("a"), chr("b")).parse("abc"); }, "a");
// word
testEqual(
"word only parses correct input", function() {
return word("abc").parse("abc"); }, "abc");
testEqual(
"word only parses what it's asked", function() {
return word("abc").parse("abcd"); }, "abc");
testEqual(
"word fails correctly", function() {
return word("abc").parse("abdc"); }, null);
testEqual(
"word can be part of another parser", function() {
return seq( word("abc"), _, word("def") ).parse("abc def"); }, "abc def");
// wsChar
testEqual("wsChar fails correctly", function() {
return wsChar.parse("ABC") }, null);
testEqual("wsChar parses single space", function() {
return wsChar.parse(" ") }, " ");
testEqual("wsChar parses single tab", function() {
return wsChar.parse("\t") }, "\t");
testEqual("wsChar parses single LF", function() {
return wsChar.parse("\n") }, "\n");
testEqual("wsChar parses single CR", function() {
return wsChar.parse("\r") }, "\r");
// many
testEqual("many fails correctly", function() {
return many( digit ).parse("ABC") }, null);
testEqual("many parses correct input", function() {
return many( digit ).parse("123") }, "123");
testEqual("many parses only what it's asked", function() {
return many( digit ).parse("123abc") }, "123");
testThrows("many doesn't take more than one parser", function() {
return many( digit, digit ).parse("123abc") });
// any
testEqual("any matches optionally", function() {
return any( digit ).parse("ABC") }, "");
testEqual("any parses correct input", function() {
return any( digit ).parse("123") }, "123");
testEqual("any parses only what it's asked", function() {
return any( digit ).parse("123abc") }, "123");
testThrows("any doesn't take more than one parser", function() {
return any( digit, digit ).parse("123abc") });
// mandatory whitespace
testEqual("_ fails correctly", function() {
return _.parse("ABC") }, null);
testEqual("_ parses single space", function() {
return _.parse(" ") }, " ");
testEqual("_ parses 4 spaces", function() {
return _.parse(" ") }, " ");
testEqual("_ parses single LF", function() {
return _.parse("\n") }, "\n");
testEqual("_ parses 4x LF", function() {
return _.parse("\n\n\n\n") }, "\n\n\n\n");
testEqual("_ parses single CR", function() {
return _.parse("\r") }, "\r");
testEqual("_ parses 4x CR", function() {
return _.parse("\r\r\r\r") }, "\r\r\r\r");
testEqual("_ parses CRLF", function() {
return _.parse("\r\n") }, "\r\n");
testEqual("_ parses LFCR", function() {
return _.parse("\n\r") }, "\n\r");
testEqual("_ parses only what asked ", function() {
return _.parse(" \r\n \t ABC") }, " \r\n \t ");
// optional whitespace
testEqual("__ matches optionally", function() {
return __.parse("ABC") }, "");
testEqual("__ parses single space", function() {
return __.parse(" ") }, " ");
testEqual("__ parses 4 spaces", function() {
return __.parse(" ") }, " ");
testEqual("__ parses single LF", function() {
return __.parse("\n") }, "\n");
testEqual("__ parses 4x LF", function() {
return __.parse("\n\n\n\n") }, "\n\n\n\n");
testEqual("__ parses single CR", function() {
return __.parse("\r") }, "\r");
testEqual("__ parses 4x CR", function() {
return __.parse("\r\r\r\r") }, "\r\r\r\r");
testEqual("__ parses CRLF", function() {
return __.parse("\r\n") }, "\r\n");
testEqual("__ parses LFCR", function() {
return __.parse("\n\r") }, "\n\r");
testEqual("__ parses only what asked ", function() {
return __.parse(" \r\n \t ABC") }, " \r\n \t ");
console.log("Tests Passed:", testsPassed);
if (testsFailed == 0) {
console.log("Tests Failed:", testsFailed);
} else {
console.warn("Tests Failed:", testsFailed);
}
})();