Skip to content

Commit 1aa327c

Browse files
authored
Add canonical tests for decoding (#632)
1 parent 7520248 commit 1aa327c

5 files changed

Lines changed: 126 additions & 55 deletions

File tree

exercises/practice/atbash-cipher/.meta/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"ryanplusplus"
44
],
55
"contributors": [
6-
"aarti"
6+
"aarti",
7+
"BNAndras"
78
],
89
"files": {
910
"solution": [
Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,17 @@
1-
local alphabet = 'abcdefghijklmnopqrstuvwxyz'
2-
local decoder_ring = setmetatable({}, {
3-
__index = function()
4-
return ''
5-
end
6-
})
7-
8-
for i = 1, #alphabet do
9-
decoder_ring[alphabet:sub(i, i)] = alphabet:reverse():sub(i, i)
10-
end
11-
12-
for i = 0, 9 do
13-
decoder_ring[tostring(i)] = tostring(i)
1+
local function inverse_char(char)
2+
return string.char(219 - string.byte(char))
143
end
154

16-
local function transcribe(plaintext)
17-
return plaintext:lower():gsub('.', decoder_ring)
5+
local function encode(phrase)
6+
local clean = string.gsub(string.lower(phrase), "[^a-z0-9]", "")
7+
local decoded = string.gsub(clean, "[a-z]", inverse_char)
8+
local spaced = string.gsub(decoded, ".....", "%0 ")
9+
return string.match(spaced, "^%s*(.-)%s*$") or spaced
1810
end
1911

20-
local function split_chunks(s)
21-
local chunks = {}
22-
s:gsub('..?.?.?.?', function(chunk)
23-
table.insert(chunks, chunk)
24-
end)
25-
return table.concat(chunks, ' ')
12+
local function decode(phrase)
13+
local clean = string.gsub(string.lower(phrase), "[^a-z0-9]", "")
14+
return string.gsub(clean, "[a-z]", inverse_char)
2615
end
2716

28-
return {
29-
encode = function(plaintext)
30-
return split_chunks(transcribe(plaintext))
31-
end
32-
}
17+
return { encode = encode, decode = decode }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
return {
2+
module_name = 'atbash_cipher',
3+
4+
generate_test = function(case)
5+
local lines = {}
6+
table.insert(lines, string.format("local phrase = '%s'", case.input.phrase))
7+
table.insert(lines, string.format("local expected = '%s'", case.expected))
8+
table.insert(lines, string.format("local result = atbash_cipher.%s(phrase)", case.property))
9+
table.insert(lines, "assert.are.same(expected, result)")
10+
11+
return table.concat(lines, "\n")
12+
end
13+
}
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
return {
2-
encode = function(plaintext)
3-
end
4-
}
1+
local function encode(phrase)
2+
end
3+
4+
local function decode(phrase)
5+
end
6+
7+
return { encode = encode, decode = decode }
Lines changed: 93 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,105 @@
1-
local encode = require('atbash-cipher').encode
1+
local atbash_cipher = require('atbash-cipher')
22

33
describe('atbash-cipher', function()
4-
it('should encode single letter plaintexts', function()
5-
assert.are.equal('m', encode('n'))
6-
end)
4+
describe('encode', function()
5+
it('encode yes', function()
6+
local phrase = 'yes'
7+
local expected = 'bvh'
8+
local result = atbash_cipher.encode(phrase)
9+
assert.are.same(expected, result)
10+
end)
711

8-
it('should encode single-chunk plaintexts', function()
9-
assert.are.equal('svool', encode('hello'))
10-
end)
12+
it('encode no', function()
13+
local phrase = 'no'
14+
local expected = 'ml'
15+
local result = atbash_cipher.encode(phrase)
16+
assert.are.same(expected, result)
17+
end)
1118

12-
it('should encode multi-chunk plaintexts', function()
13-
assert.are.equal('nrmwy oldrm tob', encode('mindblowingly'))
14-
end)
19+
it('encode omg', function()
20+
local phrase = 'OMG'
21+
local expected = 'lnt'
22+
local result = atbash_cipher.encode(phrase)
23+
assert.are.same(expected, result)
24+
end)
1525

16-
it('should encode all letters as lower-case', function()
17-
assert.are.equal('svool', encode('HeLLo'))
18-
end)
26+
it('encode spaces', function()
27+
local phrase = 'O M G'
28+
local expected = 'lnt'
29+
local result = atbash_cipher.encode(phrase)
30+
assert.are.same(expected, result)
31+
end)
1932

20-
it('should not encode whitespace', function()
21-
assert.are.equal('svool', encode('h e l l o'))
22-
end)
33+
it('encode mindblowingly', function()
34+
local phrase = 'mindblowingly'
35+
local expected = 'nrmwy oldrm tob'
36+
local result = atbash_cipher.encode(phrase)
37+
assert.are.same(expected, result)
38+
end)
2339

24-
it('should not encode punctuation', function()
25-
assert.are.equal('svool', encode('h,e,l,l,o'))
26-
end)
40+
it('encode numbers', function()
41+
local phrase = 'Testing,1 2 3, testing.'
42+
local expected = 'gvhgr mt123 gvhgr mt'
43+
local result = atbash_cipher.encode(phrase)
44+
assert.are.same(expected, result)
45+
end)
2746

28-
it('should not encode numbers', function()
29-
assert.are.equal('sv11l', encode('he11o'))
47+
it('encode deep thought', function()
48+
local phrase = 'Truth is fiction.'
49+
local expected = 'gifgs rhurx grlm'
50+
local result = atbash_cipher.encode(phrase)
51+
assert.are.same(expected, result)
52+
end)
53+
54+
it('encode all the letters', function()
55+
local phrase = 'The quick brown fox jumps over the lazy dog.'
56+
local expected = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
57+
local result = atbash_cipher.encode(phrase)
58+
assert.are.same(expected, result)
59+
end)
3060
end)
3161

32-
it('should encode all letters', function()
33-
assert.are
34-
.equal('gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt', encode('The quick brown fox jumps over the lazy dog.'))
62+
describe('decode', function()
63+
it('decode exercism', function()
64+
local phrase = 'vcvix rhn'
65+
local expected = 'exercism'
66+
local result = atbash_cipher.decode(phrase)
67+
assert.are.same(expected, result)
68+
end)
69+
70+
it('decode a sentence', function()
71+
local phrase = 'zmlyh gzxov rhlug vmzhg vkkrm thglm v'
72+
local expected = 'anobstacleisoftenasteppingstone'
73+
local result = atbash_cipher.decode(phrase)
74+
assert.are.same(expected, result)
75+
end)
76+
77+
it('decode numbers', function()
78+
local phrase = 'gvhgr mt123 gvhgr mt'
79+
local expected = 'testing123testing'
80+
local result = atbash_cipher.decode(phrase)
81+
assert.are.same(expected, result)
82+
end)
83+
84+
it('decode all the letters', function()
85+
local phrase = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt'
86+
local expected = 'thequickbrownfoxjumpsoverthelazydog'
87+
local result = atbash_cipher.decode(phrase)
88+
assert.are.same(expected, result)
89+
end)
90+
91+
it('decode with too many spaces', function()
92+
local phrase = 'vc vix r hn'
93+
local expected = 'exercism'
94+
local result = atbash_cipher.decode(phrase)
95+
assert.are.same(expected, result)
96+
end)
97+
98+
it('decode with no spaces', function()
99+
local phrase = 'zmlyhgzxovrhlugvmzhgvkkrmthglmv'
100+
local expected = 'anobstacleisoftenasteppingstone'
101+
local result = atbash_cipher.decode(phrase)
102+
assert.are.same(expected, result)
103+
end)
35104
end)
36105
end)

0 commit comments

Comments
 (0)