Skip to content

Commit

Permalink
More templates (#2355)
Browse files Browse the repository at this point in the history
* affine-cipher: add template

* allergies: add template

* strain: add template

* sublist: add template

[no important files changed]

* darts: add template

* triangle: add template

* Remove deprecated
  • Loading branch information
ErikSchierboom authored Jan 29, 2025
1 parent d504da2 commit f2c700c
Show file tree
Hide file tree
Showing 14 changed files with 250 additions and 175 deletions.
29 changes: 29 additions & 0 deletions exercises/practice/affine-cipher/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Xunit;

public class AffineCipherTests
{
{{#test_cases_by_property.encode}}
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
public void {{short_test_method_name}}()
{
{{#if expected.error}}
Assert.Throws<ArgumentException>(() => AffineCipher.Encode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
{{else}}
Assert.Equal({{lit expected}}, AffineCipher.Encode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
{{/if}}
}
{{/test_cases_by_property.encode}}

{{#test_cases_by_property.decode}}
[Fact(Skip = "Remove this Skip property to run this test")]
public void {{short_test_method_name}}()
{
{{#if expected.error}}
Assert.Throws<ArgumentException>(() => AffineCipher.Decode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
{{else}}
Assert.Equal({{lit expected}}, AffineCipher.Decode({{lit input.phrase}}, {{input.key.a}}, {{input.key.b}}));
{{/if}}
}
{{/test_cases_by_property.decode}}
}
27 changes: 27 additions & 0 deletions exercises/practice/allergies/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Xunit;

public class AllergiesTests
{
{{#test_cases_by_property.allergicTo}}
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
public void {{test_method_name}}()
{
var sut = new Allergies({{input.score}});
Assert.{{expected}}(sut.IsAllergicTo(Allergen.{{Capitalize input.item}}));
}
{{/test_cases_by_property.allergicTo}}

{{#test_cases_by_property.list}}
[Fact(Skip = "Remove this Skip property to run this test")]
public void {{test_method_name}}()
{
var sut = new Allergies({{input.score}});
{{#isempty expected}}
Assert.Empty(sut.List());
{{else}}
Allergen[] expected = [{{#each ../expected}}Allergen.{{Capitalize .}}{{#unless @last}},{{/unless}}{{/each}}];
Assert.Equal(expected, sut.List());
{{/isempty}}
}
{{/test_cases_by_property.list}}
}
38 changes: 19 additions & 19 deletions exercises/practice/allergies/AllergiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,81 +283,81 @@ public void Testing_for_cats_allergy_allergic_to_everything()
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void No_allergies()
public void List_when_no_allergies()
{
var sut = new Allergies(0);
Assert.Empty(sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Just_eggs()
public void List_when_just_eggs()
{
var sut = new Allergies(1);
var expected = new[] { Allergen.Eggs };
Allergen[] expected = [Allergen.Eggs];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Just_peanuts()
public void List_when_just_peanuts()
{
var sut = new Allergies(2);
var expected = new[] { Allergen.Peanuts };
Allergen[] expected = [Allergen.Peanuts];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Just_strawberries()
public void List_when_just_strawberries()
{
var sut = new Allergies(8);
var expected = new[] { Allergen.Strawberries };
Allergen[] expected = [Allergen.Strawberries];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Eggs_and_peanuts()
public void List_when_eggs_and_peanuts()
{
var sut = new Allergies(3);
var expected = new[] { Allergen.Eggs, Allergen.Peanuts };
Allergen[] expected = [Allergen.Eggs, Allergen.Peanuts];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void More_than_eggs_but_not_peanuts()
public void List_when_more_than_eggs_but_not_peanuts()
{
var sut = new Allergies(5);
var expected = new[] { Allergen.Eggs, Allergen.Shellfish };
Allergen[] expected = [Allergen.Eggs, Allergen.Shellfish];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Lots_of_stuff()
public void List_when_lots_of_stuff()
{
var sut = new Allergies(248);
var expected = new[] { Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
Allergen[] expected = [Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Everything()
public void List_when_everything()
{
var sut = new Allergies(255);
var expected = new[] { Allergen.Eggs, Allergen.Peanuts, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
Allergen[] expected = [Allergen.Eggs, Allergen.Peanuts, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void No_allergen_score_parts()
public void List_when_no_allergen_score_parts()
{
var sut = new Allergies(509);
var expected = new[] { Allergen.Eggs, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats };
Allergen[] expected = [Allergen.Eggs, Allergen.Shellfish, Allergen.Strawberries, Allergen.Tomatoes, Allergen.Chocolate, Allergen.Pollen, Allergen.Cats];
Assert.Equal(expected, sut.List());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void No_allergen_score_parts_without_highest_valid_score()
public void List_when_no_allergen_score_parts_without_highest_valid_score()
{
var sut = new Allergies(257);
var expected = new[] { Allergen.Eggs };
Allergen[] expected = [Allergen.Eggs];
Assert.Equal(expected, sut.List());
}
}
12 changes: 12 additions & 0 deletions exercises/practice/darts/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

public class DartsTests
{
{{#test_cases}}
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
public void {{test_method_name}}()
{
Assert.Equal({{expected}}, Darts.Score({{input.x}}, {{input.y}}));
}
{{/test_cases}}
}
50 changes: 50 additions & 0 deletions exercises/practice/strain/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections.Generic;
using System.Linq;
using Xunit;

public class StrainTests
{
{{#test_cases_by_property.keep}}
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
public void {{test_method_name}}()
{
{{#equals description "keeps lists"}}
int[][] expected = [{{../expected}}];
int[][] input = [{{#../input.list}}[{{.}}]{{#unless @last}}, {{/unless}}{{/../input.list}}];
Assert.Equal(expected, input.Keep(x => x.Contains(5)).ToArray());
{{else}}
{{#equals ../description "keeps strings"}}
string[] expected = [{{#../../expected}}{{lit .}}{{#unless @last}}, {{/unless}}{{/../../expected}}];
string[] input = [{{#../../input.list}}{{lit .}}{{#unless @last}}, {{/unless}}{{/../../input.list}}];
Assert.Equal(expected, input.Keep(x => x.StartsWith('z')).ToArray());
{{else}}
int[] expected = [{{../../expected}}];
int[] input = [{{../../input.list}}];
Assert.Equal(expected, input.Keep(x => {{replace ../../input.predicate "fn(x) -> " ""}}).ToArray());
{{/equals}}
{{/equals}}
}
{{/test_cases_by_property.keep}}

{{#test_cases_by_property.discard}}
[Fact(Skip = "Remove this Skip property to run this test")]
public void {{test_method_name}}()
{
{{#equals description "discards lists"}}
int[][] expected = [{{../expected}}];
int[][] input = [{{#../input.list}}[{{.}}]{{#unless @last}}, {{/unless}}{{/../input.list}}];
Assert.Equal(expected, input.Discard(x => x.Contains(5)).ToArray());
{{else}}
{{#equals ../description "discards strings"}}
string[] expected = [{{#../../expected}}{{lit .}}{{#unless @last}}, {{/unless}}{{/../../expected}}];
string[] input = [{{#../../input.list}}{{lit .}}{{#unless @last}}, {{/unless}}{{/../../input.list}}];
Assert.Equal(expected, input.Discard(x => x.StartsWith('z')).ToArray());
{{else}}
int[] expected = [{{../../expected}}];
int[] input = [{{../../input.list}}];
Assert.Equal(expected, input.Discard(x => {{replace ../../input.predicate "fn(x) -> " ""}}).ToArray());
{{/equals}}
{{/equals}}
}
{{/test_cases_by_property.discard}}
}
110 changes: 57 additions & 53 deletions exercises/practice/strain/StrainTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,110 +5,114 @@
public class StrainTests
{
[Fact]
public void Empty_keep()
public void Keep_on_empty_list_returns_empty_list()
{
Assert.Equal(new LinkedList<int>(), new LinkedList<int>().Keep(x => x < 10));
int[] expected = [];
int[] input = [];
Assert.Equal(expected, input.Keep(x => true).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Keep_everything()
public void Keeps_everything()
{
Assert.Equal(new HashSet<int> { 1, 2, 3 }, new HashSet<int> { 1, 2, 3 }.Keep(x => x < 10));
int[] expected = [1, 3, 5];
int[] input = [1, 3, 5];
Assert.Equal(expected, input.Keep(x => true).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Keep_nothing()
public void Keeps_nothing()
{
Assert.Equal(new HashSet<int>(), new HashSet<int> { 1, 2, 3 }.Keep(x => x > 10));
int[] expected = [];
int[] input = [1, 3, 5];
Assert.Equal(expected, input.Keep(x => false).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Keep_first_and_last()
public void Keeps_first_and_last()
{
Assert.Equal(new[] { 1, 3 }, new[] { 1, 2, 3 }.Keep(x => x % 2 != 0));
int[] expected = [1, 3];
int[] input = [1, 2, 3];
Assert.Equal(expected, input.Keep(x => x % 2 == 1).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Keep_neither_first_nor_last()
public void Keeps_neither_first_nor_last()
{
Assert.Equal(new List<int> { 2, 4 }, new List<int> { 1, 2, 3, 4, 5 }.Keep(x => x % 2 == 0));
int[] expected = [2];
int[] input = [1, 2, 3];
Assert.Equal(expected, input.Keep(x => x % 2 == 0).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Keep_strings()
public void Keeps_strings()
{
var words = "apple zebra banana zombies cherimoya zelot".Split(' ');
Assert.Equal("zebra zombies zelot".Split(' '), words.Keep(x => x.StartsWith("z")));
string[] expected = ["zebra", "zombies", "zealot"];
string[] input = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"];
Assert.Equal(expected, input.Keep(x => x.StartsWith('z')).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Keep_arrays()
public void Keeps_lists()
{
var actual = new[]
{
new[] { 1, 2, 3 },
new[] { 5, 5, 5 },
new[] { 5, 1, 2 },
new[] { 2, 1, 2 },
new[] { 1, 5, 2 },
new[] { 2, 2, 1 },
new[] { 1, 2, 5 }
};
var expected = new[] { new[] { 5, 5, 5 }, new[] { 5, 1, 2 }, new[] { 1, 5, 2 }, new[] { 1, 2, 5 } };
Assert.Equal(expected, actual.Keep(x => x.Contains(5)));
int[][] expected = [[5, 5, 5], [5, 1, 2], [1, 5, 2], [1, 2, 5]];
int[][] input = [[1, 2, 3], [5, 5, 5], [5, 1, 2], [2, 1, 2], [1, 5, 2], [2, 2, 1], [1, 2, 5]];
Assert.Equal(expected, input.Keep(x => x.Contains(5)).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Empty_discard()
public void Discard_on_empty_list_returns_empty_list()
{
Assert.Equal(new LinkedList<int>(), new LinkedList<int>().Discard(x => x < 10));
int[] expected = [];
int[] input = [];
Assert.Equal(expected, input.Discard(x => true).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Discard_everything()
public void Discards_everything()
{
Assert.Equal(new HashSet<int>(), new HashSet<int> { 1, 2, 3 }.Discard(x => x < 10));
int[] expected = [];
int[] input = [1, 3, 5];
Assert.Equal(expected, input.Discard(x => true).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Discard_nothing()
public void Discards_nothing()
{
Assert.Equal(new HashSet<int> { 1, 2, 3 }, new HashSet<int> { 1, 2, 3 }.Discard(x => x > 10));
int[] expected = [1, 3, 5];
int[] input = [1, 3, 5];
Assert.Equal(expected, input.Discard(x => false).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Discard_first_and_last()
public void Discards_first_and_last()
{
Assert.Equal(new[] { 2 }, new[] { 1, 2, 3 }.Discard(x => x % 2 != 0));
int[] expected = [2];
int[] input = [1, 2, 3];
Assert.Equal(expected, input.Discard(x => x % 2 == 1).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Discard_neither_first_nor_last()
public void Discards_neither_first_nor_last()
{
Assert.Equal(new List<int> { 1, 3, 5 }, new List<int> { 1, 2, 3, 4, 5 }.Discard(x => x % 2 == 0));
int[] expected = [1, 3];
int[] input = [1, 2, 3];
Assert.Equal(expected, input.Discard(x => x % 2 == 0).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Discard_strings()
public void Discards_strings()
{
var words = "apple zebra banana zombies cherimoya zelot".Split(' ');
Assert.Equal("apple banana cherimoya".Split(' '), words.Discard(x => x.StartsWith("z")));
string[] expected = ["apple", "banana", "cherimoya"];
string[] input = ["apple", "zebra", "banana", "zombies", "cherimoya", "zealot"];
Assert.Equal(expected, input.Discard(x => x.StartsWith('z')).ToArray());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Discard_arrays()
public void Discards_lists()
{
var actual = new[]
{
new[] { 1, 2, 3 },
new[] { 5, 5, 5 },
new[] { 5, 1, 2 },
new[] { 2, 1, 2 },
new[] { 1, 5, 2 },
new[] { 2, 2, 1 },
new[] { 1, 2, 5 }
};
var expected = new[] { new[] { 1, 2, 3 }, new[] { 2, 1, 2 }, new[] { 2, 2, 1 } };
Assert.Equal(expected, actual.Discard(x => x.Contains(5)));
int[][] expected = [[1, 2, 3], [2, 1, 2], [2, 2, 1]];
int[][] input = [[1, 2, 3], [5, 5, 5], [5, 1, 2], [2, 1, 2], [1, 5, 2], [2, 2, 1], [1, 2, 5]];
Assert.Equal(expected, input.Discard(x => x.Contains(5)).ToArray());
}
}
}
Loading

0 comments on commit f2c700c

Please sign in to comment.