blob: 8db4d8e5f35fb9cd1ab56b596f6dc439e50244b9 [file] [log] [blame]
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01001" Tests for expressions.
2
3func Test_equal()
4 let base = {}
5 func base.method()
6 return 1
7 endfunc
8 func base.other() dict
9 return 1
10 endfunc
11 let instance = copy(base)
12 call assert_true(base.method == instance.method)
13 call assert_true([base.method] == [instance.method])
14 call assert_true(base.other == instance.other)
15 call assert_true([base.other] == [instance.other])
16
17 call assert_false(base.method == base.other)
18 call assert_false([base.method] == [base.other])
19 call assert_false(base.method == instance.other)
20 call assert_false([base.method] == [instance.other])
21
22 call assert_fails('echo base.method > instance.method')
23endfunc
Bram Moolenaar819821c2016-03-26 21:24:14 +010024
25func Test_version()
26 call assert_true(has('patch-7.4.001'))
27 call assert_true(has('patch-7.4.01'))
28 call assert_true(has('patch-7.4.1'))
29 call assert_true(has('patch-6.9.999'))
30 call assert_true(has('patch-7.1.999'))
31 call assert_true(has('patch-7.4.123'))
32
33 call assert_false(has('patch-7'))
34 call assert_false(has('patch-7.4'))
35 call assert_false(has('patch-7.4.'))
36 call assert_false(has('patch-9.1.0'))
37 call assert_false(has('patch-9.9.1'))
38endfunc
Bram Moolenaar0921ecf2016-04-03 22:44:36 +020039
40func Test_dict()
41 let d = {'': 'empty', 'a': 'a', 0: 'zero'}
42 call assert_equal('empty', d[''])
43 call assert_equal('a', d['a'])
44 call assert_equal('zero', d[0])
45 call assert_true(has_key(d, ''))
46 call assert_true(has_key(d, 'a'))
47
48 let d[''] = 'none'
49 let d['a'] = 'aaa'
50 call assert_equal('none', d[''])
51 call assert_equal('aaa', d['a'])
52endfunc
Bram Moolenaar58de0e22016-04-14 15:13:46 +020053
54func Test_strgetchar()
55 call assert_equal(char2nr('a'), strgetchar('axb', 0))
56 call assert_equal(char2nr('x'), strgetchar('axb', 1))
57 call assert_equal(char2nr('b'), strgetchar('axb', 2))
58
59 call assert_equal(-1, strgetchar('axb', -1))
60 call assert_equal(-1, strgetchar('axb', 3))
61 call assert_equal(-1, strgetchar('', 0))
Bram Moolenaar58de0e22016-04-14 15:13:46 +020062endfunc
63
64func Test_strcharpart()
65 call assert_equal('a', strcharpart('axb', 0, 1))
66 call assert_equal('x', strcharpart('axb', 1, 1))
67 call assert_equal('b', strcharpart('axb', 2, 1))
68 call assert_equal('xb', strcharpart('axb', 1))
69
70 call assert_equal('', strcharpart('axb', 1, 0))
71 call assert_equal('', strcharpart('axb', 1, -1))
72 call assert_equal('', strcharpart('axb', -1, 1))
73 call assert_equal('', strcharpart('axb', -2, 2))
74
75 call assert_equal('a', strcharpart('axb', -1, 2))
Bram Moolenaar58de0e22016-04-14 15:13:46 +020076endfunc
Bram Moolenaar517ffbe2016-04-20 14:59:29 +020077
78func Test_getreg_empty_list()
79 call assert_equal('', getreg('x'))
80 call assert_equal([], getreg('x', 1, 1))
81 let x = getreg('x', 1, 1)
82 let y = x
83 call add(x, 'foo')
84 call assert_equal(['foo'], y)
85endfunc
Bram Moolenaard8585ed2016-05-01 23:05:53 +020086
87func Test_loop_over_null_list()
Bram Moolenaar574860b2016-05-24 17:33:34 +020088 let null_list = test_null_list()
Bram Moolenaard8585ed2016-05-01 23:05:53 +020089 for i in null_list
90 call assert_true(0, 'should not get here')
91 endfor
92endfunc
Bram Moolenaar13ddc5c2016-05-25 22:51:17 +020093
94func Test_compare_null_dict()
95 call assert_fails('let x = test_null_dict()[10]')
96 call assert_equal({}, {})
97 call assert_equal(test_null_dict(), test_null_dict())
98 call assert_notequal({}, test_null_dict())
99endfunc
100
101func Test_set_reg_null_list()
102 call setreg('x', test_null_list())
103endfunc
Bram Moolenaar1d90a5a2016-07-01 11:59:47 +0200104
105func Test_special_char()
106 " The failure is only visible using valgrind.
107 call assert_fails('echo "\<C-">')
108endfunc
Bram Moolenaar2acfbed2016-07-01 23:14:02 +0200109
110func Test_option_value()
111 " boolean
112 set bri
113 call assert_equal(1, &bri)
114 set nobri
115 call assert_equal(0, &bri)
116
117 " number
118 set ts=1
119 call assert_equal(1, &ts)
120 set ts=8
121 call assert_equal(8, &ts)
122
123 " string
124 exe "set cedit=\<Esc>"
125 call assert_equal("\<Esc>", &cedit)
126 set cpo=
127 call assert_equal("", &cpo)
128 set cpo=abcdefgi
129 call assert_equal("abcdefgi", &cpo)
130 set cpo&vim
131endfunc
Bram Moolenaar38ee6b02016-07-12 21:11:33 +0200132
133function Test_printf_64bit()
134 if has('num64')
135 call assert_equal("123456789012345", printf('%d', 123456789012345))
136 endif
137endfunc
Bram Moolenaar72ab7292016-07-19 19:10:51 +0200138
139func Test_substitute_expr()
140 let g:val = 'XXX'
141 call assert_equal('XXX', substitute('yyy', 'y*', '\=g:val', ''))
142 call assert_equal('XXX', substitute('yyy', 'y*', {-> g:val}, ''))
143 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
144 \ '\=nr2char("0x" . submatch(1))', 'g'))
145 call assert_equal("-\u1b \uf2-", substitute("-%1b %f2-", '%\(\x\x\)',
146 \ {-> nr2char("0x" . submatch(1))}, 'g'))
147
148 call assert_equal('231', substitute('123', '\(.\)\(.\)\(.\)',
149 \ {-> submatch(2) . submatch(3) . submatch(1)}, ''))
150
151 func Recurse()
152 return substitute('yyy', 'y*', {-> g:val}, '')
153 endfunc
154 call assert_equal('--', substitute('xxx', 'x*', {-> '-' . Recurse() . '-'}, ''))
155endfunc
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200156
157func Test_substitute_expr_arg()
158 call assert_equal('123456789-123456789=', substitute('123456789',
159 \ '\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)',
160 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
161
162 call assert_equal('123456-123456=789', substitute('123456789',
163 \ '\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)',
164 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
165
166 call assert_equal('123456789-123456789x=', substitute('123456789',
167 \ '\(.\)\(.\)\(.*\)',
168 \ {m -> m[0] . '-' . m[1] . m[2] . m[3] . 'x' . m[4] . m[5] . m[6] . m[7] . m[8] . m[9] . '='}, ''))
169
170 call assert_fails("call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')", 'E742:')
171 call assert_fails("call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')", 'E742:')
172 call assert_fails("call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')", 'E742:')
173 call assert_fails("call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')", 'E742:')
174endfunc