robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , setup =t . describe , t . it , t . setup 5local eq =t . eq 6local NIL =vim . NIL 7local eval =n . eval 8local clear =n . clear 9local api =n . api 10local fn =n . fn 11local command =n . command 12local pcall_err =t . pcall_err 13 14setup ( clear ) 15 16describe ( 'sort()' , function () 17it ( 'errors out when sorting special values' , function () 18eq ( 19'Vim(call):E362: Using a boolean value as a Float' , 20pcall_err ( command , 'call sort([v:true, v:false], "f")' ) 21) 22end ) 23 24it ( 'sorts “wrong” values between -0.0001 and 0.0001, preserving order' , function () 25api . nvim_set_var ( 'list' , { 26true , 27false , 28NIL , 29{} , 30{ a =42 } , 31'check' , 320.0001 , 33- 0.0001 , 34} ) 35command ( 'call insert(g:list, function("tr"))' ) 36local error_lines =fn . split ( fn . execute ( 'silent! call sort(g:list, "f")' ), '\n' ) 37local errors ={} 38for _ , err in ipairs ( error_lines ) do 39errors [ err ] =true 40end 41eq ( { 42[ 'E362: Using a boolean value as a Float' ] =true , 43[ 'E891: Using a Funcref as a Float' ] =true , 44[ 'E892: Using a String as a Float' ] =true , 45[ 'E893: Using a List as a Float' ] =true , 46[ 'E894: Using a Dictionary as a Float' ] =true , 47[ 'E907: Using a special value as a Float' ] =true , 48} , errors ) 49eq ( 50"[-1.0e-4, function('tr'), v:true, v:false, v:null, [], {'a': 42}, 'check', 1.0e-4]" , 51eval ( 'string(g:list)' ) 52) 53end ) 54 55it ( 'can yield E702 and stop sorting after that' , function () 56command ( [[ 57function Cmp(a, b) 58if type(a:a) == type([]) || type(a:b) == type([]) 59return [] 60endif 61return (a:a > a:b) - (a:a < a:b) 62endfunction 63]] ) 64eq ( 65'Vim(let):E745: Using a List as a Number' , 66pcall_err ( command , 'let sl = sort([1, 0, [], 3, 2], "Cmp")' ) 67) 68end ) 69 70it ( 'handles large numbers properly' , function () 71local to_sort ={ 72{ 229539777187355 , 229539777187355 } , 73{ 487766135067138 , 491977135306566 } , 74{ 188325333471071 , 188931909913550 } , 75{ 264028451845520 , 265514296554744 } , 76{ 245727634348687 , 249469249579525 } , 77{ 375117820166731 , 378942174241518 } , 78{ 535474757750378 , 535849288071548 } , 79} 80local expected ={ 81{ 188325333471071 , 188931909913550 } , 82{ 229539777187355 , 229539777187355 } , 83{ 245727634348687 , 249469249579525 } , 84{ 264028451845520 , 265514296554744 } , 85{ 375117820166731 , 378942174241518 } , 86{ 487766135067138 , 491977135306566 } , 87{ 535474757750378 , 535849288071548 } , 88} 89eq ( expected , fn . sort ( to_sort )) 90api . nvim_set_var ( 'to_sort' , to_sort ) 91eq ( expected , api . nvim_eval ( 'sort(g:to_sort)' )) 92eq ( expected , api . nvim_eval ( 'sort(g:to_sort, {a, b -> a[0] - b[0]})' )) 93end ) 94end )