Skip to content
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

Add beer-song generator #269

Merged
merged 1 commit into from
Mar 28, 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
4 changes: 2 additions & 2 deletions exercises/beer-song/BeerSong.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using System;

public static class Beer
public static class BeerSong
{
public static string Verse(int number)
{
throw new NotImplementedException("You need to implement this function.");
}

public static string Sing(int start, int stop)
public static string Verses(int begin, int end)
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
390 changes: 390 additions & 0 deletions exercises/beer-song/BeerSongTest.cs

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions exercises/beer-song/BeerTest.cs

This file was deleted.

11 changes: 3 additions & 8 deletions exercises/beer-song/Example.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq;

public static class Beer
public static class BeerSong
{
public static string Verse(int number)
{
Expand All @@ -17,11 +17,6 @@ public static string Verse(int number)
}
}

public static string Sing(int start, int stop)
{
return Enumerable.Range(stop, start - stop + 1)
.Reverse()
.Select(Verse)
.Aggregate("", (acc, verse) => acc + verse + "\n");
}
public static string Verses(int begin, int end)
=> string.Join("\n", Enumerable.Range(end, begin - end + 1).Reverse().Select(Verse));
}
20 changes: 17 additions & 3 deletions generators/Data/CanonicalDataValue.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace Generators.Data
{
public static class CanonicalDataValue
{
public static string StringArrayToString(object expected)
=> string.Join("\\n\"+\n\"", ((JArray) expected).Values<string>());
public static string ExpectedToMultiLineString(object expected)
{
switch (expected)
{
case IEnumerable<string> enumerable:
return string.Join("\\n\"+\n\"", enumerable);
case JArray jarray:
return ExpectedToMultiLineString(((JArray) expected).Values<string>());
case string str:
return ExpectedToMultiLineString(str.Split('\n'));
default:
throw new ArgumentException("Cannot convert expected value to multil-ine string.");
}
}
}
}
33 changes: 33 additions & 0 deletions generators/Exercises/BeerSongExercise.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Generators.Data;
using Generators.Methods;

namespace Generators.Exercises
{
public class BeerSongExercise : EqualityExercise
{
public BeerSongExercise() : base("beer-song")
{
}

protected override TestMethodData CreateTestMethodData(CanonicalData canonicalData, CanonicalDataCase canonicalDataCase, int index)
{
var testMethodData = base.CreateTestMethodData(canonicalData, canonicalDataCase, index);

testMethodData.Options.UseVariableForExpected = true;
testMethodData.Options.FormatExpected = true;

testMethodData.CanonicalDataCase.Expected = CanonicalDataValue.ExpectedToMultiLineString(testMethodData.CanonicalDataCase.Expected);

if (testMethodData.CanonicalDataCase.Property == "verse")
testMethodData.Options.InputProperty = "number";
else
testMethodData.CanonicalDataCase.Input = new[]
{
testMethodData.CanonicalDataCase.Data["beginning"],
testMethodData.CanonicalDataCase.Data["end"]
};

return testMethodData;
}
}
}
2 changes: 1 addition & 1 deletion generators/Exercises/FoodChainExercise.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override TestMethodData CreateTestMethodData(CanonicalData canonicalDa
var testMethodData = base.CreateTestMethodData(canonicalData, canonicalDataCase, index);

testMethodData.Options.UseVariableForExpected = true;
testMethodData.CanonicalDataCase.Expected = CanonicalDataValue.StringArrayToString(canonicalDataCase.Expected);
testMethodData.CanonicalDataCase.Expected = CanonicalDataValue.ExpectedToMultiLineString(testMethodData.CanonicalDataCase.Expected);

if (testMethodData.CanonicalDataCase.Data.ContainsKey("end verse"))
testMethodData.CanonicalDataCase.Input = new[] { testMethodData.CanonicalDataCase.Data["start verse"], testMethodData.CanonicalDataCase.Data["end verse"] };
Expand Down