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 pcall_err =t . pcall_err 9local matches =t . matches 10 11describe ( 'vim.base64' , function () 12before_each ( clear ) 13 14local function encode ( s ) 15return exec_lua ( [[return vim.base64.encode(...)]] , s ) 16end 17 18local function decode ( s ) 19return exec_lua ( [[return vim.base64.decode(...)]] , s ) 20end 21 22it ( 'works' , function () 23local values ={ 24'' , 25'Many hands make light work.' , 26[[ 27Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in 28my purse, and nothing particular to interest me on shore, I thought I would sail about a 29little and see the watery part of the world. 30]] , 31[[ 32It is a truth universally acknowledged, that a single man in possession of a good fortune, 33must be in want of a wife. 34]] , 35'Happy families are all alike; every unhappy family is unhappy in its own way.' , 36'ЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюя' , 37'ÅÍÎÏ˝ÓÔÒÚÆ☃' , 38'𐐜 𐐔𐐇𐐝𐐀𐐡𐐇𐐓 𐐙𐐊𐐡𐐝𐐓/𐐝𐐇𐐗𐐊𐐤𐐔 𐐒𐐋𐐗 𐐒𐐌 𐐜 𐐡𐐀𐐖𐐇𐐤𐐓𐐝 𐐱𐑂 𐑄 𐐔𐐇𐐝𐐀𐐡𐐇𐐓 𐐏𐐆𐐅𐐤𐐆𐐚𐐊𐐡𐐝𐐆𐐓𐐆' , 39'👨👩👦 👨👩👧👦 👨👨👦 👩👩👧 👨👦 👨👧👦 👩👦 👩👧👦' , 40'مُنَاقَشَةُ سُبُلِ اِسْتِخْدَامِ اللُّغَةِ فِي النُّظُمِ الْقَائِمَةِ وَفِيم يَخُصَّ التَّطْبِيقَاتُ الْحاسُوبِيَّةُ،' , 41[[ 42Ṱ̺̺̕o͞ ̷i̲̬͇̪͙n̝̗͕v̟̜̘̦͟o̶̙̰̠kè͚̮̺̪̹̱̤ ̖t̝͕̳̣̻̪͞h̼͓̲̦̳̘̲e͇̣̰̦̬͎ ̢̼̻̱̘h͚͎͙̜̣̲ͅi̦̲̣̰̤v̻͍e̺̭̳̪̰-m̢iͅn̖̺̞̲̯̰d̵̼̟͙̩̼̘̳ ̞̥̱̳̭r̛̗̘e͙p͠r̼̞̻̭̗e̺̠̣͟s̘͇̳͍̝͉e͉̥̯̞̲͚̬͜ǹ̬͎͎̟̖͇̤t͍̬̤͓̼̭͘ͅi̪̱n͠g̴͉ ͏͉ͅc̬̟h͡a̫̻̯͘o̫̟̖͍̙̝͉s̗̦̲.̨̹͈̣ 43̡͓̞ͅI̗̘̦͝n͇͇͙v̮̫ok̲̫̙͈i̖͙̭̹̠̞n̡̻̮̣̺g̲͈͙̭͙̬͎ ̰t͔̦h̞̲e̢̤ ͍̬̲͖f̴̘͕̣è͖ẹ̥̩l͖͔͚i͓͚̦͠n͖͍̗͓̳̮g͍ ̨o͚̪͡f̘̣̬ ̖̘͖̟͙̮c҉͔̫͖͓͇͖ͅh̵̤̣͚͔á̗̼͕ͅo̼̣̥s̱͈̺̖̦̻͢.̛̖̞̠̫̰ 44̗̺͖̹̯͓Ṯ̤͍̥͇͈h̲́e͏͓̼̗̙̼̣͔ ͇̜̱̠͓͍ͅN͕͠e̗̱z̘̝̜̺͙p̤̺̹͍̯͚e̠̻̠͜r̨̤͍̺̖͔̖̖d̠̟̭̬̝͟i̦͖̩͓͔̤a̠̗̬͉̙n͚͜ ̻̞̰͚ͅh̵͉i̳̞v̢͇ḙ͎͟-҉̭̩̼͔m̤̭̫i͕͇̝̦n̗͙ḍ̟ ̯̲͕͞ǫ̟̯̰̲͙̻̝f ̪̰̰̗̖̭̘͘c̦͍̲̞͍̩̙ḥ͚a̮͎̟̙͜ơ̩̹͎s̤.̝̝ ҉Z̡̖̜͖̰̣͉̜a͖̰͙̬͡l̲̫̳͍̩g̡̟̼̱͚̞̬ͅo̗͜.̟ 45̦H̬̤̗̤͝e͜ ̜̥̝̻͍̟́w̕h̖̯͓o̝͙̖͎̱̮ ҉̺̙̞̟͈W̷̼̭a̺̪͍į͈͕̭͙̯̜t̶̼̮s̘͙͖̕ ̠̫̠B̻͍͙͉̳ͅe̵h̵̬͇̫͙i̹͓̳̳̮͎̫̕n͟d̴̪̜̖ ̰͉̩͇͙̲͞ͅT͖̼͓̪͢h͏͓̮̻e̬̝̟ͅ ̤̹̝W͙̞̝͔͇͝ͅa͏͓͔̹̼̣l̴͔̰̤̟͔ḽ̫.͕ 46Z̮̞̠͙͔ͅḀ̗̞͈̻̗Ḷ͙͎̯̹̞͓G̻O̭̗̮ 47]] , 48'Hello\0world' , 49} 50 51for _ , v in ipairs ( values ) do 52eq ( v , decode ( encode ( v ))) 53end 54 55-- Explicitly check encoded output 56eq ( 57'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZwo=' , 58encode ( 'The quick brown fox jumps over the lazy dog\n' ) 59) 60 61-- Test vectors from rfc4648 62local rfc4648 ={ 63{ '' , '' } , 64{ 'f' , 'Zg==' } , 65{ 'fo' , 'Zm8=' } , 66{ 'foo' , 'Zm9v' } , 67{ 'foob' , 'Zm9vYg==' } , 68{ 'fooba' , 'Zm9vYmE=' } , 69{ 'foobar' , 'Zm9vYmFy' } , 70} 71 72for _ , v in ipairs ( rfc4648 ) do 73local input =v [ 1 ] 74local output =v [ 2 ] 75eq ( output , encode ( input )) 76eq ( input , decode ( output )) 77end 78end ) 79 80it ( 'detects invalid input' , function () 81local invalid ={ 82'A' , 83'AA' , 84'AAA' , 85'A..A' , 86'AA=A' , 87'AA/=' , 88'A/==' , 89'A===' , 90'====' , 91'Zm9vYmFyZm9vYmFyA..A' , 92'Zm9vYmFyZm9vYmFyAA=A' , 93'Zm9vYmFyZm9vYmFyAA/=' , 94'Zm9vYmFyZm9vYmFyA/==' , 95'Zm9vYmFyZm9vYmFyA===' , 96'A..AZm9vYmFyZm9vYmFy' , 97'Zm9vYmFyZm9vAA=A' , 98'Zm9vYmFyZm9vAA/=' , 99'Zm9vYmFyZm9vA/==' , 100'Zm9vYmFyZm9vA===' , 101} 102 103for _ , v in ipairs ( invalid ) do 104eq ( 'Invalid input' , pcall_err ( decode , v )) 105end 106 107eq ( 'Expected 1 argument' , pcall_err ( encode )) 108eq ( 'Expected 1 argument' , pcall_err ( decode )) 109matches ( 'expected string' , pcall_err ( encode , 42 )) 110matches ( 'expected string' , pcall_err ( decode , 42 )) 111end ) 112end )