blob: 4e95441a81635615f7a3df4bebb11fd13a894b4a [file] [log] [blame]
Bram Moolenaare381d3d2016-07-07 14:50:41 +02001" Test behavior of boolean-like values.
2
Bram Moolenaar5feabe02020-01-30 18:24:53 +01003source check.vim
4
Bram Moolenaare381d3d2016-07-07 14:50:41 +02005" Test what is explained at ":help TRUE" and ":help FALSE".
6func 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 Moolenaare2e40752020-09-04 21:18:46 +020043 call assert_fails('if [1]', 'E745:')
44 call assert_fails('if {1: 1}', 'E728:')
45 call assert_fails('if function("string")', 'E703:')
Bram Moolenaar73e28dc2022-09-17 21:08:33 +010046 call assert_fails('if 1.3")', 'E805:')
Bram Moolenaare381d3d2016-07-07 14:50:41 +020047endfunc
48
49function Try_arg_true_false(expr, false_val, true_val)
50 for v in ['v:false', '0', '"0"', '"foo"', '" "']
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
54 for v in ['v:true', '1', '"1"', '"1foo"']
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
58endfunc
59
60" Test using TRUE or FALSE values for an argument.
61func 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 Moolenaar6bb45012016-07-07 15:11:19 +020090
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 Moolenaar05e418d2016-07-07 16:35:16 +020096 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 Moolenaar6bb45012016-07-07 15:11:19 +0200102 call Try_arg_true_false('maparg("asdf", "i", %v%)', "", "asdff")
Bram Moolenaar05e418d2016-07-07 16:35:16 +0200103 call Try_arg_true_false('FilterMapArg(maparg("asdf", "i", 1, %v%))', "asdff", {'rhs': 'asdff'})
Bram Moolenaar6bb45012016-07-07 15:11:19 +0200104
Bram Moolenaarf9f24ce2019-08-31 21:17:39 +0200105 call Try_arg_true_false('"asdf"->hasmapto("i", %v%)', 0, 1)
Bram Moolenaar6bb45012016-07-07 15:11:19 +0200106
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 Moolenaare381d3d2016-07-07 14:50:41 +0200115endfunc
116
117function Try_arg_non_zero(expr, false_val, true_val)
Bram Moolenaare381d3d2016-07-07 14:50:41 +0200118 for v in ['v:false', '0', '[1]', '{2:3}', '3.4']
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
122 for v in ['v:true', '1', '" "', '"0"']
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
126endfunc
127
128
129" Test using non-zero-arg for an argument.
130func 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
138 for v in [v:false, 0, [1], {2:3}, 3.4]
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
145 for v in [v:true, 1, " ", "0"]
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
152endfunc
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200153
154" vim: shiftwidth=2 sts=2 expandtab