Skip to content

Commit 5c937a9

Browse files
More gens (#2378)
* 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]
1 parent cde0f16 commit 5c937a9

File tree

16 files changed

+319
-478
lines changed

16 files changed

+319
-478
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{{ func assertions(node, path)
2+
checks = [$"Assert.Equal({string.to_int node.data}, tree.{path}Value);"]
3+
4+
if node.left
5+
checks = array.add_range checks (assertions node.left (path + "Left."))
6+
end
7+
8+
if node.right
9+
checks = array.add_range checks (assertions node.right (path + "Right."))
10+
end
11+
12+
ret checks
13+
end }}
14+
15+
using System.Linq;
16+
using Xunit;
17+
18+
public class {{ testClass }}
19+
{
20+
{{- for test in tests }}
21+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
22+
public void {{ test.testMethod }}()
23+
{
24+
{{- if test.input.treeData | array.size == 1 }}
25+
var tree = new {{ testedClass }}({{ test.input.treeData[0] | @string.to_int }});
26+
{{- else -}}
27+
var tree = new {{ testedClass }}(new[] { {{ test.input.treeData | array.each @string.to_int | array.join "," }} });
28+
{{- end -}}
29+
{{- if test.property == "data" }}
30+
{{ test.expected | assertions "" | array.join "\n" }}
31+
{{- else }}
32+
int[] expected = {{ test.expected | array.each @string.to_int }};
33+
Assert.Equal(expected, tree.AsEnumerable());
34+
{{ end -}}
35+
}
36+
{{ end -}}
37+
}

exercises/practice/binary-search-tree/BinarySearchTreeTests.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@ public void Data_is_retained()
1111
}
1212

1313
[Fact(Skip = "Remove this Skip property to run this test")]
14-
public void Smaller_number_at_left_node()
14+
public void Insert_data_at_proper_node_smaller_number_at_left_node()
1515
{
1616
var tree = new BinarySearchTree(new[] { 4, 2 });
1717
Assert.Equal(4, tree.Value);
1818
Assert.Equal(2, tree.Left.Value);
1919
}
2020

2121
[Fact(Skip = "Remove this Skip property to run this test")]
22-
public void Same_number_at_left_node()
22+
public void Insert_data_at_proper_node_same_number_at_left_node()
2323
{
2424
var tree = new BinarySearchTree(new[] { 4, 4 });
2525
Assert.Equal(4, tree.Value);
2626
Assert.Equal(4, tree.Left.Value);
2727
}
2828

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

5050
[Fact(Skip = "Remove this Skip property to run this test")]
51-
public void Can_sort_single_number()
51+
public void Can_sort_data_can_sort_single_number()
5252
{
5353
var tree = new BinarySearchTree(2);
54-
Assert.Equal(new[] { 2 }, tree.AsEnumerable());
54+
int[] expected = [2];
55+
Assert.Equal(expected, tree.AsEnumerable());
5556
}
5657

5758
[Fact(Skip = "Remove this Skip property to run this test")]
58-
public void Can_sort_if_second_number_is_smaller_than_first()
59+
public void Can_sort_data_can_sort_if_second_number_is_smaller_than_first()
5960
{
6061
var tree = new BinarySearchTree(new[] { 2, 1 });
61-
Assert.Equal(new[] { 1, 2 }, tree.AsEnumerable());
62+
int[] expected = [1, 2];
63+
Assert.Equal(expected, tree.AsEnumerable());
6264
}
6365

6466
[Fact(Skip = "Remove this Skip property to run this test")]
65-
public void Can_sort_if_second_number_is_same_as_first()
67+
public void Can_sort_data_can_sort_if_second_number_is_same_as_first()
6668
{
6769
var tree = new BinarySearchTree(new[] { 2, 2 });
68-
Assert.Equal(new[] { 2, 2 }, tree.AsEnumerable());
70+
int[] expected = [2, 2];
71+
Assert.Equal(expected, tree.AsEnumerable());
6972
}
7073

7174
[Fact(Skip = "Remove this Skip property to run this test")]
72-
public void Can_sort_if_second_number_is_greater_than_first()
75+
public void Can_sort_data_can_sort_if_second_number_is_greater_than_first()
7376
{
7477
var tree = new BinarySearchTree(new[] { 2, 3 });
75-
Assert.Equal(new[] { 2, 3 }, tree.AsEnumerable());
78+
int[] expected = [2, 3];
79+
Assert.Equal(expected, tree.AsEnumerable());
7680
}
7781

7882
[Fact(Skip = "Remove this Skip property to run this test")]
79-
public void Can_sort_complex_tree()
83+
public void Can_sort_data_can_sort_complex_tree()
8084
{
8185
var tree = new BinarySearchTree(new[] { 2, 1, 3, 6, 7, 5 });
82-
Assert.Equal(new[] { 1, 2, 3, 5, 6, 7 }, tree.AsEnumerable());
86+
int[] expected = [1, 2, 3, 5, 6, 7];
87+
Assert.Equal(expected, tree.AsEnumerable());
8388
}
8489
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{{ func color
2+
case $0
3+
when "X"
4+
ret "Black"
5+
when "O"
6+
ret "White"
7+
else
8+
ret "None"
9+
end
10+
end }}
11+
12+
using Xunit;
13+
14+
public class {{ testClass }}
15+
{
16+
{{- for test in tests }}
17+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
18+
public void {{ test.testMethod }}()
19+
{
20+
string[] board = [
21+
{{- for line in test.input.board }}
22+
{{ line | string.literal }}{{- if !for.last }},{{ end -}}
23+
{{ end }}
24+
];
25+
var sut = new {{ testedClass }}(board);
26+
Assert.Equal({{ test.expected | color | enum "ConnectWinner" }}, sut.Result());
27+
}
28+
{{ end -}}
29+
}

exercises/practice/connect/ConnectTests.cs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,134 +5,124 @@ public class ConnectTests
55
[Fact]
66
public void An_empty_board_has_no_winner()
77
{
8-
var board = new[]
9-
{
8+
string[] board = [
109
". . . . .",
1110
" . . . . .",
1211
" . . . . .",
1312
" . . . . .",
1413
" . . . . ."
15-
};
14+
];
1615
var sut = new Connect(board);
1716
Assert.Equal(ConnectWinner.None, sut.Result());
1817
}
1918

2019
[Fact(Skip = "Remove this Skip property to run this test")]
2120
public void X_can_win_on_a_1x1_board()
2221
{
23-
var board = new[]
24-
{
22+
string[] board = [
2523
"X"
26-
};
24+
];
2725
var sut = new Connect(board);
2826
Assert.Equal(ConnectWinner.Black, sut.Result());
2927
}
3028

3129
[Fact(Skip = "Remove this Skip property to run this test")]
3230
public void O_can_win_on_a_1x1_board()
3331
{
34-
var board = new[]
35-
{
32+
string[] board = [
3633
"O"
37-
};
34+
];
3835
var sut = new Connect(board);
3936
Assert.Equal(ConnectWinner.White, sut.Result());
4037
}
4138

4239
[Fact(Skip = "Remove this Skip property to run this test")]
4340
public void Only_edges_does_not_make_a_winner()
4441
{
45-
var board = new[]
46-
{
42+
string[] board = [
4743
"O O O X",
4844
" X . . X",
4945
" X . . X",
5046
" X O O O"
51-
};
47+
];
5248
var sut = new Connect(board);
5349
Assert.Equal(ConnectWinner.None, sut.Result());
5450
}
5551

5652
[Fact(Skip = "Remove this Skip property to run this test")]
5753
public void Illegal_diagonal_does_not_make_a_winner()
5854
{
59-
var board = new[]
60-
{
55+
string[] board = [
6156
"X O . .",
6257
" O X X X",
6358
" O X O .",
6459
" . O X .",
6560
" X X O O"
66-
};
61+
];
6762
var sut = new Connect(board);
6863
Assert.Equal(ConnectWinner.None, sut.Result());
6964
}
7065

7166
[Fact(Skip = "Remove this Skip property to run this test")]
7267
public void Nobody_wins_crossing_adjacent_angles()
7368
{
74-
var board = new[]
75-
{
69+
string[] board = [
7670
"X . . .",
7771
" . X O .",
7872
" O . X O",
7973
" . O . X",
8074
" . . O ."
81-
};
75+
];
8276
var sut = new Connect(board);
8377
Assert.Equal(ConnectWinner.None, sut.Result());
8478
}
8579

8680
[Fact(Skip = "Remove this Skip property to run this test")]
8781
public void X_wins_crossing_from_left_to_right()
8882
{
89-
var board = new[]
90-
{
83+
string[] board = [
9184
". O . .",
9285
" O X X X",
9386
" O X O .",
9487
" X X O X",
9588
" . O X ."
96-
};
89+
];
9790
var sut = new Connect(board);
9891
Assert.Equal(ConnectWinner.Black, sut.Result());
9992
}
10093

10194
[Fact(Skip = "Remove this Skip property to run this test")]
10295
public void O_wins_crossing_from_top_to_bottom()
10396
{
104-
var board = new[]
105-
{
97+
string[] board = [
10698
". O . .",
10799
" O X X X",
108100
" O O O .",
109101
" X X O X",
110102
" . O X ."
111-
};
103+
];
112104
var sut = new Connect(board);
113105
Assert.Equal(ConnectWinner.White, sut.Result());
114106
}
115107

116108
[Fact(Skip = "Remove this Skip property to run this test")]
117109
public void X_wins_using_a_convoluted_path()
118110
{
119-
var board = new[]
120-
{
111+
string[] board = [
121112
". X X . .",
122113
" X . X . X",
123114
" . X . X .",
124115
" . X X . .",
125116
" O O O O O"
126-
};
117+
];
127118
var sut = new Connect(board);
128119
Assert.Equal(ConnectWinner.Black, sut.Result());
129120
}
130121

131122
[Fact(Skip = "Remove this Skip property to run this test")]
132123
public void X_wins_using_a_spiral_path()
133124
{
134-
var board = new[]
135-
{
125+
string[] board = [
136126
"O X X X X X X X X",
137127
" O X O O O O O O O",
138128
" O X O X X X X X O",
@@ -142,7 +132,7 @@ public void X_wins_using_a_spiral_path()
142132
" O X X X X X O X O",
143133
" O O O O O O O X O",
144134
" X X X X X X X X O"
145-
};
135+
];
146136
var sut = new Connect(board);
147137
Assert.Equal(ConnectWinner.Black, sut.Result());
148138
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{{ func toarg(input)
2+
case (object.typeof input)
3+
when "array"
4+
if input.empty?
5+
ret "Array.Empty<object>()"
6+
else
7+
elements = array.join (array.each input @toarg) ", "
8+
ret "new object[] { " + elements + " }"
9+
end
10+
else
11+
if input
12+
ret input
13+
else
14+
ret "null"
15+
end
16+
end
17+
end }}
18+
19+
using System;
20+
using Xunit;
21+
22+
public class {{ testClass }}
23+
{
24+
{{- for test in tests }}
25+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
26+
public void {{ test.testMethod }}()
27+
{
28+
{{- if test.input.array.empty? }}
29+
var array = Array.Empty<object>();
30+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}(array));
31+
{{- else }}
32+
object[] array = {{ test.input.array | toarg }};
33+
object[] expected = {{ test.expected }};
34+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}(array));
35+
{{ end -}}
36+
}
37+
{{ end -}}
38+
}

0 commit comments

Comments
 (0)