blob: 90b4f5e37a5c8f6beea11eca9cbe4f434e3a0943 [file] [log] [blame]
Bram Moolenaare381d3d2016-07-07 14:50:41 +02001" Test behavior of boolean-like values.
2
3" Test what is explained at ":help TRUE" and ":help FALSE".
4func 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')
45endfunc
46
47function 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
56endfunc
57
58" Test using TRUE or FALSE values for an argument.
59func 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
88endfunc
89
90function Try_arg_non_zero(expr, false_val, true_val)
91 for v in ['v:false', '0', '[1]', '{2:3}', '3.4']
92 let r = eval(substitute(a:expr, '%v%', v, ''))
93 call assert_equal(a:false_val, r, 'result for ' . v . ' is not ' . a:false_val . ' but ' . r)
94 endfor
95 for v in ['v:true', '1', '" "', '"0"']
96 let r = eval(substitute(a:expr, '%v%', v, ''))
97 call assert_equal(a:true_val, r, 'result for ' . v . ' is not ' . a:true_val . ' but ' . r)
98 endfor
99endfunc
100
101
102" Test using non-zero-arg for an argument.
103func Test_non_zero_arg()
104 call test_settime(93784)
105 call Try_arg_non_zero("mode(%v%)", 'x', 'x!')
106 call test_settime(0)
107
108 call Try_arg_non_zero("shellescape('foo%', %v%)", "'foo%'", "'foo\\%'")
109
110 " visualmode() needs to be called twice to check
111 for v in [v:false, 0, [1], {2:3}, 3.4]
112 normal vv
113 let r = visualmode(v)
114 call assert_equal('v', r, 'result for ' . string(v) . ' is not "v" but ' . r)
115 let r = visualmode(v)
116 call assert_equal('v', r, 'result for ' . string(v) . ' is not "v" but ' . r)
117 endfor
118 for v in [v:true, 1, " ", "0"]
119 normal vv
120 let r = visualmode(v)
121 call assert_equal('v', r, 'result for ' . v . ' is not "v" but ' . r)
122 let r = visualmode(v)
123 call assert_equal('', r, 'result for ' . v . ' is not "" but ' . r)
124 endfor
125endfunc