This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
192 lines (165 loc) · 4 KB
/
test.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
var test = require('tape')
var parse = require('./index')
test('eq num', t => {
var obj = parse('prop == 1')
var exp = {prop: {'$eq': 1}}
t.deepEquals(obj, exp)
t.end()
})
test('eq string', t => {
var obj = parse('prop == "something"')
var exp = {prop: {'$eq': 'something'}}
t.deepEquals(obj, exp)
t.end()
})
test('eq string (single quote)', t => {
var obj = parse("prop == 'something'")
var exp = {prop: {'$eq': 'something'}}
t.deepEquals(obj, exp)
t.end()
})
test('eq array', t => {
var obj = parse("prop == [1, 'something']")
var exp = {prop: {'$eq': [1, 'something']}}
t.deepEquals(obj, exp)
t.end()
})
// assume that the other comparisons work with various values
test('has num', t => {
var obj = parse("prop has 1")
var exp = {prop: {'$elemMatch': {'$eq': 1}}}
t.deepEquals(obj, exp)
t.end()
})
test('ne num', t => {
var obj = parse("prop != 1")
var exp = {prop: {'$neq': 1}}
t.deepEquals(obj, exp)
t.end()
})
test('gte num', t => {
var obj = parse("prop >= 1")
var exp = {prop: {'$gte': 1}}
t.deepEquals(obj, exp)
t.end()
})
test('lte num', t => {
var obj = parse("prop <= 1")
var exp = {prop: {'$lte': 1}}
t.deepEquals(obj, exp)
t.end()
})
test('gt num', t => {
var obj = parse("prop > 1")
var exp = {prop: {'$gt': 1}}
t.deepEquals(obj, exp)
t.end()
})
test('lt num', t => {
var obj = parse("prop < 1")
var exp = {prop: {'$lt': 1}}
t.deepEquals(obj, exp)
t.end()
})
test('e and e', t => {
var obj = parse("prop1 == 1 and prop2 == 'something'")
var exp = {'$and': [{prop1: {'$eq': 1}}, {prop2: {'$eq': 'something'}}]}
t.deepEquals(obj, exp)
t.end()
})
test('e or e', t => {
var obj = parse("prop1 == 1 or prop2 == 'something'")
var exp = {'$or': [{prop1: {'$eq': 1}}, {prop2: {'$eq': 'something'}}]}
t.deepEquals(obj, exp)
t.end()
})
test('not e', t => {
var obj = parse("not prop1 == 1")
var exp = {'$not': {prop1: {'$eq': 1}}}
t.deepEquals(obj, exp)
t.end()
})
test('multiple %and, %not, %or replaced', t => {
var obj = parse('not prop1 == 1 and not prop2 == 1')
var str = JSON.stringify(obj)
t.notOk(str.match(/\%not/g), 'replaces all %not')
var obj = parse('prop1 == 1 and prop2 == 1 and prop3 == 4')
var str = JSON.stringify(obj)
t.notOk(str.match(/\%and/g), 'replaces all %and')
var obj = parse('prop1 == 1 or prop2 == 1 or prop3 == 4')
var str = JSON.stringify(obj)
t.notOk(str.match(/\%or/g), 'replaces all %or')
t.end()
})
test('variadic ANDs array nicely', t => {
var obj = parse('a == 1 and b == 2 and c == 3 and d == 4 and e == 5')
var exp = { '$and': [
{a: {'$eq': 1}},
{b: {'$eq': 2}},
{c: {'$eq': 3}},
{d: {'$eq': 4}},
{e: {'$eq': 5}}
]}
t.deepEquals(obj, exp)
t.end()
})
test('variadic ORs array nicely', t => {
var obj = parse('a == 1 or b == 2 or c == 3 or d == 4 or e == 5')
var exp = { '$or': [
{a: {'$eq': 1}},
{b: {'$eq': 2}},
{c: {'$eq': 3}},
{d: {'$eq': 4}},
{e: {'$eq': 5}},
]}
t.deepEquals(obj, exp)
t.end()
})
test('order of operations handled properly', t => {
var obj = parse('a == 1 or b == 1 and c == 1')
var exp = {
'$or': [
{a: {'$eq': 1}},
{'$and': [
{b: {'$eq': 1}},
{c: {'$eq': 1}}
]}
]
}
t.deepEquals(obj, exp)
t.end()
})
test('handles complex nested boolean order of operations', t => {
var obj = parse('a == 1 and b == 2 and c == 3 or d == 4 and e == 5 and f == 6')
var exp = { '$or': [
{'$and': [
{a: {'$eq': 1}},
{b: {'$eq': 2}},
{c: {'$eq': 3}}
]},
{'$and': [
{d: {'$eq': 4}},
{e: {'$eq': 5}},
{f: {'$eq': 6}}
]}
]}
t.deepEquals(obj, exp)
t.end()
})
test('handles parenthetical order', t => {
var obj = parse('(a == 1 or b == 2 or c == 3) and (d == 4 or e == 5 or f == 6)')
var exp = { '$and': [
{'$or': [
{a: {'$eq': 1}},
{b: {'$eq': 2}},
{c: {'$eq': 3}}
]},
{'$or': [
{d: {'$eq': 4}},
{e: {'$eq': 5}},
{f: {'$eq': 6}}
]}
]}
t.deepEquals(obj, exp)
t.end()
})