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