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