robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
dec3d8215d
master
1local t =require ( 'test.testutil' ) 2 3local describe , it =t . describe , t . it 4local eq =t . eq 5local pcall_err =t . pcall_err 6 7describe ( 'vim.text' , function () 8describe ( 'indent()' , function () 9it ( 'validation' , function () 10t . matches ( 'size%: expected number, got string' , t . pcall_err ( vim . text . indent , 'x' , 'x' )) 11t . matches ( 'size%: expected number, got nil' , t . pcall_err ( vim . text . indent , nil , 'x' )) 12t . matches ( 'opts%: expected table, got string' , t . pcall_err ( vim . text . indent , 0 , 'x' , 'z' )) 13end ) 14 15it ( 'basic cases' , function () 16-- Basic cases. 17eq ( { '' , 0 } , { vim . text . indent ( 0 , '' ) } ) 18eq ( { '' , 0 } , { vim . text . indent ( 2 , '' ) } ) 19eq ( { ' a' , 4 } , { vim . text . indent ( 2 , ' a' ) } ) 20eq ( { ' a\n b' , 4 } , { vim . text . indent ( 2 , ' a\n b' ) } ) 21eq ( { '\t\ta' , 1 } , { vim . text . indent ( 2 , '\ta' ) } ) 22eq ( { ' a\n\n' , 5 } , { vim . text . indent ( 1 , ' a\n\n' ) } ) 23-- Indent 1 (tab) => 0. Starting with empty + blank lines. 24eq ( { '\n\naa a aa' , 1 } , { vim . text . indent ( 0 , '\n \n aa a aa' ) } ) 25-- Indent 1 (tab) => 2 (tabs). Starting with empty + blank lines, 1-tab indent. 26eq ( { '\n\t\t\n\t\taa a aa' , 1 } , { vim . text . indent ( 2 , '\n\t\n\taa a aa' ) } ) 27 28-- Indent 4 => 2, expandtab=false preserves tabs after the common indent. 29eq ( 30{ ' foo\n bar\n \tbaz\n' , 4 } , 31{ vim . text . indent ( 2 , ' foo\n bar\n \tbaz\n' ) } 32) 33-- Indent 9 => 3, expandtab=true. 34eq ( 35{ ' foo\n\n bar \t baz\n' , 9 } , 36{ vim . text . indent ( 3 , '\t foo\n\n bar \t baz\n' , { expandtab =8 } ) } 37) 38-- Indent 9 => 8, expandtab=true. 39eq ( 40{ ' foo\n\n bar\n' , 9 } , 41{ vim . text . indent ( 8 , '\t foo\n\n bar\n' , { expandtab =8 } ) } 42) 43-- Dedent: 5 => 0. 44eq ( { ' foo\n\nbar\n' , 5 } , { vim . text . indent ( 0 , ' foo\n\n bar\n' ) } ) 45-- Dedent: 1 => 0. Empty lines are ignored when deciding "common indent". 46eq ( 47{ ' \n \nfoo\n\nbar\nbaz\n \n' , 1 } , 48{ vim . text . indent ( 0 , ' \n \n foo\n\n bar\n baz\n \n' ) } 49) 50-- Tabs expand when old indent matches new indent #40932. 51eq ( { ' a' , 2 } , { vim . text . indent ( 2 , '\t a' , { expandtab =1 } ) } ) 52end ) 53 54it ( 'real-world cases' , function () 55-- Dedent. 56eq ( { 57[[ 58bufs: 59nvim args: 3 60lua args: { 61[0] = "foo.lua" 62} 63]] , 6410 , 65} , { 66vim . text . indent ( 670 , 68[[ 69bufs: 70nvim args: 3 71lua args: { 72[0] = "foo.lua" 73} 74]] 75), 76} ) 77 78-- Indent 0 => 2. 79eq ( { 80[[ 81# yay 82 83local function foo() 84if true then 85# yay 86end 87end 88 89return 90]] , 910 , 92} , { 93vim . text . indent ( 942 , 95[[ 96# yay 97 98local function foo() 99if true then 100# yay 101end 102end 103 104return 105]] 106), 107} ) 108 109-- 1-tab indent, last line spaces < tabsize. 110-- Preserves tab char immediately following the indent. 111eq ( { 'text\n\tmatch\nmatch\ntext\n' , 1 } , { 112vim . text . indent ( 0 , (( [[ 113text 114match 115match 116text 117]] ): gsub ( '\n%s-([\n]?)$' , '\n%1' ))), 118} ) 119 120-- 1-tab indent, last line spaces=tabsize. 121eq ( { 'text\n match\nmatch\ntext\n' , 6 } , { 122vim . text . indent ( 1230 , 124[[ 125text 126match 127match 128text 129]] , 130{ expandtab =6 } 131), 132} ) 133end ) 134end ) 135 136describe ( 'hexencode(), hexdecode()' , function () 137it ( 'works' , function () 138local cases ={ 139{ 'Hello world!' , '48656C6C6F20776F726C6421' } , 140{ '😂' , 'F09F9882' } , 141} 142 143for _ , v in ipairs ( cases ) do 144local input , output =unpack ( v ) 145eq ( output , vim . text . hexencode ( input )) 146eq ( input , vim . text . hexdecode ( output )) 147end 148end ) 149 150it ( 'with very large strings' , function () 151local input , output =string . rep ( '😂' , 2 ^ 16 ), string . rep ( 'F09F9882' , 2 ^ 16 ) 152eq ( output , vim . text . hexencode ( input )) 153eq ( input , vim . text . hexdecode ( output )) 154end ) 155 156it ( 'invalid input' , function () 157-- Odd number of hex characters 158do 159local res , err =vim . text . hexdecode ( 'ABC' ) 160eq ( nil , res ) 161eq ( 'string must have an even number of hex characters' , err ) 162end 163 164-- Non-hexadecimal input 165do 166local res , err =vim . text . hexdecode ( 'nothex' ) 167eq ( nil , res ) 168eq ( 'string must contain only hex characters' , err ) 169end 170end ) 171end ) 172end )