robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1-- Test for *sub-replace-special* and *sub-replace-expression* on substitute(). 2-- Test for submatch() on substitute(). 3-- Test for *:s%* on :substitute. 4 5local t =require ( 'test.testutil' ) 6local n =require ( 'test.functional.testnvim' )() 7local Screen =require ( 'test.functional.ui.screen' ) 8 9local describe , it , before_each =t . describe , t . it , t . before_each 10local feed , insert =n . feed , n . insert 11local exec =n . exec 12local clear , feed_command , expect =n . clear , n . feed_command , n . expect 13local eq , eval =t . eq , n . eval 14 15describe ( 'substitute()' , function () 16before_each ( clear ) 17 18-- The original test contained several TEST_X lines to delimit different 19-- parts. These where used to split the test into different it() blocks. 20-- The TEST_X strings are repeated in the description of the blocks to make 21-- it easier to incorporate upstream changes. 22 23local function test_1_and_2 () 24eq ( 'AA' , eval ( "substitute('A', 'A', '&&', '')" )) 25eq ( '&' , eval ( [[substitute('B', 'B', '\&', '')]] )) 26eq ( 27'C123456789987654321' , 28eval ( 29[[substitute('C123456789', ]] 30.. [['C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]] 31.. [['\0\9\8\7\6\5\4\3\2\1', '')]] 32) 33) 34eq ( 'd' , eval ( "substitute('D', 'D', 'd', '')" )) 35eq ( '~' , eval ( "substitute('E', 'E', '~', '')" )) 36eq ( '~' , eval ( [[substitute('F', 'F', '\~', '')]] )) 37eq ( 'Gg' , eval ( [[substitute('G', 'G', '\ugg', '')]] )) 38eq ( 'Hh' , eval ( [[substitute('H', 'H', '\Uh\Eh', '')]] )) 39eq ( 'iI' , eval ( [[substitute('I', 'I', '\lII', '')]] )) 40eq ( 'jJ' , eval ( [[substitute('J', 'J', '\LJ\EJ', '')]] )) 41eq ( 'Kk' , eval ( [[substitute('K', 'K', '\Uk\ek', '')]] )) 42eq ( 'l\rl' , eval ( "substitute('lLl', 'L', '\r', '')" )) 43eq ( 'm\rm' , eval ( [[substitute('mMm', 'M', '\r', '')]] )) 44eq ( 'n\rn' , eval ( "substitute('nNn', 'N', '\\\r', '')" )) 45eq ( 'o\no' , eval ( [[substitute('oOo', 'O', '\n', '')]] )) 46eq ( 'p\bp' , eval ( [[substitute('pPp', 'P', '\b', '')]] )) 47eq ( 'q\tq' , eval ( [[substitute('qQq', 'Q', '\t', '')]] )) 48eq ( 'r\\r' , eval ( [[substitute('rRr', 'R', '\\', '')]] )) 49eq ( 'scs' , eval ( [[substitute('sSs', 'S', '\c', '')]] )) 50eq ( 't\rt' , eval ( [[substitute('tTt', 'T', "\r", '')]] )) 51eq ( 'u\nu' , eval ( [[substitute('uUu', 'U', "\n", '')]] )) 52eq ( 'v\bv' , eval ( [[substitute('vVv', 'V', "\b", '')]] )) 53eq ( 'w\\w' , eval ( [[substitute('wWw', 'W', "\\", '')]] )) 54eq ( 'XxxX' , eval ( [[substitute('X', 'X', '\L\uxXx\l\EX', '')]] )) 55eq ( 'yYYy' , eval ( [[substitute('Y', 'Y', '\U\lYyY\u\Ey', '')]] )) 56end 57 58it ( 'with "set magic" (TEST_1)' , function () 59feed_command ( 'set magic' ) 60test_1_and_2 () 61end ) 62 63it ( 'with "set nomagic" (TEST_2)' , function () 64feed_command ( 'set nomagic' ) 65test_1_and_2 () 66end ) 67 68it ( 'with sub-replace-expression (TEST_3)' , function () 69feed_command ( 'set magic&' ) 70eq ( 'a\\a' , eval ( [[substitute('aAa', 'A', '\="\\"', '')]] )) 71eq ( 'b\\\\b' , eval ( [[substitute('bBb', 'B', '\="\\\\"', '')]] )) 72eq ( 'c\rc' , eval ( [[substitute('cCc', 'C', '\="]] .. '\r' .. [["', '')]] )) 73eq ( 'd\\\rd' , eval ( [[substitute('dDd', 'D', '\="\\]] .. '\r' .. [["', '')]] )) 74eq ( 'e\\\\\re' , eval ( [[substitute('eEe', 'E', '\="\\\\]] .. '\r' .. [["', '')]] )) 75eq ( 'f\\rf' , eval ( [[substitute('fFf', 'F', '\="\\r"', '')]] )) 76eq ( 'j\\nj' , eval ( [[substitute('jJj', 'J', '\="\\n"', '')]] )) 77eq ( 'k\rk' , eval ( [[substitute('kKk', 'K', '\="\r"', '')]] )) 78eq ( 'l\nl' , eval ( [[substitute('lLl', 'L', '\="\n"', '')]] )) 79end ) 80 81it ( 'with submatch() (TEST_4)' , function () 82feed_command ( 'set magic&' ) 83eq ( 84'a\\a' , 85eval ( [[substitute('aAa', 'A', ]] .. [['\=substitute(submatch(0), ".", "\\", "")', '')]] ) 86) 87eq ( 88'b\\b' , 89eval ( [[substitute('bBb', 'B', ]] .. [['\=substitute(submatch(0), ".", "\\\\", "")', '')]] ) 90) 91eq ( 92'c\rc' , 93eval ( 94[[substitute('cCc', 'C', ]] 95.. [['\=substitute(submatch(0), ".", "]] 96.. '\r' 97.. [[", "")', '')]] 98) 99) 100eq ( 101'd\rd' , 102eval ( 103[[substitute('dDd', 'D', ]] 104.. [['\=substitute(submatch(0), ".", "\\]] 105.. '\r' 106.. [[", "")', '')]] 107) 108) 109eq ( 110'e\\\re' , 111eval ( 112[[substitute('eEe', 'E', ]] 113.. [['\=substitute(submatch(0), ".", "\\\\]] 114.. '\r' 115.. [[", "")', '')]] 116) 117) 118eq ( 119'f\rf' , 120eval ( [[substitute('fFf', 'F', ]] .. [['\=substitute(submatch(0), ".", "\\r", "")', '')]] ) 121) 122eq ( 123'j\nj' , 124eval ( [[substitute('jJj', 'J', ]] .. [['\=substitute(submatch(0), ".", "\\n", "")', '')]] ) 125) 126eq ( 127'k\rk' , 128eval ( [[substitute('kKk', 'K', ]] .. [['\=substitute(submatch(0), ".", "\r", "")', '')]] ) 129) 130eq ( 131'l\nl' , 132eval ( [[substitute('lLl', 'L', ]] .. [['\=substitute(submatch(0), ".", "\n", "")', '')]] ) 133) 134end ) 135 136it ( 'with submatch() (TEST_5)' , function () 137feed_command ( 'set magic&' ) 138eq ( 139'A123456789987654321' , 140eval ( 141[[substitute('A123456789', ]] 142.. [['A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]] 143.. [['\=submatch(0) . submatch(9) . submatch(8) . submatch(7) . ]] 144.. [[submatch(6) . submatch(5) . submatch(4) . submatch(3) . ]] 145.. [[submatch(2) . submatch(1)', '')]] 146) 147) 148eq ( 149"[['A123456789'], ['9'], ['8'], ['7'], ['6'], ['5'], ['4'], ['3'], " .. "['2'], ['1']]" , 150eval ( 151[[substitute('A123456789', ]] 152.. [['A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)', ]] 153.. [['\=string([submatch(0, 1), submatch(9, 1), submatch(8, 1), ]] 154.. [[submatch(7, 1), submatch(6, 1), submatch(5, 1), submatch(4, 1), ]] 155.. [[submatch(3, 1), submatch(2, 1), submatch(1, 1)])', '')]] 156) 157) 158end ) 159 160-- TEST_6 was about the 'cpoptions' flag / which was removed in pull request 161-- #2943. 162 163it ( 'with submatch or \\ze (TEST_7)' , function () 164feed_command ( 'set magic&' ) 165eq ( 'A\rA' , eval ( "substitute('A\rA', 'A.', '\\=submatch(0)', '')" )) 166eq ( 'B\nB' , eval ( [[substitute("B\nB", 'B.', '\=submatch(0)', '')]] )) 167eq ( "['B\n']B" , eval ( [[substitute("B\nB", 'B.', '\=string(submatch(0, 1))', '')]] )) 168eq ( '-abab' , eval ( [[substitute('-bb', '\zeb', 'a', 'g')]] )) 169eq ( 'c-cbcbc' , eval ( [[substitute('-bb', '\ze', 'c', 'g')]] )) 170end ) 171 172it ( 'with \\zs and \\ze (TEST_10)' , function () 173feed_command ( 'set magic&' ) 174eq ( 'a1a2a3a' , eval ( [[substitute('123', '\zs', 'a', 'g')]] )) 175eq ( 'aaa' , eval ( [[substitute('123', '\zs.', 'a', 'g')]] )) 176eq ( '1a2a3a' , eval ( [[substitute('123', '.\zs', 'a', 'g')]] )) 177eq ( 'a1a2a3a' , eval ( [[substitute('123', '\ze', 'a', 'g')]] )) 178eq ( 'a1a2a3' , eval ( [[substitute('123', '\ze.', 'a', 'g')]] )) 179eq ( 'aaa' , eval ( [[substitute('123', '.\ze', 'a', 'g')]] )) 180eq ( 'aa2a3a' , eval ( [[substitute('123', '1\|\ze', 'a', 'g')]] )) 181eq ( '1aaa' , eval ( [[substitute('123', '1\zs\|[23]', 'a', 'g')]] )) 182end ) 183end ) 184 185describe ( ':substitute' , function () 186before_each ( clear ) 187 188it ( 'with \\ze and \\zs and confirmation dialog (TEST_8)' , function () 189insert ( [[ 190,,X 191,,Y 192,,Z]] ) 193feed_command ( 'set magic&' ) 194feed_command ( [[1s/\(^\|,\)\ze\(,\|X\)/\1N/g]] ) 195feed_command ( [[2s/\(^\|,\)\ze\(,\|Y\)/\1N/gc]] ) 196feed ( 'a' ) -- For the dialog of the previous :s command. 197feed_command ( [[3s/\(^\|,\)\ze\(,\|Z\)/\1N/gc]] ) 198feed ( 'yy' ) -- For the dialog of the previous :s command. 199expect ( [[ 200N,,NX 201N,,NY 202N,,NZ]] ) 203end ) 204 205it ( 'with confirmation dialog (TEST_9)' , function () 206insert ( 'xxx' ) 207feed_command ( 'set magic&' ) 208feed_command ( 's/x/X/gc' ) 209feed ( 'yyq' ) -- For the dialog of the previous :s command. 210expect ( 'XXx' ) 211end ) 212 213it ( 'first char is highlighted with confirmation dialog and empty match' , function () 214local screen =Screen . new ( 60 , 8 ) 215exec ( [[ 216set nohlsearch noincsearch 217call setline(1, ['one', 'two', 'three']) 218]] ) 219feed ( ':%s/^/ /c<CR>' ) 220screen : expect ( [[ 221{2:o}ne | 222two | 223three | 224{1:~ }|*2 225{3: }| 226{6:replace with ? (y)es/(n)o/(a)ll/(q)uit/(l)ast/scroll up(^}| 227{6:E)/down(^Y)}^ | 228]] ) 229end ) 230end )