Skip to content

Commit

Permalink
More-generators (#2358)
Browse files Browse the repository at this point in the history
* triangle: simplify generator

* Remove grouped test cases

* yacht: add generator

* word-count: add generator

* variable-length-quantity: add generator

* simple-cipher: add generator

* series: add generator
  • Loading branch information
ErikSchierboom authored Feb 2, 2025
1 parent d3bd5a3 commit 2783eaf
Show file tree
Hide file tree
Showing 17 changed files with 250 additions and 305 deletions.
4 changes: 2 additions & 2 deletions exercises/practice/allergies/.meta/Generator.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Xunit;

public class AllergiesTests
{
{{for testCase in testCasesByProperty.allergicTo}}
{{for testCase in testCases | property "allergicTo" }}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{testCase.testMethodName}}()
{
Expand All @@ -11,7 +11,7 @@ public class AllergiesTests
}
{{end}}

{{for testCase in testCasesByProperty.list}}
{{for testCase in testCases | property "list"}}
[Fact(Skip = "Remove this Skip property to run this test")]
public void {{testCase.testMethodName}}()
{
Expand Down
18 changes: 18 additions & 0 deletions exercises/practice/series/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using Xunit;

public class SeriesTests
{
{{for testCase in testCases}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{testCase.testMethodName}}()
{
{{if testCase.expected.error}}
Assert.Throws<ArgumentException>(() => Series.Slices({{testCase.input.series | string.literal}}, {{testCase.input.sliceLength}}));
{{else}}
string[] expected = {{testCase.expected}};
Assert.Equal(expected, Series.Slices({{testCase.input.series | string.literal}}, {{testCase.input.sliceLength}}));
{{end}}
}
{{end}}
}
35 changes: 17 additions & 18 deletions exercises/practice/series/SeriesTests.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,77 @@
using System;
using System.Linq;
using Xunit;

public class SeriesTests
{
[Fact]
public void Slices_of_one_from_one()
{
var expected = new[] { "1" };
Assert.Equal(expected, Series.Slices("1", 1).ToArray());
string[] expected = ["1"];
Assert.Equal(expected, Series.Slices("1", 1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slices_of_one_from_two()
{
var expected = new[] { "1", "2" };
Assert.Equal(expected, Series.Slices("12", 1).ToArray());
string[] expected = ["1", "2"];
Assert.Equal(expected, Series.Slices("12", 1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slices_of_two()
{
var expected = new[] { "35" };
Assert.Equal(expected, Series.Slices("35", 2).ToArray());
string[] expected = ["35"];
Assert.Equal(expected, Series.Slices("35", 2));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slices_of_two_overlap()
{
var expected = new[] { "91", "14", "42" };
Assert.Equal(expected, Series.Slices("9142", 2).ToArray());
string[] expected = ["91", "14", "42"];
Assert.Equal(expected, Series.Slices("9142", 2));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slices_can_include_duplicates()
{
var expected = new[] { "777", "777", "777", "777" };
Assert.Equal(expected, Series.Slices("777777", 3).ToArray());
string[] expected = ["777", "777", "777", "777"];
Assert.Equal(expected, Series.Slices("777777", 3));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slices_of_a_long_series()
{
var expected = new[] { "91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243" };
Assert.Equal(expected, Series.Slices("918493904243", 5).ToArray());
string[] expected = ["91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243"];
Assert.Equal(expected, Series.Slices("918493904243", 5));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slice_length_is_too_large()
{
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 6).ToArray());
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 6));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slice_length_is_way_too_large()
{
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 42).ToArray());
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 42));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slice_length_cannot_be_zero()
{
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 0).ToArray());
Assert.Throws<ArgumentException>(() => Series.Slices("12345", 0));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Slice_length_cannot_be_negative()
{
Assert.Throws<ArgumentException>(() => Series.Slices("123", -1).ToArray());
Assert.Throws<ArgumentException>(() => Series.Slices("123", -1));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Empty_series_is_invalid()
{
Assert.Throws<ArgumentException>(() => Series.Slices("", 1).ToArray());
Assert.Throws<ArgumentException>(() => Series.Slices("", 1));
}
}
37 changes: 37 additions & 0 deletions exercises/practice/simple-cipher/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Xunit;

{{func decode_arg}}
{{if $0.input.ciphertext == "cipher.encode"}}
sut.Encode({{$0.input.plaintext | string.literal}})
{{else if $0.input.ciphertext == "cipher.key.substring(0, expected.length)"}}
sut.Key.Substring(0, 10)
{{else}}
{{$0.input.ciphertext | string.literal}}
{{end}}
{{end}}

{{func expected_value}}
{{if $0 == "cipher.key.substring(0, plaintext.length)"}}
sut.Key.Substring(0, 10)
{{else}}
{{ $0 | string.literal }}
{{end}}
{{end}}

public class SimpleCipherTests
{
{{for testCase in testCases}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{testCase.testMethodName}}()
{
var sut = new SimpleCipher({{if testCase.input.key}}{{testCase.input.key | string.literal}}{{end}});
{{if testCase.property == "encode"}}
Assert.Equal({{testCase.expected | expected_value}}, sut.Encode({{testCase.input.plaintext | string.literal}}));
{{else if testCase.property == "decode"}}
Assert.Equal({{testCase.expected | string.literal}}, sut.Decode({{testCase | decode_arg}}));
{{else if testCase.property == "key"}}
Assert.Matches({{testCase.expected.match | string.literal}}, sut.Key);
{{end}}
}
{{end}}
}
5 changes: 2 additions & 3 deletions exercises/practice/simple-cipher/SimpleCipherTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Xunit;

public class SimpleCipherTests
Expand All @@ -18,7 +17,7 @@ public void Random_key_cipher_can_decode()
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Random_key_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
public void Random_key_cipher_is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
{
var sut = new SimpleCipher();
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
Expand Down Expand Up @@ -46,7 +45,7 @@ public void Substitution_cipher_can_decode()
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Substitution_cipher_is_reversible_i_e_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
public void Substitution_cipher_is_reversible_ie_if_you_apply_decode_in_a_encoded_result_you_must_see_the_same_plaintext_encode_parameter_as_a_result_of_the_decode_method()
{
var sut = new SimpleCipher("abcdefghij");
Assert.Equal("abcdefghij", sut.Decode(sut.Encode("abcdefghij")));
Expand Down
20 changes: 2 additions & 18 deletions exercises/practice/triangle/.meta/Generator.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,11 @@ using Xunit;

public class TriangleTests
{
{{for testCase in testCasesByProperty.equilateral}}
{{for testCase in testCases}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{testCase.testMethodName}}()
{
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.IsEquilateral({{testCase.input.sides | array.join ", "}}));
}
{{end}}

{{for testCase in testCasesByProperty.isosceles}}
[Fact(Skip = "Remove this Skip property to run this test")]
public void {{testCase.testMethodName}}()
{
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.IsIsosceles({{testCase.input.sides | array.join ", "}}));
}
{{end}}

{{for testCase in testCasesByProperty.scalene}}
[Fact(Skip = "Remove this Skip property to run this test")]
public void {{testCase.testMethodName}}()
{
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.IsScalene({{testCase.input.sides | array.join ", "}}));
Assert.{{testCase.expected ? "True" : "False"}}(Triangle.Is{{testCase.property | string.capitalize}}({{testCase.input.sides | array.join ", "}}));
}
{{end}}
}
27 changes: 27 additions & 0 deletions exercises/practice/variable-length-quantity/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Xunit;

public class VariableLengthQuantityTests
{
{{for testCase in testCases}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{testCase.testMethodName}}()
{
uint[] integers = [
{{for integer in testCase.input.integers}}
{{integer}}{{if !for.last}},{{end}}
{{end}}
];
{{if testCase.expected.error}}
Assert.Throws<InvalidOperationException>(() => VariableLengthQuantity.{{testCase.property | string.capitalize}}(integers));
{{else}}
uint[] expected = [
{{for expected in testCase.expected}}
{{expected}}{{if !for.last}},{{end}}
{{end}}
];
Assert.Equal(expected, VariableLengthQuantity.{{testCase.property | string.capitalize}}(integers));
{{end}}
}
{{end}}
}
Loading

0 comments on commit 2783eaf

Please sign in to comment.