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