Skip to content

Commit

Permalink
And-more-generators (#2361)
Browse files Browse the repository at this point in the history
* alphametics: add generator

* anagram: add generator

* armstrong-numbers: add generator

* Remove old generators

* atbash-cipher: add generator

[no important files changed]
  • Loading branch information
ErikSchierboom authored Feb 3, 2025
1 parent 9d88508 commit d7ceef1
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 99 deletions.
25 changes: 25 additions & 0 deletions exercises/practice/alphametics/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using Xunit;

public class AlphameticsTests
{
{{for test in tests}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{test.methodName}}()
{
{{if test.expected}}
var actual = Alphametics.Solve({{test.input.puzzle | string.literal}});
var expected = new Dictionary<char, int>
{
{{for key in test.expected | object.keys}}
['{{key}}'] = {{test.expected[key]}}{{if !for.last}},{{end}}
{{end}}
};
Assert.Equal(expected, actual);
{{else}}
Assert.Throws<ArgumentException>(() => Alphametics.Solve({{test.input.puzzle | string.literal}}));
{{end}}
}
{{end}}
}
19 changes: 19 additions & 0 deletions exercises/practice/anagram/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Xunit;

public class AnagramTests
{
{{for test in tests}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{test.methodName}}()
{
string[] candidates = {{test.input.candidates}};
var sut = new Anagram({{test.input.subject | string.literal}});
{{if test.expected.empty?}}
Assert.Empty(sut.FindAnagrams(candidates));
{{else}}
string[] expected = {{test.expected}};
Assert.Equal(expected, sut.FindAnagrams(candidates));
{{end}}
}
{{end}}
}
11 changes: 11 additions & 0 deletions exercises/practice/anagram/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ description = "detects anagrams using case-insensitive possible matches"

[7cc195ad-e3c7-44ee-9fd2-d3c344806a2c]
description = "does not detect an anagram if the original word is repeated"
include = false

[630abb71-a94e-4715-8395-179ec1df9f91]
description = "does not detect an anagram if the original word is repeated"
reimplements = "7cc195ad-e3c7-44ee-9fd2-d3c344806a2c"

[9878a1c9-d6ea-4235-ae51-3ea2befd6842]
description = "anagrams must use all letters exactly once"
Expand Down Expand Up @@ -73,3 +78,9 @@ include = false
[33d3f67e-fbb9-49d3-a90e-0beb00861da7]
description = "words other than themselves can be anagrams"
reimplements = "a0705568-628c-4b55-9798-82e4acde51ca"

[a6854f66-eec1-4afd-a137-62ef2870c051]
description = "handles case of greek letters"

[fd3509e5-e3ba-409d-ac3d-a9ac84d13296]
description = "different characters may have the same bytes"
65 changes: 41 additions & 24 deletions exercises/practice/anagram/AnagramTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,136 +5,153 @@ public class AnagramTests
[Fact]
public void No_matches()
{
var candidates = new[] { "hello", "world", "zombies", "pants" };
string[] candidates = ["hello", "world", "zombies", "pants"];
var sut = new Anagram("diaper");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_two_anagrams()
{
var candidates = new[] { "lemons", "cherry", "melons" };
string[] candidates = ["lemons", "cherry", "melons"];
var sut = new Anagram("solemn");
var expected = new[] { "lemons", "melons" };
string[] expected = ["lemons", "melons"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Does_not_detect_anagram_subsets()
{
var candidates = new[] { "dog", "goody" };
string[] candidates = ["dog", "goody"];
var sut = new Anagram("good");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_anagram()
{
var candidates = new[] { "enlists", "google", "inlets", "banana" };
string[] candidates = ["enlists", "google", "inlets", "banana"];
var sut = new Anagram("listen");
var expected = new[] { "inlets" };
string[] expected = ["inlets"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_three_anagrams()
{
var candidates = new[] { "gallery", "ballerina", "regally", "clergy", "largely", "leading" };
string[] candidates = ["gallery", "ballerina", "regally", "clergy", "largely", "leading"];
var sut = new Anagram("allergy");
var expected = new[] { "gallery", "regally", "largely" };
string[] expected = ["gallery", "regally", "largely"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_multiple_anagrams_with_different_case()
{
var candidates = new[] { "Eons", "ONES" };
string[] candidates = ["Eons", "ONES"];
var sut = new Anagram("nose");
var expected = new[] { "Eons", "ONES" };
string[] expected = ["Eons", "ONES"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Does_not_detect_non_anagrams_with_identical_checksum()
{
var candidates = new[] { "last" };
string[] candidates = ["last"];
var sut = new Anagram("mass");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_anagrams_case_insensitively()
{
var candidates = new[] { "cashregister", "Carthorse", "radishes" };
string[] candidates = ["cashregister", "Carthorse", "radishes"];
var sut = new Anagram("Orchestra");
var expected = new[] { "Carthorse" };
string[] expected = ["Carthorse"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_anagrams_using_case_insensitive_subject()
{
var candidates = new[] { "cashregister", "carthorse", "radishes" };
string[] candidates = ["cashregister", "carthorse", "radishes"];
var sut = new Anagram("Orchestra");
var expected = new[] { "carthorse" };
string[] expected = ["carthorse"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Detects_anagrams_using_case_insensitive_possible_matches()
{
var candidates = new[] { "cashregister", "Carthorse", "radishes" };
string[] candidates = ["cashregister", "Carthorse", "radishes"];
var sut = new Anagram("orchestra");
var expected = new[] { "Carthorse" };
string[] expected = ["Carthorse"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Does_not_detect_an_anagram_if_the_original_word_is_repeated()
{
var candidates = new[] { "go Go GO" };
string[] candidates = ["goGoGO"];
var sut = new Anagram("go");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Anagrams_must_use_all_letters_exactly_once()
{
var candidates = new[] { "patter" };
string[] candidates = ["patter"];
var sut = new Anagram("tapper");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Words_are_not_anagrams_of_themselves()
{
var candidates = new[] { "BANANA" };
string[] candidates = ["BANANA"];
var sut = new Anagram("BANANA");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different()
{
var candidates = new[] { "Banana" };
string[] candidates = ["Banana"];
var sut = new Anagram("BANANA");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different()
{
var candidates = new[] { "banana" };
string[] candidates = ["banana"];
var sut = new Anagram("BANANA");
Assert.Empty(sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Words_other_than_themselves_can_be_anagrams()
{
var candidates = new[] { "LISTEN", "Silent" };
string[] candidates = ["LISTEN", "Silent"];
var sut = new Anagram("LISTEN");
var expected = new[] { "Silent" };
string[] expected = ["Silent"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Handles_case_of_greek_letters()
{
string[] candidates = ["ΒΓΑ", "ΒΓΔ", "γβα", "αβγ"];
var sut = new Anagram("ΑΒΓ");
string[] expected = ["ΒΓΑ", "γβα"];
Assert.Equal(expected, sut.FindAnagrams(candidates));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Different_characters_may_have_the_same_bytes()
{
string[] candidates = ["€a"];
var sut = new Anagram("a⬂");
Assert.Empty(sut.FindAnagrams(candidates));
}
}
12 changes: 12 additions & 0 deletions exercises/practice/armstrong-numbers/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

public class ArmstrongNumbersTests
{
{{for test in tests}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{test.methodName}}()
{
Assert.{{test.expected ? "True" : "False"}}(ArmstrongNumbers.IsArmstrongNumber({{test.input.number}}));
}
{{end}}
}
12 changes: 12 additions & 0 deletions exercises/practice/atbash-cipher/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Xunit;

public class AtbashCipherTests
{
{{for test in tests}}
[Fact{{if !for.first}}(Skip = "Remove this Skip property to run this test"){{end}}]
public void {{test.shortMethodName}}()
{
Assert.Equal({{test.expected | string.literal}}, AtbashCipher.{{test.property | string.capitalize}}({{test.input.phrase | string.literal}}));
}
{{end}}
}
23 changes: 0 additions & 23 deletions generators.deprecated/Exercises/Generators/AllYourBase.cs

This file was deleted.

28 changes: 0 additions & 28 deletions generators.deprecated/Exercises/Generators/Alphametics.cs

This file was deleted.

14 changes: 0 additions & 14 deletions generators.deprecated/Exercises/Generators/Anagram.cs

This file was deleted.

This file was deleted.

5 changes: 0 additions & 5 deletions generators.deprecated/Exercises/Generators/AtbashCipher.cs

This file was deleted.

0 comments on commit d7ceef1

Please sign in to comment.