From 475e9adfe1ed72120366c03db8c3fde56fd70a23 Mon Sep 17 00:00:00 2001 From: Francis Wang Date: Thu, 15 Jun 2017 12:06:19 -0700 Subject: [PATCH] Add test cases for of_rna (fix #267) * 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`. --- exercises/protein-translation/example.rs | 9 ++++ .../protein-translation/tests/proteins.rs | 43 ++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/exercises/protein-translation/example.rs b/exercises/protein-translation/example.rs index e8f280562..fb6480480 100644 --- a/exercises/protein-translation/example.rs +++ b/exercises/protein-translation/example.rs @@ -17,4 +17,13 @@ impl<'a> CodonInfo<'a> { None => Err("Invalid") } } + + pub fn of_rna(&self, strand: &str) -> Result, &'static str> { + strand.chars() + .collect::>() + .chunks(3) + .map(|chars| self.name_for(&chars.iter().collect::())) + .take_while(|result| result.is_err() || result.unwrap() != "stop codon") + .collect() + } } diff --git a/exercises/protein-translation/tests/proteins.rs b/exercises/protein-translation/tests/proteins.rs index b1828007b..cfeb25675 100644 --- a/exercises/protein-translation/tests/proteins.rs +++ b/exercises/protein-translation/tests/proteins.rs @@ -71,22 +71,53 @@ 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"]), @@ -94,7 +125,7 @@ fn make_pairs() -> Vec<(&'static str, &'static str)> { ("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 {