|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class SnippetExtractor::Languages::VimscriptTest < Minitest::Test |
| 4 | + def test_full_example |
| 5 | + code = <<~'CODE' |
| 6 | + " |
| 7 | + " Create an implementation of the atbash cipher, an ancient encryption system |
| 8 | + " created in the Middle East. |
| 9 | + " |
| 10 | + " Examples: |
| 11 | + " |
| 12 | + " :echo AtbashEncode('test') |
| 13 | + " gvhg |
| 14 | + " |
| 15 | + " :echo AtbashDecode('gvhg') |
| 16 | + " test |
| 17 | + " |
| 18 | + " :echo AtbashDecode('gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt') |
| 19 | + " thequickbrownfoxjumpsoverthelazydog |
| 20 | + " |
| 21 | +
|
| 22 | + function! AtbashDecode(cipher) abort |
| 23 | + let str = tolower(substitute(a:cipher, '[^[:alnum:]]', '', 'g')) |
| 24 | + let str = tr(str, 'abcdefghijklmnopqrstuvwxyz', 'zyxwvutsrqponmlkjihgfedcba') |
| 25 | + return str |
| 26 | + endfunction |
| 27 | +
|
| 28 | + function! AtbashEncode(plaintext) abort |
| 29 | + let str = AtbashDecode(a:plaintext) |
| 30 | + " let str = substitute(str, '.\{5\}', '& ', 'g') |
| 31 | + " return substitute(str, ' $', '', '') |
| 32 | + return join(split(str, '.\{5\}\zs'), ' ') |
| 33 | + endfunction |
| 34 | + CODE |
| 35 | + |
| 36 | + expected = <<~'CODE' |
| 37 | + function! AtbashDecode(cipher) abort |
| 38 | + let str = tolower(substitute(a:cipher, '[^[:alnum:]]', '', 'g')) |
| 39 | + let str = tr(str, 'abcdefghijklmnopqrstuvwxyz', 'zyxwvutsrqponmlkjihgfedcba') |
| 40 | + return str |
| 41 | + endfunction |
| 42 | +
|
| 43 | + function! AtbashEncode(plaintext) abort |
| 44 | + let str = AtbashDecode(a:plaintext) |
| 45 | + " let str = substitute(str, '.\{5\}', '& ', 'g') |
| 46 | + " return substitute(str, ' $', '', '') |
| 47 | + CODE |
| 48 | + assert_equal expected, SnippetExtractor::ExtractSnippet.(code, :vimscript) |
| 49 | + end |
| 50 | +end |
0 commit comments