2 # Copyright Simon Lydell 2014, 2016.
4 # This file is part of VimFx.
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.
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.
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/>.
20 assert = require('./assert')
21 utils = require('../lib/utils')
23 exports['test selectAllSubstringMatches'] = ->
24 window = utils.getCurrentWindow()
26 selection = window.getSelection()
28 # Element creation helper.
29 e = (tagName, childNodes = []) ->
30 element = document.createElement(tagName)
31 element.appendChild(childNode) for childNode in childNodes
34 # Text node creation helper.
35 t = (text) -> document.createTextNode(text)
37 test = (name, element, string, options, expected) ->
38 msg = (message) -> "#{name}: #{message}"
40 selection.removeAllRanges()
41 utils.selectAllSubstringMatches(element, string, options)
43 assert.equal(selection.rangeCount, expected.length, msg('rangeCount'))
45 for index in [0...selection.rangeCount] by 1
46 range = selection.getRangeAt(index)
48 startContainer, startOffset
49 endContainer, endOffset
50 expectedString = string
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()'))
60 do (name = 'simple case') ->
64 test(name, element, 'es', null, [
68 do (name = 'several matches per text node') ->
70 (t1 = t('es test best es'))
72 test(name, element, 'es', null, [
79 do (name = 'split across two text nodes') ->
84 test(name, element, 'es', null, [
88 do (name = 'split across three text nodes') ->
94 test(name, element, 'test', null, [
98 do (name = 'empty text nodes skipped') ->
109 test(name, element, 'test', null, [
113 do (name = 'across several elements') ->
125 test(name, element, 'test', null, [
129 do (name = 'overlapping matches') ->
131 (t1 = t('ababaabacaba'))
133 test(name, element, 'aba', null, [
134 [t1, 0, t1, 8, 'ababaaba']
138 do (name = 'case sensitivity') ->
142 test(name, element, 'TesT', null, [])
144 do (name = 'case insensitivity') ->
148 test(name, element, 'TesT', {caseSensitive: false}, [
149 [t1, 0, t1, 4, 'tESt']
152 exports['test bisect'] = ->
153 fn = (num) -> num > 7
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])
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])
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])
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])
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])
195 exports['test removeDuplicates'] = ->
196 assert.arrayEqual(utils.removeDuplicates(
200 assert.arrayEqual(utils.removeDuplicates(
201 ['a', 'b', 'c', 'b', 'd', 'a']),