Skip to content

Commit e4b4a2a

Browse files
authored
Updated protein-translation-tests (#590)
1 parent cc28783 commit e4b4a2a

File tree

5 files changed

+178
-67
lines changed

5 files changed

+178
-67
lines changed

exercises/practice/protein-translation/.meta/example.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ local codon_to_protein = {
1818
UGA = 'STOP'
1919
}
2020

21-
local function translate_codon(codon)
21+
local function protein_for_codon(codon)
2222
local protein = codon_to_protein[codon]
2323
assert(protein ~= nil, 'Unknown codon')
2424
return protein
2525
end
2626

27-
local function translate_rna_strand(rna_strand)
27+
local function proteins(strand)
2828
local proteins = {}
29-
for codon in rna_strand:gmatch('...') do
30-
local protein = translate_codon(codon)
29+
for codon in strand:gmatch('...') do
30+
local protein = protein_for_codon(codon)
3131
if protein == 'STOP' then
3232
break
3333
end
@@ -36,4 +36,4 @@ local function translate_rna_strand(rna_strand)
3636
return proteins
3737
end
3838

39-
return { codon = translate_codon, rna_strand = translate_rna_strand }
39+
return { proteins = proteins }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local map = function(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function stringify(v)
10+
return ("'%s'"):format(tostring(v))
11+
end
12+
13+
return {
14+
module_name = 'protein_translation',
15+
16+
generate_test = function(case)
17+
if case.expected.error then
18+
local template = [[
19+
assert.has_error(function()
20+
protein_translation.%s('%s')
21+
end)]]
22+
23+
return template:format(case.property, case.input.strand)
24+
else
25+
local template = [[
26+
assert.are.same({ %s }, protein_translation.%s('%s'))]]
27+
28+
return template:format(table.concat(map(case.expected, stringify), ', '), case.property, case.input.strand)
29+
end
30+
end
31+
}

exercises/practice/protein-translation/.meta/tests.toml

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[2c44f7bf-ba20-43f7-a3bf-f2219c0c3f98]
13+
description = "Empty RNA sequence results in no proteins"
414

515
[96d3d44f-34a2-4db4-84cd-fff523e069be]
616
description = "Methionine RNA sequence"
@@ -53,6 +63,12 @@ description = "STOP codon RNA sequence 2"
5363
[9c2ad527-ebc9-4ace-808b-2b6447cb54cb]
5464
description = "STOP codon RNA sequence 3"
5565

66+
[f4d9d8ee-00a8-47bf-a1e3-1641d4428e54]
67+
description = "Sequence of two protein codons translates into proteins"
68+
69+
[dd22eef3-b4f1-4ad6-bb0b-27093c090a9d]
70+
description = "Sequence of two different protein codons translates into proteins"
71+
5672
[d0f295df-fb70-425c-946c-ec2ec185388e]
5773
description = "Translate RNA strand into correct protein list"
5874

@@ -70,3 +86,21 @@ description = "Translation stops if STOP codon in middle of three-codon sequence
7086

7187
[2c2a2a60-401f-4a80-b977-e0715b23b93d]
7288
description = "Translation stops if STOP codon in middle of six-codon sequence"
89+
90+
[f6f92714-769f-4187-9524-e353e8a41a80]
91+
description = "Sequence of two non-STOP codons does not translate to a STOP codon"
92+
93+
[1e75ea2a-f907-4994-ae5c-118632a1cb0f]
94+
description = "Non-existing codon can't translate"
95+
include = false
96+
97+
[9eac93f3-627a-4c90-8653-6d0a0595bc6f]
98+
description = "Unknown amino acids, not part of a codon, can't translate"
99+
reimplements = "1e75ea2a-f907-4994-ae5c-118632a1cb0f"
100+
101+
[9d73899f-e68e-4291-b1e2-7bf87c00f024]
102+
description = "Incomplete RNA sequence can't translate"
103+
include = false
104+
105+
[43945cf7-9968-402d-ab9f-b8a28750b050]
106+
description = "Incomplete RNA sequence can translate if valid until a STOP codon"
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
local function translate_codon(codon)
2-
end
1+
local function proteins(strand)
32

4-
local function translate_rna_strand(rna_strand)
53
end
64

7-
return { codon = translate_codon, rna_strand = translate_rna_strand }
5+
return { proteins = proteins }
Lines changed: 103 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,121 @@
1-
local translate = require('protein-translation')
1+
local protein_translation = require('protein-translation')
22

33
describe('protein-translation', function()
4-
describe('translate.codon', function()
5-
it('should translate AUG to Methionine', function()
6-
assert.equal('Methionine', translate.codon('AUG'))
7-
end)
4+
it('empty rna sequence results in no proteins', function()
5+
assert.are.same({}, protein_translation.proteins(''))
6+
end)
87

9-
it('should translate UUU and UUC to Phenylalanine', function()
10-
assert.equal('Phenylalanine', translate.codon('UUU'))
11-
assert.equal('Phenylalanine', translate.codon('UUC'))
12-
end)
8+
it('methionine rna sequence', function()
9+
assert.are.same({ 'Methionine' }, protein_translation.proteins('AUG'))
10+
end)
1311

14-
it('should translate UUA and UUG to Leucine', function()
15-
assert.equal('Leucine', translate.codon('UUA'))
16-
assert.equal('Leucine', translate.codon('UUG'))
17-
end)
12+
it('phenylalanine rna sequence 1', function()
13+
assert.are.same({ 'Phenylalanine' }, protein_translation.proteins('UUU'))
14+
end)
1815

19-
it('should translate UCU, UCC, UCA, and UCG to Serine', function()
20-
assert.equal('Serine', translate.codon('UCU'))
21-
assert.equal('Serine', translate.codon('UCC'))
22-
assert.equal('Serine', translate.codon('UCA'))
23-
assert.equal('Serine', translate.codon('UCG'))
24-
end)
16+
it('phenylalanine rna sequence 2', function()
17+
assert.are.same({ 'Phenylalanine' }, protein_translation.proteins('UUC'))
18+
end)
2519

26-
it('should translate UAU and UAC to Tyrosine', function()
27-
assert.equal('Tyrosine', translate.codon('UAU'))
28-
assert.equal('Tyrosine', translate.codon('UAC'))
29-
end)
20+
it('leucine rna sequence 1', function()
21+
assert.are.same({ 'Leucine' }, protein_translation.proteins('UUA'))
22+
end)
3023

31-
it('should translate UGU and UGC to Cysteine', function()
32-
assert.equal('Cysteine', translate.codon('UGU'))
33-
assert.equal('Cysteine', translate.codon('UGC'))
34-
end)
24+
it('leucine rna sequence 2', function()
25+
assert.are.same({ 'Leucine' }, protein_translation.proteins('UUG'))
26+
end)
3527

36-
it('should translate UGG to Tryptophan', function()
37-
assert.equal('Tryptophan', translate.codon('UGG'))
38-
end)
28+
it('serine rna sequence 1', function()
29+
assert.are.same({ 'Serine' }, protein_translation.proteins('UCU'))
30+
end)
3931

40-
it('should translate UAA, UAG, and UGA to STOP codons', function()
41-
assert.equal('STOP', translate.codon('UAA'))
42-
assert.equal('STOP', translate.codon('UAG'))
43-
assert.equal('STOP', translate.codon('UGA'))
44-
end)
32+
it('serine rna sequence 2', function()
33+
assert.are.same({ 'Serine' }, protein_translation.proteins('UCC'))
34+
end)
4535

46-
it('should raise an error when an unknown codon is translated', function()
47-
assert.has_error(function()
48-
translate.codon('MIA')
49-
end)
50-
end)
36+
it('serine rna sequence 3', function()
37+
assert.are.same({ 'Serine' }, protein_translation.proteins('UCA'))
5138
end)
5239

53-
describe('translate.rna_strand', function()
54-
it('should translate each codon in a strand into the corresponding protein', function()
55-
assert.same({ 'Methionine', 'Phenylalanine', 'Tryptophan' }, translate.rna_strand('AUGUUUUGG'))
56-
end)
40+
it('serine rna sequence 4', function()
41+
assert.are.same({ 'Serine' }, protein_translation.proteins('UCG'))
42+
end)
5743

58-
it('should stop translation when a STOP codon is encountered', function()
59-
assert.same({ 'Methionine', 'Phenylalanine' }, translate.rna_strand('AUGUUUUAA'))
60-
assert.same({ 'Tryptophan', 'Cysteine', 'Tyrosine' }, translate.rna_strand('UGGUGUUAUUAAUGGUUU'))
61-
end)
44+
it('tyrosine rna sequence 1', function()
45+
assert.are.same({ 'Tyrosine' }, protein_translation.proteins('UAU'))
46+
end)
6247

63-
it('should raise an error when an unknown codon is encountered', function()
64-
assert.has_error(function()
65-
translate.rna_strand('UGUCARROT')
66-
end)
67-
end)
48+
it('tyrosine rna sequence 2', function()
49+
assert.are.same({ 'Tyrosine' }, protein_translation.proteins('UAC'))
50+
end)
51+
52+
it('cysteine rna sequence 1', function()
53+
assert.are.same({ 'Cysteine' }, protein_translation.proteins('UGU'))
54+
end)
6855

69-
it('should not raise an error when an unknown codon occurs after a STOP codon', function()
70-
assert.same({ 'Cysteine' }, translate.rna_strand('UGUUGACARROT'))
56+
it('cysteine rna sequence 2', function()
57+
assert.are.same({ 'Cysteine' }, protein_translation.proteins('UGC'))
58+
end)
59+
60+
it('tryptophan rna sequence', function()
61+
assert.are.same({ 'Tryptophan' }, protein_translation.proteins('UGG'))
62+
end)
63+
64+
it('stop codon rna sequence 1', function()
65+
assert.are.same({}, protein_translation.proteins('UAA'))
66+
end)
67+
68+
it('stop codon rna sequence 2', function()
69+
assert.are.same({}, protein_translation.proteins('UAG'))
70+
end)
71+
72+
it('stop codon rna sequence 3', function()
73+
assert.are.same({}, protein_translation.proteins('UGA'))
74+
end)
75+
76+
it('sequence of two protein codons translates into proteins', function()
77+
assert.are.same({ 'Phenylalanine', 'Phenylalanine' }, protein_translation.proteins('UUUUUU'))
78+
end)
79+
80+
it('sequence of two different protein codons translates into proteins', function()
81+
assert.are.same({ 'Leucine', 'Leucine' }, protein_translation.proteins('UUAUUG'))
82+
end)
83+
84+
it('translate rna strand into correct protein list', function()
85+
assert.are.same({ 'Methionine', 'Phenylalanine', 'Tryptophan' }, protein_translation.proteins('AUGUUUUGG'))
86+
end)
87+
88+
it('translation stops if stop codon at beginning of sequence', function()
89+
assert.are.same({}, protein_translation.proteins('UAGUGG'))
90+
end)
91+
92+
it('translation stops if stop codon at end of two-codon sequence', function()
93+
assert.are.same({ 'Tryptophan' }, protein_translation.proteins('UGGUAG'))
94+
end)
95+
96+
it('translation stops if stop codon at end of three-codon sequence', function()
97+
assert.are.same({ 'Methionine', 'Phenylalanine' }, protein_translation.proteins('AUGUUUUAA'))
98+
end)
99+
100+
it('translation stops if stop codon in middle of three-codon sequence', function()
101+
assert.are.same({ 'Tryptophan' }, protein_translation.proteins('UGGUAGUGG'))
102+
end)
103+
104+
it('translation stops if stop codon in middle of six-codon sequence', function()
105+
assert.are.same({ 'Tryptophan', 'Cysteine', 'Tyrosine' }, protein_translation.proteins('UGGUGUUAUUAAUGGUUU'))
106+
end)
107+
108+
it('sequence of two non-stop codons does not translate to a stop codon', function()
109+
assert.are.same({ 'Methionine', 'Methionine' }, protein_translation.proteins('AUGAUG'))
110+
end)
111+
112+
it('unknown amino acids, not part of a codon, can\'t translate', function()
113+
assert.has_error(function()
114+
protein_translation.proteins('XYZ')
71115
end)
72116
end)
117+
118+
it('incomplete rna sequence can translate if valid until a stop codon', function()
119+
assert.are.same({ 'Phenylalanine', 'Phenylalanine' }, protein_translation.proteins('UUCUUCUAAUGGU'))
120+
end)
73121
end)

0 commit comments

Comments
 (0)