]> git.gir.st - VimFx.git/blob - extension/test/test-utils.coffee
Fix `selectAllSubstringMatches` test
[VimFx.git] / extension / test / test-utils.coffee
1 ###
2 # Copyright Simon Lydell 2014, 2016, 2017.
3 #
4 # This file is part of VimFx.
5 #
6 # VimFx is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # VimFx is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
18 ###
19
20 assert = require('./assert')
21 utils = require('../lib/utils')
22
23 exports['test selectAllSubstringMatches'] = ($vimfx, teardown) ->
24 window = utils.getCurrentWindow()
25 {document} = window
26 selection = window.getSelection()
27
28 # Element creation helper.
29 e = (tagName, childNodes = []) ->
30 element = document.createElement(tagName)
31 element.appendChild(childNode) for childNode in childNodes
32 return element
33
34 # Text node creation helper.
35 t = (text) -> document.createTextNode(text)
36
37 test = (name, element, string, options, expected) ->
38 msg = (message) -> "#{name}: #{message}"
39
40 document.documentElement.appendChild(element)
41 teardown(->
42 element.remove()
43 )
44
45 selection.removeAllRanges()
46 utils.selectAllSubstringMatches(element, string, options)
47
48 assert.equal(selection.rangeCount, expected.length, msg('rangeCount'))
49
50 for index in [0...selection.rangeCount] by 1
51 range = selection.getRangeAt(index)
52 [
53 startContainer, startOffset
54 endContainer, endOffset
55 expectedString = string
56 ] = expected[index]
57 assert.equal(range.startContainer, startContainer, msg('startContainer'))
58 assert.equal(range.startOffset, startOffset, msg('startOffset'))
59 assert.equal(range.endContainer, endContainer, msg('endContainer'))
60 assert.equal(range.endOffset, endOffset, msg('endOffset'))
61 assert.equal(range.toString(), expectedString, msg('toString()'))
62
63 return
64
65 do (name = 'simple case') ->
66 element = e('p', [
67 (t1 = t('test'))
68 ])
69 test(name, element, 'es', null, [
70 [t1, 1, t1, 3]
71 ])
72
73 do (name = 'several matches per text node') ->
74 element = e('p', [
75 (t1 = t('es test best es'))
76 ])
77 test(name, element, 'es', null, [
78 [t1, 0, t1, 2]
79 [t1, 4, t1, 6]
80 [t1, 9, t1, 11]
81 [t1, 13, t1, 15]
82 ])
83
84 do (name = 'split across two text nodes') ->
85 element = e('p', [
86 (t1 = t('te'))
87 (t2 = t('st'))
88 ])
89 test(name, element, 'es', null, [
90 [t1, 1, t2, 1]
91 ])
92
93 do (name = 'split across three text nodes') ->
94 element = e('p', [
95 (t1 = t('te'))
96 t('s')
97 (t2 = t('t'))
98 ])
99 test(name, element, 'test', null, [
100 [t1, 0, t2, 1]
101 ])
102
103 do (name = 'empty text nodes skipped') ->
104 element = e('p', [
105 t('')
106 (t1 = t('a te'))
107 t('')
108 t('')
109 t('s')
110 t('')
111 (t2 = t('t!'))
112 t('')
113 ])
114 test(name, element, 'test', null, [
115 [t1, 2, t2, 1]
116 ])
117
118 do (name = 'across several elements') ->
119 element = e('p', [
120 t('\n ')
121 e('span', [
122 (t1 = t('\tte'))
123 e('i', [
124 t('s')
125 ])
126 ])
127 e('span')
128 (t2 = t('t'))
129 ])
130 test(name, element, 'test', null, [
131 [t1, 1, t2, 1]
132 ])
133
134 do (name = 'overlapping matches') ->
135 element = e('p', [
136 (t1 = t('ababaabacaba'))
137 ])
138 test(name, element, 'aba', null, [
139 [t1, 0, t1, 8, 'ababaaba']
140 [t1, 9, t1, 12]
141 ])
142
143 do (name = 'case sensitivity') ->
144 element = e('p', [
145 (t1 = t('tESt'))
146 ])
147 test(name, element, 'TesT', null, [])
148
149 do (name = 'case insensitivity') ->
150 element = e('p', [
151 (t1 = t('tESt'))
152 ])
153 test(name, element, 'TesT', {caseSensitive: false}, [
154 [t1, 0, t1, 4, 'tESt']
155 ])
156
157 exports['test bisect'] = ->
158 fn = (num) -> num > 7
159
160 # Non-sensical input.
161 assert.arrayEqual(utils.bisect(5, 2, fn), [null, null])
162 assert.arrayEqual(utils.bisect(7.5, 8, fn), [null, null])
163 assert.arrayEqual(utils.bisect(7, 8.5, fn), [null, null])
164 assert.arrayEqual(utils.bisect(7.5, 8.5, fn), [null, null])
165
166 # Unfindable bounds.
167 assert.arrayEqual(utils.bisect(8, 8, fn), [null, 8])
168 assert.arrayEqual(utils.bisect(7, 7, fn), [7, null])
169 assert.arrayEqual(utils.bisect(6, 7, fn), [7, null])
170 assert.arrayEqual(utils.bisect(7, 8, fn), [7, 8])
171 assert.arrayEqual(utils.bisect(1, 2, (n) -> n == 1), [null, null])
172 assert.arrayEqual(utils.bisect(0, 0, fn), [0, null])
173
174 # Less than.
175 assert.arrayEqual(utils.bisect(0, 7, fn), [7, null])
176 assert.arrayEqual(utils.bisect(0, 8, fn), [7, 8])
177 assert.arrayEqual(utils.bisect(1, 8, fn), [7, 8])
178 assert.arrayEqual(utils.bisect(2, 8, fn), [7, 8])
179 assert.arrayEqual(utils.bisect(3, 8, fn), [7, 8])
180 assert.arrayEqual(utils.bisect(4, 8, fn), [7, 8])
181 assert.arrayEqual(utils.bisect(5, 8, fn), [7, 8])
182 assert.arrayEqual(utils.bisect(6, 8, fn), [7, 8])
183
184 # Greater than.
185 assert.arrayEqual(utils.bisect(7, 9, fn), [7, 8])
186 assert.arrayEqual(utils.bisect(7, 10, fn), [7, 8])
187 assert.arrayEqual(utils.bisect(7, 11, fn), [7, 8])
188 assert.arrayEqual(utils.bisect(7, 12, fn), [7, 8])
189 assert.arrayEqual(utils.bisect(7, 13, fn), [7, 8])
190 assert.arrayEqual(utils.bisect(7, 14, fn), [7, 8])
191 assert.arrayEqual(utils.bisect(7, 15, fn), [7, 8])
192 assert.arrayEqual(utils.bisect(7, 16, fn), [7, 8])
193
194 # Various cases.
195 assert.arrayEqual(utils.bisect(0, 9, fn), [7, 8])
196 assert.arrayEqual(utils.bisect(5, 9, fn), [7, 8])
197 assert.arrayEqual(utils.bisect(6, 10, fn), [7, 8])
198 assert.arrayEqual(utils.bisect(0, 12345, fn), [7, 8])
199
200 exports['test removeDuplicates'] = ->
201 assert.arrayEqual(utils.removeDuplicates(
202 [1, 1, 2, 1, 3, 2]),
203 [1, 2, 3]
204 )
205 assert.arrayEqual(utils.removeDuplicates(
206 ['a', 'b', 'c', 'b', 'd', 'a']),
207 ['a', 'b', 'c', 'd']
208 )
Imprint / Impressum