-
Notifications
You must be signed in to change notification settings - Fork 6
/
tests.js
205 lines (202 loc) · 5.69 KB
/
tests.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
var matcher = require("./index.js");
var assert = require("assert");
describe("Matching", function() {
it("matches a string", function(){
var m = matcher({x:"[A-Z]"})("A");
assert.equal(m.x, "A");
});
it("matches two string", function() {
var m = matcher({x:"[A-Z]", y:"[A-Z]"})("AB");
assert.equal(m.x, "A");
assert.equal(m.y, "B");
});
it("returns null on no match", function() {
var m = matcher({x:"B"})("A");
assert.equal(m, null);
});
it("matches multi character strings", function() {
var m = matcher({x:"[A-Z]+"})("AB");
assert.equal(m.x, "AB");
});
it("ignores patterns with _", function() {
var m = matcher({_:"[A-Z]+"})("AB");
assert.equal(m._, undefined);
});
it("ignores patterns containing _", function() {
var m = matcher({_1:"[A-Z]+"})("AB");
assert.equal(m._1, undefined);
});
it("ignores multiple patterns containing _", function() {
var m = matcher({_1:"[A-Z]+", _2:"[A-Z]+"})("AB");
assert.equal(m._1, undefined);
assert.equal(m._2, undefined);
});
it("matches lazily when it has to", function() {
var m = matcher({x:"[A-Z]+?", _:"C"})("ABCD");
assert.equal(m.x, "AB");
});
it("matches numbers", function() {
var m = matcher({x:"\\d+"})("123123");
assert.equal(m.x, "123123");
});
it("matches emails", function() {
var m = matcher({
_ : "^",
user : "\\w+",
_2 : "@",
host : "[^@]+",
_3 : "$"
})("[email protected]")
assert.equal(m.user, "benji");
assert.equal(m.host, "nono.com");
});
it("matches primitive http/s urls", function() {
var m = matcher({
protocol : "http|https",
_1 : "://",
host : "[^/]+",
_2 : "/",
path : ".+",
_3 : "$"
})("http://www.google.com/search?foo=bar")
assert.equal(m.protocol, "http");
assert.equal(m.host, "www.google.com");
assert.equal(m.path, "search?foo=bar");
});
it("parses 'primitive' CSV", function() {
var p = matcher({
first : "\\w+?",
_1 : ",",
last : "\\w+?",
_2 : ",",
age : "\\d+?",
_3 : "$"
});
var m = p("Benjamin,Gruenbaum,27")
assert.equal(m.first, "Benjamin");
assert.equal(m.last, "Gruenbaum");
assert.equal(m.age, "27");
});
it("works with dynamic objects", function() {
var o = {};
o["A"] = "A";
var p = matcher(o)("A").A;
assert.equal(p, "A");
});
it("parses HTML", function(){
var he = matcher({
_1 : "<a href='",
url : ".+?",
_2 : "'.*",
});
var comes = he("<a href='http://www.google.com'>Google!</a>");
assert.equal(comes.url, "http://www.google.com");
});
it("accepts REs ", function(){
var m = matcher({ x: /a/});
var p = m("a");
assert.equal(p.x, "a");
});
it("accepts multiple REs ", function(){
var m = matcher({
x: /a/,
y: /A+/
})("aAAAAA");
assert.equal(m.x, "a");
assert.equal(m.y, "AAAAA");
});
it("matches emails with RE syntax", function() {
var m = matcher({
_ : /^/,
user : /\w+/,
_2 : /@/,
host : /[^@]+/,
_3 : /$/
})("[email protected]")
assert.equal(m.user, "benji");
assert.equal(m.host, "nono.com");
});
it("accepts flags as a second parameter", function() {
var m = matcher({
x: "a+",
}, "i")("aAAAAA");
assert.equal(m.x, "aAAAAA");
});
it("behaves like native RE on invalid flags", function() {
assert.throws(function() {
var m = matcher({
x: "a+",
}, "13qwshfd")("aAAAAA");
});
});
it("throws on objects with numeric keys (inconsistent iteration order)",
function(){
assert.throws(function() {
var m = matcher([1,2,3]);
});
});
it("works with a large number of properties", function() {
var o = {};
var str = "";
for(var i = 0; i < 100; i++) {
o["x_" + i] = "A";
str += "A"
}
var parser = matcher(o);
var o2 = parser(str);
assert.equal(o2.x_50, "A");
assert.equal(o2.x_99, "A");
assert.equal(o2.x_0, "A");
});
});
describe("the parser nesting abilities", function() {
it("should accept nested objects", function() {
var m = matcher({x: {x: "A"}});
var p = m("A");
assert.equal(p.x.x, "A");
});
it("should accept multiple nested objects", function() {
var p = matcher({x: {x: "A"}, y: {y: "B"}})("AB");
assert.equal(p.x.x, "A");
assert.equal(p.y.y, "B");
});
it("should accept deep nested objects", function() {
var p = matcher({x: {x:"A", y: "B"}})("AB");
assert.equal(p.x.x, "A");
assert.equal(p.x.y, "B");
});
it("should accept nested matchers", function() {
var m1 = matcher({x: "A"});
var m2 = matcher({x: m1 });
var p = m2("A");
assert.equal(p.x.x, "A");
});
it("should accept the same matcher twice", function() {
var m1 = matcher({x: "A"});
var m2 = matcher({x: m1, y: m1 });
var p = m2("AA");
assert.equal(p.x.x, "A");
assert.equal(p.y.x, "A");
});
it("should accept the same matcher twice", function() {
var m1 = matcher({x: "A"});
var m2 = matcher({x: m1, y: m1 });
var p = m2("AA");
assert.equal(p.x.x, "A");
assert.equal(p.y.x, "A");
});
it("should be able to mix REs with objects", function() {
var m1 = matcher({x: "A"});
var m2 = matcher({x: m1, y: /a/ });
var p = m2("Aa");
assert.equal(p.x.x, "A");
assert.equal(p.y, "a");
});
});
describe("properties of the return value", function() {
it("does not return extra properties", function() {
assert.equal(Object.keys({}).length, 0); // nothing crazy
var m = matcher({x:"A",y:"B"})("AB");
assert.equal(Object.keys(m).length, 2); // nothing crazy
});
});