assert_spec.lua (5428B)
1 local t = require('test.testutil') 2 local n = require('test.functional.testnvim')() 3 4 local api, call = n.api, n.call 5 local clear, eq = n.clear, t.eq 6 local source, command = n.source, n.command 7 local exc_exec = n.exc_exec 8 local eval = n.eval 9 10 local function expected_errors(errors) 11 eq(errors, api.nvim_get_vvar('errors')) 12 end 13 14 local function expected_empty() 15 eq({}, api.nvim_get_vvar('errors')) 16 end 17 18 describe('assert function:', function() 19 before_each(function() 20 clear() 21 end) 22 23 -- assert_equal({expected}, {actual}, [, {msg}]) 24 describe('assert_equal', function() 25 it('should not change v:errors when expected is equal to actual', function() 26 source([[ 27 fu Func() 28 endfu 29 let F1 = function('Func') 30 let F2 = function('Func') 31 call assert_equal(F1, F2) 32 ]]) 33 expected_empty() 34 end) 35 36 it('should not change v:errors when expected is equal to actual', function() 37 eq(0, call('assert_equal', '', '')) 38 eq(0, call('assert_equal', 'string', 'string')) 39 expected_empty() 40 end) 41 42 it('should change v:errors when expected is not equal to actual', function() 43 eq(1, call('assert_equal', 0, { 0 })) 44 expected_errors({ 'Expected 0 but got [0]' }) 45 end) 46 47 it('should change v:errors when expected is not equal to actual', function() 48 eq(1, call('assert_equal', 0, '0')) 49 expected_errors({ "Expected 0 but got '0'" }) 50 end) 51 52 it('should change v:errors when expected is not equal to actual', function() 53 -- Lua does not tell integer from float. 54 command('call assert_equal(1, 1.0)') 55 expected_errors({ 'Expected 1 but got 1.0' }) 56 end) 57 58 it('should change v:errors when expected is not equal to actual', function() 59 call('assert_equal', 'true', 'false') 60 expected_errors({ "Expected 'true' but got 'false'" }) 61 end) 62 63 it('should change v:errors when expected is not equal to actual', function() 64 source([[ 65 function CheckAssert() 66 let s:v = {} 67 let s:x = {"a": s:v} 68 let s:v["b"] = s:x 69 let s:w = {"c": s:x, "d": ''} 70 call assert_equal(s:w, '') 71 endfunction 72 ]]) 73 eq( 74 'Vim(call):E724: unable to correctly dump variable with self-referencing container', 75 exc_exec('call CheckAssert()') 76 ) 77 end) 78 end) 79 80 -- assert_false({actual}, [, {msg}]) 81 describe('assert_false', function() 82 it('should not change v:errors when actual is false', function() 83 eq(0, call('assert_false', 0)) 84 eq(0, call('assert_false', false)) 85 expected_empty() 86 end) 87 88 it('should change v:errors when actual is not false', function() 89 eq(1, call('assert_false', 1)) 90 expected_errors({ 'Expected False but got 1' }) 91 end) 92 93 it('should change v:errors when actual is not false', function() 94 call('assert_false', {}) 95 expected_errors({ 'Expected False but got []' }) 96 end) 97 end) 98 99 -- assert_true({actual}, [, {msg}]) 100 describe('assert_true', function() 101 it('should not change v:errors when actual is true', function() 102 eq(0, call('assert_true', 1)) 103 eq(0, call('assert_true', -1)) -- In Vim script, non-zero Numbers are TRUE. 104 eq(0, call('assert_true', true)) 105 expected_empty() 106 end) 107 108 it('should change v:errors when actual is not true', function() 109 eq(1, call('assert_true', 1.5)) 110 expected_errors({ 'Expected True but got 1.5' }) 111 end) 112 end) 113 114 describe('v:errors', function() 115 it('should be initialized at startup', function() 116 expected_empty() 117 end) 118 119 it('should have function names and relative line numbers', function() 120 source([[ 121 fu Func_one() 122 call assert_equal([0], {'0' : 0}) 123 call assert_false('False') 124 call assert_true("True") 125 endfu 126 fu Func_two() 127 " for shifting a line number 128 call assert_true('line two') 129 endfu 130 ]]) 131 call('Func_one') 132 call('Func_two') 133 expected_errors({ 134 "function Func_one line 1: Expected [0] but got {'0': 0}", 135 "function Func_one line 2: Expected False but got 'False'", 136 "function Func_one line 3: Expected True but got 'True'", 137 "function Func_two line 2: Expected True but got 'line two'", 138 }) 139 end) 140 141 it('should have file names and passed messages', function() 142 source([[ 143 call assert_equal(1, 100, 'equal assertion failed') 144 call assert_false('true', 'true assertion failed') 145 call assert_true('false', 'false assertion failed') 146 ]]) 147 source([[ 148 call assert_true('', 'file two') 149 ]]) 150 expected_errors({ 151 'nvim_exec2() line 1: equal assertion failed: Expected 1 but got 100', 152 "nvim_exec2() line 2: true assertion failed: Expected False but got 'true'", 153 "nvim_exec2() line 3: false assertion failed: Expected True but got 'false'", 154 "nvim_exec2() line 1: file two: Expected True but got ''", 155 }) 156 end) 157 end) 158 159 -- assert_fails({cmd}, [, {error}]) 160 describe('assert_fails', function() 161 it('should not change v:errors when cmd errors', function() 162 eq(0, eval([[assert_fails('NonexistentCmd')]])) 163 expected_empty() 164 end) 165 166 it('should change v:errors when cmd succeeds', function() 167 eq(1, eval([[assert_fails('call empty("")', '')]])) 168 expected_errors({ 'command did not fail: call empty("")' }) 169 end) 170 end) 171 end)