diff --git a/exercises/practice/bowling/.meta/Generator.tpl b/exercises/practice/bowling/.meta/Generator.tpl new file mode 100644 index 0000000000..58aff57c84 --- /dev/null +++ b/exercises/practice/bowling/.meta/Generator.tpl @@ -0,0 +1,26 @@ +using System; +using System.Collections.Generic; +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.testMethod }}() + { + var sut = new BowlingGame(); + {{- if !test.input.previousRolls.empty? }} + int[] previousRolls = {{ test.input.previousRolls }}; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + {{- end -}} + {{- if test.expected.error }} + Assert.Throws(() => sut.{{ test.testedMethod }}({{ test.input.roll }})); + {{ else }} + Assert.Equal({{ test.expected }}, sut.{{ test.testedMethod }}({{ test.input.roll }})); + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/bowling/BowlingTests.cs b/exercises/practice/bowling/BowlingTests.cs index d6a66401b3..cdc82b0bbe 100644 --- a/exercises/practice/bowling/BowlingTests.cs +++ b/exercises/practice/bowling/BowlingTests.cs @@ -8,158 +8,186 @@ public class BowlingTests public void Should_be_able_to_score_a_game_with_all_zeros() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(0, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(0, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Should_be_able_to_score_a_game_with_no_strikes_or_spares() { var sut = new BowlingGame(); - var previousRolls = new[] { 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(90, actual); + int[] previousRolls = [3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6, 3, 6]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(90, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void A_spare_followed_by_zeros_is_worth_ten_points() { var sut = new BowlingGame(); - var previousRolls = new[] { 6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(10, actual); + int[] previousRolls = [6, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(10, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Points_scored_in_the_roll_after_a_spare_are_counted_twice() { var sut = new BowlingGame(); - var previousRolls = new[] { 6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(16, actual); + int[] previousRolls = [6, 4, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(16, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Consecutive_spares_each_get_a_one_roll_bonus() { var sut = new BowlingGame(); - var previousRolls = new[] { 5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(31, actual); + int[] previousRolls = [5, 5, 3, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(31, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void A_spare_in_the_last_frame_gets_a_one_roll_bonus_that_is_counted_once() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(17, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 7]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(17, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void A_strike_earns_ten_points_in_a_frame_with_a_single_roll() { var sut = new BowlingGame(); - var previousRolls = new[] { 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(10, actual); + int[] previousRolls = [10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(10, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Points_scored_in_the_two_rolls_after_a_strike_are_counted_twice_as_a_bonus() { var sut = new BowlingGame(); - var previousRolls = new[] { 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(26, actual); + int[] previousRolls = [10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(26, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Consecutive_strikes_each_get_the_two_roll_bonus() { var sut = new BowlingGame(); - var previousRolls = new[] { 10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(81, actual); + int[] previousRolls = [10, 10, 10, 5, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(81, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void A_strike_in_the_last_frame_gets_a_two_roll_bonus_that_is_counted_once() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(18, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 1]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(18, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Rolling_a_spare_with_the_two_roll_bonus_does_not_get_a_bonus_roll() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(20, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 7, 3]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(20, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Strikes_with_the_two_roll_bonus_do_not_get_bonus_rolls() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(30, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(30, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Last_two_strikes_followed_by_only_last_bonus_with_non_strike_points() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 1 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(31, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 0, 1]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(31, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void A_strike_with_the_one_roll_bonus_after_a_spare_in_the_last_frame_does_not_get_a_bonus() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(20, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(20, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void All_strikes_is_a_perfect_game() { var sut = new BowlingGame(); - var previousRolls = new[] { 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(300, actual); + int[] previousRolls = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(300, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Rolls_cannot_score_negative_points() { var sut = new BowlingGame(); - var previousRolls = Array.Empty(); - DoRoll(previousRolls, sut); Assert.Throws(() => sut.Roll(-1)); } @@ -167,8 +195,6 @@ public void Rolls_cannot_score_negative_points() public void A_roll_cannot_score_more_than_10_points() { var sut = new BowlingGame(); - var previousRolls = Array.Empty(); - DoRoll(previousRolls, sut); Assert.Throws(() => sut.Roll(11)); } @@ -176,8 +202,11 @@ public void A_roll_cannot_score_more_than_10_points() public void Two_rolls_in_a_frame_cannot_score_more_than_10_points() { var sut = new BowlingGame(); - var previousRolls = new[] { 5 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [5]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(6)); } @@ -185,8 +214,11 @@ public void Two_rolls_in_a_frame_cannot_score_more_than_10_points() public void Bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(11)); } @@ -194,8 +226,11 @@ public void Bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_1 public void Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 5]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(6)); } @@ -203,18 +238,23 @@ public void Two_bonus_rolls_after_a_strike_in_the_last_frame_cannot_score_more_t public void Two_bonus_rolls_after_a_strike_in_the_last_frame_can_score_more_than_10_points_if_one_is_a_strike() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6 }; - DoRoll(previousRolls, sut); - var actual = sut.Score(); - Assert.Equal(26, actual); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10, 6]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } + Assert.Equal(26, sut.Score()); } [Fact(Skip = "Remove this Skip property to run this test")] public void The_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_strike_if_the_first_one_is_not_a_strike() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 6]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(10)); } @@ -222,8 +262,11 @@ public void The_second_bonus_rolls_after_a_strike_in_the_last_frame_cannot_be_a_ public void Second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more_than_10_points() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(11)); } @@ -231,8 +274,6 @@ public void Second_bonus_roll_after_a_strike_in_the_last_frame_cannot_score_more public void An_unstarted_game_cannot_be_scored() { var sut = new BowlingGame(); - var previousRolls = Array.Empty(); - DoRoll(previousRolls, sut); Assert.Throws(() => sut.Score()); } @@ -240,8 +281,11 @@ public void An_unstarted_game_cannot_be_scored() public void An_incomplete_game_cannot_be_scored() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Score()); } @@ -249,8 +293,11 @@ public void An_incomplete_game_cannot_be_scored() public void Cannot_roll_if_game_already_has_ten_frames() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(0)); } @@ -258,8 +305,11 @@ public void Cannot_roll_if_game_already_has_ten_frames() public void Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Score()); } @@ -267,8 +317,11 @@ public void Bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_sco public void Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_before_score_can_be_calculated() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 10]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Score()); } @@ -276,8 +329,11 @@ public void Both_bonus_rolls_for_a_strike_in_the_last_frame_must_be_rolled_befor public void Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score_can_be_calculated() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Score()); } @@ -285,8 +341,11 @@ public void Bonus_roll_for_a_spare_in_the_last_frame_must_be_rolled_before_score public void Cannot_roll_after_bonus_roll_for_spare() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2 }; - DoRoll(previousRolls, sut); + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 3, 2]; + foreach (var roll in previousRolls) + { + sut.Roll(roll); + } Assert.Throws(() => sut.Roll(2)); } @@ -294,16 +353,11 @@ public void Cannot_roll_after_bonus_roll_for_spare() public void Cannot_roll_after_bonus_rolls_for_strike() { var sut = new BowlingGame(); - var previousRolls = new[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2 }; - DoRoll(previousRolls, sut); - Assert.Throws(() => sut.Roll(2)); - } - - private void DoRoll(IEnumerable rolls, BowlingGame sut) - { - foreach (var roll in rolls) + int[] previousRolls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 3, 2]; + foreach (var roll in previousRolls) { sut.Roll(roll); } + Assert.Throws(() => sut.Roll(2)); } } diff --git a/exercises/practice/circular-buffer/.meta/Generator.tpl b/exercises/practice/circular-buffer/.meta/Generator.tpl new file mode 100644 index 0000000000..8b36da908b --- /dev/null +++ b/exercises/practice/circular-buffer/.meta/Generator.tpl @@ -0,0 +1,24 @@ +using System; +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.testMethod }}() + { + var buffer = new {{ testedClass }}(capacity: {{ test.input.capacity }}); + {{- for operation in test.input.operations }} + {{- if operation.should_succeed != false }} + {{- if operation.operation == "read" }} + Assert.Equal({{ operation.expected }}, buffer.{{ operation.operation | string.capitalize }}()); + {{- else }} + buffer.{{ operation.operation | string.capitalize }}({{ operation.item }}); + {{- end -}} + {{- else }} + Assert.Throws(() => buffer.{{ operation.operation | string.capitalize }}({{ operation.item }})); + {{ end -}} + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/circular-buffer/CircularBufferTests.cs b/exercises/practice/circular-buffer/CircularBufferTests.cs index 55e2a2362d..dbf2af7ab8 100644 --- a/exercises/practice/circular-buffer/CircularBufferTests.cs +++ b/exercises/practice/circular-buffer/CircularBufferTests.cs @@ -38,7 +38,7 @@ public void Items_are_read_in_the_order_they_are_written() } [Fact(Skip = "Remove this Skip property to run this test")] - public void Full_buffer_cant_be_written_to() + public void Full_buffer_can_t_be_written_to() { var buffer = new CircularBuffer(capacity: 1); buffer.Write(1); @@ -68,7 +68,7 @@ public void Read_position_is_maintained_even_across_multiple_writes() } [Fact(Skip = "Remove this Skip property to run this test")] - public void Items_cleared_out_of_buffer_cant_be_read() + public void Items_cleared_out_of_buffer_can_t_be_read() { var buffer = new CircularBuffer(capacity: 1); buffer.Write(1); diff --git a/exercises/practice/complex-numbers/.meta/Generator.tpl b/exercises/practice/complex-numbers/.meta/Generator.tpl new file mode 100644 index 0000000000..b7a21a2cfd --- /dev/null +++ b/exercises/practice/complex-numbers/.meta/Generator.tpl @@ -0,0 +1,39 @@ +{{ func toarg + case (object.typeof $0) + when "array" + ret $"new ComplexNumber({toarg $0[0]}, {toarg $0[1]})" + when "string" + str = string.replace $0 "ln(2)" "Math.Log(2.0)" + str = string.replace str "pi" "Math.PI" + str = string.replace str "e" "Math.E" + ret str + else + ret $0 + end +end }} + +using System; +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.shortTestMethod }}() + { + {{- if test.input.z1 }} + var actual = {{ test.input.z1 | toarg }}.{{ test.testedMethod }}({{ test.input.z2 | toarg }}); + Assert.Equal({{ test.expected[0] | toarg }}, actual.Real()); + Assert.Equal({{ test.expected[1] | toarg }}, actual.Imaginary()); + {{- else if test.input.z }} + {{- if test.expected | object.typeof == "number" }} + Assert.Equal({{ test.expected | toarg }}, {{ test.input.z | toarg }}.{{ test.testedMethod }}()); + {{- else }} + var actual = {{ test.input.z | toarg }}.{{ test.testedMethod }}(); + Assert.Equal({{ test.expected[0] | toarg }}, actual.Real(){{ if test.property == "exp" }}, precision: 7{{ end }}); + Assert.Equal({{ test.expected[1] | toarg }}, actual.Imaginary(){{ if test.property == "exp" }}, precision: 7{{ end }}); + {{ end -}} + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/complex-numbers/ComplexNumbersTests.cs b/exercises/practice/complex-numbers/ComplexNumbersTests.cs index 213784cb34..32c114021e 100644 --- a/exercises/practice/complex-numbers/ComplexNumbersTests.cs +++ b/exercises/practice/complex-numbers/ComplexNumbersTests.cs @@ -6,293 +6,258 @@ public class ComplexNumbersTests [Fact] public void Real_part_of_a_purely_real_number() { - var sut = new ComplexNumber(1, 0); - Assert.Equal(1, sut.Real()); + Assert.Equal(1, new ComplexNumber(1, 0).Real()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Real_part_of_a_purely_imaginary_number() { - var sut = new ComplexNumber(0, 1); - Assert.Equal(0, sut.Real()); + Assert.Equal(0, new ComplexNumber(0, 1).Real()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Real_part_of_a_number_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 2); - Assert.Equal(1, sut.Real()); + Assert.Equal(1, new ComplexNumber(1, 2).Real()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Imaginary_part_of_a_purely_real_number() { - var sut = new ComplexNumber(1, 0); - Assert.Equal(0, sut.Imaginary()); + Assert.Equal(0, new ComplexNumber(1, 0).Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Imaginary_part_of_a_purely_imaginary_number() { - var sut = new ComplexNumber(0, 1); - Assert.Equal(1, sut.Imaginary()); + Assert.Equal(1, new ComplexNumber(0, 1).Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Imaginary_part_of_a_number_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 2); - Assert.Equal(2, sut.Imaginary()); + Assert.Equal(2, new ComplexNumber(1, 2).Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Imaginary_unit() { - var sut = new ComplexNumber(0, 1); - var expected = new ComplexNumber(-1, 0); - Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 1)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 1)).Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 1).Mul(new ComplexNumber(0, 1)); + Assert.Equal(-1, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_purely_real_numbers() { - var sut = new ComplexNumber(1, 0); - var expected = new ComplexNumber(3, 0); - Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(2, 0)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(2, 0)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 0).Add(new ComplexNumber(2, 0)); + Assert.Equal(3, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_purely_imaginary_numbers() { - var sut = new ComplexNumber(0, 1); - var expected = new ComplexNumber(0, 3); - Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(0, 2)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(0, 2)).Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 1).Add(new ComplexNumber(0, 2)); + Assert.Equal(0, actual.Real()); + Assert.Equal(3, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_numbers_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 2); - var expected = new ComplexNumber(4, 6); - Assert.Equal(expected.Real(), sut.Add(new ComplexNumber(3, 4)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Add(new ComplexNumber(3, 4)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 2).Add(new ComplexNumber(3, 4)); + Assert.Equal(4, actual.Real()); + Assert.Equal(6, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_purely_real_numbers() { - var sut = new ComplexNumber(1, 0); - var expected = new ComplexNumber(-1, 0); - Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(2, 0)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(2, 0)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 0).Sub(new ComplexNumber(2, 0)); + Assert.Equal(-1, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_purely_imaginary_numbers() { - var sut = new ComplexNumber(0, 1); - var expected = new ComplexNumber(0, -1); - Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(0, 2)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(0, 2)).Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 1).Sub(new ComplexNumber(0, 2)); + Assert.Equal(0, actual.Real()); + Assert.Equal(-1, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_numbers_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 2); - var expected = new ComplexNumber(-2, -2); - Assert.Equal(expected.Real(), sut.Sub(new ComplexNumber(3, 4)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Sub(new ComplexNumber(3, 4)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 2).Sub(new ComplexNumber(3, 4)); + Assert.Equal(-2, actual.Real()); + Assert.Equal(-2, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_purely_real_numbers() { - var sut = new ComplexNumber(1, 0); - var expected = new ComplexNumber(2, 0); - Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(2, 0)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(2, 0)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 0).Mul(new ComplexNumber(2, 0)); + Assert.Equal(2, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_purely_imaginary_numbers() { - var sut = new ComplexNumber(0, 1); - var expected = new ComplexNumber(-2, 0); - Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(0, 2)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(0, 2)).Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 1).Mul(new ComplexNumber(0, 2)); + Assert.Equal(-2, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_numbers_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 2); - var expected = new ComplexNumber(-5, 10); - Assert.Equal(expected.Real(), sut.Mul(new ComplexNumber(3, 4)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Mul(new ComplexNumber(3, 4)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 2).Mul(new ComplexNumber(3, 4)); + Assert.Equal(-5, actual.Real()); + Assert.Equal(10, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_purely_real_numbers() { - var sut = new ComplexNumber(1, 0); - var expected = new ComplexNumber(0.5, 0); - Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(2, 0)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(2, 0)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 0).Div(new ComplexNumber(2, 0)); + Assert.Equal(0.5, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_purely_imaginary_numbers() { - var sut = new ComplexNumber(0, 1); - var expected = new ComplexNumber(0.5, 0); - Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(0, 2)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(0, 2)).Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 1).Div(new ComplexNumber(0, 2)); + Assert.Equal(0.5, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_numbers_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 2); - var expected = new ComplexNumber(0.44, 0.08); - Assert.Equal(expected.Real(), sut.Div(new ComplexNumber(3, 4)).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Div(new ComplexNumber(3, 4)).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 2).Div(new ComplexNumber(3, 4)); + Assert.Equal(0.44, actual.Real()); + Assert.Equal(0.08, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Absolute_value_of_a_positive_purely_real_number() { - var sut = new ComplexNumber(5, 0); - Assert.Equal(5, sut.Abs()); + Assert.Equal(5, new ComplexNumber(5, 0).Abs()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Absolute_value_of_a_negative_purely_real_number() { - var sut = new ComplexNumber(-5, 0); - Assert.Equal(5, sut.Abs()); + Assert.Equal(5, new ComplexNumber(-5, 0).Abs()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Absolute_value_of_a_purely_imaginary_number_with_positive_imaginary_part() { - var sut = new ComplexNumber(0, 5); - Assert.Equal(5, sut.Abs()); + Assert.Equal(5, new ComplexNumber(0, 5).Abs()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Absolute_value_of_a_purely_imaginary_number_with_negative_imaginary_part() { - var sut = new ComplexNumber(0, -5); - Assert.Equal(5, sut.Abs()); + Assert.Equal(5, new ComplexNumber(0, -5).Abs()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Absolute_value_of_a_number_with_real_and_imaginary_part() { - var sut = new ComplexNumber(3, 4); - Assert.Equal(5, sut.Abs()); + Assert.Equal(5, new ComplexNumber(3, 4).Abs()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Conjugate_a_purely_real_number() { - var sut = new ComplexNumber(5, 0); - var expected = new ComplexNumber(5, 0); - Assert.Equal(expected.Real(), sut.Conjugate().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), precision: 7); + var actual = new ComplexNumber(5, 0).Conjugate(); + Assert.Equal(5, actual.Real()); + Assert.Equal(0, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Conjugate_a_purely_imaginary_number() { - var sut = new ComplexNumber(0, 5); - var expected = new ComplexNumber(0, -5); - Assert.Equal(expected.Real(), sut.Conjugate().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 5).Conjugate(); + Assert.Equal(0, actual.Real()); + Assert.Equal(-5, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Conjugate_a_number_with_real_and_imaginary_part() { - var sut = new ComplexNumber(1, 1); - var expected = new ComplexNumber(1, -1); - Assert.Equal(expected.Real(), sut.Conjugate().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Conjugate().Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 1).Conjugate(); + Assert.Equal(1, actual.Real()); + Assert.Equal(-1, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Eulers_identity_formula() + public void Euler_s_identity_formula() { - var sut = new ComplexNumber(0, Math.PI); - var expected = new ComplexNumber(-1, 0); - Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 7); + var actual = new ComplexNumber(0, Math.PI).Exp(); + Assert.Equal(-1, actual.Real(), precision: 7); + Assert.Equal(0, actual.Imaginary(), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Exponential_of_0() { - var sut = new ComplexNumber(0, 0); - var expected = new ComplexNumber(1, 0); - Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 7); + var actual = new ComplexNumber(0, 0).Exp(); + Assert.Equal(1, actual.Real(), precision: 7); + Assert.Equal(0, actual.Imaginary(), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Exponential_of_a_purely_real_number() { - var sut = new ComplexNumber(1, 0); - var expected = new ComplexNumber(Math.E, 0); - Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 0).Exp(); + Assert.Equal(Math.E, actual.Real(), precision: 7); + Assert.Equal(0, actual.Imaginary(), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Exponential_of_a_number_with_real_and_imaginary_part() { - var sut = new ComplexNumber(Math.Log(2.0), Math.PI); - var expected = new ComplexNumber(-2, 0); - Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 7); + var actual = new ComplexNumber(Math.Log(2.0), Math.PI).Exp(); + Assert.Equal(-2, actual.Real(), precision: 7); + Assert.Equal(0, actual.Imaginary(), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Exponential_resulting_in_a_number_with_real_and_imaginary_part() { - var sut = new ComplexNumber(Math.Log(2.0)/2, Math.PI/4); - var expected = new ComplexNumber(1, 1); - Assert.Equal(expected.Real(), sut.Exp().Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Exp().Imaginary(), precision: 7); + var actual = new ComplexNumber(Math.Log(2.0) / 2, Math.PI / 4).Exp(); + Assert.Equal(1, actual.Real(), precision: 7); + Assert.Equal(1, actual.Imaginary(), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_real_number_to_complex_number() { - var sut = new ComplexNumber(1, 2); - var expected = new ComplexNumber(6, 2); - Assert.Equal(expected.Real(), sut.Add(5).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Add(5).Imaginary(), precision: 7); + var actual = new ComplexNumber(1, 2).Add(5); + Assert.Equal(6, actual.Real()); + Assert.Equal(2, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_complex_number_by_real_number() { - var sut = new ComplexNumber(2, 5); - var expected = new ComplexNumber(10, 25); - Assert.Equal(expected.Real(), sut.Mul(5).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Mul(5).Imaginary(), precision: 7); + var actual = new ComplexNumber(2, 5).Mul(5); + Assert.Equal(10, actual.Real()); + Assert.Equal(25, actual.Imaginary()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_complex_number_by_real_number() { - var sut = new ComplexNumber(10, 100); - var expected = new ComplexNumber(1, 10); - Assert.Equal(expected.Real(), sut.Div(10).Real(), precision: 7); - Assert.Equal(expected.Imaginary(), sut.Div(10).Imaginary(), precision: 7); + var actual = new ComplexNumber(10, 100).Div(10); + Assert.Equal(1, actual.Real()); + Assert.Equal(10, actual.Imaginary()); } } diff --git a/exercises/practice/custom-set/.meta/Generator.tpl b/exercises/practice/custom-set/.meta/Generator.tpl new file mode 100644 index 0000000000..7a31420291 --- /dev/null +++ b/exercises/practice/custom-set/.meta/Generator.tpl @@ -0,0 +1,34 @@ +{{ func toset + $"new CustomSet({$0})" +end }} +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.testMethod }}() + { + var sut = {{ test.input.set | toset }}; + {{- if test.property == "empty" }} + Assert.{{ test.expected ? "True" : "False" }}(sut.{{ test.testedMethod }}()); + {{- else if test.property == "contains" }} + Assert.{{ test.expected ? "True" : "False" }}(sut.{{ test.testedMethod }}({{ test.input.element }})); + {{- else if test.property == "add" }} + var expected = {{ test.expected | toset }}; + Assert.Equal(expected, sut.{{ test.testedMethod }}({{ test.input.element }})); + {{- else }} + var set1 = {{ test.input.set1 | toset }}; + var set2 = {{ test.input.set2 | toset }}; + {{- if test.property == "equal" }} + Assert.{{ test.expected ? "Equal" : "NotEqual" }}(set1, set2); + {{- else if test.expected | object.typeof == "array"}} + var expected = {{ test.expected | toset }}; + Assert.Equal(expected, set1.{{ test.testedMethod }}(set2)); + {{- else }} + Assert.{{ test.expected ? "True" : "False" }}(set1.{{ test.testedMethod }}(set2)); + {{ end -}} + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/custom-set/CustomSetTests.cs b/exercises/practice/custom-set/CustomSetTests.cs index 4f9a3dd7d9..5e0a344b50 100644 --- a/exercises/practice/custom-set/CustomSetTests.cs +++ b/exercises/practice/custom-set/CustomSetTests.cs @@ -3,304 +3,363 @@ public class CustomSetTests { [Fact] - public void Sets_with_no_elements_are_empty() + public void Returns_true_if_the_set_contains_no_elements_sets_with_no_elements_are_empty() { - var sut = new CustomSet(); + var sut = new CustomSet([]); Assert.True(sut.Empty()); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Sets_with_elements_are_not_empty() + public void Returns_true_if_the_set_contains_no_elements_sets_with_elements_are_not_empty() { - var sut = new CustomSet(new[] { 1 }); + var sut = new CustomSet([1]); Assert.False(sut.Empty()); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Nothing_is_contained_in_an_empty_set() + public void Sets_can_report_if_they_contain_an_element_nothing_is_contained_in_an_empty_set() { - var element = 1; - var sut = new CustomSet(); - Assert.False(sut.Contains(element)); + var sut = new CustomSet([]); + Assert.False(sut.Contains(1)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void When_the_element_is_in_the_set() + public void Sets_can_report_if_they_contain_an_element_when_the_element_is_in_the_set() { - var element = 1; - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.True(sut.Contains(element)); + var sut = new CustomSet([1, 2, 3]); + Assert.True(sut.Contains(1)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void When_the_element_is_not_in_the_set() + public void Sets_can_report_if_they_contain_an_element_when_the_element_is_not_in_the_set() { - var element = 4; - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.False(sut.Contains(element)); + var sut = new CustomSet([1, 2, 3]); + Assert.False(sut.Contains(4)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Empty_set_is_a_subset_of_another_empty_set() + public void A_set_is_a_subset_if_all_of_its_elements_are_contained_in_the_other_set_empty_set_is_a_subset_of_another_empty_set() { - var set2 = new CustomSet(); var sut = new CustomSet(); - Assert.True(sut.Subset(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([]); + Assert.True(set1.Subset(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Empty_set_is_a_subset_of_non_empty_set() + public void A_set_is_a_subset_if_all_of_its_elements_are_contained_in_the_other_set_empty_set_is_a_subset_of_non_empty_set() { - var set2 = new CustomSet(new[] { 1 }); var sut = new CustomSet(); - Assert.True(sut.Subset(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([1]); + Assert.True(set1.Subset(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Non_empty_set_is_not_a_subset_of_empty_set() + public void A_set_is_a_subset_if_all_of_its_elements_are_contained_in_the_other_set_non_empty_set_is_not_a_subset_of_empty_set() { - var set2 = new CustomSet(); - var sut = new CustomSet(new[] { 1 }); - Assert.False(sut.Subset(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1]); + var set2 = new CustomSet([]); + Assert.False(set1.Subset(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Set_is_a_subset_of_set_with_exact_same_elements() + public void A_set_is_a_subset_if_all_of_its_elements_are_contained_in_the_other_set_set_is_a_subset_of_set_with_exact_same_elements() { - var set2 = new CustomSet(new[] { 1, 2, 3 }); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.True(sut.Subset(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([1, 2, 3]); + Assert.True(set1.Subset(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Set_is_a_subset_of_larger_set_with_same_elements() + public void A_set_is_a_subset_if_all_of_its_elements_are_contained_in_the_other_set_set_is_a_subset_of_larger_set_with_same_elements() { - var set2 = new CustomSet(new[] { 4, 1, 2, 3 }); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.True(sut.Subset(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([4, 1, 2, 3]); + Assert.True(set1.Subset(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Set_is_not_a_subset_of_set_that_does_not_contain_its_elements() + public void A_set_is_a_subset_if_all_of_its_elements_are_contained_in_the_other_set_set_is_not_a_subset_of_set_that_does_not_contain_its_elements() { - var set2 = new CustomSet(new[] { 4, 1, 3 }); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.False(sut.Subset(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([4, 1, 3]); + Assert.False(set1.Subset(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void The_empty_set_is_disjoint_with_itself() + public void Sets_are_disjoint_if_they_share_no_elements_the_empty_set_is_disjoint_with_itself() { - var set2 = new CustomSet(); var sut = new CustomSet(); - Assert.True(sut.Disjoint(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([]); + Assert.True(set1.Disjoint(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Empty_set_is_disjoint_with_non_empty_set() + public void Sets_are_disjoint_if_they_share_no_elements_empty_set_is_disjoint_with_non_empty_set() { - var set2 = new CustomSet(new[] { 1 }); var sut = new CustomSet(); - Assert.True(sut.Disjoint(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([1]); + Assert.True(set1.Disjoint(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Non_empty_set_is_disjoint_with_empty_set() + public void Sets_are_disjoint_if_they_share_no_elements_non_empty_set_is_disjoint_with_empty_set() { - var set2 = new CustomSet(); - var sut = new CustomSet(new[] { 1 }); - Assert.True(sut.Disjoint(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1]); + var set2 = new CustomSet([]); + Assert.True(set1.Disjoint(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Sets_are_not_disjoint_if_they_share_an_element() + public void Sets_are_disjoint_if_they_share_no_elements_sets_are_not_disjoint_if_they_share_an_element() { - var set2 = new CustomSet(new[] { 2, 3 }); - var sut = new CustomSet(new[] { 1, 2 }); - Assert.False(sut.Disjoint(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2]); + var set2 = new CustomSet([2, 3]); + Assert.False(set1.Disjoint(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Sets_are_disjoint_if_they_share_no_elements() + public void Sets_are_disjoint_if_they_share_no_elements_sets_are_disjoint_if_they_share_no_elements() { - var set2 = new CustomSet(new[] { 3, 4 }); - var sut = new CustomSet(new[] { 1, 2 }); - Assert.True(sut.Disjoint(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2]); + var set2 = new CustomSet([3, 4]); + Assert.True(set1.Disjoint(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Empty_sets_are_equal() + public void Sets_with_the_same_elements_are_equal_empty_sets_are_equal() { - var set2 = new CustomSet(); var sut = new CustomSet(); - Assert.True(sut.Equals(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([]); + Assert.Equal(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Empty_set_is_not_equal_to_non_empty_set() + public void Sets_with_the_same_elements_are_equal_empty_set_is_not_equal_to_non_empty_set() { - var set2 = new CustomSet(new[] { 1, 2, 3 }); var sut = new CustomSet(); - Assert.False(sut.Equals(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([1, 2, 3]); + Assert.NotEqual(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Non_empty_set_is_not_equal_to_empty_set() + public void Sets_with_the_same_elements_are_equal_non_empty_set_is_not_equal_to_empty_set() { - var set2 = new CustomSet(); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.False(sut.Equals(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([]); + Assert.NotEqual(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Sets_with_the_same_elements_are_equal() + public void Sets_with_the_same_elements_are_equal_sets_with_the_same_elements_are_equal() { - var set2 = new CustomSet(new[] { 2, 1 }); - var sut = new CustomSet(new[] { 1, 2 }); - Assert.True(sut.Equals(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2]); + var set2 = new CustomSet([2, 1]); + Assert.Equal(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Sets_with_different_elements_are_not_equal() + public void Sets_with_the_same_elements_are_equal_sets_with_different_elements_are_not_equal() { - var set2 = new CustomSet(new[] { 1, 2, 4 }); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.False(sut.Equals(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([1, 2, 4]); + Assert.NotEqual(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Set_is_not_equal_to_larger_set_with_same_elements() + public void Sets_with_the_same_elements_are_equal_set_is_not_equal_to_larger_set_with_same_elements() { - var set2 = new CustomSet(new[] { 1, 2, 3, 4 }); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.False(sut.Equals(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([1, 2, 3, 4]); + Assert.NotEqual(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Add_to_empty_set() + public void Sets_with_the_same_elements_are_equal_set_is_equal_to_a_set_constructed_from_an_array_with_duplicates() { - var element = 3; var sut = new CustomSet(); - Assert.Equal(new CustomSet(new[] { 3 }), sut.Add(element)); + var set1 = new CustomSet([1]); + var set2 = new CustomSet([1, 1]); + Assert.Equal(set1, set2); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Add_to_non_empty_set() + public void Unique_elements_can_be_added_to_a_set_add_to_empty_set() { - var element = 3; - var sut = new CustomSet(new[] { 1, 2, 4 }); - Assert.Equal(new CustomSet(new[] { 1, 2, 3, 4 }), sut.Add(element)); + var sut = new CustomSet([]); + var expected = new CustomSet([3]); + Assert.Equal(expected, sut.Add(3)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Adding_an_existing_element_does_not_change_the_set() + public void Unique_elements_can_be_added_to_a_set_add_to_non_empty_set() { - var element = 3; - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.Equal(new CustomSet(new[] { 1, 2, 3 }), sut.Add(element)); + var sut = new CustomSet([1, 2, 4]); + var expected = new CustomSet([1, 2, 3, 4]); + Assert.Equal(expected, sut.Add(3)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Intersection_of_two_empty_sets_is_an_empty_set() + public void Unique_elements_can_be_added_to_a_set_adding_an_existing_element_does_not_change_the_set() + { + var sut = new CustomSet([1, 2, 3]); + var expected = new CustomSet([1, 2, 3]); + Assert.Equal(expected, sut.Add(3)); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Intersection_returns_a_set_of_all_shared_elements_intersection_of_two_empty_sets_is_an_empty_set() + { + var sut = new CustomSet(); + var set1 = new CustomSet([]); + var set2 = new CustomSet([]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Intersection(set2)); + } + + [Fact(Skip = "Remove this Skip property to run this test")] + public void Intersection_returns_a_set_of_all_shared_elements_intersection_of_an_empty_set_and_non_empty_set_is_an_empty_set() { - var set2 = new CustomSet(); var sut = new CustomSet(); - Assert.Equal(new CustomSet(), sut.Intersection(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([3, 2, 5]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Intersection(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Intersection_of_an_empty_set_and_non_empty_set_is_an_empty_set() + public void Intersection_returns_a_set_of_all_shared_elements_intersection_of_a_non_empty_set_and_an_empty_set_is_an_empty_set() { - var set2 = new CustomSet(new[] { 3, 2, 5 }); var sut = new CustomSet(); - Assert.Equal(new CustomSet(), sut.Intersection(set2)); + var set1 = new CustomSet([1, 2, 3, 4]); + var set2 = new CustomSet([]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Intersection(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Intersection_of_a_non_empty_set_and_an_empty_set_is_an_empty_set() + public void Intersection_returns_a_set_of_all_shared_elements_intersection_of_two_sets_with_no_shared_elements_is_an_empty_set() { - var set2 = new CustomSet(); - var sut = new CustomSet(new[] { 1, 2, 3, 4 }); - Assert.Equal(new CustomSet(), sut.Intersection(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3]); + var set2 = new CustomSet([4, 5, 6]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Intersection(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Intersection_of_two_sets_with_no_shared_elements_is_an_empty_set() + public void Intersection_returns_a_set_of_all_shared_elements_intersection_of_two_sets_with_shared_elements_is_a_set_of_the_shared_elements() { - var set2 = new CustomSet(new[] { 4, 5, 6 }); - var sut = new CustomSet(new[] { 1, 2, 3 }); - Assert.Equal(new CustomSet(), sut.Intersection(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 2, 3, 4]); + var set2 = new CustomSet([3, 2, 5]); + var expected = new CustomSet([2, 3]); + Assert.Equal(expected, set1.Intersection(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Intersection_of_two_sets_with_shared_elements_is_a_set_of_the_shared_elements() + public void Difference_or_complement_of_a_set_is_a_set_of_all_elements_that_are_only_in_the_first_set_difference_of_two_empty_sets_is_an_empty_set() { - var set2 = new CustomSet(new[] { 3, 2, 5 }); - var sut = new CustomSet(new[] { 1, 2, 3, 4 }); - Assert.Equal(new CustomSet(new[] { 2, 3 }), sut.Intersection(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([]); + var set2 = new CustomSet([]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Difference(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Difference_of_two_empty_sets_is_an_empty_set() + public void Difference_or_complement_of_a_set_is_a_set_of_all_elements_that_are_only_in_the_first_set_difference_of_empty_set_and_non_empty_set_is_an_empty_set() { - var set2 = new CustomSet(); var sut = new CustomSet(); - Assert.Equal(new CustomSet(), sut.Difference(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([3, 2, 5]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Difference(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Difference_of_empty_set_and_non_empty_set_is_an_empty_set() + public void Difference_or_complement_of_a_set_is_a_set_of_all_elements_that_are_only_in_the_first_set_difference_of_a_non_empty_set_and_an_empty_set_is_the_non_empty_set() { - var set2 = new CustomSet(new[] { 3, 2, 5 }); var sut = new CustomSet(); - Assert.Equal(new CustomSet(), sut.Difference(set2)); + var set1 = new CustomSet([1, 2, 3, 4]); + var set2 = new CustomSet([]); + var expected = new CustomSet([1, 2, 3, 4]); + Assert.Equal(expected, set1.Difference(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Difference_of_a_non_empty_set_and_an_empty_set_is_the_non_empty_set() + public void Difference_or_complement_of_a_set_is_a_set_of_all_elements_that_are_only_in_the_first_set_difference_of_two_non_empty_sets_is_a_set_of_elements_that_are_only_in_the_first_set() { - var set2 = new CustomSet(); - var sut = new CustomSet(new[] { 1, 2, 3, 4 }); - Assert.Equal(new CustomSet(new[] { 1, 2, 3, 4 }), sut.Difference(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([3, 2, 1]); + var set2 = new CustomSet([2, 4]); + var expected = new CustomSet([1, 3]); + Assert.Equal(expected, set1.Difference(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Difference_of_two_non_empty_sets_is_a_set_of_elements_that_are_only_in_the_first_set() + public void Difference_or_complement_of_a_set_is_a_set_of_all_elements_that_are_only_in_the_first_set_difference_removes_all_duplicates_in_the_first_set() { - var set2 = new CustomSet(new[] { 2, 4 }); - var sut = new CustomSet(new[] { 3, 2, 1 }); - Assert.Equal(new CustomSet(new[] { 1, 3 }), sut.Difference(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 1]); + var set2 = new CustomSet([1]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Difference(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Union_of_empty_sets_is_an_empty_set() + public void Union_returns_a_set_of_all_elements_in_either_set_union_of_empty_sets_is_an_empty_set() { - var set2 = new CustomSet(); var sut = new CustomSet(); - Assert.Equal(new CustomSet(), sut.Union(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([]); + var expected = new CustomSet([]); + Assert.Equal(expected, set1.Union(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Union_of_an_empty_set_and_non_empty_set_is_the_non_empty_set() + public void Union_returns_a_set_of_all_elements_in_either_set_union_of_an_empty_set_and_non_empty_set_is_the_non_empty_set() { - var set2 = new CustomSet(new[] { 2 }); var sut = new CustomSet(); - Assert.Equal(new CustomSet(new[] { 2 }), sut.Union(set2)); + var set1 = new CustomSet([]); + var set2 = new CustomSet([2]); + var expected = new CustomSet([2]); + Assert.Equal(expected, set1.Union(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Union_of_a_non_empty_set_and_empty_set_is_the_non_empty_set() + public void Union_returns_a_set_of_all_elements_in_either_set_union_of_a_non_empty_set_and_empty_set_is_the_non_empty_set() { - var set2 = new CustomSet(); - var sut = new CustomSet(new[] { 1, 3 }); - Assert.Equal(new CustomSet(new[] { 1, 3 }), sut.Union(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 3]); + var set2 = new CustomSet([]); + var expected = new CustomSet([1, 3]); + Assert.Equal(expected, set1.Union(set2)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Union_of_non_empty_sets_contains_all_unique_elements() + public void Union_returns_a_set_of_all_elements_in_either_set_union_of_non_empty_sets_contains_all_unique_elements() { - var set2 = new CustomSet(new[] { 2, 3 }); - var sut = new CustomSet(new[] { 1, 3 }); - Assert.Equal(new CustomSet(new[] { 3, 2, 1 }), sut.Union(set2)); + var sut = new CustomSet(); + var set1 = new CustomSet([1, 3]); + var set2 = new CustomSet([2, 3]); + var expected = new CustomSet([3, 2, 1]); + Assert.Equal(expected, set1.Union(set2)); } } diff --git a/exercises/practice/dnd-character/.meta/Generator.tpl b/exercises/practice/dnd-character/.meta/Generator.tpl new file mode 100644 index 0000000000..b76b3dc91b --- /dev/null +++ b/exercises/practice/dnd-character/.meta/Generator.tpl @@ -0,0 +1,72 @@ +using System.Collections.Generic; +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.shortTestMethod }}() + { + {{- if test.property == "ability" }} + for (var i = 0; i < 10; i++) + { + Assert.InRange(DndCharacter.Ability(), 3, 18); + } + {{- else if test.property == "character" && (test.scenarios | array.contains "random") }} + for (var i = 0; i < 10; i++) + { + var sut = DndCharacter.Generate(); + Assert.InRange(sut.Strength, 3, 18); + Assert.InRange(sut.Dexterity, 3, 18); + Assert.InRange(sut.Constitution, 3, 18); + Assert.InRange(sut.Intelligence, 3, 18); + Assert.InRange(sut.Wisdom, 3, 18); + Assert.InRange(sut.Charisma, 3, 18); + Assert.Equal(sut.Hitpoints, 10 + DndCharacter.Modifier(sut.Constitution)); + } + {{- else if test.property == "character" }} + for (var i = 0; i < 10; i++) + { + var sut = DndCharacter.Generate(); + Assert.Equal(sut.Strength, sut.Strength); + Assert.Equal(sut.Dexterity, sut.Dexterity); + Assert.Equal(sut.Constitution, sut.Constitution); + Assert.Equal(sut.Intelligence, sut.Intelligence); + Assert.Equal(sut.Wisdom, sut.Wisdom); + Assert.Equal(sut.Charisma, sut.Charisma); + } + {{ else }} + Assert.Equal({{ test.expected }}, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.score }})); + {{ end -}} + } + {{ end }} + [Fact(Skip = "Remove this Skip property to run this test")] + public void Random_ability_is_distributed_correctly() + { + var expectedDistribution = new Dictionary + { + [3] = 1, [4] = 4, + [5] = 10, [6] = 21, + [7] = 38, [8] = 62, + [9] = 91, [10] = 122, + [11] = 148, [12] = 167, + [13] = 172, [14] = 160, + [15] = 131, [16] = 94, + [17] = 54, [18] = 21 + }; + + var actualDistribution = new Dictionary(expectedDistribution); + foreach (var key in actualDistribution.Keys) + actualDistribution[key] = 0; + + const int times = 250; + const int possibleCombinationsCount = 6*6*6*6; // 4d6 + for (var i = 0; i < times * possibleCombinationsCount; i++) + actualDistribution[DndCharacter.Ability()]++; + + const double minTimes = times * 0.8; + const double maxTimes = times * 1.2; + foreach (var k in expectedDistribution.Keys) + Assert.InRange(actualDistribution[k], expectedDistribution[k] * minTimes, expectedDistribution[k] * maxTimes); + } +} diff --git a/exercises/practice/dnd-character/DndCharacterTests.cs b/exercises/practice/dnd-character/DndCharacterTests.cs index b37690d7fc..09a15588ce 100644 --- a/exercises/practice/dnd-character/DndCharacterTests.cs +++ b/exercises/practice/dnd-character/DndCharacterTests.cs @@ -1,47 +1,46 @@ using System.Collections.Generic; -using System.Linq; using Xunit; public class DndCharacterTests { [Fact] - public void Ability_modifier_for_score_3_is_minus_4() + public void Ability_modifier_for_score_3_is_4() { Assert.Equal(-4, DndCharacter.Modifier(3)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Ability_modifier_for_score_4_is_minus_3() + public void Ability_modifier_for_score_4_is_3() { Assert.Equal(-3, DndCharacter.Modifier(4)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Ability_modifier_for_score_5_is_minus_3() + public void Ability_modifier_for_score_5_is_3() { Assert.Equal(-3, DndCharacter.Modifier(5)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Ability_modifier_for_score_6_is_minus_2() + public void Ability_modifier_for_score_6_is_2() { Assert.Equal(-2, DndCharacter.Modifier(6)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Ability_modifier_for_score_7_is_minus_2() + public void Ability_modifier_for_score_7_is_2() { Assert.Equal(-2, DndCharacter.Modifier(7)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Ability_modifier_for_score_8_is_minus_1() + public void Ability_modifier_for_score_8_is_1() { Assert.Equal(-1, DndCharacter.Modifier(8)); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Ability_modifier_for_score_9_is_minus_1() + public void Ability_modifier_for_score_9_is_1() { Assert.Equal(-1, DndCharacter.Modifier(9)); } @@ -145,25 +144,33 @@ public void Random_ability_is_distributed_correctly() { var expectedDistribution = new Dictionary { - [3] = 1, [4] = 4, - [5] = 10, [6] = 21, - [7] = 38, [8] = 62, - [9] = 91, [10] = 122, - [11] = 148, [12] = 167, - [13] = 172, [14] = 160, - [15] = 131, [16] = 94, - [17] = 54, [18] = 21 + [3] = 1, + [4] = 4, + [5] = 10, + [6] = 21, + [7] = 38, + [8] = 62, + [9] = 91, + [10] = 122, + [11] = 148, + [12] = 167, + [13] = 172, + [14] = 160, + [15] = 131, + [16] = 94, + [17] = 54, + [18] = 21 }; - + var actualDistribution = new Dictionary(expectedDistribution); foreach (var key in actualDistribution.Keys) - actualDistribution[key] = 0; - + actualDistribution[key] = 0; + const int times = 250; - const int possibleCombinationsCount = 6*6*6*6; // 4d6 + const int possibleCombinationsCount = 6 * 6 * 6 * 6; // 4d6 for (var i = 0; i < times * possibleCombinationsCount; i++) actualDistribution[DndCharacter.Ability()]++; - + const double minTimes = times * 0.8; const double maxTimes = times * 1.2; foreach (var k in expectedDistribution.Keys) diff --git a/exercises/practice/go-counting/.meta/Generator.tpl b/exercises/practice/go-counting/.meta/Generator.tpl new file mode 100644 index 0000000000..84dd267e2f --- /dev/null +++ b/exercises/practice/go-counting/.meta/Generator.tpl @@ -0,0 +1,44 @@ +using System; +using System.Collections.Generic; +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.testMethod }}() + { + var board = + {{- for line in test.input.board }} + {{ if for.last -}} + {{ line | string.literal -}}; + {{- else -}} + {{ line | string.append "\n" | string.literal }} + + {{- end -}} + {{- end }} + var sut = new {{ testedClass }}(board); + {{- if test.expected.error }} + var coordinate = ({{ test.input.x }}, {{ test.input.y }}); + Assert.Throws(() => sut.Territory(coordinate)); + {{- else if test.property == "territory" }} + var coordinate = ({{ test.input.x }}, {{ test.input.y }}); + var (territoryOwner, territoryCoordinates) = sut.Territory(coordinate); + var expectedOwner = {{ test.expected.owner | string.downcase | enum "Owner" }}; + var expectedCoordinates = new HashSet<(int, int)>{{ if test.expected.territory.empty? }}(){{ else }}{ {{ for territory in test.expected.territory }}({{ territory | array.join ", " }}){{ if !for.last }}, {{ end }}{{ end }} }{{ end }}; + Assert.Equal(expectedOwner, territoryOwner); + Assert.Equal(expectedCoordinates, territoryCoordinates); + {{- else if test.property == "territories" }} + var actual = sut.Territories(); + var expected = new Dictionary> + { + [Owner.Black] = new HashSet<(int, int)>() { {{ for territory in test.expected.territoryBlack }}({{ territory | array.join ", " }}){{ if !for.last }}, {{ end }}{{ end }} }, + [Owner.White] = new HashSet<(int, int)>() { {{ for territory in test.expected.territoryWhite }}({{ territory | array.join ", " }}){{ if !for.last }}, {{ end }}{{ end }} }, + [Owner.None] = new HashSet<(int, int)>() { {{ for territory in test.expected.territoryNone }}({{ territory | array.join ", " }}){{ if !for.last }}, {{ end }}{{ end }} } + }; + Assert.Equal(expected[Owner.Black], actual[Owner.Black]); + Assert.Equal(expected[Owner.White], actual[Owner.White]); + Assert.Equal(expected[Owner.None], actual[Owner.None]); + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/go-counting/GoCountingTests.cs b/exercises/practice/go-counting/GoCountingTests.cs index 78792dcbd1..f7e27f2af9 100644 --- a/exercises/practice/go-counting/GoCountingTests.cs +++ b/exercises/practice/go-counting/GoCountingTests.cs @@ -7,138 +7,143 @@ public class GoCountingTests [Fact] public void Black_corner_territory_on_5x5_board() { - var coordinate = (0, 1); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); - var actual = sut.Territory(coordinate); - var expected = (Owner.Black, new HashSet<(int, int)> { (0, 0), (0, 1), (1, 0) }); - Assert.Equal(expected.Item1, actual.Item1); - Assert.Equal(expected.Item2, actual.Item2); + var coordinate = (0, 1); + var (territoryOwner, territoryCoordinates) = sut.Territory(coordinate); + var expectedOwner = Owner.Black; + var expectedCoordinates = new HashSet<(int, int)> { (0, 0), (0, 1), (1, 0) }; + Assert.Equal(expectedOwner, territoryOwner); + Assert.Equal(expectedCoordinates, territoryCoordinates); } [Fact(Skip = "Remove this Skip property to run this test")] public void White_center_territory_on_5x5_board() { - var coordinate = (2, 3); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); - var actual = sut.Territory(coordinate); - var expected = (Owner.White, new HashSet<(int, int)> { (2, 3) }); - Assert.Equal(expected.Item1, actual.Item1); - Assert.Equal(expected.Item2, actual.Item2); + var coordinate = (2, 3); + var (territoryOwner, territoryCoordinates) = sut.Territory(coordinate); + var expectedOwner = Owner.White; + var expectedCoordinates = new HashSet<(int, int)> { (2, 3) }; + Assert.Equal(expectedOwner, territoryOwner); + Assert.Equal(expectedCoordinates, territoryCoordinates); } [Fact(Skip = "Remove this Skip property to run this test")] public void Open_corner_territory_on_5x5_board() { - var coordinate = (1, 4); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); - var actual = sut.Territory(coordinate); - var expected = (Owner.None, new HashSet<(int, int)> { (0, 3), (0, 4), (1, 4) }); - Assert.Equal(expected.Item1, actual.Item1); - Assert.Equal(expected.Item2, actual.Item2); + var coordinate = (1, 4); + var (territoryOwner, territoryCoordinates) = sut.Territory(coordinate); + var expectedOwner = Owner.None; + var expectedCoordinates = new HashSet<(int, int)> { (0, 3), (0, 4), (1, 4) }; + Assert.Equal(expectedOwner, territoryOwner); + Assert.Equal(expectedCoordinates, territoryCoordinates); } [Fact(Skip = "Remove this Skip property to run this test")] public void A_stone_and_not_a_territory_on_5x5_board() { - var coordinate = (1, 1); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); - var actual = sut.Territory(coordinate); - var expected = (Owner.None, new HashSet<(int, int)>()); - Assert.Equal(expected.Item1, actual.Item1); - Assert.Equal(expected.Item2, actual.Item2); + var coordinate = (1, 1); + var (territoryOwner, territoryCoordinates) = sut.Territory(coordinate); + var expectedOwner = Owner.None; + var expectedCoordinates = new HashSet<(int, int)>(); + Assert.Equal(expectedOwner, territoryOwner); + Assert.Equal(expectedCoordinates, territoryCoordinates); } [Fact(Skip = "Remove this Skip property to run this test")] public void Invalid_because_x_is_too_low_for_5x5_board() { - var coordinate = (-1, 1); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); + var coordinate = (-1, 1); Assert.Throws(() => sut.Territory(coordinate)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Invalid_because_x_is_too_high_for_5x5_board() { - var coordinate = (5, 1); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); + var coordinate = (5, 1); Assert.Throws(() => sut.Territory(coordinate)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Invalid_because_y_is_too_low_for_5x5_board() { - var coordinate = (1, -1); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); + var coordinate = (1, -1); Assert.Throws(() => sut.Territory(coordinate)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Invalid_because_y_is_too_high_for_5x5_board() { - var coordinate = (1, 5); - var board = + var board = " B \n" + " B B \n" + "B W B\n" + " W W \n" + " W "; var sut = new GoCounting(board); + var coordinate = (1, 5); Assert.Throws(() => sut.Territory(coordinate)); } [Fact(Skip = "Remove this Skip property to run this test")] public void One_territory_is_the_whole_board() { - var board = " "; + var board = + " "; var sut = new GoCounting(board); var actual = sut.Territories(); var expected = new Dictionary> { - [Owner.Black] = new HashSet<(int, int)>(), - [Owner.White] = new HashSet<(int, int)>(), - [Owner.None] = new HashSet<(int, int)> { (0, 0) } + [Owner.Black] = new HashSet<(int, int)>() { }, + [Owner.White] = new HashSet<(int, int)>() { }, + [Owner.None] = new HashSet<(int, int)>() { (0, 0) } }; Assert.Equal(expected[Owner.Black], actual[Owner.Black]); Assert.Equal(expected[Owner.White], actual[Owner.White]); @@ -148,16 +153,16 @@ public void One_territory_is_the_whole_board() [Fact(Skip = "Remove this Skip property to run this test")] public void Two_territory_rectangular_board() { - var board = + var board = " BW \n" + " BW "; var sut = new GoCounting(board); var actual = sut.Territories(); var expected = new Dictionary> { - [Owner.Black] = new HashSet<(int, int)> { (0, 0), (0, 1) }, - [Owner.White] = new HashSet<(int, int)> { (3, 0), (3, 1) }, - [Owner.None] = new HashSet<(int, int)>() + [Owner.Black] = new HashSet<(int, int)>() { (0, 0), (0, 1) }, + [Owner.White] = new HashSet<(int, int)>() { (3, 0), (3, 1) }, + [Owner.None] = new HashSet<(int, int)>() { } }; Assert.Equal(expected[Owner.Black], actual[Owner.Black]); Assert.Equal(expected[Owner.White], actual[Owner.White]); @@ -167,14 +172,15 @@ public void Two_territory_rectangular_board() [Fact(Skip = "Remove this Skip property to run this test")] public void Two_region_rectangular_board() { - var board = " B "; + var board = + " B "; var sut = new GoCounting(board); var actual = sut.Territories(); var expected = new Dictionary> { - [Owner.Black] = new HashSet<(int, int)> { (0, 0), (2, 0) }, - [Owner.White] = new HashSet<(int, int)>(), - [Owner.None] = new HashSet<(int, int)>() + [Owner.Black] = new HashSet<(int, int)>() { (0, 0), (2, 0) }, + [Owner.White] = new HashSet<(int, int)>() { }, + [Owner.None] = new HashSet<(int, int)>() { } }; Assert.Equal(expected[Owner.Black], actual[Owner.Black]); Assert.Equal(expected[Owner.White], actual[Owner.White]); diff --git a/exercises/practice/high-scores/.meta/Generator.tpl b/exercises/practice/high-scores/.meta/Generator.tpl new file mode 100644 index 0000000000..b52f96dfcb --- /dev/null +++ b/exercises/practice/high-scores/.meta/Generator.tpl @@ -0,0 +1,30 @@ +{{ func toarg + if (object.typeof $0) == "array" + ret "new List {" + (array.join $0 ", ") + "}" + else + ret $0 + end +end }} + +using System.Collections.Generic; +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.shortTestMethod }}() + { + var sut = new {{ testedClass }}({{ test.input.scores | toarg }}); + {{- if test.property == "latestAfterTopThree" || test.property == "scoresAfterTopThree" }} + var _ = sut.PersonalTopThree(); + Assert.Equal({{ test.expected | toarg }}, sut.{{ test.testedMethod | string.replace "AfterTopThree" "" | string.capitalize }}()); + {{- else if test.property == "latestAfterBest" || test.property == "scoresAfterBest" }} + var _ = sut.PersonalBest(); + Assert.Equal({{ test.expected | toarg }}, sut.{{ test.testedMethod | string.replace "AfterBest" "" | string.capitalize }}()); + {{- else }} + Assert.Equal({{ test.expected | toarg }}, sut.{{ test.testedMethod }}()); + {{- end }} + } + {{ end -}} +} diff --git a/exercises/practice/rational-numbers/.meta/Generator.tpl b/exercises/practice/rational-numbers/.meta/Generator.tpl new file mode 100644 index 0000000000..27bce40f9b --- /dev/null +++ b/exercises/practice/rational-numbers/.meta/Generator.tpl @@ -0,0 +1,40 @@ +{{ func operand + case $0 + when "add" + ret "+" + when "sub" + ret "-" + when "mul" + ret "*" + when "div" + ret "/" + end +end }} + +{{ func toarg + case (object.typeof $0) + when "array" + ret $"new RationalNumber({toarg $0[0]}, {toarg $0[1]})" + else + ret $0 + end +end }} + +using Xunit; + +public class {{ testClass }} +{ + {{- for test in tests }} + [Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}] + public void {{ test.shortTestMethod }}() + { + {{- if test.input.r1 }} + Assert.Equal({{ test.expected | toarg }}, {{ test.input.r1 | toarg }} {{ test.property | operand }} {{ test.input.r2 | toarg }}); + {{- else if test.input.x }} + Assert.Equal({{ test.expected | toarg }}, {{ test.input.x | toarg }}.{{ test.testedMethod }}({{ test.input.r | toarg }}), precision: 7); + {{- else if test.input.r }} + Assert.Equal({{ test.expected | toarg }}, {{ test.input.r | toarg }}.{{ test.testedMethod }}({{ test.input.n | toarg }})); + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/rational-numbers/RationalNumbersTests.cs b/exercises/practice/rational-numbers/RationalNumbersTests.cs index 3fbd095b33..859505baa8 100644 --- a/exercises/practice/rational-numbers/RationalNumbersTests.cs +++ b/exercises/practice/rational-numbers/RationalNumbersTests.cs @@ -5,109 +5,109 @@ public class RationalNumbersTests [Fact] public void Add_two_positive_rational_numbers() { - Assert.Equal(new RationalNumber(7, 6), new RationalNumber(1, 2) + (new RationalNumber(2, 3))); + Assert.Equal(new RationalNumber(7, 6), new RationalNumber(1, 2) + new RationalNumber(2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_a_positive_rational_number_and_a_negative_rational_number() { - Assert.Equal(new RationalNumber(-1, 6), new RationalNumber(1, 2) + (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(-1, 6), new RationalNumber(1, 2) + new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_two_negative_rational_numbers() { - Assert.Equal(new RationalNumber(-7, 6), new RationalNumber(-1, 2) + (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(-7, 6), new RationalNumber(-1, 2) + new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_a_rational_number_to_its_additive_inverse() { - Assert.Equal(new RationalNumber(0, 1), new RationalNumber(1, 2) + (new RationalNumber(-1, 2))); + Assert.Equal(new RationalNumber(0, 1), new RationalNumber(1, 2) + new RationalNumber(-1, 2)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_two_positive_rational_numbers() { - Assert.Equal(new RationalNumber(-1, 6), new RationalNumber(1, 2) - (new RationalNumber(2, 3))); + Assert.Equal(new RationalNumber(-1, 6), new RationalNumber(1, 2) - new RationalNumber(2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_a_positive_rational_number_and_a_negative_rational_number() { - Assert.Equal(new RationalNumber(7, 6), new RationalNumber(1, 2) - (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(7, 6), new RationalNumber(1, 2) - new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_two_negative_rational_numbers() { - Assert.Equal(new RationalNumber(1, 6), new RationalNumber(-1, 2) - (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(1, 6), new RationalNumber(-1, 2) - new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_a_rational_number_from_itself() { - Assert.Equal(new RationalNumber(0, 1), new RationalNumber(1, 2) - (new RationalNumber(1, 2))); + Assert.Equal(new RationalNumber(0, 1), new RationalNumber(1, 2) - new RationalNumber(1, 2)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_two_positive_rational_numbers() { - Assert.Equal(new RationalNumber(1, 3), new RationalNumber(1, 2) * (new RationalNumber(2, 3))); + Assert.Equal(new RationalNumber(1, 3), new RationalNumber(1, 2) * new RationalNumber(2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_a_negative_rational_number_by_a_positive_rational_number() { - Assert.Equal(new RationalNumber(-1, 3), new RationalNumber(-1, 2) * (new RationalNumber(2, 3))); + Assert.Equal(new RationalNumber(-1, 3), new RationalNumber(-1, 2) * new RationalNumber(2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_two_negative_rational_numbers() { - Assert.Equal(new RationalNumber(1, 3), new RationalNumber(-1, 2) * (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(1, 3), new RationalNumber(-1, 2) * new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_a_rational_number_by_its_reciprocal() { - Assert.Equal(new RationalNumber(1, 1), new RationalNumber(1, 2) * (new RationalNumber(2, 1))); + Assert.Equal(new RationalNumber(1, 1), new RationalNumber(1, 2) * new RationalNumber(2, 1)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_a_rational_number_by_1() { - Assert.Equal(new RationalNumber(1, 2), new RationalNumber(1, 2) * (new RationalNumber(1, 1))); + Assert.Equal(new RationalNumber(1, 2), new RationalNumber(1, 2) * new RationalNumber(1, 1)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Multiply_a_rational_number_by_0() { - Assert.Equal(new RationalNumber(0, 1), new RationalNumber(1, 2) * (new RationalNumber(0, 1))); + Assert.Equal(new RationalNumber(0, 1), new RationalNumber(1, 2) * new RationalNumber(0, 1)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_two_positive_rational_numbers() { - Assert.Equal(new RationalNumber(3, 4), new RationalNumber(1, 2) / (new RationalNumber(2, 3))); + Assert.Equal(new RationalNumber(3, 4), new RationalNumber(1, 2) / new RationalNumber(2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_a_positive_rational_number_by_a_negative_rational_number() { - Assert.Equal(new RationalNumber(-3, 4), new RationalNumber(1, 2) / (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(-3, 4), new RationalNumber(1, 2) / new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_two_negative_rational_numbers() { - Assert.Equal(new RationalNumber(3, 4), new RationalNumber(-1, 2) / (new RationalNumber(-2, 3))); + Assert.Equal(new RationalNumber(3, 4), new RationalNumber(-1, 2) / new RationalNumber(-2, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Divide_a_rational_number_by_1() { - Assert.Equal(new RationalNumber(1, 2), new RationalNumber(1, 2) / (new RationalNumber(1, 1))); + Assert.Equal(new RationalNumber(1, 2), new RationalNumber(1, 2) / new RationalNumber(1, 1)); } [Fact(Skip = "Remove this Skip property to run this test")] @@ -203,19 +203,19 @@ public void Raise_a_negative_rational_number_to_the_power_of_zero() [Fact(Skip = "Remove this Skip property to run this test")] public void Raise_a_real_number_to_a_positive_rational_number() { - Assert.Equal(16, 8.Expreal(new RationalNumber(4, 3)), precision: 7); + Assert.Equal(16.0, 8.Expreal(new RationalNumber(4, 3)), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Raise_a_real_number_to_a_negative_rational_number() { - Assert.Equal(0.33333334, 9.Expreal(new RationalNumber(-1, 2)), precision: 7); + Assert.Equal(0.3333333333333333, 9.Expreal(new RationalNumber(-1, 2)), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] public void Raise_a_real_number_to_a_zero_rational_number() { - Assert.Equal(1, 2.Expreal(new RationalNumber(0, 1)), precision: 7); + Assert.Equal(1.0, 2.Expreal(new RationalNumber(0, 1)), precision: 7); } [Fact(Skip = "Remove this Skip property to run this test")] diff --git a/generators.deprecated/Exercises/Generators/Bowling.cs b/generators.deprecated/Exercises/Generators/Bowling.cs deleted file mode 100644 index a502d017a1..0000000000 --- a/generators.deprecated/Exercises/Generators/Bowling.cs +++ /dev/null @@ -1,90 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Exercism.CSharp.Output; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class Bowling : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - if (testMethod.Expected is int) - testMethod.UseVariableForTested = true; - else - testMethod.ExceptionThrown = typeof(ArgumentException); - - testMethod.InputParameters = Array.Empty(); - - testMethod.Arrange = RenderArrange(testMethod); - testMethod.Act = RenderAct(testMethod); - testMethod.Assert = RenderAssert(testMethod); - } - - private string RenderArrange(TestMethod testMethod) - { - var builder = new StringBuilder(); - builder.AppendLine(Render.Variable("sut", "new BowlingGame()")); - - if (!testMethod.Input.ContainsKey("previousRolls")) - return builder.ToString(); - - var previousRolls = testMethod.Input["previousRolls"] as int[] ?? Array.Empty(); - builder.Append(Render.Variable("previousRolls", Render.ObjectMultiLine(previousRolls))); - - return builder.ToString(); - } - - private string? RenderAssert(TestMethod testMethod) - { - if (testMethod.ExceptionThrown != null && testMethod.Input.ContainsKey("roll")) - { - var actual = Render.Object(testMethod.Input["roll"]); - return Render.AssertThrows(testMethod.ExceptionThrown, $"sut.Roll({actual})"); - } - - if (testMethod.ExceptionThrown == null || testMethod.Property != "score") - return testMethod.Assert; - - return Render.AssertThrows(testMethod.ExceptionThrown, "sut.Score()"); - } - - private string RenderAct(TestMethod testMethod) - { - var act = new StringBuilder(); - act.AppendLine("DoRoll(previousRolls, sut);"); - - if (testMethod.ExceptionThrown != null) - { - return act.ToString(); - } - - if (testMethod.Input.ContainsKey("roll")) - { - act.AppendLine($"sut.Roll({testMethod.Input["roll"]});"); - act.AppendLine(Render.Variable("actual", "sut.Score()")); - return act.ToString(); - } - - act.AppendLine(Render.Variable("actual", "sut.Score()")); - return act.ToString(); - } - - protected override void UpdateTestClass(TestClass testClass) => AddDoRollMethod(testClass); - - private static void AddDoRollMethod(TestClass testClass) => - testClass.AdditionalMethods.Add(@" -private void DoRoll(IEnumerable rolls, BowlingGame sut) -{ - foreach (var roll in rolls) - { - sut.Roll(roll); - } -}"); - - protected override void UpdateNamespaces(ISet namespaces) - { - namespaces.Add(typeof(Array).Namespace!); - namespaces.Add(typeof(ICollection<>).Namespace!); - } -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/CircularBuffer.cs b/generators.deprecated/Exercises/Generators/CircularBuffer.cs deleted file mode 100644 index 134ea0fbf8..0000000000 --- a/generators.deprecated/Exercises/Generators/CircularBuffer.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; -using Exercism.CSharp.Output; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class CircularBuffer : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) => testMethod.Assert = RenderAssert(testMethod); - - private string RenderAssert(TestMethod testMethod) - { - var assert = new StringBuilder(); - assert.AppendLine(RenderSut(testMethod)); - - foreach (var operation in testMethod.Input["operations"]) - assert.AppendLine(RenderOperation(operation)); - - return assert.ToString(); - } - - private string RenderSut(TestMethod testMethod) - => Render.Variable("buffer", $"new CircularBuffer(capacity: {testMethod.Input["capacity"]})"); - - private string RenderOperation(dynamic operation) => - operation["operation"] switch - { - "read" => RenderReadOperation(operation), - "write" => RenderWriteOperation(operation), - "overwrite" => RenderOverwriteOperation(operation), - "clear" => RenderClearOperation(), - _ => throw new ArgumentOutOfRangeException($"Unknown operation type: {operation["operation"]}") - }; - - private string RenderReadOperation(dynamic operation) - => operation["should_succeed"] - ? Render.AssertEqual(operation["expected"].ToString(), "buffer.Read()") - : Render.AssertThrows("buffer.Read()"); - - private string RenderWriteOperation(dynamic operation) - => operation["should_succeed"] - ? $"buffer.Write({operation["item"]});" - : Render.AssertThrows($"buffer.Write({operation["item"]})"); - - private static string RenderOverwriteOperation(dynamic operation) - => $"buffer.Overwrite({operation["item"]});"; - - private static string RenderClearOperation() - => "buffer.Clear();"; - - protected override void UpdateNamespaces(ISet namespaces) => namespaces.Add(typeof(InvalidOperationException).Namespace!); -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/Clock.cs b/generators.deprecated/Exercises/Generators/Clock.cs deleted file mode 100644 index 92d601cd28..0000000000 --- a/generators.deprecated/Exercises/Generators/Clock.cs +++ /dev/null @@ -1,57 +0,0 @@ -using Exercism.CSharp.Output; -using Exercism.CSharp.Output.Rendering; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class Clock : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - testMethod.ConstructorInputParameters = new[] { "hour", "minute" }; - testMethod.TestedMethodType = TestedMethodType.InstanceMethod; - - if (testMethod.Property == "create") - UpdateTestMethodForCreateProperty(testMethod); - else if (testMethod.Property == "equal") - UpdateTestMethodForEqualProperty(testMethod); - else - UpdateTestMethodForConsistencyProperty(testMethod); - } - - protected override void UpdateTestClass(TestClass testClass) => - testClass.AdditionalMethods.Add(@"[Fact(Skip = ""Remove this Skip property to run this test"")] -public void Clocks_are_immutable() -{ - var sut = new Clock(0, 0); - var sutPlus1 = sut.Add(1); - Assert.NotEqual(sutPlus1, sut); -}"); - - private static void UpdateTestMethodForCreateProperty(TestMethod testMethod) => testMethod.TestedMethod = "ToString"; - - private void UpdateTestMethodForEqualProperty(TestMethod testMethod) - { - var clock1 = testMethod.Input["clock1"]; - testMethod.Input["clock1"] = new UnescapedValue($"new Clock({clock1["hour"]}, {clock1["minute"]})"); - - var clock2 = testMethod.Input["clock2"]; - testMethod.Input["hour"] = clock2["hour"]; - testMethod.Input["minute"] = clock2["minute"]; - - testMethod.Assert = RenderEqualToAssert(testMethod); - } - - private string RenderEqualToAssert(TestMethod testMethod) - { - var expected = Render.Object(testMethod.Input["clock1"]); - - return testMethod.Expected - ? Render.AssertEqual(expected, "sut") - : Render.AssertNotEqual(expected, "sut"); - } - - private void UpdateTestMethodForConsistencyProperty(TestMethod testMethod) => testMethod.Assert = RenderConsistencyToAssert(testMethod); - - private string RenderConsistencyToAssert(TestMethod testMethod) - => Render.AssertEqual(Render.Object(testMethod.Expected), $"sut.{testMethod.TestedMethod}({testMethod.Input["value"]}).ToString()"); -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/ComplexNumbers.cs b/generators.deprecated/Exercises/Generators/ComplexNumbers.cs deleted file mode 100644 index c2ed6881c5..0000000000 --- a/generators.deprecated/Exercises/Generators/ComplexNumbers.cs +++ /dev/null @@ -1,84 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; - -using DotLiquid.Tags; - -using Exercism.CSharp.Output; -using Exercism.CSharp.Output.Rendering; -using Newtonsoft.Json.Linq; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class ComplexNumbers : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - testMethod.TestedClass = "ComplexNumber"; - testMethod.TestedMethodType = TestedMethodType.InstanceMethod; - - testMethod.UseVariableForExpected = IsComplexNumber(testMethod.Expected); - testMethod.Expected = ConvertToType(testMethod.Expected); - - var constructorParamName = testMethod.Input.ContainsKey("z") ? "z" : "z1"; - testMethod.Input["real"] = ConvertToDouble(testMethod.Input[constructorParamName], 0); - testMethod.Input["imaginary"] = ConvertToDouble(testMethod.Input[constructorParamName], 1); - - testMethod.InputParameters = GetInputParameters(testMethod, constructorParamName); - testMethod.ConstructorInputParameters = new[] {"real", "imaginary"}; - - var keys = testMethod.Input.Keys.ToArray(); - - foreach (var key in keys) - testMethod.Input[key] = ConvertToType(testMethod.Input[key]); - - testMethod.Assert = RenderAssert(testMethod); - } - - private static string[] GetInputParameters(TestMethod testMethod, string constructorParamName) - => testMethod.Input.Keys.Where(x => x != constructorParamName).ToArray(); - - private string? RenderAssert(TestMethod testMethod) => testMethod.UseVariableForExpected - ? RenderComplexNumberAssert(testMethod) - : testMethod.Assert; - - private string RenderComplexNumberAssert(TestMethod testMethod) - { - var inputParameters = testMethod.Input.ContainsKey("z") ? "" : Render.Object(testMethod.Input["z2"]); - var actual = $"sut.{testMethod.TestedMethod}({inputParameters})"; - - var assert = new StringBuilder(); - assert.AppendLine(Render.AssertEqualWithin("expected.Real()", $"{actual}.Real()", 7)); - assert.AppendLine(Render.AssertEqualWithin("expected.Imaginary()", $"{actual}.Imaginary()", 7)); - - return assert.ToString(); - } - - private object ConvertToType(dynamic rawValue) - => IsComplexNumber(rawValue) - ? RenderComplexNumber(rawValue) - : rawValue; - - private UnescapedValue RenderComplexNumber(dynamic rawValue) - => new($"new ComplexNumber({Render.Object(ConvertToDouble(rawValue, 0))}, {Render.Object(ConvertToDouble(rawValue, 1))})"); - - private static bool IsComplexNumber(object rawValue) => rawValue is int[] || rawValue is double[] || rawValue is float[] || rawValue is JArray; - - private static object ConvertToDouble(dynamic value, int index) => - value switch - { - string _ => ParseDouble(value), - int _ => ParseDouble(value), - double _ => ParseDouble(value), - _ => ParseDouble(value[index]) - }; - - private static object ParseDouble(dynamic value) => - double.TryParse(value.ToString(), out double result ) ? - result : - new UnescapedValue(value.ToString().Replace("e", "Math.E").Replace("pi", "Math.PI") - .Replace("ln(2)", "Math.Log(2.0)")); - - protected override void UpdateNamespaces(ISet namespaces) => namespaces.Add(typeof(Math).Namespace!); -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/CustomSet.cs b/generators.deprecated/Exercises/Generators/CustomSet.cs deleted file mode 100644 index 2069a2a7e1..0000000000 --- a/generators.deprecated/Exercises/Generators/CustomSet.cs +++ /dev/null @@ -1,54 +0,0 @@ -using Exercism.CSharp.Output; -using Exercism.CSharp.Output.Rendering; -using Newtonsoft.Json.Linq; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class CustomSet : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - testMethod.UseVariablesForInput = true; - testMethod.TestedMethodType = TestedMethodType.InstanceMethod; - testMethod.Expected = ConvertToCustomSet(testMethod.Expected); - - if (testMethod.Input.ContainsKey("set")) - UpdateTestMethodForSingleSetProperty(testMethod); - else - UpdateTestMethodForMultipleSetsProperty(testMethod); - } - - private static void UpdateTestMethodForSingleSetProperty(TestMethod testMethod) - { - if (testMethod.Input["set"] is JArray) - { - testMethod.Input["set"] = new UnescapedValue(""); - } - - testMethod.ConstructorInputParameters = new[] {"set"}; - } - - private void UpdateTestMethodForMultipleSetsProperty(TestMethod testMethod) - { - if (testMethod.Input["set1"] is JArray) - { - testMethod.Input["set1"] = new UnescapedValue(""); - } - - testMethod.ConstructorInputParameters = new[] {"set1"}; - testMethod.Input["set2"] = ConvertToCustomSet(testMethod.Input["set2"]); - - if (testMethod.Property == "equal") - { - testMethod.TestedMethod = "Equals"; - } - } - - private dynamic ConvertToCustomSet(dynamic value) => - value switch - { - bool _ => value, - int[] values when values.Length > 0 => new UnescapedValue($"new CustomSet({Render.Object(values)})"), - _ => new UnescapedValue("new CustomSet()") - }; -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/DndCharacter.cs b/generators.deprecated/Exercises/Generators/DndCharacter.cs deleted file mode 100644 index 34f52e643f..0000000000 --- a/generators.deprecated/Exercises/Generators/DndCharacter.cs +++ /dev/null @@ -1,117 +0,0 @@ -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Exercism.CSharp.Helpers; -using Exercism.CSharp.Output; -using Exercism.CSharp.Output.Rendering; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class DndCharacter : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - testMethod.TestMethodName = testMethod.Description.Replace("-", "minus_").ToTestMethodName(); - - if (testMethod.Property == "ability") - UpdateTestMethodForAbilityProperty(testMethod); - else if (testMethod.Property == "character") - UpdateTestMethodForCharacterProperty(testMethod); - else if (testMethod.Property == "strength") - UpdateTestMethodForStrengthProperty(testMethod); - } - - protected override void UpdateTestClass(TestClass testClass) => AddTestMethodForDistribution(testClass); - - private void UpdateTestMethodForAbilityProperty(TestMethod testMethod) - => testMethod.Assert = RenderAssertForAbilityProperty(testMethod); - - private string RenderAssertForAbilityProperty(TestMethod testMethod) - { - var assert = new StringBuilder(); - assert.AppendLine("for (var i = 0; i < 10; i++)"); - assert.AppendLine("{"); - assert.AppendLine(RenderAssertForAbilityRange($"{testMethod.TestedClass}.{testMethod.TestedMethod}()").Indent()); - assert.AppendLine("}"); - return assert.ToString(); - } - - private void UpdateTestMethodForCharacterProperty(TestMethod testMethod) - => testMethod.Assert = RenderAssertForCharacterProperty(testMethod); - - private string RenderAssertForCharacterProperty(TestMethod testMethod) - { - var assert = new StringBuilder(); - assert.AppendLine("for (var i = 0; i < 10; i++)"); - assert.AppendLine("{"); - assert.AppendLine(Render.Variable("sut", $"{testMethod.TestedClass}.Generate()").Indent()); - assert.AppendLine(RenderAssertForAbilityRange("sut.Strength").Indent()); - assert.AppendLine(RenderAssertForAbilityRange("sut.Dexterity").Indent()); - assert.AppendLine(RenderAssertForAbilityRange("sut.Constitution").Indent()); - assert.AppendLine(RenderAssertForAbilityRange("sut.Intelligence").Indent()); - assert.AppendLine(RenderAssertForAbilityRange("sut.Wisdom").Indent()); - assert.AppendLine(RenderAssertForAbilityRange("sut.Charisma").Indent()); - assert.AppendLine(Render.AssertEqual($"sut.Hitpoints", $"10 + {testMethod.TestedClass}.Modifier(sut.Constitution)").Indent()); - assert.AppendLine("}"); - return assert.ToString(); - } - - private string RenderAssertForAbilityRange(string ability) => Render.AssertInRange(ability, 3, 18); - - private void UpdateTestMethodForStrengthProperty(TestMethod testMethod) - => testMethod.Assert = RenderAssertForStrengthProperty(testMethod); - - private string RenderAssertForStrengthProperty(TestMethod testMethod) - { - var assert = new StringBuilder(); - assert.AppendLine("for (var i = 0; i < 10; i++)"); - assert.AppendLine("{"); - assert.AppendLine(Render.Variable("sut", $"{testMethod.TestedClass}.Generate()").Indent()); - assert.AppendLine(Render.AssertEqual("sut.Strength", "sut.Strength").Indent()); - assert.AppendLine(Render.AssertEqual("sut.Dexterity", "sut.Dexterity").Indent()); - assert.AppendLine(Render.AssertEqual("sut.Constitution", "sut.Constitution").Indent()); - assert.AppendLine(Render.AssertEqual("sut.Intelligence", "sut.Intelligence").Indent()); - assert.AppendLine(Render.AssertEqual("sut.Wisdom", "sut.Wisdom").Indent()); - assert.AppendLine(Render.AssertEqual("sut.Charisma", "sut.Charisma").Indent()); - assert.AppendLine("}"); - return assert.ToString(); - } - - private static void AddTestMethodForDistribution(TestClass testClass) => - testClass.AdditionalMethods.Add(@" -[Fact(Skip = ""Remove this Skip property to run this test"")] -public void Random_ability_is_distributed_correctly() -{ - var expectedDistribution = new Dictionary - { - [3] = 1, [4] = 4, - [5] = 10, [6] = 21, - [7] = 38, [8] = 62, - [9] = 91, [10] = 122, - [11] = 148, [12] = 167, - [13] = 172, [14] = 160, - [15] = 131, [16] = 94, - [17] = 54, [18] = 21 - }; - - var actualDistribution = new Dictionary(expectedDistribution); - foreach (var key in actualDistribution.Keys) - actualDistribution[key] = 0; - - const int times = 250; - const int possibleCombinationsCount = 6*6*6*6; // 4d6 - for (var i = 0; i < times * possibleCombinationsCount; i++) - actualDistribution[DndCharacter.Ability()]++; - - const double minTimes = times * 0.8; - const double maxTimes = times * 1.2; - foreach (var k in expectedDistribution.Keys) - Assert.InRange(actualDistribution[k], expectedDistribution[k] * minTimes, expectedDistribution[k] * maxTimes); -}"); - - protected override void UpdateNamespaces(ISet namespaces) - { - namespaces.Add(typeof(Enumerable).Namespace!); - namespaces.Add(typeof(Dictionary).Namespace!); - } -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/GoCounting.cs b/generators.deprecated/Exercises/Generators/GoCounting.cs deleted file mode 100644 index 98badcee65..0000000000 --- a/generators.deprecated/Exercises/Generators/GoCounting.cs +++ /dev/null @@ -1,87 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Exercism.CSharp.Output; -using Exercism.CSharp.Output.Rendering; -using Newtonsoft.Json.Linq; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class GoCounting : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - testMethod.UseVariablesForInput = true; - testMethod.UseVariableForExpected = true; - testMethod.UseVariablesForConstructorParameters = true; - testMethod.UseVariableForTested = true; - - testMethod.Input["board"] = new MultiLineString(testMethod.Input["board"]); - testMethod.ConstructorInputParameters = new[] { "board" }; - testMethod.TestedMethodType = TestedMethodType.InstanceMethod; - - if (testMethod.Property == "territory") - UpdateTestMethodForTerritoryProperty(testMethod); - else - UpdateTestMethodForTerritoriesProperty(testMethod); - } - - private void UpdateTestMethodForTerritoryProperty(TestMethod testMethod) - { - testMethod.Input["coordinate"] = (testMethod.Input["x"], testMethod.Input["y"]); - testMethod.InputParameters = new[] {"coordinate"}; - - if (testMethod.ExpectedIsError) - { - testMethod.ExceptionThrown = typeof(ArgumentException); - } - else - { - var owner = RenderOwner(testMethod.Expected!["owner"]!); - var territory = RenderTerritory(testMethod.Expected["territory"]); - testMethod.Expected = (owner, territory); - testMethod.Assert = RenderTerritoryAssert(); - } - } - - private void UpdateTestMethodForTerritoriesProperty(TestMethod testMethod) - { - var expected = new[] - { - "new Dictionary>", - "{", - $" [Owner.Black] = {RenderTerritory(testMethod.Expected!["territoryBlack"])},", - $" [Owner.White] = {RenderTerritory(testMethod.Expected!["territoryWhite"])},", - $" [Owner.None] = {RenderTerritory(testMethod.Expected!["territoryNone"])}", - "}" - }; - - testMethod.Expected = new UnescapedValue(string.Join(Environment.NewLine, expected)); - testMethod.Assert = RenderTerritoriesAssert(); - } - - private string RenderTerritoryAssert() - { - var assert = new StringBuilder(); - assert.AppendLine(Render.AssertEqual("expected.Item1", "actual.Item1")); - assert.AppendLine(Render.AssertEqual("expected.Item2", "actual.Item2")); - return assert.ToString(); - } - - private string RenderTerritoriesAssert() - { - var territoriesAssert = new StringBuilder(); - territoriesAssert.AppendLine(Render.AssertEqual("expected[Owner.Black]", "actual[Owner.Black]")); - territoriesAssert.AppendLine(Render.AssertEqual("expected[Owner.White]", "actual[Owner.White]")); - territoriesAssert.AppendLine(Render.AssertEqual("expected[Owner.None]", "actual[Owner.None]")); - return territoriesAssert.ToString(); - } - - private UnescapedValue RenderOwner(dynamic owner) => Render.Enum("Owner", owner); - - private string RenderTerritory(dynamic territory) - => Render.Object(((JArray)territory).Select(coordinate => (coordinate[0]!.ToObject(), coordinate[1]!.ToObject())).ToHashSet()); - - protected override void UpdateNamespaces(ISet namespaces) => namespaces.Add(typeof(Dictionary).Namespace!); -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/HighScores.cs b/generators.deprecated/Exercises/Generators/HighScores.cs deleted file mode 100644 index 03963a6d6b..0000000000 --- a/generators.deprecated/Exercises/Generators/HighScores.cs +++ /dev/null @@ -1,36 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Channels; - -using Exercism.CSharp.Output; - -using Humanizer; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class HighScores : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) - { - testMethod.TestedMethodType = TestedMethodType.InstanceMethod; - testMethod.ConstructorInputParameters = new[] { "scores" }; - - testMethod.Input["scores"] = new List(testMethod.Input["scores"]); - - if (testMethod.Expected is IEnumerable) - testMethod.Expected = new List(testMethod.Expected); - - if (testMethod.Scenarios.Contains("immutable")) - { - var beforeProperty = testMethod.Property.Substring(0, testMethod.Property.IndexOf("After", StringComparison.Ordinal)).Replace("Best", "PersonalBest").Replace("TopThree", "PersonalTopThree").Pascalize(); - var afterProperty = testMethod.Property.Substring(testMethod.Property.IndexOf("After", StringComparison.Ordinal) + 5).Replace("Best", "PersonalBest").Replace("TopThree", "PersonalTopThree").Pascalize(); - - testMethod.Act = $"var _ = sut.{afterProperty}();"; - testMethod.TestedMethod = beforeProperty; - } - } - - protected override void UpdateNamespaces(ISet namespaces) => - namespaces.Add(typeof(List).Namespace!); -} \ No newline at end of file diff --git a/generators.deprecated/Exercises/Generators/RationalNumbers.cs b/generators.deprecated/Exercises/Generators/RationalNumbers.cs deleted file mode 100644 index 817ac233bb..0000000000 --- a/generators.deprecated/Exercises/Generators/RationalNumbers.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using Exercism.CSharp.Output; - -namespace Exercism.CSharp.Exercises.Generators; - -internal class RationalNumbers : ExerciseGenerator -{ - protected override void UpdateTestMethod(TestMethod testMethod) => testMethod.Assert = RenderAssert(testMethod); - - private string RenderAssert(TestMethod testMethod) - { - switch (testMethod.Property) - { - case "add": - case "sub": - case "mul": - case "div": - const string operationsWithOverloading = "add|+|sub|-|mul|*|div|/"; - var operationCode = operationsWithOverloading.Substring(operationsWithOverloading.IndexOf(testMethod.Property, StringComparison.OrdinalIgnoreCase) + 4, 1); - return Render.AssertEqual(RenderRationalNumber(testMethod.Expected), $"{RenderRationalNumber(testMethod.Input["r1"])} {operationCode} ({RenderRationalNumber(testMethod.Input["r2"])})"); - case "abs": - case "reduce": - return Render.AssertEqual(RenderRationalNumber(testMethod.Expected), $"{RenderRationalNumber(testMethod.Input["r"])}.{testMethod.TestedMethod}()"); - case "exprational": - return Render.AssertEqual(RenderRationalNumber(testMethod.Expected), $"{RenderRationalNumber(testMethod.Input["r"])}.{testMethod.TestedMethod}({testMethod.Input["n"]})"); - case "expreal": - return Render.AssertEqualWithin(Render.Object(testMethod.Expected), $"{testMethod.Input["x"]}.{testMethod.TestedMethod}({RenderRationalNumber(testMethod.Input["r"])})", 7); - default: - throw new ArgumentOutOfRangeException(); - } - } - - private static string RenderRationalNumber(dynamic input) => $"new RationalNumber({input[0]}, {input[1]})"; -} \ No newline at end of file