blob: 86c86c58d5a853fa31f2ecd399a7b5c8fbd50ccd [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