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 , before_each =t . describe , t . it , t . before_each 5local eq =t . eq 6local pcall_err =t . pcall_err 7local neq =t . neq 8local eval =n . eval 9local clear =n . clear 10local source =n . source 11local command =n . command 12 13describe ( 'sort' , function () 14before_each ( clear ) 15 16it ( 'numbers compared as strings' , function () 17eq ( { 1 , 2 , 3 } , eval ( 'sort([3, 2, 1])' )) 18eq ( { 13 , 28 , 3 } , eval ( 'sort([3, 28, 13])' )) 19end ) 20 21it ( 'numbers compared as numeric' , function () 22eq ( { 1 , 2 , 3 } , eval ( "sort([3, 2, 1], 'n')" )) 23eq ( { 3 , 13 , 28 } , eval ( "sort([3, 28, 13], 'n')" )) 24-- Strings are not sorted. 25eq ( { '13' , '28' , '3' } , eval ( "sort(['13', '28', '3'], 'n')" )) 26end ) 27 28it ( 'numbers compared as numbers' , function () 29eq ( { 3 , 13 , 28 } , eval ( "sort([13, 28, 3], 'N')" )) 30eq ( { '3' , '13' , '28' } , eval ( "sort(['13', '28', '3'], 'N')" )) 31end ) 32 33it ( 'numbers compared as float' , function () 34eq ( { 0.28 , 3 , 13.5 } , eval ( "sort([13.5, 0.28, 3], 'f')" )) 35end ) 36 37it ( 'ability to call sort() from a compare function' , function () 38source ( [[ 39function Compare1(a, b) abort 40call sort(range(3), 'Compare2') 41return a:a - a:b 42endfunc 43 44function Compare2(a, b) abort 45return a:a - a:b 46endfunc 47]] ) 48 49eq ( { 1 , 3 , 5 } , eval ( "sort([3, 1, 5], 'Compare1')" )) 50end ) 51 52it ( 'default sort' , function () 53-- docs say omitted, empty or zero argument sorts on string representation 54eq ( { '2' , 'A' , 'AA' , 'a' , 1 , 3.3 } , eval ( 'sort([3.3, 1, "2", "A", "a", "AA"])' )) 55eq ( { '2' , 'A' , 'AA' , 'a' , 1 , 3.3 } , eval ( [[sort([3.3, 1, "2", "A", "a", "AA"], '')]] )) 56eq ( { '2' , 'A' , 'AA' , 'a' , 1 , 3.3 } , eval ( 'sort([3.3, 1, "2", "A", "a", "AA"], 0)' )) 57eq ( { '2' , 'A' , 'a' , 'AA' , 1 , 3.3 } , eval ( 'sort([3.3, 1, "2", "A", "a", "AA"], 1)' )) 58neq ( nil , pcall_err ( command , 'call sort([3.3, 1, "2"], 3)' ): find ( 'E474:' )) 59end ) 60end )