" Tests for ":highlight" and highlighting.
source view_util.vim
source screendump.vim
source check.vim
source script_util.vim
source vim9.vim
func ClearDict(d)
for k in keys(a:d)
call remove(a:d, k)
endfor
endfunc
func Test_highlight()
" basic test if ":highlight" doesn't crash
highlight
hi Search
" test setting colors.
" test clearing one color and all doesn't generate error or warning
silent! hi NewGroup term=bold cterm=italic ctermfg=DarkBlue ctermbg=Grey gui= guifg=#00ff00 guibg=Cyan
silent! hi Group2 term= cterm=
hi Group3 term=underline cterm=bold
let res = split(execute("hi NewGroup"), "\n")[0]
" filter ctermfg and ctermbg, the numbers depend on the terminal
let res = substitute(res, 'ctermfg=\d*', 'ctermfg=2', '')
let res = substitute(res, 'ctermbg=\d*', 'ctermbg=3', '')
call assert_equal("NewGroup xxx term=bold cterm=italic ctermfg=2 ctermbg=3",
\ res)
call assert_equal("Group2 xxx cleared",
\ split(execute("hi Group2"), "\n")[0])
call assert_equal("Group3 xxx term=underline cterm=bold",
\ split(execute("hi Group3"), "\n")[0])
hi clear NewGroup
call assert_equal("NewGroup xxx cleared",
\ split(execute("hi NewGroup"), "\n")[0])
call assert_equal("Group2 xxx cleared",
\ split(execute("hi Group2"), "\n")[0])
hi Group2 NONE
call assert_equal("Group2 xxx cleared",
\ split(execute("hi Group2"), "\n")[0])
hi clear
call assert_equal("Group3 xxx cleared",
\ split(execute("hi Group3"), "\n")[0])
call assert_fails("hi Crash term='asdf", "E475:")
endfunc
func HighlightArgs(name)
return 'hi ' . substitute(split(execute('hi ' . a:name), '\n')[0], '\<xxx\>', '', '')
endfunc
func IsColorable()
return has('gui_running') || str2nr(&t_Co) >= 8
endfunc
func HiCursorLine()
let hiCursorLine = HighlightArgs('CursorLine')
if has('gui_running')
let guibg = matchstr(hiCursorLine, 'guibg=\w\+')
let hi_ul = 'hi CursorLine gui=underline guibg=NONE'
let hi_bg = 'hi CursorLine gui=NONE ' . guibg
else
let hi_ul = 'hi CursorLine cterm=underline ctermbg=NONE'
let hi_bg = 'hi CursorLine cterm=NONE ctermbg=Gray'
endif
return [hiCursorLine, hi_ul, hi_bg]
endfunc
func Check_lcs_eol_attrs(attrs, row, col)
let save_lcs = &lcs
set list
call assert_equal(a:attrs, ScreenAttrs(a:row, a:col)[0])
set nolist
let &lcs = save_lcs
endfunc
func Test_highlight_eol_with_cursorline()
let [hiCursorLine, hi_ul, hi_bg] = HiCursorLine()
call NewWindow('topleft 5', 20)
call setline(1, 'abcd')
call matchadd('Search', '\n')
" expected:
" 'abcd '
" ^^^^ ^^^^^ no highlight
" ^ 'Search' highlight
let attrs0 = ScreenAttrs(1, 10)[0]
call assert_equal(repeat([attrs0[0]], 4), attrs0[0:3])
call assert_equal(repeat([attrs0[0]], 5), attrs0[5:9])
call assert_notequal(attrs0[0], attrs0[4])
setlocal cursorline
" underline
exe hi_ul
" expected:
" 'abcd '
" ^^^^ underline
" ^ 'Search' highlight with underline
" ^^^^^ underline
let attrs = ScreenAttrs(1, 10)[0]
call assert_equal(repeat([attrs[0]], 4), attrs[0:3])
call assert_equal([attrs[4]] + repeat([attrs[5]], 5), attrs[4:9])
call assert_notequal(attrs[0], attrs[4])
call assert_notequal(attrs[4], attrs[5])
call assert_notequal(attrs0[0], attrs[0])
call assert_notequal(attrs0[4], attrs[4])
call Check_lcs_eol_attrs(attrs, 1, 10)
if IsColorable()
" bg-color
exe hi_bg
" expected:
" 'abcd '
" ^^^^ bg-color of 'CursorLine'
" ^ 'Search' highlight
" ^^^^^ bg-color of 'CursorLine'
let attrs = ScreenAttrs(1, 10)[0]
call assert_equal(repeat([attrs[0]], 4), attrs[0:3])
call assert_equal(repeat([attrs[5]], 5), attrs[5:9])
call assert_equal(attrs0[4], attrs[4])
call assert_notequal(attrs[0], attrs[4])
call assert_notequal(attrs[4], attrs[5])
call assert_notequal(attrs0[0], attrs[0])
call assert_notequal(attrs0[5], attrs[5])
call Check_lcs_eol_attrs(attrs, 1, 10)
endif
call CloseWindow()
exe hiCursorLine
endfunc
func Test_highlight_eol_with_cursorline_vertsplit()
let [hiCursorLine, hi_ul, hi_bg] = HiCursorLine()
call NewWindow('topleft 5', 5)
call setline(1, 'abcd')
call matchadd('Search', '\n')
let expected = "abcd |abcd "
let actual = ScreenLines(1, 15)[0]
call assert_equal(expected, act