Skip to content

Commit

Permalink
Add test cases for of_rna (fix exercism#267)
Browse files Browse the repository at this point in the history
* Added test cases for function `of_rna`, which translates a strand of
  RNA into a sequence of proteins per README. The test cases are copied
  from the test cases in
  [xpython](https://github.com/exercism/xpython/blob/master/exercises/protein-translation/protein_translation_test.py).
* Added example for `of_rna`.
  • Loading branch information
go717franciswang committed Jun 15, 2017
1 parent 733519e commit 475e9ad
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
9 changes: 9 additions & 0 deletions exercises/protein-translation/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,13 @@ impl<'a> CodonInfo<'a> {
None => Err("Invalid")
}
}

pub fn of_rna(&self, strand: &str) -> Result<Vec<&'a str>, &'static str> {
strand.chars()
.collect::<Vec<char>>()
.chunks(3)
.map(|chars| self.name_for(&chars.iter().collect::<String>()))
.take_while(|result| result.is_err() || result.unwrap() != "stop codon")
.collect()
}
}
43 changes: 37 additions & 6 deletions exercises/protein-translation/tests/proteins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,61 @@ fn too_long_is_invalid() {
assert!(info.name_for("ATTA").is_err());
}

#[test]
#[ignore]
fn test_translates_rna_strand_into_correct_protein() {
let info = proteins::parse(make_pairs());
assert_eq!(info.of_rna("AUGUUUUGG").unwrap(),
vec!["methionine", "phenylalanine", "tryptophan"]);
}

#[test]
#[ignore]
fn test_stops_translation_if_stop_codon_present() {
let info = proteins::parse(make_pairs());
assert_eq!(info.of_rna("AUGUUUUAA").unwrap(),
vec!["methionine", "phenylalanine"]);
}

#[test]
#[ignore]
fn test_stops_translation_of_longer_strand() {
let info = proteins::parse(make_pairs());
assert_eq!(info.of_rna("UGGUGUUAUUAAUGGUUU").unwrap(),
vec!["tryptophan", "cysteine", "tyrosine"]);
}

#[test]
#[ignore]
fn test_invalid_codons() {
let info = proteins::parse(make_pairs());
assert!(info.of_rna("CARROT").is_err());
}

// The input data constructor. Returns a list of codon, name pairs.
fn make_pairs() -> Vec<(&'static str, &'static str)> {
let grouped = vec![
("isoleucine", vec!["ATT", "ATC", "ATA"]),
("leucine", vec!["CTT", "CTC", "CTA", "CTG", "TTA", "TTG"]),
("valine", vec!["GTT", "GTC", "GTA", "GTG"]),
("phenylalanine", vec!["TTT", "TTC"]),
("methionine", vec!["ATG"]),
("cysteine", vec!["TGT", "TGC"]),
("phenylalanine", vec!["UUU", "TTT", "TTC"]),
("methionine", vec!["AUG", "ATG"]),
("cysteine", vec!["UGU", "TGT", "TGC"]),
("alanine", vec!["GCT", "GCC", "GCA", "GCG"]),
("glycine", vec!["GGT", "GGC", "GGA", "GGG"]),
("proline", vec!["CCT", "CCC", "CCA", "CCG"]),
("threonine", vec!["ACT", "ACC", "ACA", "ACG"]),
("serine", vec!["TCT", "TCC", "TCA", "TCG", "AGT", "AGC"]),
("tyrosine", vec!["TAT", "TAC"]),
("tryptophan", vec!["TGG"]),
("tyrosine", vec!["UAU", "TAT", "TAC"]),
("tryptophan", vec!["UGG", "TGG"]),
("glutamine", vec!["CAA", "CAG"]),
("asparagine", vec!["AAT", "AAC"]),
("histidine", vec!["CAT", "CAC"]),
("glutamic acid", vec!["GAA", "GAG"]),
("aspartic acid", vec!["GAT", "GAC"]),
("lysine", vec!["AAA", "AAG"]),
("arginine", vec!["CGT", "CGC", "CGA", "CGG", "AGA", "AGG"]),
("stop codon", vec!["TAA", "TAG", "TGA"])];
("stop codon", vec!["UAA", "TAA", "TAG", "TGA"])];
let mut pairs = Vec::<(&'static str, &'static str)>::new();
for (name, codons) in grouped.into_iter() {
for codon in codons {
Expand Down

0 comments on commit 475e9ad

Please sign in to comment.