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 command =n . command 6local fn =n . fn 7local clear =n . clear 8local eval =n . eval 9local eq =t . eq 10local pcall_err =t . pcall_err 11local api =n . api 12local NIL =vim . NIL 13 14describe ( 'Special values' , function () 15before_each ( clear ) 16 17it ( 'do not cause error when freed' , function () 18command ( [[ 19function Test() 20try 21return v:true 22finally 23return 'something else' 24endtry 25endfunction 26]] ) 27command ( 'call Test()' ) 28end ) 29 30it ( 'work with empty()' , function () 31eq ( 0 , fn . empty ( true )) 32eq ( 1 , fn . empty ( false )) 33eq ( 1 , fn . empty ( NIL )) 34end ) 35 36it ( 'can be stringified and eval’ed back' , function () 37eq ( true , fn . eval ( fn . string ( true ))) 38eq ( false , fn . eval ( fn . string ( false ))) 39eq ( NIL , fn . eval ( fn . string ( NIL ))) 40end ) 41 42it ( 'work with is/isnot properly' , function () 43eq ( 1 , eval ( 'v:null is v:null' )) 44eq ( 0 , eval ( 'v:null is v:true' )) 45eq ( 0 , eval ( 'v:null is v:false' )) 46eq ( 1 , eval ( 'v:true is v:true' )) 47eq ( 0 , eval ( 'v:true is v:false' )) 48eq ( 1 , eval ( 'v:false is v:false' )) 49 50eq ( 0 , eval ( 'v:null is 0' )) 51eq ( 0 , eval ( 'v:true is 0' )) 52eq ( 0 , eval ( 'v:false is 0' )) 53 54eq ( 0 , eval ( 'v:null is 1' )) 55eq ( 0 , eval ( 'v:true is 1' )) 56eq ( 0 , eval ( 'v:false is 1' )) 57 58eq ( 0 , eval ( 'v:null is ""' )) 59eq ( 0 , eval ( 'v:true is ""' )) 60eq ( 0 , eval ( 'v:false is ""' )) 61 62eq ( 0 , eval ( 'v:null is "null"' )) 63eq ( 0 , eval ( 'v:true is "true"' )) 64eq ( 0 , eval ( 'v:false is "false"' )) 65 66eq ( 0 , eval ( 'v:null is []' )) 67eq ( 0 , eval ( 'v:true is []' )) 68eq ( 0 , eval ( 'v:false is []' )) 69 70eq ( 0 , eval ( 'v:null isnot v:null' )) 71eq ( 1 , eval ( 'v:null isnot v:true' )) 72eq ( 1 , eval ( 'v:null isnot v:false' )) 73eq ( 0 , eval ( 'v:true isnot v:true' )) 74eq ( 1 , eval ( 'v:true isnot v:false' )) 75eq ( 0 , eval ( 'v:false isnot v:false' )) 76 77eq ( 1 , eval ( 'v:null isnot 0' )) 78eq ( 1 , eval ( 'v:true isnot 0' )) 79eq ( 1 , eval ( 'v:false isnot 0' )) 80 81eq ( 1 , eval ( 'v:null isnot 1' )) 82eq ( 1 , eval ( 'v:true isnot 1' )) 83eq ( 1 , eval ( 'v:false isnot 1' )) 84 85eq ( 1 , eval ( 'v:null isnot ""' )) 86eq ( 1 , eval ( 'v:true isnot ""' )) 87eq ( 1 , eval ( 'v:false isnot ""' )) 88 89eq ( 1 , eval ( 'v:null isnot "null"' )) 90eq ( 1 , eval ( 'v:true isnot "true"' )) 91eq ( 1 , eval ( 'v:false isnot "false"' )) 92 93eq ( 1 , eval ( 'v:null isnot []' )) 94eq ( 1 , eval ( 'v:true isnot []' )) 95eq ( 1 , eval ( 'v:false isnot []' )) 96end ) 97 98it ( 'work with +/-/* properly' , function () 99eq ( 1 , eval ( '0 + v:true' )) 100eq ( 0 , eval ( '0 + v:null' )) 101eq ( 0 , eval ( '0 + v:false' )) 102 103eq ( - 1 , eval ( '0 - v:true' )) 104eq ( 0 , eval ( '0 - v:null' )) 105eq ( 0 , eval ( '0 - v:false' )) 106 107eq ( 1 , eval ( '1 * v:true' )) 108eq ( 0 , eval ( '1 * v:null' )) 109eq ( 0 , eval ( '1 * v:false' )) 110end ) 111 112it ( 'does not work with +=/-=/.=' , function () 113api . nvim_set_var ( 'true' , true ) 114api . nvim_set_var ( 'false' , false ) 115command ( 'let null = v:null' ) 116 117eq ( 'Vim(let):E734: Wrong variable type for +=' , pcall_err ( command , 'let true += 1' )) 118eq ( 'Vim(let):E734: Wrong variable type for +=' , pcall_err ( command , 'let false += 1' )) 119eq ( 'Vim(let):E734: Wrong variable type for +=' , pcall_err ( command , 'let null += 1' )) 120 121eq ( 'Vim(let):E734: Wrong variable type for -=' , pcall_err ( command , 'let true -= 1' )) 122eq ( 'Vim(let):E734: Wrong variable type for -=' , pcall_err ( command , 'let false -= 1' )) 123eq ( 'Vim(let):E734: Wrong variable type for -=' , pcall_err ( command , 'let null -= 1' )) 124 125eq ( 'Vim(let):E734: Wrong variable type for .=' , pcall_err ( command , 'let true .= 1' )) 126eq ( 'Vim(let):E734: Wrong variable type for .=' , pcall_err ( command , 'let false .= 1' )) 127eq ( 'Vim(let):E734: Wrong variable type for .=' , pcall_err ( command , 'let null .= 1' )) 128end ) 129 130it ( 'work with . (concat) properly' , function () 131eq ( 'v:true' , eval ( '"" . v:true' )) 132eq ( 'v:null' , eval ( '"" . v:null' )) 133eq ( 'v:false' , eval ( '"" . v:false' )) 134end ) 135 136it ( 'work with ?? (falsy operator)' , function () 137eq ( true , eval ( 'v:true ?? 42' )) 138eq ( 42 , eval ( 'v:false ?? 42' )) 139eq ( 42 , eval ( 'v:null ?? 42' )) 140end ) 141 142it ( 'work with type()' , function () 143eq ( 6 , fn . type ( true )) 144eq ( 6 , fn . type ( false )) 145eq ( 7 , fn . type ( NIL )) 146end ) 147 148it ( 'work with copy() and deepcopy()' , function () 149eq ( true , fn . deepcopy ( true )) 150eq ( false , fn . deepcopy ( false )) 151eq ( NIL , fn . deepcopy ( NIL )) 152 153eq ( true , fn . copy ( true )) 154eq ( false , fn . copy ( false )) 155eq ( NIL , fn . copy ( NIL )) 156end ) 157 158it ( 'fails in index' , function () 159eq ( 'Vim(echo):E909: Cannot index a special variable' , pcall_err ( command , 'echo v:true[0]' )) 160eq ( 'Vim(echo):E909: Cannot index a special variable' , pcall_err ( command , 'echo v:false[0]' )) 161eq ( 'Vim(echo):E909: Cannot index a special variable' , pcall_err ( command , 'echo v:null[0]' )) 162end ) 163 164it ( 'is accepted by assert_true and assert_false' , function () 165fn . assert_false ( false ) 166fn . assert_false ( true ) 167fn . assert_false ( NIL ) 168 169fn . assert_true ( false ) 170fn . assert_true ( true ) 171fn . assert_true ( NIL ) 172 173eq ( { 174'Expected False but got v:true' , 175'Expected False but got v:null' , 176'Expected True but got v:false' , 177'Expected True but got v:null' , 178} , api . nvim_get_vvar ( 'errors' )) 179end ) 180 181describe ( 'compat' , function () 182it ( 'v:count is distinct from count' , function () 183command ( 'let count = []' ) -- v:count is readonly 184eq ( 1 , eval ( 'count is# g:["count"]' )) 185end ) 186it ( 'v:errmsg is distinct from errmsg' , function () 187command ( 'let errmsg = 1' ) 188eq ( 1 , eval ( 'errmsg is# g:["errmsg"]' )) 189end ) 190it ( 'v:shell_error is distinct from shell_error' , function () 191command ( 'let shell_error = []' ) -- v:shell_error is readonly 192eq ( 1 , eval ( 'shell_error is# g:["shell_error"]' )) 193end ) 194it ( 'v:this_session is distinct from this_session' , function () 195command ( 'let this_session = []' ) 196eq ( 1 , eval ( 'this_session is# g:["this_session"]' )) 197end ) 198end ) 199end )