robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Justin M. KeyesMerge #40865 from justinmk/fixcif907614088

master
4.7 KiB112 linesraw
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()
12  before_each(clear)
13
14  local function encode(s)
15    return exec_lua([[return vim.base64.encode(...)]], s)
16  end
17
18  local function decode(s)
19    return exec_lua([[return vim.base64.decode(...)]], s)
20  end
21
22  it('works', function()
23    local values = {
24      '',
25      'Many hands make light work.',
26      [[
27        Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in
28        my purse, and nothing particular to interest me on shore, I thought I would sail about a
29        little and see the watery part of the world.
30      ]],
31      [[
32        It is a truth universally acknowledged, that a single man in possession of a good fortune,
33        must 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̴͔̰̤̟͔ḽ̫.͕
46        Z̮̞̠͙͔ͅḀ̗̞͈̻̗Ḷ͙͎̯̹̞͓G̻O̭̗̮
47      ]],
48      'Hello\0world',
49    }
50
51    for _, v in ipairs(values) do
52      eq(v, decode(encode(v)))
53    end
54
55    -- Explicitly check encoded output
56    eq(
57      'VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZwo=',
58      encode('The quick brown fox jumps over the lazy dog\n')
59    )
60
61    -- Test vectors from rfc4648
62    local rfc4648 = {
63      { '', '' },
64      { 'f', 'Zg==' },
65      { 'fo', 'Zm8=' },
66      { 'foo', 'Zm9v' },
67      { 'foob', 'Zm9vYg==' },
68      { 'fooba', 'Zm9vYmE=' },
69      { 'foobar', 'Zm9vYmFy' },
70    }
71
72    for _, v in ipairs(rfc4648) do
73      local input = v[1]
74      local output = v[2]
75      eq(output, encode(input))
76      eq(input, decode(output))
77    end
78  end)
79
80  it('detects invalid input', function()
81    local 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
103    for _, v in ipairs(invalid) do
104      eq('Invalid input', pcall_err(decode, v))
105    end
106
107    eq('Expected 1 argument', pcall_err(encode))
108    eq('Expected 1 argument', pcall_err(decode))
109    matches('expected string', pcall_err(encode, 42))
110    matches('expected string', pcall_err(decode, 42))
111  end)
112end)