Skip to content

Add Two Bucket Test Generator #447

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 7 commits into from
Oct 2, 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
14 changes: 7 additions & 7 deletions exercises/two-bucket/Example.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class TwoBucketResult
{
public int Moves { get; set; }
public Bucket GoalBucket { get; set; }
public int OtherBucketContents { get; set; }
public int OtherBucket { get; set; }
}

public class BucketContainer
Expand Down Expand Up @@ -49,21 +49,21 @@ public void PourTo(BucketContainer other)
public bool CanPourTo(BucketContainer other) => !IsEmpty && !other.IsFull;
}

public class TwoBuckets
public class TwoBucket
{
private readonly BucketContainer bucketOne;
private readonly BucketContainer bucketTwo;
private readonly Action strategy;

public TwoBuckets(int bucketOneSize, int bucketTwoSize, Bucket startBucket)
public TwoBucket(int bucketOneSize, int bucketTwoSize, Bucket startBucket)
{
bucketOne = new BucketContainer(startBucket == Bucket.One ? bucketOneSize : 0, bucketOneSize);
bucketTwo = new BucketContainer(startBucket == Bucket.Two ? bucketTwoSize : 0, bucketTwoSize);
strategy = startBucket == Bucket.One ? (Action)StartFromFirstBucket : StartFromSecondBucket;

}

public TwoBucketResult Solve(int goal)
public TwoBucketResult Measure(int goal)
{
var moves = 0;

Expand All @@ -72,10 +72,10 @@ public TwoBucketResult Solve(int goal)
moves++;

if (bucketOne.Contents == goal)
return new TwoBucketResult { Moves = moves, GoalBucket = Bucket.One, OtherBucketContents = bucketTwo.Contents };
return new TwoBucketResult { Moves = moves, GoalBucket = Bucket.One, OtherBucket = bucketTwo.Contents };

if (bucketTwo.Contents == goal)
return new TwoBucketResult { Moves = moves, GoalBucket = Bucket.Two, OtherBucketContents = bucketOne.Contents };
return new TwoBucketResult { Moves = moves, GoalBucket = Bucket.Two, OtherBucket = bucketOne.Contents };

strategy();
}
Expand All @@ -94,7 +94,7 @@ public void StartFromFirstBucket()
else
throw new InvalidOperationException("Cannot transition from current state.");
}

public void StartFromSecondBucket()
{
if (bucketOne.IsFull)
Expand Down
8 changes: 4 additions & 4 deletions exercises/two-bucket/TwoBucket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ public class TwoBucketResult
{
public int Moves { get; set; }
public Bucket GoalBucket { get; set; }
public int OtherBucketContents { get; set; }
public int OtherBucket { get; set; }
}

public class TwoBuckets
public class TwoBucket
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example.cs should be changed as well.

{
public TwoBuckets(int bucketOneSize, int bucketTwoSize, Bucket startBucket)
public TwoBucket(int bucketOne, int bucketTwo, Bucket startBucket)
{
throw new NotImplementedException("You need to implement this function.");
}

public TwoBucketResult Solve(int goal)
public TwoBucketResult Measure(int goal)
{
throw new NotImplementedException("You need to implement this function.");
}
Expand Down
92 changes: 45 additions & 47 deletions exercises/two-bucket/TwoBucketTest.cs
Original file line number Diff line number Diff line change
@@ -1,68 +1,66 @@
using Xunit;
// This file was auto-generated based on version 1.1.0 of the canonical data.

using Xunit;

public class TwoBucketTest
{
[Fact]
public void First_example()
public void Measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_one()
{
var bucketOneSize = 3;
var bucketTwoSize = 5;
var goal = 1;
var startBucket = Bucket.One;
var twoBuckets = new TwoBuckets(bucketOneSize, bucketTwoSize, startBucket);

var actual = twoBuckets.Solve(goal);

Assert.Equal(4, actual.Moves);
Assert.Equal(Bucket.One, actual.GoalBucket);
Assert.Equal(5, actual.OtherBucketContents);
var sut = new TwoBucket(3, 5, Bucket.One);
var result = sut.Measure(1);
Assert.Equal(4, result.Moves);
Assert.Equal(5, result.OtherBucket);
Assert.Equal(Bucket.One, result.GoalBucket);
}

[Fact(Skip = "Remove to run test")]
public void Second_example()
public void Measure_using_bucket_one_of_size_3_and_bucket_two_of_size_5_start_with_bucket_two()
{
var bucketOneSize = 3;
var bucketTwoSize = 5;
var goal = 1;
var startBucket = Bucket.Two;
var twoBuckets = new TwoBuckets(bucketOneSize, bucketTwoSize, startBucket);

var actual = twoBuckets.Solve(goal);

Assert.Equal(8, actual.Moves);
Assert.Equal(Bucket.Two, actual.GoalBucket);
Assert.Equal(3, actual.OtherBucketContents);
var sut = new TwoBucket(3, 5, Bucket.Two);
var result = sut.Measure(1);
Assert.Equal(8, result.Moves);
Assert.Equal(3, result.OtherBucket);
Assert.Equal(Bucket.Two, result.GoalBucket);
}

[Fact(Skip = "Remove to run test")]
public void Third_example()
public void Measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_one()
{
var bucketOneSize = 7;
var bucketTwoSize = 11;
var goal = 2;
var startBucket = Bucket.One;
var twoBuckets = new TwoBuckets(bucketOneSize, bucketTwoSize, startBucket);

var actual = twoBuckets.Solve(goal);

Assert.Equal(14, actual.Moves);
Assert.Equal(Bucket.One, actual.GoalBucket);
Assert.Equal(11, actual.OtherBucketContents);
var sut = new TwoBucket(7, 11, Bucket.One);
var result = sut.Measure(2);
Assert.Equal(14, result.Moves);
Assert.Equal(11, result.OtherBucket);
Assert.Equal(Bucket.One, result.GoalBucket);
}

[Fact(Skip = "Remove to run test")]
public void Fourth_example()
public void Measure_using_bucket_one_of_size_7_and_bucket_two_of_size_11_start_with_bucket_two()
{
var bucketOneSize = 7;
var bucketTwoSize = 11;
var goal = 2;
var startBucket = Bucket.Two;
var twoBuckets = new TwoBuckets(bucketOneSize, bucketTwoSize, startBucket);
var sut = new TwoBucket(7, 11, Bucket.Two);
var result = sut.Measure(2);
Assert.Equal(18, result.Moves);
Assert.Equal(7, result.OtherBucket);
Assert.Equal(Bucket.Two, result.GoalBucket);
}

var actual = twoBuckets.Solve(goal);
[Fact(Skip = "Remove to run test")]
public void Measure_one_step_using_bucket_one_of_size_1_and_bucket_two_of_size_3_start_with_bucket_two()
{
var sut = new TwoBucket(1, 3, Bucket.Two);
var result = sut.Measure(3);
Assert.Equal(1, result.Moves);
Assert.Equal(0, result.OtherBucket);
Assert.Equal(Bucket.Two, result.GoalBucket);
}

Assert.Equal(18, actual.Moves);
Assert.Equal(Bucket.Two, actual.GoalBucket);
Assert.Equal(7, actual.OtherBucketContents);
[Fact(Skip = "Remove to run test")]
public void Measure_using_bucket_one_of_size_2_and_bucket_two_of_size_3_start_with_bucket_one_and_end_with_bucket_two()
{
var sut = new TwoBucket(2, 3, Bucket.One);
var result = sut.Measure(3);
Assert.Equal(4, result.Moves);
Assert.Equal(1, result.OtherBucket);
Assert.Equal(Bucket.Two, result.GoalBucket);
}
}
49 changes: 49 additions & 0 deletions generators/Exercises/TwoBucket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Generators.Input;
using Generators.Output;

namespace Generators.Exercises
{
public class TwoBucket : Exercise
{
protected override void UpdateCanonicalData(CanonicalData canonicalData)
{
foreach (var canonicalDataCase in canonicalData.Cases)
{
canonicalDataCase.TestedMethodType = TestedMethodType.Instance;
canonicalDataCase.SetConstructorInputParameters("bucket_one", "bucket_two", "start_bucket");

var start_bucket = canonicalDataCase.Properties["start_bucket"];
canonicalDataCase.Properties["start_bucket"] = new UnescapedValue(start_bucket == "two" ? "Bucket.Two" : "Bucket.One");
}
}

protected override string RenderTestMethodBodyAct(TestMethodBody testMethodBody)
{
const string template = @"var result = {{MethodInvocation}};";

var templateParameters = new
{
MethodInvocation = testMethodBody.Data.TestedMethodInvocation
};

return TemplateRenderer.RenderInline(template, templateParameters);
}

protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
{
const string template =
@"Assert.Equal({{MovesExpected}}, result.Moves);
Assert.Equal({{OtherBucketExpected}}, result.OtherBucket);
Assert.Equal({% if GoalBucketExpected == 'two' %}Bucket.Two{% else %}Bucket.One{% endif %}, result.GoalBucket);";

var templateParameters = new
{
MovesExpected = testMethodBody.CanonicalDataCase.Expected["moves"],
OtherBucketExpected = testMethodBody.CanonicalDataCase.Expected["other_bucket"],
GoalBucketExpected = testMethodBody.CanonicalDataCase.Expected["goal_bucket"],
};

return TemplateRenderer.RenderInline(template, templateParameters);
}
}
}