Skip to content

Commit

Permalink
More gens (#2378)
Browse files Browse the repository at this point in the history
* flatten-array: add generator

* connect: add generator

* Remove old

* sgf-parsing: add generator

* linked-list: add generator

* binary-search-tree: add generator

* Remove

[no important files changed]
  • Loading branch information
ErikSchierboom authored Feb 6, 2025
1 parent cde0f16 commit 5c937a9
Show file tree
Hide file tree
Showing 16 changed files with 319 additions and 478 deletions.
37 changes: 37 additions & 0 deletions exercises/practice/binary-search-tree/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{ func assertions(node, path)
checks = [$"Assert.Equal({string.to_int node.data}, tree.{path}Value);"]
if node.left
checks = array.add_range checks (assertions node.left (path + "Left."))
end
if node.right
checks = array.add_range checks (assertions node.right (path + "Right."))
end
ret checks
end }}

using System.Linq;
using Xunit;

public class {{ testClass }}
{
{{- for test in tests }}
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
public void {{ test.testMethod }}()
{
{{- if test.input.treeData | array.size == 1 }}
var tree = new {{ testedClass }}({{ test.input.treeData[0] | @string.to_int }});
{{- else -}}
var tree = new {{ testedClass }}(new[] { {{ test.input.treeData | array.each @string.to_int | array.join "," }} });
{{- end -}}
{{- if test.property == "data" }}
{{ test.expected | assertions "" | array.join "\n" }}
{{- else }}
int[] expected = {{ test.expected | array.each @string.to_int }};
Assert.Equal(expected, tree.AsEnumerable());
{{ end -}}
}
{{ end -}}
}
31 changes: 18 additions & 13 deletions exercises/practice/binary-search-tree/BinarySearchTreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ public void Data_is_retained()
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Smaller_number_at_left_node()
public void Insert_data_at_proper_node_smaller_number_at_left_node()
{
var tree = new BinarySearchTree(new[] { 4, 2 });
Assert.Equal(4, tree.Value);
Assert.Equal(2, tree.Left.Value);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Same_number_at_left_node()
public void Insert_data_at_proper_node_same_number_at_left_node()
{
var tree = new BinarySearchTree(new[] { 4, 4 });
Assert.Equal(4, tree.Value);
Assert.Equal(4, tree.Left.Value);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Greater_number_at_right_node()
public void Insert_data_at_proper_node_greater_number_at_right_node()
{
var tree = new BinarySearchTree(new[] { 4, 5 });
Assert.Equal(4, tree.Value);
Expand All @@ -48,37 +48,42 @@ public void Can_create_complex_tree()
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_single_number()
public void Can_sort_data_can_sort_single_number()
{
var tree = new BinarySearchTree(2);
Assert.Equal(new[] { 2 }, tree.AsEnumerable());
int[] expected = [2];
Assert.Equal(expected, tree.AsEnumerable());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_if_second_number_is_smaller_than_first()
public void Can_sort_data_can_sort_if_second_number_is_smaller_than_first()
{
var tree = new BinarySearchTree(new[] { 2, 1 });
Assert.Equal(new[] { 1, 2 }, tree.AsEnumerable());
int[] expected = [1, 2];
Assert.Equal(expected, tree.AsEnumerable());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_if_second_number_is_same_as_first()
public void Can_sort_data_can_sort_if_second_number_is_same_as_first()
{
var tree = new BinarySearchTree(new[] { 2, 2 });
Assert.Equal(new[] { 2, 2 }, tree.AsEnumerable());
int[] expected = [2, 2];
Assert.Equal(expected, tree.AsEnumerable());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_if_second_number_is_greater_than_first()
public void Can_sort_data_can_sort_if_second_number_is_greater_than_first()
{
var tree = new BinarySearchTree(new[] { 2, 3 });
Assert.Equal(new[] { 2, 3 }, tree.AsEnumerable());
int[] expected = [2, 3];
Assert.Equal(expected, tree.AsEnumerable());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Can_sort_complex_tree()
public void Can_sort_data_can_sort_complex_tree()
{
var tree = new BinarySearchTree(new[] { 2, 1, 3, 6, 7, 5 });
Assert.Equal(new[] { 1, 2, 3, 5, 6, 7 }, tree.AsEnumerable());
int[] expected = [1, 2, 3, 5, 6, 7];
Assert.Equal(expected, tree.AsEnumerable());
}
}
29 changes: 29 additions & 0 deletions exercises/practice/connect/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{{ func color
case $0
when "X"
ret "Black"
when "O"
ret "White"
else
ret "None"
end
end }}

using Xunit;

public class {{ testClass }}
{
{{- for test in tests }}
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
public void {{ test.testMethod }}()
{
string[] board = [
{{- for line in test.input.board }}
{{ line | string.literal }}{{- if !for.last }},{{ end -}}
{{ end }}
];
var sut = new {{ testedClass }}(board);
Assert.Equal({{ test.expected | color | enum "ConnectWinner" }}, sut.Result());
}
{{ end -}}
}
50 changes: 20 additions & 30 deletions exercises/practice/connect/ConnectTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,134 +5,124 @@ public class ConnectTests
[Fact]
public void An_empty_board_has_no_winner()
{
var board = new[]
{
string[] board = [
". . . . .",
" . . . . .",
" . . . . .",
" . . . . .",
" . . . . ."
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.None, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void X_can_win_on_a_1x1_board()
{
var board = new[]
{
string[] board = [
"X"
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.Black, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void O_can_win_on_a_1x1_board()
{
var board = new[]
{
string[] board = [
"O"
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.White, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Only_edges_does_not_make_a_winner()
{
var board = new[]
{
string[] board = [
"O O O X",
" X . . X",
" X . . X",
" X O O O"
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.None, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Illegal_diagonal_does_not_make_a_winner()
{
var board = new[]
{
string[] board = [
"X O . .",
" O X X X",
" O X O .",
" . O X .",
" X X O O"
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.None, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Nobody_wins_crossing_adjacent_angles()
{
var board = new[]
{
string[] board = [
"X . . .",
" . X O .",
" O . X O",
" . O . X",
" . . O ."
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.None, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void X_wins_crossing_from_left_to_right()
{
var board = new[]
{
string[] board = [
". O . .",
" O X X X",
" O X O .",
" X X O X",
" . O X ."
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.Black, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void O_wins_crossing_from_top_to_bottom()
{
var board = new[]
{
string[] board = [
". O . .",
" O X X X",
" O O O .",
" X X O X",
" . O X ."
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.White, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void X_wins_using_a_convoluted_path()
{
var board = new[]
{
string[] board = [
". X X . .",
" X . X . X",
" . X . X .",
" . X X . .",
" O O O O O"
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.Black, sut.Result());
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void X_wins_using_a_spiral_path()
{
var board = new[]
{
string[] board = [
"O X X X X X X X X",
" O X O O O O O O O",
" O X O X X X X X O",
Expand All @@ -142,7 +132,7 @@ public void X_wins_using_a_spiral_path()
" O X X X X X O X O",
" O O O O O O O X O",
" X X X X X X X X O"
};
];
var sut = new Connect(board);
Assert.Equal(ConnectWinner.Black, sut.Result());
}
Expand Down
38 changes: 38 additions & 0 deletions exercises/practice/flatten-array/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{ func toarg(input)
case (object.typeof input)
when "array"
if input.empty?
ret "Array.Empty<object>()"
else
elements = array.join (array.each input @toarg) ", "
ret "new object[] { " + elements + " }"
end
else
if input
ret input
else
ret "null"
end
end
end }}

using System;
using Xunit;

public class {{ testClass }}
{
{{- for test in tests }}
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
public void {{ test.testMethod }}()
{
{{- if test.input.array.empty? }}
var array = Array.Empty<object>();
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(array));
{{- else }}
object[] array = {{ test.input.array | toarg }};
object[] expected = {{ test.expected }};
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(array));
{{ end -}}
}
{{ end -}}
}
Loading

0 comments on commit 5c937a9

Please sign in to comment.