Skip to content

Add Tournament test generator #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion exercises/tournament/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
219 changes: 151 additions & 68 deletions exercises/tournament/TournamentTest.cs
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
}
}
66 changes: 66 additions & 0 deletions generators/Exercises/Tournament.cs
Original file line number Diff line number Diff line change
@@ -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<string> AddAdditionalNamespaces()
{
return new HashSet<string>
{
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);
}


}
}