robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
8634ec3827
master
1" Test behavior of boolean-like values. 2 3source check.vim 4 5" Test what is explained at ":help TRUE" and ":help FALSE". 6func Test_if () 7if v: false 8call assert_true ( false , 'v:false is false' ) 9endif 10if 0 11call assert_true ( false , 'zero is false' ) 12endif 13if "0" 14call assert_true ( false , 'zero string is false' ) 15endif 16if "foo" 17call assert_true ( false , 'foo is false' ) 18endif 19if " " 20call assert_true ( false , 'space is false' ) 21endif 22if empty ( "foo" ) 23call assert_true ( false , 'foo is not empty' ) 24endif 25 26if v: true 27else 28call assert_true ( false , 'v:true is true' ) 29endif 30if 1 31else 32call assert_true ( false , 'one is true' ) 33endif 34if "1" 35else 36call assert_true ( false , 'one string is true' ) 37endif 38if "1foo" 39else 40call assert_true ( false , 'one in string is true' ) 41endif 42 43call assert_fails ( 'if [1]' , 'E745' ) 44call assert_fails ( 'if {1: 1}' , 'E728' ) 45call assert_fails ( 'if function("string")' , 'E703' ) 46if has ( 'float' ) 47call assert_fails ( 'if 1.3")' , 'E805' ) 48endif 49endfunc 50 51function Try_arg_true_false ( expr , false_val , true_val ) 52for v in [ 'v:false' , '0' , '"0"' , '"foo"' , '" "' ] 53let r = eval ( substitute ( a: expr , '%v%' , v , '' )) 54call assert_equal ( a: false_val , r , 'result for ' . v . ' is not ' . string ( a: false_val ) . ' but ' . string ( r )) 55endfor 56for v in [ 'v:true' , '1' , '"1"' , '"1foo"' ] 57let r = eval ( substitute ( a: expr , '%v%' , v , '' )) 58call assert_equal ( a: true_val , r , 'result for ' . v . ' is not ' . string ( a: true_val ) . ' but ' . string ( r )) 59endfor 60endfunc 61 62" Test using TRUE or FALSE values for an argument. 63func Test_true_false_arg () 64call Try_arg_true_false ( 'count(["a", "A"], "a", %v%)' , 1 , 2 ) 65 66set wildignore = *.swp 67call Try_arg_true_false ( 'expand("foo.swp", %v%)' , "" , "foo.swp" ) 68call Try_arg_true_false ( 'expand("foo.vim", 0, %v%)' , "foo.vim" , [ "foo.vim" ]) 69 70call setreg ( 'a' , [ 'x' , 'y' ]) 71call Try_arg_true_false ( 'getreg("a", 1, %v%)' , "x\ny\n" , [ 'x' , 'y' ]) 72 73set wildignore = *.vim 74call Try_arg_true_false ( 'glob("runtest.vim", %v%)' , "" , "runtest.vim" ) 75set wildignore = *.swp 76call Try_arg_true_false ( 'glob("runtest.vim", 0, %v%)' , "runtest.vim" , [ "runtest.vim" ]) 77if has ( 'unix' ) 78silent !ln -s doesntexitXlink 79call Try_arg_true_false ( 'glob("Xlink", 0, 0, %v%)' , "" , "Xlink" ) 80silent !rmXlink 81endif 82 83set wildignore = *.vim 84call Try_arg_true_false ( 'globpath(".", "runtest.vim", %v%)' , "" , "./runtest.vim" ) 85set wildignore = *.swp 86call Try_arg_true_false ( 'globpath(".", "runtest.vim", 0, %v%)' , "./runtest.vim" , [ "./runtest.vim" ]) 87if has ( 'unix' ) 88silent !ln -s doesntexitXlink 89call Try_arg_true_false ( 'globpath(".", "Xlink", 0, 0, %v%)' , "" , "./Xlink" ) 90silent !rmXlink 91endif 92 93abbr asdf asdff 94call Try_arg_true_false ( 'hasmapto("asdff", "i", %v%)' , 0 , 1 ) 95 96call Try_arg_true_false ( 'index(["a", "A"], "A", 0, %v%)' , 1 , 0 ) 97 98function FilterMapArg ( d ) 99if type ( a: d ) == type ({}) 100return filter ( a: d , 'v:key == "rhs"' ) 101endif 102return a: d 103endfunction 104call Try_arg_true_false ( 'maparg("asdf", "i", %v%)' , "" , "asdff" ) 105call Try_arg_true_false ( 'FilterMapArg(maparg("asdf", "i", 1, %v%))' , "asdff" , { 'rhs' : 'asdff' }) 106 107call Try_arg_true_false ( '"asdf"->hasmapto("i", %v%)' , 0 , 1 ) 108 109new colored 110call setline ( 1 , '<here>' ) 111syn match brackets "<. * > " 112syn match here "here "transparent 113let brackets_id = synID ( 1 , 1 , 0 ) 114let here_id = synID ( 1 , 3 , 0 ) 115call Try_arg_true_false ( 'synID(1, 3, %v%)' , here_id , brackets_id ) 116bwipe ! 117endfunc 118 119function Try_arg_non_zero ( expr , false_val , true_val ) 120for v in [ 'v:false' , '0' , '[1]' , '{2:3}' , '3.4' ] 121let r = eval ( substitute ( a: expr , '%v%' , v , '' )) 122call assert_equal ( a: false_val , r , 'result for ' . v . ' is not ' . a: false_val . ' but ' . r ) 123endfor 124for v in [ 'v:true' , '1' , '" "' , '"0"' ] 125let r = eval ( substitute ( a: expr , '%v%' , v , '' )) 126call assert_equal ( a: true_val , r , 'result for ' . v . ' is not ' . a: true_val . ' but ' . r ) 127endfor 128endfunc 129 130 131" Test using non-zero-arg for an argument. 132func Test_non_zero_arg () 133" call test_settime(93784) 134" call Try_arg_non_zero("mode(%v%)", 'x', 'x!') 135" call test_settime(0) 136 137call Try_arg_non_zero ( "shellescape('foo%', %v%)" , "'foo%'" , "'foo\\%'" ) 138 139" visualmode() needs to be called twice to check 140for v in [ v: false , 0 , [ 1 ], { 2 : 3 }, 3.4 ] 141normal vv 142let r = visualmode ( v ) 143call assert_equal ( 'v' , r , 'result for ' . string ( v ) . ' is not "v" but ' . r ) 144let r = visualmode ( v ) 145call assert_equal ( 'v' , r , 'result for ' . string ( v ) . ' is not "v" but ' . r ) 146endfor 147for v in [ v: true , 1 , " " , "0" ] 148normal vv 149let r = visualmode ( v ) 150call assert_equal ( 'v' , r , 'result for ' . v . ' is not "v" but ' . r ) 151let r = visualmode ( v ) 152call assert_equal ( '' , r , 'result for ' . v . ' is not "" but ' . r ) 153endfor 154endfunc