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