Skip to content

Commit e85c9dc

Browse files
hymccordErikSchierboom
authored andcommitted
Add Nucleotide Count Test Generator #416 (#456)
* Add nucleotide count generator (#416) This commit also renames the nucleotide exercise class to emulate the rest of the exercises. I also decided to remove the count method as the canonical data doesn't reference that this method should be tested. * Update nucleotide count example * Fix class accessibility
1 parent 2cbf4c7 commit e85c9dc

File tree

4 files changed

+110
-65
lines changed

4 files changed

+110
-65
lines changed

exercises/nucleotide-count/Example.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
using System;
22
using System.Collections.Generic;
33

4-
public class DNA
4+
public class NucleotideCount
55
{
66
public IDictionary<char, int> NucleotideCounts { get; private set; }
77

8-
public DNA(string sequence)
8+
public NucleotideCount(string sequence)
99
{
1010
InitializeNucleotideCounts(sequence);
1111
}
1212

1313
private void InitializeNucleotideCounts(string sequence)
1414
{
1515
NucleotideCounts = new Dictionary<char, int> { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 0 } };
16-
foreach (var s in sequence)
17-
NucleotideCounts[s] += 1;
18-
}
19-
20-
public int Count(char nucleotide)
21-
{
22-
int count;
23-
if (!NucleotideCounts.TryGetValue(nucleotide, out count))
16+
try
17+
{
18+
foreach (var s in sequence)
19+
NucleotideCounts[s] += 1;
20+
}
21+
catch (KeyNotFoundException)
22+
{
2423
throw new InvalidNucleotideException();
25-
return count;
24+
}
2625
}
2726
}
2827

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System;
22
using System.Collections.Generic;
33

4-
public class DNA
4+
public class NucleotideCount
55
{
6-
public DNA(string sequence)
6+
public NucleotideCount(string sequence)
77
{
88
}
99

@@ -14,11 +14,6 @@ public IDictionary<char, int> NucleotideCounts
1414
throw new NotImplementedException("You need to implement this function.");
1515
}
1616
}
17-
18-
public int Count(char nucleotide)
19-
{
20-
throw new NotImplementedException("You need to implement this function.");
21-
}
2217
}
2318

2419
public class InvalidNucleotideException : Exception { }
Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,55 @@
1-
using System.Collections.Generic;
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
23
using Xunit;
4+
using System.Collections.Generic;
35

4-
public class NucleoTideCountTest
6+
public class NucleotideCountTest
57
{
68
[Fact]
7-
public void Has_no_nucleotides()
8-
{
9-
var dna = new DNA("");
10-
var expected = new Dictionary<char, int> { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 0 } };
11-
Assert.Equal(expected, dna.NucleotideCounts);
12-
}
13-
14-
[Fact(Skip = "Remove to run test")]
15-
public void Has_no_adenosine()
16-
{
17-
var dna = new DNA("");
18-
Assert.Equal(0, dna.Count('A'));
19-
}
20-
21-
[Fact(Skip = "Remove to run test")]
22-
public void Repetitive_cytidine_gets_counts()
23-
{
24-
var dna = new DNA("CCCCC");
25-
Assert.Equal(5, dna.Count('C'));
26-
}
27-
28-
[Fact(Skip = "Remove to run test")]
29-
public void Repetitive_sequence_has_only_guanosine()
30-
{
31-
var dna = new DNA("GGGGGGGG");
32-
var expected = new Dictionary<char, int> { { 'A', 0 }, { 'T', 0 }, { 'C', 0 }, { 'G', 8 } };
33-
Assert.Equal(expected, dna.NucleotideCounts);
34-
}
35-
36-
[Fact(Skip = "Remove to run test")]
37-
public void Counts_only_thymidine()
9+
public void Empty_strand()
3810
{
39-
var dna = new DNA("GGGGTAACCCGG");
40-
Assert.Equal(1, dna.Count('T'));
11+
var sut = new NucleotideCount("");
12+
var expected = new Dictionary<char, int>
13+
{
14+
['A'] = 0,
15+
['C'] = 0,
16+
['G'] = 0,
17+
['T'] = 0
18+
};
19+
Assert.Equal(expected, sut.NucleotideCounts);
4120
}
4221

4322
[Fact(Skip = "Remove to run test")]
44-
public void Counts_a_nucleotide_only_once()
23+
public void Strand_with_repeated_nucleotide()
4524
{
46-
var dna = new DNA("GGTTGG");
47-
dna.Count('T');
48-
Assert.Equal(2, dna.Count('T'));
25+
var sut = new NucleotideCount("GGGGGGG");
26+
var expected = new Dictionary<char, int>
27+
{
28+
['A'] = 0,
29+
['C'] = 0,
30+
['G'] = 7,
31+
['T'] = 0
32+
};
33+
Assert.Equal(expected, sut.NucleotideCounts);
4934
}
5035

5136
[Fact(Skip = "Remove to run test")]
52-
public void Validates_nucleotides()
37+
public void Strand_with_multiple_nucleotides()
5338
{
54-
var dna = new DNA("GGTTGG");
55-
Assert.Throws<InvalidNucleotideException>(() => dna.Count('X'));
39+
var sut = new NucleotideCount("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC");
40+
var expected = new Dictionary<char, int>
41+
{
42+
['A'] = 20,
43+
['C'] = 12,
44+
['G'] = 17,
45+
['T'] = 21
46+
};
47+
Assert.Equal(expected, sut.NucleotideCounts);
5648
}
5749

5850
[Fact(Skip = "Remove to run test")]
59-
public void Counts_all_nucleotides()
51+
public void Strand_with_invalid_nucleotides()
6052
{
61-
var dna = new DNA("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC");
62-
var expected = new Dictionary<char, int> { { 'A', 20 }, { 'T', 21 }, { 'C', 12 }, { 'G', 17 } };
63-
Assert.Equal(expected, dna.NucleotideCounts);
53+
Assert.Throws<InvalidNucleotideException>(() => new NucleotideCount("AGXXACT"));
6454
}
65-
}
55+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Generators.Input;
2+
using Generators.Output;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Generators.Exercises
7+
{
8+
public class NucleotideCount : Exercise
9+
{
10+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
11+
{
12+
foreach (var canonicalDataCase in canonicalData.Cases)
13+
{
14+
if (!((Dictionary<string, object>)canonicalDataCase.Expected).ContainsKey("error"))
15+
{
16+
canonicalDataCase.UseVariableForExpected = true;
17+
canonicalDataCase.SetConstructorInputParameters("strand");
18+
canonicalDataCase.Expected = ConvertExpected(canonicalDataCase.Expected);
19+
}
20+
}
21+
}
22+
23+
private static dynamic ConvertExpected(dynamic expected)
24+
=> ((Dictionary<string, object>)expected).ToDictionary(kv => kv.Key[0], kv => int.Parse($"{kv.Value}"));
25+
26+
protected override HashSet<string> AddAdditionalNamespaces() => new HashSet<string>() { typeof(Dictionary<char, int>).Namespace };
27+
28+
protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
29+
{
30+
if (testMethodBody.UseVariableForExpected)
31+
{
32+
return RenderEqualBodyAssert(testMethodBody);
33+
}
34+
return RenderThrowsBodyAssert(testMethodBody);
35+
}
36+
37+
private string RenderEqualBodyAssert(TestMethodBody testMethodBody)
38+
{
39+
const string template = @"Assert.Equal(expected, sut.{{ TestedMethodName }});";
40+
41+
var templateParameters = new
42+
{
43+
TestedMethodName = testMethodBody.CanonicalDataCase.Property.ToTestedMethodName()
44+
};
45+
46+
return TemplateRenderer.RenderInline(template, templateParameters);
47+
}
48+
49+
private string RenderThrowsBodyAssert(TestMethodBody testMethodBody)
50+
{
51+
const string template = @"Assert.Throws<InvalidNucleotideException>(() => new NucleotideCount(""{{ Input }}""));";
52+
53+
var templateParameters = new
54+
{
55+
Input = testMethodBody.CanonicalDataCase.Input["strand"]
56+
};
57+
58+
return TemplateRenderer.RenderInline(template, templateParameters);
59+
}
60+
}
61+
}

0 commit comments

Comments
 (0)