robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3 4local snippet =require ( 'vim.lsp._snippet_grammar' ) 5local describe , it , before_each =t . describe , t . it , t . before_each 6local type =snippet . NodeType 7 8local eq =t . eq 9local exec_lua =n . exec_lua 10 11describe ( 'vim.lsp._snippet_grammar' , function () 12before_each ( n . clear ) 13 14local parse =function ( ... ) 15local results =exec_lua ( 'return { require("vim.lsp._snippet_grammar").parse(...) }' , ... ) 16eq ( 1 , # results ) 17return results [ 1 ]. data . children 18end 19 20it ( 'parses only text' , function () 21eq ( { 22{ type =type . Text , data ={ text ='TE$}XT' } } , 23} , parse ( 'TE\\$\\}XT' )) 24end ) 25 26it ( 'parses tabstops' , function () 27eq ( { 28{ type =type . Tabstop , data ={ tabstop =1 } } , 29{ type =type . Tabstop , data ={ tabstop =2 } } , 30} , parse ( '$1${2}' )) 31end ) 32 33it ( 'parses nested placeholders' , function () 34eq ( { 35{ 36type =type . Placeholder , 37data ={ 38tabstop =1 , 39value ={ 40type =type . Placeholder , 41data ={ 42tabstop =2 , 43value ={ type =type . Tabstop , data ={ tabstop =3 } } , 44} , 45} , 46} , 47} , 48} , parse ( '${1:${2:${3}}}' )) 49end ) 50 51it ( 'parses variables' , function () 52eq ( { 53{ type =type . Variable , data ={ name ='VAR' } } , 54{ type =type . Variable , data ={ name ='VAR' } } , 55{ 56type =type . Variable , 57data ={ 58name ='VAR' , 59default ={ type =type . Tabstop , data ={ tabstop =1 } } , 60} , 61} , 62{ 63type =type . Variable , 64data ={ 65name ='VAR' , 66regex ='regex' , 67options ='' , 68format ={ 69{ 70type =type . Format , 71data ={ capture =1 , modifier ='upcase' } , 72} , 73} , 74} , 75} , 76} , parse ( '$VAR${VAR}${VAR:$1}${VAR/regex/${1:/upcase}/}' )) 77end ) 78 79it ( 'parses choice' , function () 80eq ( { 81{ 82type =type . Choice , 83data ={ tabstop =1 , values ={ ',' , '|' } } , 84} , 85} , parse ( '${1|\\,,\\||}' )) 86end ) 87 88it ( 'parses format' , function () 89eq ( 90{ 91{ 92type =type . Variable , 93data ={ 94name ='VAR' , 95regex ='regex' , 96options ='' , 97format ={ 98{ 99type =type . Format , 100data ={ capture =1 , modifier ='upcase' } , 101} , 102{ 103type =type . Format , 104data ={ capture =1 , if_text ='if_text' } , 105} , 106{ 107type =type . Format , 108data ={ capture =1 , else_text ='else_text' } , 109} , 110{ 111type =type . Format , 112data ={ capture =1 , if_text ='if_text' , else_text ='else_text' } , 113} , 114{ 115type =type . Format , 116data ={ capture =1 , else_text ='else_text' } , 117} , 118} , 119} , 120} , 121} , 122parse ( 123'${VAR/regex/${1:/upcase}${1:+if_text}${1:-else_text}${1:?if_text:else_text}${1:else_text}/}' 124) 125) 126end ) 127 128it ( 'parses empty strings' , function () 129eq ( { 130{ 131type =type . Placeholder , 132data ={ 133tabstop =1 , 134value ={ type =type . Text , data ={ text ='' } } , 135} , 136} , 137{ 138type =type . Text , 139data ={ text =' ' } , 140} , 141{ 142type =type . Variable , 143data ={ 144name ='VAR' , 145regex ='erg' , 146format ={ 147{ 148type =type . Format , 149data ={ capture =1 , if_text ='' } , 150} , 151} , 152options ='g' , 153} , 154} , 155} , parse ( '${1:} ${VAR/erg/${1:+}/g}' )) 156end ) 157 158it ( 'parses closing curly brace as text' , function () 159eq ( 160{ 161{ type =type . Text , data ={ text ='function ' } } , 162{ type =type . Tabstop , data ={ tabstop =1 } } , 163{ type =type . Text , data ={ text ='() {\n ' } } , 164{ type =type . Tabstop , data ={ tabstop =0 } } , 165{ type =type . Text , data ={ text ='\n}' } } , 166} , 167parse ( table . concat ( { 168'function $1() {' , 169' $0' , 170'}' , 171} , '\n' )) 172) 173end ) 174end )