robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local describe , it , before_each =t . describe , t . it , t . before_each 5local clear =n . clear 6local exec_lua =n . exec_lua 7local eq =t . eq 8local matches =t . matches 9local pcall_err =t . pcall_err 10 11describe ( 'xdiff bindings' , function () 12before_each ( function () 13clear () 14end ) 15 16describe ( 'can diff text' , function () 17local a1 ='Hello\n' 18local b1 ='Helli\n' 19 20local a2 ='Hello\nbye\nfoo\n' 21local b2 ='Helli\nbye\nbar\nbaz\n' 22 23it ( 'with no callback' , function () 24eq ( 25table . concat ( { 26'@@ -1 +1 @@' , 27'-Hello' , 28'+Helli' , 29'' , 30} , '\n' ), 31exec_lua ( function () 32return vim . text . diff ( a1 , b1 ) 33end ) 34) 35 36eq ( 37table . concat ( { 38'@@ -1 +1 @@' , 39'-Hello' , 40'+Helli' , 41'@@ -3 +3,2 @@' , 42'-foo' , 43'+bar' , 44'+baz' , 45'' , 46} , '\n' ), 47exec_lua ( function () 48return vim . text . diff ( a2 , b2 ) 49end ) 50) 51end ) 52 53it ( 'with callback' , function () 54eq ( 55{ { 1 , 1 , 1 , 1 } } , 56exec_lua ( function () 57local exp ={} --- @type table[] 58assert ( vim . text . diff ( a1 , b1 , { 59on_hunk =function ( ... ) 60exp [ # exp + 1 ] ={ ... } 61end , 62} ) == nil ) 63return exp 64end ) 65) 66 67eq ( 68{ { 1 , 1 , 1 , 1 } , { 3 , 1 , 3 , 2 } } , 69exec_lua ( function () 70local exp ={} --- @type table[] 71assert ( vim . text . diff ( a2 , b2 , { 72on_hunk =function ( ... ) 73exp [ # exp + 1 ] ={ ... } 74end , 75} ) == nil ) 76return exp 77end ) 78) 79 80-- gives higher precedence to on_hunk over result_type 81eq ( 82{ { 1 , 1 , 1 , 1 } , { 3 , 1 , 3 , 2 } } , 83exec_lua ( function () 84local exp ={} --- @type table[] 85assert ( vim . text . diff ( a2 , b2 , { 86on_hunk =function ( ... ) 87exp [ # exp + 1 ] ={ ... } 88end , 89result_type ='indices' , 90} ) == nil ) 91return exp 92end ) 93) 94end ) 95 96it ( 'with error callback' , function () 97t . matches ( 98[[on_hunk: %.%.%./xdiff_spec%.lua%:0%: ERROR1]] , 99pcall_err ( exec_lua , function () 100vim . text . diff ( a1 , b1 , { 101on_hunk =function () 102error ( 'ERROR1' ) 103end , 104} ) 105end ) 106) 107end ) 108 109it ( 'with hunk_lines' , function () 110eq ( 111{ { 1 , 1 , 1 , 1 } } , 112exec_lua ( function () 113return vim . text . diff ( a1 , b1 , { result_type ='indices' } ) 114end ) 115) 116 117eq ( 118{ { 1 , 1 , 1 , 1 } , { 3 , 1 , 3 , 2 } } , 119exec_lua ( function () 120return vim . text . diff ( a2 , b2 , { result_type ='indices' } ) 121end ) 122) 123end ) 124 125it ( 'can run different algorithms' , function () 126local a =table . concat ( { 127'.foo1 {' , 128' margin: 0;' , 129'}' , 130'' , 131'.bar {' , 132' margin: 0;' , 133'}' , 134'' , 135} , '\n' ) 136 137local b =table . concat ( { 138'.bar {' , 139' margin: 0;' , 140'}' , 141'' , 142'.foo1 {' , 143' margin: 0;' , 144' color: green;' , 145'}' , 146'' , 147} , '\n' ) 148 149eq ( 150table . concat ( { 151'@@ -1,4 +0,0 @@' , 152'-.foo1 {' , 153'- margin: 0;' , 154'-}' , 155'-' , 156'@@ -7,0 +4,5 @@' , 157'+' , 158'+.foo1 {' , 159'+ margin: 0;' , 160'+ color: green;' , 161'+}' , 162'' , 163} , '\n' ), 164exec_lua ( function () 165return vim . text . diff ( a , b , { 166algorithm ='patience' , 167} ) 168end ) 169) 170end ) 171end ) 172 173it ( 'can handle bad args' , function () 174matches ( [[Expected at least 2 arguments$]] , pcall_err ( exec_lua , [[vim.text.diff('a')]] )) 175 176matches ( 177[[bad argument %#1 to '_?diff' %(expected string%)]] , 178pcall_err ( exec_lua , [[vim.text.diff(1, 2)]] ) 179) 180 181matches ( 182[[bad argument %#3 to '_?diff' %(expected table%)]] , 183pcall_err ( exec_lua , [[vim.text.diff('a', 'b', true)]] ) 184) 185 186matches ( 187[[invalid key: bad_key$]] , 188pcall_err ( exec_lua , [[vim.text.diff('a', 'b', { bad_key = true })]] ) 189) 190 191matches ( 192[[on_hunk is not a function$]] , 193pcall_err ( exec_lua , [[vim.text.diff('a', 'b', { on_hunk = true })]] ) 194) 195end ) 196 197it ( 'can handle strings with embedded NUL characters (GitHub #30305)' , function () 198eq ( 199{ { 0 , 0 , 1 , 1 } , { 1 , 0 , 3 , 2 } } , 200exec_lua ( function () 201return vim . text . diff ( '\n' , '\0\n\n\nb' , { linematch =true , result_type ='indices' } ) 202end ) 203) 204end ) 205end )