diff --git a/exercises/tournament/Example.cs b/exercises/tournament/Example.cs index cd288d3457..f4fcec0802 100644 --- a/exercises/tournament/Example.cs +++ b/exercises/tournament/Example.cs @@ -84,7 +84,7 @@ private void WriteResults(TextWriter writer) writer.WriteLine( "{0,-30:S} | {1,2:D} | {2,2:D} | {3,2:D} | {4,2:D} | {5,2:D}", "Team", "MP", "W", "D", "L", "P"); - foreach (var pair in this.teams.OrderByDescending(pair => pair.Value.Score)) { + foreach (var pair in this.teams.OrderByDescending(pair => pair.Value.Score).ThenBy(pair => pair.Key)) { writer.WriteLine( "{0,-30:S} | {1,2:D} | {2,2:D} | {3,2:D} | {4,2:D} | {5,2:D}", pair.Key, pair.Value.Played, pair.Value.Wins, diff --git a/exercises/tournament/TournamentTest.cs b/exercises/tournament/TournamentTest.cs index 6f7f5443a7..6c0d79c62d 100644 --- a/exercises/tournament/TournamentTest.cs +++ b/exercises/tournament/TournamentTest.cs @@ -1,87 +1,170 @@ -using System; +// This file was auto-generated based on version 1.3.0 of the canonical data. + +using Xunit; +using System; using System.IO; using System.Text; -using Xunit; public class TournamentTest { - readonly string input1 = - "Αllegoric Alaskians;Blithering Badgers;win" + Environment.NewLine + - "Devastating Donkeys;Courageous Californians;draw" + Environment.NewLine + - "Devastating Donkeys;Αllegoric Alaskians;win" + Environment.NewLine + - "Courageous Californians;Blithering Badgers;loss" + Environment.NewLine + - "Blithering Badgers;Devastating Donkeys;loss" + Environment.NewLine + - "Αllegoric Alaskians;Courageous Californians;win".Trim(); - - private readonly string input2 = - "Allegoric Alaskians;Blithering Badgers;win" + Environment.NewLine + - "Devastating Donkeys_Courageous Californians;draw" + Environment.NewLine + - "Devastating Donkeys;Allegoric Alaskians;win" + Environment.NewLine + - "" + Environment.NewLine + - "Courageous Californians;Blithering Badgers;loss" + Environment.NewLine + - "Bla;Bla;Bla" + Environment.NewLine + - "Blithering Badgers;Devastating Donkeys;loss" + Environment.NewLine + - "# Yackity yackity yack" + Environment.NewLine + - "Allegoric Alaskians;Courageous Californians;win" + Environment.NewLine + - "Devastating Donkeys;Courageous Californians;draw" + Environment.NewLine + - "Devastating Donkeys@Courageous Californians;draw"; // Trim() omitted by design - - private readonly string input3 = - "Allegoric Alaskians;Blithering Badgers;win" + Environment.NewLine + - "Devastating Donkeys;Allegoric Alaskians;win" + Environment.NewLine + - "Courageous Californians;Blithering Badgers;loss" + Environment.NewLine + - "Allegoric Alaskians;Courageous Californians;win".Trim(); - - readonly string expected1 = - "Team | MP | W | D | L | P" + Environment.NewLine + - "Devastating Donkeys | 3 | 2 | 1 | 0 | 7" + Environment.NewLine + - "Αllegoric Alaskians | 3 | 2 | 0 | 1 | 6" + Environment.NewLine + - "Blithering Badgers | 3 | 1 | 0 | 2 | 3" + Environment.NewLine + - "Courageous Californians | 3 | 0 | 1 | 2 | 1".Trim(); - - private readonly string expected2 = - "Team | MP | W | D | L | P" + Environment.NewLine + - "Devastating Donkeys | 3 | 2 | 1 | 0 | 7" + Environment.NewLine + - "Allegoric Alaskians | 3 | 2 | 0 | 1 | 6" + Environment.NewLine + - "Blithering Badgers | 3 | 1 | 0 | 2 | 3" + Environment.NewLine + - "Courageous Californians | 3 | 0 | 1 | 2 | 1".Trim(); - - private readonly string expected3 = - "Team | MP | W | D | L | P" + Environment.NewLine + - "Allegoric Alaskians | 3 | 2 | 0 | 1 | 6" + Environment.NewLine + - "Blithering Badgers | 2 | 1 | 0 | 1 | 3" + Environment.NewLine + - "Devastating Donkeys | 1 | 1 | 0 | 0 | 3" + Environment.NewLine + - "Courageous Californians | 2 | 0 | 0 | 2 | 0".Trim(); + [Fact] + public void Just_the_header_if_no_input() + { + var input = string.Empty; + var expected = "Team | MP | W | D | L | P".Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } - private string RunTally(string input) + [Fact(Skip = "Remove to run test")] + public void A_win_is_three_points_a_loss_is_zero_points() { - var encoding = new UTF8Encoding(); - - using (var inStream = new MemoryStream(encoding.GetBytes(input))) - { - using (var outStream = new MemoryStream()) - { - Tournament.Tally(inStream, outStream); - return encoding.GetString(outStream.ToArray()); - } - } + var input = "Allegoric Alaskans;Blithering Badgers;win".Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3" + Environment.NewLine + + "Blithering Badgers | 1 | 0 | 0 | 1 | 0" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); } - [Fact] - public void Test_good() + [Fact(Skip = "Remove to run test")] + public void A_win_can_also_be_expressed_as_a_loss() + { + var input = "Blithering Badgers;Allegoric Alaskans;loss".Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3" + Environment.NewLine + + "Blithering Badgers | 1 | 0 | 0 | 1 | 0" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + [Fact(Skip = "Remove to run test")] + public void A_different_team_can_win() + { + var input = "Blithering Badgers;Allegoric Alaskans;win".Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Blithering Badgers | 1 | 1 | 0 | 0 | 3" + Environment.NewLine + + "Allegoric Alaskans | 1 | 0 | 0 | 1 | 0" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + [Fact(Skip = "Remove to run test")] + public void A_draw_is_one_point_each() + { + var input = "Allegoric Alaskans;Blithering Badgers;draw".Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 1 | 0 | 1 | 0 | 1" + Environment.NewLine + + "Blithering Badgers | 1 | 0 | 1 | 0 | 1" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + [Fact(Skip = "Remove to run test")] + public void There_can_be_more_than_one_match() + { + var input = "Allegoric Alaskans;Blithering Badgers;win" + Environment.NewLine + + "Allegoric Alaskans;Blithering Badgers;win" + .Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6" + Environment.NewLine + + "Blithering Badgers | 2 | 0 | 0 | 2 | 0" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + [Fact(Skip = "Remove to run test")] + public void There_can_be_more_than_one_winner() + { + var input = "Allegoric Alaskans;Blithering Badgers;loss" + Environment.NewLine + + "Allegoric Alaskans;Blithering Badgers;win" + .Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 2 | 1 | 0 | 1 | 3" + Environment.NewLine + + "Blithering Badgers | 2 | 1 | 0 | 1 | 3" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + [Fact(Skip = "Remove to run test")] + public void There_can_be_more_than_two_teams() + { + var input = "Allegoric Alaskans;Blithering Badgers;win" + Environment.NewLine + + "Blithering Badgers;Courageous Californians;win" + Environment.NewLine + + "Courageous Californians;Allegoric Alaskans;loss" + .Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6" + Environment.NewLine + + "Blithering Badgers | 2 | 1 | 0 | 1 | 3" + Environment.NewLine + + "Courageous Californians | 2 | 0 | 0 | 2 | 0" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + [Fact(Skip = "Remove to run test")] + public void Typical_input() { - Assert.Equal(expected1, RunTally(input1).Trim()); + var input = "Allegoric Alaskans;Blithering Badgers;win" + Environment.NewLine + + "Devastating Donkeys;Courageous Californians;draw" + Environment.NewLine + + "Devastating Donkeys;Allegoric Alaskans;win" + Environment.NewLine + + "Courageous Californians;Blithering Badgers;loss" + Environment.NewLine + + "Blithering Badgers;Devastating Donkeys;loss" + Environment.NewLine + + "Allegoric Alaskans;Courageous Californians;win" + .Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Devastating Donkeys | 3 | 2 | 1 | 0 | 7" + Environment.NewLine + + "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6" + Environment.NewLine + + "Blithering Badgers | 3 | 1 | 0 | 2 | 3" + Environment.NewLine + + "Courageous Californians | 3 | 0 | 1 | 2 | 1" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); } [Fact(Skip = "Remove to run test")] - public void Test_ignore_bad_lines() + public void Incomplete_competition_not_all_pairs_have_played_() { - Assert.Equal(expected2, RunTally(input2).Trim()); + var input = "Allegoric Alaskans;Blithering Badgers;loss" + Environment.NewLine + + "Devastating Donkeys;Allegoric Alaskans;loss" + Environment.NewLine + + "Courageous Californians;Blithering Badgers;draw" + Environment.NewLine + + "Allegoric Alaskans;Courageous Californians;win" + .Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6" + Environment.NewLine + + "Blithering Badgers | 2 | 1 | 1 | 0 | 4" + Environment.NewLine + + "Courageous Californians | 2 | 0 | 1 | 1 | 1" + Environment.NewLine + + "Devastating Donkeys | 1 | 0 | 0 | 1 | 0" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); } [Fact(Skip = "Remove to run test")] - public void Test_incomplete_competition() + public void Ties_broken_alphabetically() { - Assert.Equal(expected3, RunTally(input3).Trim()); + var input = "Courageous Californians;Devastating Donkeys;win" + Environment.NewLine + + "Allegoric Alaskans;Blithering Badgers;win" + Environment.NewLine + + "Devastating Donkeys;Allegoric Alaskans;loss" + Environment.NewLine + + "Courageous Californians;Blithering Badgers;win" + Environment.NewLine + + "Blithering Badgers;Devastating Donkeys;draw" + Environment.NewLine + + "Allegoric Alaskans;Courageous Californians;draw" + .Trim(); + var expected = "Team | MP | W | D | L | P" + Environment.NewLine + + "Allegoric Alaskans | 3 | 2 | 1 | 0 | 7" + Environment.NewLine + + "Courageous Californians | 3 | 2 | 1 | 0 | 7" + Environment.NewLine + + "Blithering Badgers | 3 | 0 | 1 | 2 | 1" + Environment.NewLine + + "Devastating Donkeys | 3 | 0 | 1 | 2 | 1" + .Trim(); + Assert.Equal(expected, RunTally(input).Trim()); + } + + private string RunTally(string input) + { + var encoding = new UTF8Encoding(); + using (var inStream = new MemoryStream(encoding.GetBytes(input))) + { + using (var outStream = new MemoryStream()) + { + Tournament.Tally(inStream, outStream); + return encoding.GetString(outStream.ToArray()); + } + } } -} +} \ No newline at end of file diff --git a/generators/Exercises/Tournament.cs b/generators/Exercises/Tournament.cs new file mode 100644 index 0000000000..87a5556360 --- /dev/null +++ b/generators/Exercises/Tournament.cs @@ -0,0 +1,66 @@ +using System.Collections.Generic; +using Generators.Input; +using Generators.Output; + +namespace Generators.Exercises +{ + public class Tournament : Exercise + { + protected override void UpdateCanonicalData(CanonicalData canonicalData) + { + foreach (var canonicalDataCase in canonicalData.Cases) + { + canonicalDataCase.TestedMethodType = TestedMethodType.Static; + canonicalDataCase.UseVariablesForInput = true; + canonicalDataCase.UseVariableForExpected = true; + canonicalDataCase.Properties["input"] = ToMultiLineString(canonicalDataCase.Properties["input"]); + canonicalDataCase.Expected = ToMultiLineString(canonicalDataCase.Expected); + } + } + + protected override HashSet AddAdditionalNamespaces() + { + return new HashSet + { + typeof(System.String).Namespace, + typeof(System.IO.Stream).Namespace, + typeof(System.Text.UTF8Encoding).Namespace + }; + } + + protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody) + { + string template = @"Assert.Equal(expected, RunTally(input).Trim());"; + return TemplateRenderer.RenderInline(template, new { }); + } + + private UnescapedValue ToMultiLineString(string[] input) + { + const string template = @"{% if input.size == 0 %}string.Empty{% else %}{% for item in {{input}} %}{% if forloop.length == 1 %}""{{item}}""{% break %}{% endif %}""{{item}}""{% if forloop.last == false %} + Environment.NewLine +{% endif %} + {% endfor %}.Trim(){% endif %}"; + + return new UnescapedValue(TemplateRenderer.RenderInline(template, new { input })); + } + + protected override string[] RenderAdditionalMethods() + { + var methods = @" +private string RunTally(string input) +{ + var encoding = new UTF8Encoding(); + + using (var inStream = new MemoryStream(encoding.GetBytes(input))) + { + using (var outStream = new MemoryStream()) + { + Tournament.Tally(inStream, outStream); + return encoding.GetString(outStream.ToArray()); + } + } +}"; + return methods.Split("", System.StringSplitOptions.None); + } + + + } +}