Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto-square: fix tests not comparing actual to expected #467

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions exercises/practice/crypto-square/.meta/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,34 @@ local function segments(plaintext)
local segments = {}

while #normalized_plaintext > 0 do
table.insert(segments, normalized_plaintext:sub(1, segment_size))
local segment = normalized_plaintext:sub(1, segment_size)
while #segment < segment_size do
segment = segment .. " "
end
table.insert(segments, segment)
normalized_plaintext = normalized_plaintext:sub(segment_size + 1)
end

return segments
end

local function normalized_ciphertext(plaintext)
local normalized_ciphertext = ''
local segments = segments(plaintext)
local enciphered = {}

for i = 1, size(plaintext) do
local normalized_ciphertext = ''
for _, segment in ipairs(segments) do
normalized_ciphertext = normalized_ciphertext .. segment:sub(i, i)
end

normalized_ciphertext = normalized_ciphertext .. ' '
table.insert(enciphered, normalized_ciphertext)
end

return normalized_ciphertext:gsub('%s+$', '')
return table.concat(enciphered, ' ')
end

return {
ciphertext = function(plaintext)
return normalized_ciphertext(plaintext):gsub('%s', '')
return normalized_ciphertext(plaintext)
end
}
19 changes: 10 additions & 9 deletions exercises/practice/crypto-square/crypto-square_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@ local crypto_square = require('crypto-square')

describe('crypto-square', function()
it('empty plaintext results in an empty ciphertext', function()
crypto_square.ciphertext('', '')
assert.are.equal('', crypto_square.ciphertext(''))
end)

it('normalization results in empty plaintext', function()
crypto_square.ciphertext('... --- ...', '')
assert.are.equal('', crypto_square.ciphertext('... --- ...'))
end)

it('Lowercase', function()
crypto_square.ciphertext('A', 'a')
assert.are.equal('a', crypto_square.ciphertext('A'))
end)

it('Remove spaces', function()
crypto_square.ciphertext(' b ', 'b')
assert.are.equal('b', crypto_square.ciphertext(' b '))
end)

it('Remove punctuation', function()
crypto_square.ciphertext('@1,%!', '1')
assert.are.equal('1', crypto_square.ciphertext('@1,%!'))
end)

it('9 character plaintext results in 3 chunks of 3 characters', function()
crypto_square.ciphertext('This is fun!', 'tsf hiu isn')
assert.are.equal('tsf hiu isn', crypto_square.ciphertext('This is fun!'))
end)

it('8 character plaintext results in 3 chunks, the last one with a trailing space', function()
crypto_square.ciphertext('Chill out.', 'clu hlt io ')
assert.are.equal('clu hlt io ', crypto_square.ciphertext('Chill out.'))
end)

it('54 character plaintext results in 7 chunks, the last two with trailing spaces', function()
crypto_square.ciphertext('If man was meant to stay on the ground, god would have given us roots.',
'imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ')
assert.are.equal('imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau ',
crypto_square.ciphertext('If man was meant to stay on the ground, god would have given us roots.')
)
end)
end)