-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathquery-parser.test.js
142 lines (124 loc) · 4.95 KB
/
query-parser.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
const { test } = require('tap')
const { parseQuery } = require('../lib/query-parser')
test('should extract CSS selector from query and keep it intact', (t) => {
const query = '.home li > a'
const result = parseQuery(query)
t.strictSame(result.selector, '.home li > a')
t.end()
})
test('should extract CSS selector from query and keep it intact, even if getter query is present', (t) => {
const query = '.home li > a ($)'
const result = parseQuery(query)
t.strictSame(result.selector, '.home li > a')
t.end()
})
test('should extract CSS selector from query and keep it intact, even if getter and filter queries are present', (t) => {
const query = '.home li > a ($ | trim:both)'
const result = parseQuery(query)
t.strictSame(result.selector, '.home li > a')
t.end()
})
test('should extract CSS selector from query and keep it intact, but trim whitespace', (t) => {
const query = ' .home li > a ($ | trim:both)'
const result = parseQuery(query)
t.strictSame(result.selector, '.home li > a')
t.end()
})
test('should return null for `getter` if not present in the query', (t) => {
const query = '.home li > a'
const result = parseQuery(query)
t.strictSame(result.getter, null)
t.end()
})
test('should extract `getter` from query if present', (t) => {
const query = '.home li > a (href)'
const result = parseQuery(query)
t.strictSame(result.getter, 'href')
t.end()
})
test('should ignore whitespace around `getter`', (t) => {
const query = '.home li > a ( href )'
const result = parseQuery(query)
t.strictSame(result.getter, 'href')
t.end()
})
test('should return empty array for `filters` if not present in the query', (t) => {
const query = '.home li > a'
const result = parseQuery(query)
t.same(result.filters, [])
t.end()
})
test('should extract filter name', (t) => {
const query = '.home li > a ($ | trim)'
const result = parseQuery(query)
t.strictSame(result.filters[0].name, 'trim')
t.end()
})
test('should extract all filter names in order', (t) => {
const query = '.home li > a ($ | trim | normalizeWhitespace)'
const result = parseQuery(query)
t.strictSame(result.filters[0].name, 'trim')
t.strictSame(result.filters[1].name, 'normalizeWhitespace')
t.end()
})
test("should extract filters' argument as strings when double quoted", (t) => {
const query = '.home li > a ($ | filter:"double" | anotherFilter:"quotes")'
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], 'double')
t.strictSame(result.filters[1].args[0], 'quotes')
t.end()
})
test("should extract filters' argument as strings when single quoted", (t) => {
const query = ".home li > a ($ | trim:'single' | anotherFilter:'quotes')"
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], 'single')
t.strictSame(result.filters[1].args[0], 'quotes')
t.end()
})
test('should preserve any unicode char when using quoted strings', (t) => {
const query = '.home li > a ($ | say:"helló világ":"こんにちは世界":"Привет, мир")'
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], 'helló világ')
t.strictSame(result.filters[0].args[1], 'こんにちは世界')
t.strictSame(result.filters[0].args[2], 'Привет, мир')
t.end()
})
test('should allow to escape single quotes inside of single quoted strings', (t) => {
// The double backslash is to escape the backslash symbol inside a JS string.
// The string bellow is actually:
// .home li > a ($ | say:'this: \' is an escaped quote')
const query = ".home li > a ($ | say:'this: \\' is an escaped quote')"
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], "this: ' is an escaped quote")
t.end()
})
test('should allow to escape double quotes inside of double quoted strings', (t) => {
// The double backslash is to escape the backslash symbol inside a JS string.
// The string bellow is actually:
// .home li > a ($ | say:"this: \" is an escaped quote")
const query = '.home li > a ($ | say:"this: \\" is an escaped quote")'
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], 'this: " is an escaped quote')
t.end()
})
test("should extract filters' argument symbols as strings", (t) => {
const query = '.home li > a ($ | trim:right | anotherFilter:test)'
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], 'right')
t.strictSame(result.filters[1].args[0], 'test')
t.end()
})
test('should return empty array for filter arguments if none provided', (t) => {
const query = '.home li > a ($ | trim)'
const result = parseQuery(query)
t.same(result.filters[0].args, [])
t.end()
})
test("should extract all filter's colon-separated arguments as strings in order", (t) => {
const query = '.home li > a ($ | trim:left:right:both)'
const result = parseQuery(query)
t.strictSame(result.filters[0].args[0], 'left')
t.strictSame(result.filters[0].args[1], 'right')
t.strictSame(result.filters[0].args[2], 'both')
t.end()
})