Skip to content

Commit fd23081

Browse files
John ReeseErikSchierboom
authored andcommitted
Implement Queen Attack generator (#393)
* Implement Queen Attack generator * Change test name to include asserting no exception is thrown * Update test name to include assertion not thrown
1 parent 181b11a commit fd23081

4 files changed

Lines changed: 162 additions & 21 deletions

File tree

exercises/queen-attack/Example.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public Queen(int row, int column)
1212
public int Column { get; }
1313
}
1414

15-
public static class Queens
15+
public static class QueenAttack
1616
{
1717
public static bool CanAttack(Queen white, Queen black)
1818
{
@@ -25,4 +25,17 @@ public static bool CanAttack(Queen white, Queen black)
2525
black.Column == white.Column ||
2626
Math.Abs(black.Row - white.Row) == Math.Abs(black.Column - white.Column);
2727
}
28+
29+
public static Queen Create(int row, int column)
30+
{
31+
const int MIN_VALUE = 0;
32+
const int MAX_VALUE = 7;
33+
34+
if (row < MIN_VALUE || column < MIN_VALUE || row > MAX_VALUE || column > MAX_VALUE)
35+
{
36+
throw new ArgumentOutOfRangeException("invalid queen position");
37+
}
38+
39+
return new Queen(row, column);
40+
}
2841
}

exercises/queen-attack/QueenAttack.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,15 @@ public Queen(int row, int column)
1212
public int Column { get; }
1313
}
1414

15-
public static class Queens
15+
public static class QueenAttack
1616
{
1717
public static bool CanAttack(Queen white, Queen black)
1818
{
1919
throw new NotImplementedException("You need to implement this function.");
2020
}
21+
22+
public static Queen Create(int row, int column)
23+
{
24+
throw new NotImplementedException("You need to implement this function.");
25+
}
2126
}
Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,93 @@
1-
using System;
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
23
using Xunit;
4+
using System;
35

46
public class QueenAttackTest
57
{
68
[Fact]
7-
public void Cannot_occupy_same_space()
9+
public void Queen_with_a_valid_position_does_not_throw_exception()
10+
{
11+
var actual = QueenAttack.Create(2, 2);
12+
}
13+
14+
[Fact(Skip = "Remove to run test")]
15+
public void Queen_must_have_positive_rank()
16+
{
17+
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(-2, 2));
18+
}
19+
20+
[Fact(Skip = "Remove to run test")]
21+
public void Queen_must_have_rank_on_board()
22+
{
23+
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(8, 4));
24+
}
25+
26+
[Fact(Skip = "Remove to run test")]
27+
public void Queen_must_have_positive_file()
28+
{
29+
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(2, -2));
30+
}
31+
32+
[Fact(Skip = "Remove to run test")]
33+
public void Queen_must_have_file_on_board()
834
{
9-
var white = new Queen(2, 4);
10-
var black = new Queen(2, 4);
11-
Assert.Throws<ArgumentException>(() => Queens.CanAttack(white, black));
35+
Assert.Throws<ArgumentOutOfRangeException>(() => QueenAttack.Create(4, 8));
1236
}
1337

1438
[Fact(Skip = "Remove to run test")]
15-
public void Cannot_attack()
39+
public void Can_not_attack()
1640
{
17-
Assert.False(Queens.CanAttack(new Queen(2, 3), new Queen(4, 7)));
41+
var whiteQueen = QueenAttack.Create(2,4);
42+
var blackQueen = QueenAttack.Create(6,6);
43+
Assert.False(QueenAttack.CanAttack(whiteQueen, blackQueen));
1844
}
1945

2046
[Fact(Skip = "Remove to run test")]
21-
public void Can_attack_on_same_row()
47+
public void Can_attack_on_same_rank()
2248
{
23-
Assert.True(Queens.CanAttack(new Queen(2, 4), new Queen(2, 7)));
49+
var whiteQueen = QueenAttack.Create(2,4);
50+
var blackQueen = QueenAttack.Create(2,6);
51+
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
2452
}
2553

2654
[Fact(Skip = "Remove to run test")]
27-
public void Can_attack_on_same_column()
55+
public void Can_attack_on_same_file()
2856
{
29-
Assert.True(Queens.CanAttack(new Queen(5, 4), new Queen(2, 4)));
57+
var whiteQueen = QueenAttack.Create(4,5);
58+
var blackQueen = QueenAttack.Create(2,5);
59+
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
3060
}
3161

3262
[Fact(Skip = "Remove to run test")]
33-
public void Can_attack_on_diagonal()
63+
public void Can_attack_on_first_diagonal()
3464
{
35-
Assert.True(Queens.CanAttack(new Queen(1, 1), new Queen(6, 6)));
65+
var whiteQueen = QueenAttack.Create(2,2);
66+
var blackQueen = QueenAttack.Create(0,4);
67+
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
3668
}
3769

3870
[Fact(Skip = "Remove to run test")]
39-
public void Can_attack_on_other_diagonal()
71+
public void Can_attack_on_second_diagonal()
4072
{
41-
Assert.True(Queens.CanAttack(new Queen(0, 6), new Queen(1, 7)));
73+
var whiteQueen = QueenAttack.Create(2,2);
74+
var blackQueen = QueenAttack.Create(3,1);
75+
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
4276
}
4377

4478
[Fact(Skip = "Remove to run test")]
45-
public void Can_attack_on_yet_another_diagonal()
79+
public void Can_attack_on_third_diagonal()
4680
{
47-
Assert.True(Queens.CanAttack(new Queen(4, 1), new Queen(6, 3)));
81+
var whiteQueen = QueenAttack.Create(2,2);
82+
var blackQueen = QueenAttack.Create(1,1);
83+
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
4884
}
4985

5086
[Fact(Skip = "Remove to run test")]
51-
public void Can_attack_on_a_diagonal_slanted_the_other_way()
87+
public void Can_attack_on_fourth_diagonal()
5288
{
53-
Assert.True(Queens.CanAttack(new Queen(6, 1), new Queen(1, 6)));
89+
var whiteQueen = QueenAttack.Create(2,2);
90+
var blackQueen = QueenAttack.Create(5,5);
91+
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
5492
}
5593
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using Generators.Input;
2+
using Generators.Output;
3+
using Newtonsoft.Json.Linq;
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Generators.Exercises
8+
{
9+
public class QueenAttack : Exercise
10+
{
11+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
12+
{
13+
foreach (var canonicalDataCase in canonicalData.Cases)
14+
{
15+
if (canonicalDataCase.Property == "create")
16+
{
17+
SetCreatePropertyData(canonicalDataCase);
18+
}
19+
}
20+
}
21+
22+
protected override string RenderTestMethodBodyAssert(TestMethodBody testMethodBody)
23+
{
24+
if (testMethodBody.CanonicalDataCase.Property == "canAttack")
25+
{
26+
return RenderCanAttackAssert(testMethodBody);
27+
}
28+
29+
if (testMethodBody.UseVariableForTested)
30+
{
31+
return string.Empty;
32+
}
33+
34+
return base.RenderTestMethodBodyAssert(testMethodBody);
35+
}
36+
37+
private static string RenderCanAttackAssert(TestMethodBody testMethodBody)
38+
{
39+
const string template =
40+
@"var whiteQueen = QueenAttack.Create({{whiteQueenX}},{{whiteQueenY}});
41+
var blackQueen = QueenAttack.Create({{blackQueenX}},{{blackQueenY}});
42+
Assert.{% if Expected %}True{% else %}False{% endif %}(QueenAttack.CanAttack(whiteQueen, blackQueen));";
43+
44+
var whiteQueenPositions = GetCoordinatesFromPosition(testMethodBody.CanonicalDataCase.Input["white_queen"]);
45+
var blackQueenPositions = GetCoordinatesFromPosition(testMethodBody.CanonicalDataCase.Input["black_queen"]);
46+
47+
var templateParameters = new
48+
{
49+
whiteQueenX = whiteQueenPositions.Item1,
50+
whiteQueenY = whiteQueenPositions.Item2,
51+
blackQueenX = blackQueenPositions.Item1,
52+
blackQueenY = blackQueenPositions.Item2,
53+
Expected = testMethodBody.CanonicalDataCase.Expected
54+
};
55+
56+
return TemplateRenderer.RenderInline(template, templateParameters);
57+
}
58+
59+
private static Tuple<int, int> GetCoordinatesFromPosition(object rawPosition)
60+
{
61+
var coordinates = ((JObject)rawPosition).ToObject<Dictionary<string, string>>()["position"].Split(',');
62+
63+
var positionX = int.Parse(coordinates[0].TrimStart('('));
64+
var positionY = int.Parse(coordinates[1].TrimEnd(')'));
65+
66+
return Tuple.Create(positionX, positionY);
67+
}
68+
69+
private static void SetCreatePropertyData(CanonicalDataCase canonicalDataCase)
70+
{
71+
var validExpected = (long)canonicalDataCase.Expected >= 0;
72+
73+
canonicalDataCase.UseVariableForTested = validExpected;
74+
canonicalDataCase.ExceptionThrown = validExpected ? null : typeof(ArgumentOutOfRangeException);
75+
canonicalDataCase.Description = validExpected ? canonicalDataCase.Description + " does not throw exception" : canonicalDataCase.Description;
76+
77+
var coordinates = GetCoordinatesFromPosition(canonicalDataCase.Input["queen"]);
78+
canonicalDataCase.Input = new Dictionary<string, object>
79+
{
80+
["X"] = coordinates.Item1,
81+
["Y"] = coordinates.Item2
82+
};
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)