robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each , setup , teardown = 5t . describe , t . it , t . before_each , t . setup , t . teardown 6local clear =n . clear 7local command =n . command 8local eq =t . eq 9local pathsep =n . get_pathsep () 10local fn =n . fn 11local api =n . api 12local feed =n . feed 13 14local testdir ='Xtest-editorconfig' 15 16--- @param name string 17--- @param expected table<string,any> 18local function test_case ( name , expected ) 19local filename =testdir .. pathsep .. name 20command ( 'edit ' .. filename ) 21 22for opt , val in pairs ( expected ) do 23local opt_info =api . nvim_get_option_info2 ( opt , {} ) 24if opt_info . scope == 'win' then 25eq ( val , api . nvim_get_option_value ( opt , { win =0 } ), name ) 26elseif opt_info . scope == 'buf' then 27eq ( val , api . nvim_get_option_value ( opt , { buf =0 } ), name ) 28else 29eq ( val , api . nvim_get_option_value ( opt , {} ), name ) 30end 31end 32end 33 34setup ( function () 35n . mkdir_p ( testdir ) 36t . write_file ( 37testdir .. pathsep .. '.editorconfig' , 38[[ 39root = true 40 41[3_space.txt] 42indent_style = space 43indent_size = 3 44tab_width = 3 45 46[4_space.py] 47indent_style = space 48indent_size = 4 49tab_width = 8 50 51[space.txt] 52indent_style = space 53indent_size = tab 54 55[tab.txt] 56indent_style = tab 57 58[4_tab.txt] 59indent_style = tab 60indent_size = 4 61tab_width = 4 62 63[4_tab_width_of_8.txt] 64indent_style = tab 65indent_size = 4 66tab_width = 8 67 68[lf.txt] 69end_of_line = lf 70 71[crlf.txt] 72end_of_line = crlf 73 74[cr.txt] 75end_of_line = cr 76 77[utf-8.txt] 78charset = utf-8 79 80[utf-8-bom.txt] 81charset = utf-8-bom 82 83[utf-16be.txt] 84charset = utf-16be 85 86[utf-16le.txt] 87charset = utf-16le 88 89[latin1.txt] 90charset = latin1 91 92[with_newline.txt] 93insert_final_newline = true 94 95[without_newline.txt] 96insert_final_newline = false 97 98[trim.txt] 99trim_trailing_whitespace = true 100 101[no_trim.txt] 102trim_trailing_whitespace = false 103 104[skip_trim_when_insert.txt] 105trim_trailing_whitespace = true 106 107[skip_trim_when_replace.txt] 108trim_trailing_whitespace = true 109 110[skip_trim_when_insert_normal.txt] 111trim_trailing_whitespace = true 112 113[max_line_length.txt] 114max_line_length = 42 115 116[short_spelling_language.txt] 117spelling_language = de 118 119[long_spelling_language.txt] 120spelling_language = en-NZ 121 122[custom_properties.txt] 123property1 = something1 124property2 = x[something2]x 125]] 126) 127end ) 128 129teardown ( function () 130n . rmdir ( testdir ) 131end ) 132 133describe ( 'editorconfig' , function () 134before_each ( function () 135-- Use --clean so that plugins (i.e. editorconfig.lua) are loaded 136clear ( { args ={ '--clean' } } ) 137end ) 138 139it ( 'stops at root even when a child sets root=false' , function () 140local root =testdir .. pathsep .. 'root' 141local child =root .. pathsep .. 'child' 142n . mkdir_p ( child ) 143t . write_file ( root .. pathsep .. '.editorconfig' , 'root = true\n[*]\nindent_style = space\n' ) 144t . write_file ( child .. pathsep .. '.editorconfig' , 'root = false\n[*]\ntab_width = 5\n' ) 145 146command ( 'set shiftwidth=4' ) 147-- The outer .editorconfig sets indent_size=3 for this filename. 148test_case ( 'root' .. pathsep .. 'child' .. pathsep .. '3_space.txt' , { 149expandtab =true , 150shiftwidth =4 , 151tabstop =5 , 152} ) 153eq ( { indent_style ='space' , tab_width ='5' } , fn . luaeval ( 'vim.b.editorconfig' )) 154end ) 155 156it ( 'sets indent options' , function () 157test_case ( '3_space.txt' , { 158expandtab =true , 159shiftwidth =3 , 160softtabstop =- 1 , 161tabstop =3 , 162} ) 163 164test_case ( '4_space.py' , { 165expandtab =true , 166shiftwidth =4 , 167softtabstop =- 1 , 168tabstop =8 , 169} ) 170 171test_case ( 'space.txt' , { 172expandtab =true , 173shiftwidth =0 , 174softtabstop =0 , 175} ) 176 177test_case ( 'tab.txt' , { 178expandtab =false , 179shiftwidth =0 , 180softtabstop =0 , 181} ) 182 183test_case ( '4_tab.txt' , { 184expandtab =false , 185shiftwidth =4 , 186softtabstop =- 1 , 187tabstop =4 , 188} ) 189 190test_case ( '4_tab_width_of_8.txt' , { 191expandtab =false , 192shiftwidth =4 , 193softtabstop =- 1 , 194tabstop =8 , 195} ) 196end ) 197 198it ( 'sets end-of-line options' , function () 199test_case ( 'lf.txt' , { fileformat ='unix' } ) 200test_case ( 'crlf.txt' , { fileformat ='dos' } ) 201test_case ( 'cr.txt' , { fileformat ='mac' } ) 202end ) 203 204it ( 'sets encoding options' , function () 205test_case ( 'utf-8.txt' , { fileencoding ='utf-8' , bomb =false } ) 206test_case ( 'utf-8-bom.txt' , { fileencoding ='utf-8' , bomb =true } ) 207test_case ( 'utf-16be.txt' , { fileencoding ='utf-16' , bomb =false } ) 208test_case ( 'utf-16le.txt' , { fileencoding ='utf-16le' , bomb =false } ) 209test_case ( 'latin1.txt' , { fileencoding ='latin1' , bomb =false } ) 210end ) 211 212it ( 'sets newline options' , function () 213test_case ( 'with_newline.txt' , { fixendofline =true } ) 214test_case ( 'without_newline.txt' , { fixendofline =false } ) 215end ) 216 217it ( 'respects trim_trailing_whitespace' , function () 218local filename =testdir .. pathsep .. 'trim.txt' 219-- luacheck: push ignore 613 220local untrimmed =[[ 221This line ends in whitespace 222So does this one 223And this one 224But not this one 225]] 226-- luacheck: pop 227local trimmed =untrimmed : gsub ( '%s+\n' , '\n' ) 228 229t . write_file ( filename , untrimmed ) 230command ( 'edit ' .. filename ) 231command ( 'write' ) 232command ( 'bdelete' ) 233eq ( trimmed , t . read_file ( filename )) 234 235filename =testdir .. pathsep .. 'no_trim.txt' 236t . write_file ( filename , untrimmed ) 237command ( 'edit ' .. filename ) 238command ( 'write' ) 239command ( 'bdelete' ) 240eq ( untrimmed , t . read_file ( filename )) 241 242filename =testdir .. pathsep .. 'skip_trim_when_insert.txt' 243t . write_file ( filename , untrimmed ) 244command ( 'edit ' .. filename ) 245command ( 'startinsert' ) 246command ( 'write' ) 247command ( 'bdelete' ) 248eq ( untrimmed , t . read_file ( filename )) 249 250filename =testdir .. pathsep .. 'skip_trim_when_replace.txt' 251t . write_file ( filename , untrimmed ) 252command ( 'edit ' .. filename ) 253command ( 'startreplace' ) 254command ( 'write' ) 255command ( 'bdelete' ) 256eq ( untrimmed , t . read_file ( filename )) 257 258filename =testdir .. pathsep .. 'skip_trim_when_insert_normal.txt' 259t . write_file ( filename , untrimmed ) 260command ( 'edit ' .. filename ) 261command ( 'startinsert' ) 262feed ( '<C-o>:write<CR>' ) 263command ( 'bdelete' ) 264eq ( untrimmed , t . read_file ( filename )) 265end ) 266 267it ( 'sets textwidth' , function () 268test_case ( 'max_line_length.txt' , { textwidth =42 } ) 269end ) 270 271it ( 'can be disabled globally' , function () 272api . nvim_set_var ( 'editorconfig' , false ) 273api . nvim_set_option_value ( 'shiftwidth' , 42 , {} ) 274test_case ( '3_space.txt' , { shiftwidth =42 } ) 275end ) 276 277it ( 'can be disabled per-buffer' , function () 278api . nvim_set_option_value ( 'shiftwidth' , 42 , {} ) 279local bufnr =fn . bufadd ( testdir .. pathsep .. '3_space.txt' ) 280api . nvim_buf_set_var ( bufnr , 'editorconfig' , false ) 281test_case ( '3_space.txt' , { shiftwidth =42 } ) 282test_case ( '4_space.py' , { shiftwidth =4 } ) 283end ) 284 285it ( 'does not operate on invalid buffers' , function () 286local ok , err =unpack ( n . exec_lua ( function () 287vim . cmd . edit ( 'test.txt' ) 288local bufnr =vim . api . nvim_get_current_buf () 289vim . cmd . bwipeout ( bufnr ) 290return { pcall ( require ( 'editorconfig' ). config , bufnr ) } 291end )) 292 293eq ( true , ok , err ) 294end ) 295 296it ( 'sets spelllang' , function () 297test_case ( 'short_spelling_language.txt' , { spelllang ='de' } ) 298test_case ( 'long_spelling_language.txt' , { spelllang ='en_nz' } ) 299end ) 300 301it ( 'set custom properties' , function () 302n . exec_lua ( function () 303local editorconfig =require ( 'editorconfig' ) 304editorconfig . properties . property1 =function ( bufnr , val , _opts ) 305vim . b [ bufnr ]. property1 =val 306end 307editorconfig . properties . property2 =function ( bufnr , val , _opts ) 308vim . b [ bufnr ]. property2 =val 309end 310end ) 311 312command ( 'edit ' .. testdir .. pathsep .. 'custom_properties.txt' ) 313eq ( 'something1' , fn . luaeval ( 'vim.b.property1' ), 'property1' ) 314eq ( 'x[something2]x' , fn . luaeval ( 'vim.b.property2' ), 'property2' ) 315end ) 316end )