]> git.gir.st - VimFx.git/blob - extension/test/test-parse-prefs.coffee
Implement filtering hints by text and related changes
[VimFx.git] / extension / test / test-parse-prefs.coffee
1 ###
2 # Copyright Simon Lydell 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 prefs = require('../lib/prefs')
21
22 resetPrefOnTeardown = (pref, teardown) ->
23 previousValue = if prefs.has(pref) then prefs.get(pref) else null
24 teardown(->
25 prefs.set(pref, previousValue)
26 )
27
28 testPref = (pref, fn) ->
29 return (assert, $vimfx, teardown) ->
30 resetPrefOnTeardown(pref, teardown)
31 test = (input, output) ->
32 prefs.set(pref, input)
33 actual = prefs.get(pref)
34 if typeof output == 'string'
35 assert.equal(actual, output)
36 else
37 assert.ok(
38 output.test(actual),
39 "#{output}.test(#{JSON.stringify(actual)})"
40 )
41 fn(test)
42
43 testPrefParsed = (pref, fn) ->
44 return (assert, $vimfx, teardown) ->
45 resetPrefOnTeardown(pref, teardown)
46 test = (input, fn2) ->
47 prefs.set(pref, input)
48 fn2($vimfx.options[pref])
49 fn(assert, test)
50
51 exports['test hints.chars'] = testPref('hints.chars', (test) ->
52 # Invalid values.
53 test('', /^([a-z]) (?!\1)[a-z]$/)
54 test(' ', /^([a-z]) (?!\1)[a-z]$/)
55 test('a', /^a [b-z]$/)
56 test('aa', /^a [b-z]$/)
57
58 # Whitespace handling.
59 test('ab', 'a b')
60 test(' a b\t', 'a b')
61 test('a\tb', 'a b')
62
63 # Automatic grouping.
64 test('abc', 'ab c')
65 test('abcd', 'ab cd')
66 test('abcde', 'abc de')
67 test('abcdef', 'abcd ef')
68
69 # Use last space.
70 test('ab cde f ', 'abcde f')
71 test('ab cde\tf ', 'abcde f')
72
73 # Remove duplicates.
74 test('aba fcAde\tf!.!e ', 'abfcAde !.')
75 )
76
77 spaceDelimitedStringPrefs = [
78 'prev_patterns', 'next_patterns', 'blacklist', 'prevent_autofocus_modes'
79 'adjustable_element_keys', 'activatable_element_keys', 'pattern_attrs'
80 ]
81 spaceDelimitedStringPrefs.forEach((pref) ->
82 exports["test #{pref}"] = testPref(pref, (test) ->
83 # Empty values.
84 test('', '')
85 test(' ', '')
86 test('\t ', '')
87
88 # Simple cases.
89 test('a', 'a')
90 test(' a', 'a')
91 test('a ', 'a')
92 test('a\t', 'a')
93 test(' abc def\tg', 'abc def g')
94
95 # Remove duplicates.
96 test('a a ab A aB AB ABC AB', 'a ab A aB AB ABC')
97 )
98 )
99
100 ['prev_patterns', 'next_patterns'].forEach((pref) ->
101 exports["test #{pref} regex"] = testPrefParsed(pref, (assert, test) ->
102 test('previous previous\\S* foo(', (parsed) ->
103 # Case insensitivity.
104 assert.ok(parsed[0].test('previous'))
105 assert.ok(parsed[0].test('PREVIOUS'))
106 assert.ok(parsed[0].test('Previous'))
107
108 # Whitespace handling.
109 assert.ok(parsed[0].test(' previous'))
110 assert.ok(parsed[0].test('previous '))
111 assert.ok(parsed[0].test(' previous '))
112
113 # Must match at start or end.
114 assert.ok(parsed[0].test('previous b'))
115 assert.ok(parsed[0].test('a previous'))
116 assert.ok(not parsed[0].test('a previous b'))
117
118 # Must match entire words.
119 assert.ok(not parsed[0].test('previously'))
120 assert.ok(not parsed[0].test('previouså'))
121
122 # Regex.
123 assert.ok(parsed[1].test('previous'))
124 assert.ok(parsed[1].test('previously'))
125 assert.ok(not parsed[1].test('foopreviously'))
126 assert.ok(not parsed[1].test('a previously b'))
127
128 # Regex escape.
129 assert.ok(parsed[2].test('foo('))
130 )
131 )
132 )
133
134 exports['test blacklist regex'] = testPrefParsed('blacklist', (assert, test) ->
135 test('example *EXAMPLE* *example.com/?*=}*', (parsed) ->
136 # Case insensitivity.
137 assert.ok(parsed[0].test('example'))
138 assert.ok(parsed[0].test('EXAMPLE'))
139 assert.ok(parsed[0].test('Example'))
140
141 # Must match entire string.
142 assert.ok(not parsed[0].test('http://example.com'))
143
144 # Wildcard.
145 assert.ok(parsed[1].test('http://example.com'))
146 assert.ok(parsed[1].test('http://foobar/?q=examples'))
147
148 # Regex escape.
149 assert.ok(parsed[2].test('https://www.example.com/?test=}&foo=bar'))
150 )
151 )
Imprint / Impressum