-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.spec.ts
271 lines (247 loc) · 6.83 KB
/
index.spec.ts
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { expect } from 'chai';
import { generateAst, COMMANDER_IDENTITY_QUERY_ID, NUMBER_OF_COMMANDER_IDENTITY_COLORS_QUERY_ID, NUMBER_VALUE_ID, NUMBER_OF_COLORS_QUERY_ID, NUMBER_OF_TYPES_QUERY_ID } from './index';
describe('generation of AST', () => {
const operatorMap = {
'>=': 'GTE',
'>': 'GT',
'=': 'EQ',
'!=': 'NEQ',
'<=': 'LTE',
'<': 'LT',
'GTE': 'GTE',
'GT': 'GT',
'LT': 'LT',
'LTE': 'LTE',
'EQ': 'EQ',
'NEQ': 'NEQ',
'EQUALS': 'EQ',
'TO EQUAL': 'EQ',
'TO EQUALS': 'EQ',
'TO NOT EQUAL': 'NEQ',
'NOT EQUALS': 'NEQ',
}
Object.entries(operatorMap).forEach(([operator, expected]) => {
it('handles operator: ' + operator, () => {
const query = `cmd ${operator} r`;
const astNode = generateAst(query)[0];
expect(astNode.type).to.equal(COMMANDER_IDENTITY_QUERY_ID);
expect(astNode.operator).to.equal(expected, 'failed for: ' + query)
expect(astNode.value.value).to.deep.equal(['R'], 'failed for: ' + query)
})
})
it('should work for single color query (full color name)', () => {
const input = "cmd < red";
const expected = [
{
type: 'commander-identity-query',
value: { type: 'color-value', value: ['R'] },
operator: 'LT'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for single color query', () => {
const input = "cmd < r";
const expected = [
{
type: 'commander-identity-query',
value: { type: 'color-value', value: ['R'] },
operator: 'LT'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for number of colors queries', () => {
const input = "|color| < 2";
const expected = [
{
type: NUMBER_OF_COLORS_QUERY_ID,
value: { type: NUMBER_VALUE_ID, value: 2 },
operator: 'LT'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for number of cmd queries', () => {
const input = "|cmd| = 2";
const expected = [
{
type: NUMBER_OF_COMMANDER_IDENTITY_COLORS_QUERY_ID,
value: { type: NUMBER_VALUE_ID, value: 2 },
operator: 'EQ'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for number of types queries', () => {
const input = "|type| > 2";
const expected = [
{
type: NUMBER_OF_TYPES_QUERY_ID,
value: { type: NUMBER_VALUE_ID, value: 2 },
operator: 'GT'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for multiple color query', () => {
const input = "cmd < rg";
const expected = [
{
type: 'commander-identity-query',
value: { type: 'color-value', value: ['R', 'G'] },
operator: 'LT'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should handle similarity query (inexact values)', () => {
const expected = [{
type: 'similarity-query',
value: { type: 'text-value', value: 'Elenda', isExact: false },
operator: 'EQ'
}]
const input = "sim Elenda";
const equalInputs = [
'sim Elenda',
'sim = Elenda',
'similar Elenda',
'similarTo Elenda',
'similar = Elenda',
'similarTo = Elenda',
'similar to = Elenda',
'similar to = Elenda',
]
equalInputs.forEach(input => {
expect(generateAst(input)).to.deep.equal(expected);
})
})
it('should handle similarity query (exact values)', () => {
const expected = [{
type: 'similarity-query',
value: { type: 'text-value', value: 'Elenda', isExact: true },
operator: 'EQ'
}]
const input = "sim Elenda";
const equalInputs = [
'sim "Elenda"',
'sim = "Elenda"',
'similar "Elenda"',
'similarTo "Elenda"',
'similar = "Elenda"',
'similarTo = "Elenda"',
'similar to = "Elenda"',
'similar to = "Elenda"',
]
equalInputs.forEach(input => {
expect(generateAst(input)).to.deep.equal(expected);
})
})
it('should handle "escaped" values correctly', () => {
const input = 'name = "Arguel\'s Blood Fast // Temple of Aclazotz"';
const expected = [
{
type: 'name-query',
value:
{
type: 'text-value',
isExact: true,
value: 'Arguel\'s Blood Fast // Temple of Aclazotz'
},
operator: 'EQ'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for complex queries', () => {
const input = "t:creature set = znr cmc > 3 pow > 1 tou < 10 cmd < r name = \"Niv-Mizzet, the Firemind\" draw discard";
const expected = [
{
type: 'type-query',
value: { type: 'text-value', isExact: false, value: 'creature' },
operator: 'EQ'
},
{
type: 'set-query',
value: { type: 'text-value', isExact: false, value: 'znr' },
operator: 'EQ'
},
{
type: 'converted-manacost-query',
value: { type: 'number-value', value: 3 },
operator: 'GT'
},
{
type: 'power-query',
value: { type: 'number-value', value: 1 },
operator: 'GT'
},
{
type: 'toughness-query',
value: { type: 'number-value', value: 10 },
operator: 'LT'
},
{
type: 'commander-identity-query',
value: { type: 'color-value', value: ['R'] },
operator: 'LT'
},
{
type: 'name-query',
value:
{
type: 'text-value',
isExact: true,
value: 'Niv-Mizzet, the Firemind'
},
operator: 'EQ'
},
{
type: 'text-query',
value: { type: 'text-value', isExact: false, value: 'draw' },
operator: 'EQ'
},
{
type: 'text-query',
value: { type: 'text-value', isExact: false, value: 'discard' },
operator: 'EQ'
}
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should work for set name query', () => {
const input = 'set_name: "Commander 2019"';
const expected = [
{
type: 'set-name-query',
value: { type: 'text-value', value: 'Commander 2019', isExact: true },
operator: 'EQ'
},
]
expect(generateAst(input)).to.deep.equal(expected);
})
it('should handle \' correctly', () => {
const input = 'commander\'s sphere'
const expected = [
{
type: 'text-query',
operator: 'EQ',
value: {
type: 'text-value',
value: 'commander\'s',
isExact: false,
}
},
{
type: 'text-query',
operator: 'EQ',
value: {
type: 'text-value',
value: 'sphere',
isExact: false,
}
}
]
expect(generateAst(input)).to.deep.equal(expected)
})
})