diff --git a/exercises/practice/accumulate/.meta/Generator.tpl b/exercises/practice/accumulate/.meta/Generator.tpl new file mode 100644 index 0000000000..88f179cc57 --- /dev/null +++ b/exercises/practice/accumulate/.meta/Generator.tpl @@ -0,0 +1,41 @@ +{{ func normalize + normalized = string.replace $0 "upcase(x)" "x.ToUpper()" + normalized = string.replace normalized "reverse(x)" "new string(x.Reverse().ToArray())" + string.replace normalized "accumulate([\"1\", \"2\", \"3\"], " "new[] { \"1\", \"2\", \"3\" }.Accumulate(" +end }} + +{{ func vartype + case (object.typeof (array.first $0)) + when "string" + ret "string[]" + when "array" + ret "string[][]" + else + ret "int[]" + end +end }} +using System.Linq; +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 }}() + { + {{ test.input.list | vartype }} input = {{ test.input.list }}; + {{ test.expected | vartype }} expected = {{ test.expected }}; + Assert.Equal(expected, input.{{ test.testedMethod }}({{ test.input.accumulator | normalize }})); + } + {{ end }} + [Fact(Skip = "Remove this Skip property to run this test")] + public void Accumulate_is_lazy() + { + var counter = 0; + int[] input = [1, 2, 3]; + var accumulation = input.Accumulate(x => x * counter++); + Assert.Equal(0, counter); + var _ = accumulation.ToList(); + Assert.Equal(3, counter); + } +} diff --git a/exercises/practice/accumulate/AccumulateTests.cs b/exercises/practice/accumulate/AccumulateTests.cs index ac11800077..9db1daab85 100644 --- a/exercises/practice/accumulate/AccumulateTests.cs +++ b/exercises/practice/accumulate/AccumulateTests.cs @@ -1,63 +1,56 @@ -using System; -using System.Collections.Generic; using System.Linq; using Xunit; public class AccumulateTests { [Fact] - public void Empty_accumulation_produces_empty_accumulation() + public void Accumulate_empty() { - Assert.Equal(new int[0], new int[0].Accumulate(x => x * x)); + int[] input = []; + int[] expected = []; + Assert.Equal(expected, input.Accumulate((x) => x * x)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Accumulate_squares() { - Assert.Equal(new[] { 1, 4, 9 }, new[] { 1, 2, 3 }.Accumulate(x => x * x)); + int[] input = [1, 2, 3]; + int[] expected = [1, 4, 9]; + Assert.Equal(expected, input.Accumulate((x) => x * x)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Accumulate_upcases() { - Assert.Equal(new List { "HELLO", "WORLD" }, new List { "hello", "world" }.Accumulate(x => x.ToUpper())); + string[] input = ["Hello", "world"]; + string[] expected = ["HELLO", "WORLD"]; + Assert.Equal(expected, input.Accumulate((x) => x.ToUpper())); } [Fact(Skip = "Remove this Skip property to run this test")] public void Accumulate_reversed_strings() { - Assert.Equal("eht kciuq nworb xof cte".Split(' '), "the quick brown fox etc".Split(' ').Accumulate(Reverse)); - } - - private static string Reverse(string value) - { - var array = value.ToCharArray(); - Array.Reverse(array); - return new string(array); + string[] input = ["the", "quick", "brown", "fox", "etc"]; + string[] expected = ["eht", "kciuq", "nworb", "xof", "cte"]; + Assert.Equal(expected, input.Accumulate((x) => new string(x.Reverse().ToArray()))); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Accumulate_within_accumulate() + public void Accumulate_recursively() { - var actual = new[] { "a", "b", "c" }.Accumulate(c => - string.Join(" ", new[] { "1", "2", "3" }.Accumulate(d => c + d))); - Assert.Equal(new[] { "a1 a2 a3", "b1 b2 b3", "c1 c2 c3" }, actual); + string[] input = ["a", "b", "c"]; + string[][] expected = [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]; + Assert.Equal(expected, input.Accumulate((x) => new[] { "1", "2", "3" }.Accumulate((y) => x + y))); } [Fact(Skip = "Remove this Skip property to run this test")] public void Accumulate_is_lazy() { var counter = 0; - var accumulation = new[] { 1, 2, 3 }.Accumulate(x => x * counter++); - + int[] input = [1, 2, 3]; + var accumulation = input.Accumulate(x => x * counter++); Assert.Equal(0, counter); - accumulation.ToList(); + var _ = accumulation.ToList(); Assert.Equal(3, counter); } - - [Fact(Skip = "Remove this Skip property to run this test")] - public void Accumulate_allows_different_return_type() - { - Assert.Equal(new[] { "1", "2", "3" }, new[] { 1, 2, 3 }.Accumulate(x => x.ToString())); - } -} \ No newline at end of file +} diff --git a/exercises/practice/clock/.meta/Generator.tpl b/exercises/practice/clock/.meta/Generator.tpl new file mode 100644 index 0000000000..f3f6bc2c1e --- /dev/null +++ b/exercises/practice/clock/.meta/Generator.tpl @@ -0,0 +1,22 @@ +{{ func clock + $"new Clock({$0.hour}, {$0.minute})" +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.property == "equal" }} + Assert.{{ test.expected ? "Equal" : "NotEqual" }}({{ test.input.clock1 | clock }}, {{ test.input.clock2 | clock }}); + {{- else if test.property == "create" }} + Assert.Equal({{ test.expected | string.literal }}, {{ test.input | clock }}.ToString()); + {{- else }} + Assert.Equal({{ test.expected | string.literal }}, {{ test.input | clock }}.{{ test.testedMethod }}({{ test.input.value }}).ToString()); + {{ end -}} + } + {{ end -}} +} diff --git a/exercises/practice/clock/ClockTests.cs b/exercises/practice/clock/ClockTests.cs index 7205d6cd50..69e0243721 100644 --- a/exercises/practice/clock/ClockTests.cs +++ b/exercises/practice/clock/ClockTests.cs @@ -5,372 +5,312 @@ public class ClockTests [Fact] public void On_the_hour() { - var sut = new Clock(8, 0); - Assert.Equal("08:00", sut.ToString()); + Assert.Equal("08:00", new Clock(8, 0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Past_the_hour() { - var sut = new Clock(11, 9); - Assert.Equal("11:09", sut.ToString()); + Assert.Equal("11:09", new Clock(11, 9).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Midnight_is_zero_hours() { - var sut = new Clock(24, 0); - Assert.Equal("00:00", sut.ToString()); + Assert.Equal("00:00", new Clock(24, 0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Hour_rolls_over() { - var sut = new Clock(25, 0); - Assert.Equal("01:00", sut.ToString()); + Assert.Equal("01:00", new Clock(25, 0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Hour_rolls_over_continuously() { - var sut = new Clock(100, 0); - Assert.Equal("04:00", sut.ToString()); + Assert.Equal("04:00", new Clock(100, 0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Sixty_minutes_is_next_hour() { - var sut = new Clock(1, 60); - Assert.Equal("02:00", sut.ToString()); + Assert.Equal("02:00", new Clock(1, 60).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Minutes_roll_over() { - var sut = new Clock(0, 160); - Assert.Equal("02:40", sut.ToString()); + Assert.Equal("02:40", new Clock(0, 160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Minutes_roll_over_continuously() { - var sut = new Clock(0, 1723); - Assert.Equal("04:43", sut.ToString()); + Assert.Equal("04:43", new Clock(0, 1723).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Hour_and_minutes_roll_over() { - var sut = new Clock(25, 160); - Assert.Equal("03:40", sut.ToString()); + Assert.Equal("03:40", new Clock(25, 160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Hour_and_minutes_roll_over_continuously() { - var sut = new Clock(201, 3001); - Assert.Equal("11:01", sut.ToString()); + Assert.Equal("11:01", new Clock(201, 3001).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Hour_and_minutes_roll_over_to_exactly_midnight() { - var sut = new Clock(72, 8640); - Assert.Equal("00:00", sut.ToString()); + Assert.Equal("00:00", new Clock(72, 8640).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_hour() { - var sut = new Clock(-1, 15); - Assert.Equal("23:15", sut.ToString()); + Assert.Equal("23:15", new Clock(-1, 15).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_hour_rolls_over() { - var sut = new Clock(-25, 0); - Assert.Equal("23:00", sut.ToString()); + Assert.Equal("23:00", new Clock(-25, 0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_hour_rolls_over_continuously() { - var sut = new Clock(-91, 0); - Assert.Equal("05:00", sut.ToString()); + Assert.Equal("05:00", new Clock(-91, 0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_minutes() { - var sut = new Clock(1, -40); - Assert.Equal("00:20", sut.ToString()); + Assert.Equal("00:20", new Clock(1, -40).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_minutes_roll_over() { - var sut = new Clock(1, -160); - Assert.Equal("22:20", sut.ToString()); + Assert.Equal("22:20", new Clock(1, -160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_minutes_roll_over_continuously() { - var sut = new Clock(1, -4820); - Assert.Equal("16:40", sut.ToString()); + Assert.Equal("16:40", new Clock(1, -4820).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_sixty_minutes_is_previous_hour() { - var sut = new Clock(2, -60); - Assert.Equal("01:00", sut.ToString()); + Assert.Equal("01:00", new Clock(2, -60).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_hour_and_minutes_both_roll_over() { - var sut = new Clock(-25, -160); - Assert.Equal("20:20", sut.ToString()); + Assert.Equal("20:20", new Clock(-25, -160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Negative_hour_and_minutes_both_roll_over_continuously() { - var sut = new Clock(-121, -5810); - Assert.Equal("22:10", sut.ToString()); + Assert.Equal("22:10", new Clock(-121, -5810).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_minutes() { - var sut = new Clock(10, 0); - Assert.Equal("10:03", sut.Add(3).ToString()); + Assert.Equal("10:03", new Clock(10, 0).Add(3).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_no_minutes() { - var sut = new Clock(6, 41); - Assert.Equal("06:41", sut.Add(0).ToString()); + Assert.Equal("06:41", new Clock(6, 41).Add(0).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_to_next_hour() { - var sut = new Clock(0, 45); - Assert.Equal("01:25", sut.Add(40).ToString()); + Assert.Equal("01:25", new Clock(0, 45).Add(40).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_more_than_one_hour() { - var sut = new Clock(10, 0); - Assert.Equal("11:01", sut.Add(61).ToString()); + Assert.Equal("11:01", new Clock(10, 0).Add(61).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_more_than_two_hours_with_carry() { - var sut = new Clock(0, 45); - Assert.Equal("03:25", sut.Add(160).ToString()); + Assert.Equal("03:25", new Clock(0, 45).Add(160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_across_midnight() { - var sut = new Clock(23, 59); - Assert.Equal("00:01", sut.Add(2).ToString()); + Assert.Equal("00:01", new Clock(23, 59).Add(2).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Add_more_than_one_day_1500_min_25_hrs_() + public void Add_more_than_one_day_1500_min_25_hrs() { - var sut = new Clock(5, 32); - Assert.Equal("06:32", sut.Add(1500).ToString()); + Assert.Equal("06:32", new Clock(5, 32).Add(1500).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Add_more_than_two_days() { - var sut = new Clock(1, 1); - Assert.Equal("11:21", sut.Add(3500).ToString()); + Assert.Equal("11:21", new Clock(1, 1).Add(3500).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_minutes() { - var sut = new Clock(10, 3); - Assert.Equal("10:00", sut.Subtract(3).ToString()); + Assert.Equal("10:00", new Clock(10, 3).Subtract(3).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_to_previous_hour() { - var sut = new Clock(10, 3); - Assert.Equal("09:33", sut.Subtract(30).ToString()); + Assert.Equal("09:33", new Clock(10, 3).Subtract(30).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_more_than_an_hour() { - var sut = new Clock(10, 3); - Assert.Equal("08:53", sut.Subtract(70).ToString()); + Assert.Equal("08:53", new Clock(10, 3).Subtract(70).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_across_midnight() { - var sut = new Clock(0, 3); - Assert.Equal("23:59", sut.Subtract(4).ToString()); + Assert.Equal("23:59", new Clock(0, 3).Subtract(4).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_more_than_two_hours() { - var sut = new Clock(0, 0); - Assert.Equal("21:20", sut.Subtract(160).ToString()); + Assert.Equal("21:20", new Clock(0, 0).Subtract(160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_more_than_two_hours_with_borrow() { - var sut = new Clock(6, 15); - Assert.Equal("03:35", sut.Subtract(160).ToString()); + Assert.Equal("03:35", new Clock(6, 15).Subtract(160).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] - public void Subtract_more_than_one_day_1500_min_25_hrs_() + public void Subtract_more_than_one_day_1500_min_25_hrs() { - var sut = new Clock(5, 32); - Assert.Equal("04:32", sut.Subtract(1500).ToString()); + Assert.Equal("04:32", new Clock(5, 32).Subtract(1500).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Subtract_more_than_two_days() { - var sut = new Clock(2, 20); - Assert.Equal("00:20", sut.Subtract(3000).ToString()); + Assert.Equal("00:20", new Clock(2, 20).Subtract(3000).ToString()); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_same_time() { - var sut = new Clock(15, 37); - Assert.Equal(new Clock(15, 37), sut); + Assert.Equal(new Clock(15, 37), new Clock(15, 37)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_a_minute_apart() { - var sut = new Clock(15, 37); - Assert.NotEqual(new Clock(15, 36), sut); + Assert.NotEqual(new Clock(15, 36), new Clock(15, 37)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_an_hour_apart() { - var sut = new Clock(15, 37); - Assert.NotEqual(new Clock(14, 37), sut); + Assert.NotEqual(new Clock(14, 37), new Clock(15, 37)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_hour_overflow() { - var sut = new Clock(34, 37); - Assert.Equal(new Clock(10, 37), sut); + Assert.Equal(new Clock(10, 37), new Clock(34, 37)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_hour_overflow_by_several_days() { - var sut = new Clock(99, 11); - Assert.Equal(new Clock(3, 11), sut); + Assert.Equal(new Clock(3, 11), new Clock(99, 11)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_hour() { - var sut = new Clock(-2, 40); - Assert.Equal(new Clock(22, 40), sut); + Assert.Equal(new Clock(22, 40), new Clock(-2, 40)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_hour_that_wraps() { - var sut = new Clock(-31, 3); - Assert.Equal(new Clock(17, 3), sut); + Assert.Equal(new Clock(17, 3), new Clock(-31, 3)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_hour_that_wraps_multiple_times() { - var sut = new Clock(-83, 49); - Assert.Equal(new Clock(13, 49), sut); + Assert.Equal(new Clock(13, 49), new Clock(-83, 49)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_minute_overflow() { - var sut = new Clock(0, 1441); - Assert.Equal(new Clock(0, 1), sut); + Assert.Equal(new Clock(0, 1), new Clock(0, 1441)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_minute_overflow_by_several_days() { - var sut = new Clock(2, 4322); - Assert.Equal(new Clock(2, 2), sut); + Assert.Equal(new Clock(2, 2), new Clock(2, 4322)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_minute() { - var sut = new Clock(3, -20); - Assert.Equal(new Clock(2, 40), sut); + Assert.Equal(new Clock(2, 40), new Clock(3, -20)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_minute_that_wraps() { - var sut = new Clock(5, -1490); - Assert.Equal(new Clock(4, 10), sut); + Assert.Equal(new Clock(4, 10), new Clock(5, -1490)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_minute_that_wraps_multiple_times() { - var sut = new Clock(6, -4305); - Assert.Equal(new Clock(6, 15), sut); + Assert.Equal(new Clock(6, 15), new Clock(6, -4305)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_hours_and_minutes() { - var sut = new Clock(-12, -268); - Assert.Equal(new Clock(7, 32), sut); + Assert.Equal(new Clock(7, 32), new Clock(-12, -268)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Clocks_with_negative_hours_and_minutes_that_wrap() { - var sut = new Clock(-54, -11513); - Assert.Equal(new Clock(18, 7), sut); + Assert.Equal(new Clock(18, 7), new Clock(-54, -11513)); } [Fact(Skip = "Remove this Skip property to run this test")] public void Full_clock_and_zeroed_clock() { - var sut = new Clock(0, 0); - Assert.Equal(new Clock(24, 0), sut); - } - - [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); + Assert.Equal(new Clock(24, 0), new Clock(0, 0)); } }