diff --git a/exercises/nucleotide-count/Example.cs b/exercises/nucleotide-count/Example.cs index 4dbd18ddc5..6fb509a90d 100644 --- a/exercises/nucleotide-count/Example.cs +++ b/exercises/nucleotide-count/Example.cs @@ -1,11 +1,11 @@ using System; using System.Collections.Generic; -public class DNA +public class NucleotideCount { public IDictionary NucleotideCounts { get; private set; } - public DNA(string sequence) + public NucleotideCount(string sequence) { InitializeNucleotideCounts(sequence); } @@ -13,16 +13,15 @@ public DNA(string sequence) private void InitializeNucleotideCounts(string sequence) { NucleotideCounts = new Dictionary { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 0 } }; - foreach (var s in sequence) - NucleotideCounts[s] += 1; - } - - public int Count(char nucleotide) - { - int count; - if (!NucleotideCounts.TryGetValue(nucleotide, out count)) + try + { + foreach (var s in sequence) + NucleotideCounts[s] += 1; + } + catch (KeyNotFoundException) + { throw new InvalidNucleotideException(); - return count; + } } } diff --git a/exercises/nucleotide-count/NucleotideCount.cs b/exercises/nucleotide-count/NucleotideCount.cs index 1c5782f612..4f71c41d2e 100644 --- a/exercises/nucleotide-count/NucleotideCount.cs +++ b/exercises/nucleotide-count/NucleotideCount.cs @@ -1,9 +1,9 @@ using System; using System.Collections.Generic; -public class DNA +public class NucleotideCount { - public DNA(string sequence) + public NucleotideCount(string sequence) { } @@ -14,11 +14,6 @@ public IDictionary NucleotideCounts throw new NotImplementedException("You need to implement this function."); } } - - public int Count(char nucleotide) - { - throw new NotImplementedException("You need to implement this function."); - } } public class InvalidNucleotideException : Exception { } diff --git a/exercises/nucleotide-count/NucleotideCountTest.cs b/exercises/nucleotide-count/NucleotideCountTest.cs index 71ce18f736..144c623bfd 100644 --- a/exercises/nucleotide-count/NucleotideCountTest.cs +++ b/exercises/nucleotide-count/NucleotideCountTest.cs @@ -1,65 +1,55 @@ -using System.Collections.Generic; +// This file was auto-generated based on version 1.0.0 of the canonical data. + using Xunit; +using System.Collections.Generic; -public class NucleoTideCountTest +public class NucleotideCountTest { [Fact] - public void Has_no_nucleotides() - { - var dna = new DNA(""); - var expected = new Dictionary { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 0 } }; - Assert.Equal(expected, dna.NucleotideCounts); - } - - [Fact(Skip = "Remove to run test")] - public void Has_no_adenosine() - { - var dna = new DNA(""); - Assert.Equal(0, dna.Count('A')); - } - - [Fact(Skip = "Remove to run test")] - public void Repetitive_cytidine_gets_counts() - { - var dna = new DNA("CCCCC"); - Assert.Equal(5, dna.Count('C')); - } - - [Fact(Skip = "Remove to run test")] - public void Repetitive_sequence_has_only_guanosine() - { - var dna = new DNA("GGGGGGGG"); - var expected = new Dictionary { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 8 } }; - Assert.Equal(expected, dna.NucleotideCounts); - } - - [Fact(Skip = "Remove to run test")] - public void Counts_only_thymidine() + public void Empty_strand() { - var dna = new DNA("GGGGTAACCCGG"); - Assert.Equal(1, dna.Count('T')); + var sut = new NucleotideCount(""); + var expected = new Dictionary + { + ['A'] = 0, + ['C'] = 0, + ['G'] = 0, + ['T'] = 0 + }; + Assert.Equal(expected, sut.NucleotideCounts); } [Fact(Skip = "Remove to run test")] - public void Counts_a_nucleotide_only_once() + public void Strand_with_repeated_nucleotide() { - var dna = new DNA("GGTTGG"); - dna.Count('T'); - Assert.Equal(2, dna.Count('T')); + var sut = new NucleotideCount("GGGGGGG"); + var expected = new Dictionary + { + ['A'] = 0, + ['C'] = 0, + ['G'] = 7, + ['T'] = 0 + }; + Assert.Equal(expected, sut.NucleotideCounts); } [Fact(Skip = "Remove to run test")] - public void Validates_nucleotides() + public void Strand_with_multiple_nucleotides() { - var dna = new DNA("GGTTGG"); - Assert.Throws(() => dna.Count('X')); + var sut = new NucleotideCount("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"); + var expected = new Dictionary + { + ['A'] = 20, + ['C'] = 12, + ['G'] = 17, + ['T'] = 21 + }; + Assert.Equal(expected, sut.NucleotideCounts); } [Fact(Skip = "Remove to run test")] - public void Counts_all_nucleotides() + public void Strand_with_invalid_nucleotides() { - var dna = new DNA("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC"); - var expected = new Dictionary { { 'A', 20 }, { 'T', 21 }, { 'C', 12 }, { 'G', 17 } }; - Assert.Equal(expected, dna.NucleotideCounts); + Assert.Throws(() => new NucleotideCount("AGXXACT")); } -} +} \ No newline at end of file diff --git a/generators/Exercises/NucleotideCount.cs b/generators/Exercises/NucleotideCount.cs new file mode 100644 index 0000000000..a7217aac03 --- /dev/null +++ b/generators/Exercises/NucleotideCount.cs @@ -0,0 +1,61 @@ +using Generators.Input; +using Generators.Output; +using System.Collections.Generic; +using System.Linq; + +namespace Generators.Exercises +{ + public class NucleotideCount : Exercise + { + protected override void UpdateCanonicalData(CanonicalData canonicalData) + { + foreach (var canonicalDataCase in canonicalData.Cases) + { + if (!((Dictionary)canonicalDataCase.Expected).ContainsKey("error")) + { + canonicalDataCase.UseVariableForExpected = true; + canonicalDataCase.SetConstructorInputParameters("strand"); + canonicalDataCase.Expected = ConvertExpected(canonicalDataCase.Expected); + } + } + } + + private static dynamic ConvertExpected(dynamic expected) + => ((Dictionary)expected).ToDictionary(kv => kv.Key[0], kv => int.Parse($"{kv.Value}")); + + protected override HashSet AddAdditionalNamespaces() => new HashSet() { typeof(Dictionary).Namespace }; + + protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody) + { + if (testMethodBody.UseVariableForExpected) + { + return RenderEqualBodyAssert(testMethodBody); + } + return RenderThrowsBodyAssert(testMethodBody); + } + + private string RenderEqualBodyAssert(TestMethodBody testMethodBody) + { + const string template = @"Assert.Equal(expected, sut.{{ TestedMethodName }});"; + + var templateParameters = new + { + TestedMethodName = testMethodBody.CanonicalDataCase.Property.ToTestedMethodName() + }; + + return TemplateRenderer.RenderInline(template, templateParameters); + } + + private string RenderThrowsBodyAssert(TestMethodBody testMethodBody) + { + const string template = @"Assert.Throws(() => new NucleotideCount(""{{ Input }}""));"; + + var templateParameters = new + { + Input = testMethodBody.CanonicalDataCase.Input["strand"] + }; + + return TemplateRenderer.RenderInline(template, templateParameters); + } + } +}