Skip to content

Add Nucleotide Count Test Generator #416 #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions exercises/nucleotide-count/Example.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
using System;
using System.Collections.Generic;

public class DNA
public class NucleotideCount
{
public IDictionary<char, int> NucleotideCounts { get; private set; }

public DNA(string sequence)
public NucleotideCount(string sequence)
{
InitializeNucleotideCounts(sequence);
}

private void InitializeNucleotideCounts(string sequence)
{
NucleotideCounts = new Dictionary<char, int> { { '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;
}
}
}

Expand Down
9 changes: 2 additions & 7 deletions exercises/nucleotide-count/NucleotideCount.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;

public class DNA
public class NucleotideCount
{
public DNA(string sequence)
public NucleotideCount(string sequence)
{
}

Expand All @@ -14,11 +14,6 @@ public IDictionary<char, int> 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 { }
84 changes: 37 additions & 47 deletions exercises/nucleotide-count/NucleotideCountTest.cs
Original file line number Diff line number Diff line change
@@ -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<char, int> { { '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<char, int> { { '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<char, int>
{
['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<char, int>
{
['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<InvalidNucleotideException>(() => dna.Count('X'));
var sut = new NucleotideCount("AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC");
var expected = new Dictionary<char, int>
{
['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<char, int> { { 'A', 20 }, { 'T', 21 }, { 'C', 12 }, { 'G', 17 } };
Assert.Equal(expected, dna.NucleotideCounts);
Assert.Throws<InvalidNucleotideException>(() => new NucleotideCount("AGXXACT"));
}
}
}
61 changes: 61 additions & 0 deletions generators/Exercises/NucleotideCount.cs
Original file line number Diff line number Diff line change
@@ -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<string, object>)canonicalDataCase.Expected).ContainsKey("error"))
{
canonicalDataCase.UseVariableForExpected = true;
canonicalDataCase.SetConstructorInputParameters("strand");
canonicalDataCase.Expected = ConvertExpected(canonicalDataCase.Expected);
}
}
}

private static dynamic ConvertExpected(dynamic expected)
=> ((Dictionary<string, object>)expected).ToDictionary(kv => kv.Key[0], kv => int.Parse($"{kv.Value}"));

protected override HashSet<string> AddAdditionalNamespaces() => new HashSet<string>() { typeof(Dictionary<char, int>).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<InvalidNucleotideException>(() => new NucleotideCount(""{{ Input }}""));";

var templateParameters = new
{
Input = testMethodBody.CanonicalDataCase.Input["strand"]
};

return TemplateRenderer.RenderInline(template, templateParameters);
}
}
}