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