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