]> git.gir.st - VimFx.git/blob - extension/test/test-utils.coffee
Improve scrolling when holding h/l/j/k down
[VimFx.git] / extension / test / test-utils.coffee
1 ###
2 # Copyright Simon Lydell 2014, 2016.
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 utils = require('../lib/utils')
21
22 exports['test selectAllSubstringMatches'] = (assert) ->
23 window = utils.getCurrentWindow()
24 {document} = window
25 selection = window.getSelection()
26
27 # Element creation helper.
28 e = (tagName, childNodes = []) ->
29 element = document.createElement(tagName)
30 element.appendChild(childNode) for childNode in childNodes
31 return element
32
33 # Text node creation helper.
34 t = (text) -> document.createTextNode(text)
35
36 test = (name, element, string, options, expected) ->
37 msg = (message) -> "#{name}: #{message}"
38
39 selection.removeAllRanges()
40 utils.selectAllSubstringMatches(element, string, options)
41
42 assert.equal(selection.rangeCount, expected.length, msg('rangeCount'))
43
44 for index in [0...selection.rangeCount] by 1
45 range = selection.getRangeAt(index)
46 [
47 startContainer, startOffset
48 endContainer, endOffset
49 expectedString = string
50 ] = expected[index]
51 assert.equal(range.startContainer, startContainer, msg('startContainer'))
52 assert.equal(range.startOffset, startOffset, msg('startOffset'))
53 assert.equal(range.endContainer, endContainer, msg('endContainer'))
54 assert.equal(range.endOffset, endOffset, msg('endOffset'))
55 assert.equal(range.toString(), expectedString, msg('toString()'))
56
57 return
58
59 do (name = 'simple case') ->
60 element = e('p', [
61 (t1 = t('test'))
62 ])
63 test(name, element, 'es', null, [
64 [t1, 1, t1, 3]
65 ])
66
67 do (name = 'several matches per text node') ->
68 element = e('p', [
69 (t1 = t('es test best es'))
70 ])
71 test(name, element, 'es', null, [
72 [t1, 0, t1, 2]
73 [t1, 4, t1, 6]
74 [t1, 9, t1, 11]
75 [t1, 13, t1, 15]
76 ])
77
78 do (name = 'split across two text nodes') ->
79 element = e('p', [
80 (t1 = t('te'))
81 (t2 = t('st'))
82 ])
83 test(name, element, 'es', null, [
84 [t1, 1, t2, 1]
85 ])
86
87 do (name = 'split across three text nodes') ->
88 element = e('p', [
89 (t1 = t('te'))
90 t('s')
91 (t2 = t('t'))
92 ])
93 test(name, element, 'test', null, [
94 [t1, 0, t2, 1]
95 ])
96
97 do (name = 'empty text nodes skipped') ->
98 element = e('p', [
99 t('')
100 (t1 = t('a te'))
101 t('')
102 t('')
103 t('s')
104 t('')
105 (t2 = t('t!'))
106 t('')
107 ])
108 test(name, element, 'test', null, [
109 [t1, 2, t2, 1]
110 ])
111
112 do (name = 'across several elements') ->
113 element = e('p', [
114 t('\n ')
115 e('span', [
116 (t1 = t('\tte'))
117 e('i', [
118 t('s')
119 ])
120 ])
121 e('span')
122 (t2 = t('t'))
123 ])
124 test(name, element, 'test', null, [
125 [t1, 1, t2, 1]
126 ])
127
128 do (name = 'overlapping matches') ->
129 element = e('p', [
130 (t1 = t('ababaabacaba'))
131 ])
132 test(name, element, 'aba', null, [
133 [t1, 0, t1, 8, 'ababaaba']
134 [t1, 9, t1, 12]
135 ])
136
137 do (name = 'case sensitivity') ->
138 element = e('p', [
139 (t1 = t('tESt'))
140 ])
141 test(name, element, 'TesT', null, [])
142
143 do (name = 'case insensitivity') ->
144 element = e('p', [
145 (t1 = t('tESt'))
146 ])
147 test(name, element, 'TesT', {caseSensitive: false}, [
148 [t1, 0, t1, 4, 'tESt']
149 ])
150
151 exports['test bisect'] = (assert) ->
152 fn = (num) -> num > 7
153
154 # Non-sensical input.
155 assert.deepEqual(utils.bisect(5, 2, fn), [null, null])
156 assert.deepEqual(utils.bisect(7.5, 8, fn), [null, null])
157 assert.deepEqual(utils.bisect(7, 8.5, fn), [null, null])
158 assert.deepEqual(utils.bisect(7.5, 8.5, fn), [null, null])
159
160 # Unfindable bounds.
161 assert.deepEqual(utils.bisect(8, 8, fn), [null, 8])
162 assert.deepEqual(utils.bisect(7, 7, fn), [7, null])
163 assert.deepEqual(utils.bisect(6, 7, fn), [7, null])
164 assert.deepEqual(utils.bisect(7, 8, fn), [7, 8])
165 assert.deepEqual(utils.bisect(1, 2, (n) -> n == 1), [null, null])
166 assert.deepEqual(utils.bisect(0, 0, fn), [0, null])
167
168 # Less than.
169 assert.deepEqual(utils.bisect(0, 7, fn), [7, null])
170 assert.deepEqual(utils.bisect(0, 8, fn), [7, 8])
171 assert.deepEqual(utils.bisect(1, 8, fn), [7, 8])
172 assert.deepEqual(utils.bisect(2, 8, fn), [7, 8])
173 assert.deepEqual(utils.bisect(3, 8, fn), [7, 8])
174 assert.deepEqual(utils.bisect(4, 8, fn), [7, 8])
175 assert.deepEqual(utils.bisect(5, 8, fn), [7, 8])
176 assert.deepEqual(utils.bisect(6, 8, fn), [7, 8])
177
178 # Greater than.
179 assert.deepEqual(utils.bisect(7, 9, fn), [7, 8])
180 assert.deepEqual(utils.bisect(7, 10, fn), [7, 8])
181 assert.deepEqual(utils.bisect(7, 11, fn), [7, 8])
182 assert.deepEqual(utils.bisect(7, 12, fn), [7, 8])
183 assert.deepEqual(utils.bisect(7, 13, fn), [7, 8])
184 assert.deepEqual(utils.bisect(7, 14, fn), [7, 8])
185 assert.deepEqual(utils.bisect(7, 15, fn), [7, 8])
186 assert.deepEqual(utils.bisect(7, 16, fn), [7, 8])
187
188 # Various cases.
189 assert.deepEqual(utils.bisect(0, 9, fn), [7, 8])
190 assert.deepEqual(utils.bisect(5, 9, fn), [7, 8])
191 assert.deepEqual(utils.bisect(6, 10, fn), [7, 8])
192 assert.deepEqual(utils.bisect(0, 12345, fn), [7, 8])
193
194 exports['test removeDuplicates'] = (assert) ->
195 assert.deepEqual(utils.removeDuplicates(
196 [1, 1, 2, 1, 3, 2]),
197 [1, 2, 3]
198 )
199 assert.deepEqual(utils.removeDuplicates(
200 ['a', 'b', 'c', 'b', 'd', 'a']),
201 ['a', 'b', 'c', 'd']
202 )
Imprint / Impressum