Skip to content

Commit 8f36e48

Browse files
committed
Color: add error test cases
1 parent 8d079ca commit 8f36e48

File tree

2 files changed

+139
-6
lines changed

2 files changed

+139
-6
lines changed

autoload/vital/__vital__/Color.vim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ let s:HSL_RE = '\v^hsl\((\d+),\s*(\d+)\%,\s*(\d+)\%\)$'
6868
let s:VIM_RGB_FILE = expand('$VIMRUNTIME/rgb.txt')
6969
function! s:parse(str) abort
7070
if type(a:str) !=# type('')
71-
throw 'vital: Color: parse(): invalid format: ' . a:str
71+
throw 'vital: Color: parse(): invalid value type: ' . string(a:str)
7272
endif
7373
" e.g. #FFFFFF
7474
let m = matchlist(a:str, s:RGB_HEX_RE)
@@ -136,9 +136,9 @@ function! s:rgb(r, g, b) abort
136136
endfunction
137137

138138
function! s:_check_rgb_range(r, g, b) abort
139-
for n in [a:r, a:g, a:b]
140-
if type(n) !=# type(0) && type(n) !=# type(0.0)
141-
\ || 0 ># n || n ># 255
139+
for l:N in [a:r, a:g, a:b]
140+
if type(l:N) !=# type(0) && type(l:N) !=# type(0.0)
141+
\ || 0 ># l:N || l:N ># 255
142142
return 0
143143
endif
144144
endfor
@@ -154,8 +154,8 @@ function! s:hsl(h, s, l) abort
154154
endfunction
155155

156156
function! s:_check_hsl_range(h, s, l) abort
157-
for n in [a:h, a:s, a:l]
158-
if type(n) !=# type(0) && type(n) !=# type(0.0)
157+
for l:N in [a:h, a:s, a:l]
158+
if type(l:N) !=# type(0) && type(l:N) !=# type(0.0)
159159
return 0
160160
endif
161161
endfor

test/Color.vim

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,136 @@ function! s:suite.distance() abort
8383
call s:assert.compare(r.distance(l), '<', 3, r.as_rgb_hex() . ' distance ' . l.as_rgb_hex())
8484
endfor
8585
endfunction
86+
87+
function! s:suite.error_parse()
88+
for l:V in [
89+
\ 0,
90+
\ function('function'),
91+
\ [],
92+
\ {},
93+
\ 0.0,
94+
\ v:false,
95+
\ v:null,
96+
\ test_null_job(),
97+
\ test_null_channel(),
98+
\ test_null_blob(),
99+
\ '',
100+
\ '#',
101+
\ '#d',
102+
\ '#de',
103+
\ '#dead',
104+
\ '#deadb',
105+
\ '#deadbee',
106+
\ '#deadbeef',
107+
\ 'd',
108+
\ 'de',
109+
\ 'dea',
110+
\ 'dead',
111+
\ 'deadb',
112+
\ 'deadbe',
113+
\ 'deadbee',
114+
\ 'deadbeef',
115+
\ '0xc0ffee',
116+
\ 'rgb(0,0%,0%)',
117+
\ 'hsl(0,0,0)',
118+
\ 'rgb(0,0,0);',
119+
\ 'hsl(0,0%,0%);',
120+
\ 'unknown_color_name',
121+
\]
122+
try
123+
call s:C.parse(l:V)
124+
catch /vital: Color: parse():/
125+
call s:assert.true(1, string(l:V) . ' should not be parsed')
126+
endtry
127+
endfor
128+
endfunction
129+
130+
function! s:suite.error_rgb()
131+
for rgb in [
132+
\ repeat([0], 3),
133+
\ repeat([function('function')], 3),
134+
\ repeat([[]], 3),
135+
\ repeat([{}], 3),
136+
\ repeat([0.0], 3),
137+
\ repeat([v:false], 3),
138+
\ repeat([v:null], 3),
139+
\ repeat([test_null_job()], 3),
140+
\ repeat([test_null_channel()], 3),
141+
\ repeat([test_null_blob()], 3),
142+
\ [-1, 0, 0],
143+
\ [-2, 0, 0],
144+
\ [0, -1, 0],
145+
\ [0, -2, 0],
146+
\ [0, 0, -1],
147+
\ [0, 0, -2],
148+
\ [256, 0, 0],
149+
\ [257, 0, 0],
150+
\ [0, 256, 0],
151+
\ [0, 257, 0],
152+
\ [0, 0, 256],
153+
\ [0, 0, 257],
154+
\]
155+
try
156+
call s:C.rgb(rgb[0], rgb[1], rgb[2])
157+
catch /vital: Color: rgb():/
158+
call s:assert.true(1, 'rgb() disallow ' . string(rgb))
159+
endtry
160+
endfor
161+
endfunction
162+
163+
function! s:suite.error_hsl()
164+
for hsl in [
165+
\ repeat([0], 3),
166+
\ repeat([function('function')], 3),
167+
\ repeat([[]], 3),
168+
\ repeat([{}], 3),
169+
\ repeat([0.0], 3),
170+
\ repeat([v:false], 3),
171+
\ repeat([v:null], 3),
172+
\ repeat([test_null_job()], 3),
173+
\ repeat([test_null_channel()], 3),
174+
\ repeat([test_null_blob()], 3),
175+
\ [-1, 0, 0],
176+
\ [-2, 0, 0],
177+
\ [0, -1, 0],
178+
\ [0, -2, 0],
179+
\ [0, 0, -1],
180+
\ [0, 0, -2],
181+
\ [361, 0, 0],
182+
\ [362, 0, 0],
183+
\ [0, 101, 0],
184+
\ [0, 102, 0],
185+
\ [0, 0, 101],
186+
\ [0, 0, 102],
187+
\]
188+
try
189+
call s:C.hsl(hsl[0], hsl[1], hsl[2])
190+
catch /vital: Color: hsl():/
191+
call s:assert.true(1, 'hsl() disallow ' . string(hsl))
192+
endtry
193+
endfor
194+
endfunction
195+
196+
function! s:suite.error_xterm()
197+
for l:Value in [
198+
\ function('function'),
199+
\ [],
200+
\ {},
201+
\ 0.0,
202+
\ v:false,
203+
\ v:null,
204+
\ test_null_job(),
205+
\ test_null_channel(),
206+
\ test_null_blob(),
207+
\ -2,
208+
\ -1,
209+
\ 256,
210+
\ 257,
211+
\]
212+
try
213+
call s:C.xterm(l:Value)
214+
catch /vital: Color: xterm():/
215+
call s:assert.true(1, 'xterm() disallow ' . string(l:Value))
216+
endtry
217+
endfor
218+
endfunction

0 commit comments

Comments
 (0)