Skip to content

Commit 0de1537

Browse files
word-search: add generator (#2374)
* queen-attack: add generator * prime-factors: add generator * pythagorean-triplet: add generator * nucleotide-count: add generator [no important files changed] * word-search: add generator
1 parent 85c19dd commit 0de1537

File tree

14 files changed

+298
-489
lines changed

14 files changed

+298
-489
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Xunit;
4+
5+
public class {{ testClass }}
6+
{
7+
{{- for test in tests }}
8+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
9+
public void {{ test.testMethod }}()
10+
{
11+
{{- if test.expected.error }}
12+
Assert.Throws<ArgumentException>(() => {{ testedClass }}.Count({{ test.input.strand | string.literal }}));
13+
{{ else }}
14+
var expected = new Dictionary<char, int>
15+
{
16+
{{- for key in test.expected | object.keys }}
17+
['{{ key }}'] = {{ test.expected[key] }}{{- if !for.last }},{{ end -}}
18+
{{ end -}}
19+
};
20+
Assert.Equal(expected, {{ testedClass }}.Count({{ test.input.strand | string.literal }}));
21+
{{ end -}}
22+
}
23+
{{ end -}}
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Xunit;
2+
3+
public class {{ testClass }}
4+
{
5+
{{- for test in tests }}
6+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
7+
public void {{ test.testMethod }}()
8+
{
9+
{{- if test.expected.empty? }}
10+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}({{ test.input.value }}));
11+
{{- else }}
12+
long[] expected = {{ test.expected }};
13+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.value }}));
14+
{{ end -}}
15+
}
16+
{{ end -}}
17+
}

exercises/practice/prime-factors/PrimeFactorsTests.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,83 @@ public class PrimeFactorsTests
55
[Fact]
66
public void No_factors()
77
{
8-
Assert.Empty(PrimeFactors.Factors(1L));
8+
Assert.Empty(PrimeFactors.Factors(1));
99
}
1010

1111
[Fact(Skip = "Remove this Skip property to run this test")]
1212
public void Prime_number()
1313
{
14-
Assert.Equal(new[] { 2L }, PrimeFactors.Factors(2L));
14+
long[] expected = [2];
15+
Assert.Equal(expected, PrimeFactors.Factors(2));
1516
}
1617

1718
[Fact(Skip = "Remove this Skip property to run this test")]
1819
public void Another_prime_number()
1920
{
20-
Assert.Equal(new[] { 3L }, PrimeFactors.Factors(3L));
21+
long[] expected = [3];
22+
Assert.Equal(expected, PrimeFactors.Factors(3));
2123
}
2224

2325
[Fact(Skip = "Remove this Skip property to run this test")]
2426
public void Square_of_a_prime()
2527
{
26-
Assert.Equal(new[] { 3L, 3L }, PrimeFactors.Factors(9L));
28+
long[] expected = [3, 3];
29+
Assert.Equal(expected, PrimeFactors.Factors(9));
2730
}
2831

2932
[Fact(Skip = "Remove this Skip property to run this test")]
3033
public void Product_of_first_prime()
3134
{
32-
Assert.Equal(new[] { 2L, 2L }, PrimeFactors.Factors(4L));
35+
long[] expected = [2, 2];
36+
Assert.Equal(expected, PrimeFactors.Factors(4));
3337
}
3438

3539
[Fact(Skip = "Remove this Skip property to run this test")]
3640
public void Cube_of_a_prime()
3741
{
38-
Assert.Equal(new[] { 2L, 2L, 2L }, PrimeFactors.Factors(8L));
42+
long[] expected = [2, 2, 2];
43+
Assert.Equal(expected, PrimeFactors.Factors(8));
3944
}
4045

4146
[Fact(Skip = "Remove this Skip property to run this test")]
4247
public void Product_of_second_prime()
4348
{
44-
Assert.Equal(new[] { 3L, 3L, 3L }, PrimeFactors.Factors(27L));
49+
long[] expected = [3, 3, 3];
50+
Assert.Equal(expected, PrimeFactors.Factors(27));
4551
}
4652

4753
[Fact(Skip = "Remove this Skip property to run this test")]
4854
public void Product_of_third_prime()
4955
{
50-
Assert.Equal(new[] { 5L, 5L, 5L, 5L }, PrimeFactors.Factors(625L));
56+
long[] expected = [5, 5, 5, 5];
57+
Assert.Equal(expected, PrimeFactors.Factors(625));
5158
}
5259

5360
[Fact(Skip = "Remove this Skip property to run this test")]
5461
public void Product_of_first_and_second_prime()
5562
{
56-
Assert.Equal(new[] { 2L, 3L }, PrimeFactors.Factors(6L));
63+
long[] expected = [2, 3];
64+
Assert.Equal(expected, PrimeFactors.Factors(6));
5765
}
5866

5967
[Fact(Skip = "Remove this Skip property to run this test")]
6068
public void Product_of_primes_and_non_primes()
6169
{
62-
Assert.Equal(new[] { 2L, 2L, 3L }, PrimeFactors.Factors(12L));
70+
long[] expected = [2, 2, 3];
71+
Assert.Equal(expected, PrimeFactors.Factors(12));
6372
}
6473

6574
[Fact(Skip = "Remove this Skip property to run this test")]
6675
public void Product_of_primes()
6776
{
68-
Assert.Equal(new[] { 5L, 17L, 23L, 461L }, PrimeFactors.Factors(901255L));
77+
long[] expected = [5, 17, 23, 461];
78+
Assert.Equal(expected, PrimeFactors.Factors(901255));
6979
}
7080

7181
[Fact(Skip = "Remove this Skip property to run this test")]
7282
public void Factors_include_a_large_prime()
7383
{
74-
Assert.Equal(new[] { 11L, 9539L, 894119L }, PrimeFactors.Factors(93819012551L));
84+
long[] expected = [11, 9539, 894119];
85+
Assert.Equal(expected, PrimeFactors.Factors(93819012551));
7586
}
7687
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Xunit;
2+
3+
public class {{ testClass }}
4+
{
5+
{{- for test in tests }}
6+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
7+
public void {{ test.testMethod }}()
8+
{
9+
{{- if test.expected.empty? }}
10+
Assert.Empty({{ testedClass }}.{{ test.testedMethod }}({{ test.input.n }}));
11+
{{- else }}
12+
(int,int,int)[] expected = [
13+
{{- for expected in test.expected }}
14+
({{ expected | array.join ", " }}){{- if !for.last }},{{ end -}}
15+
{{ end }}
16+
];
17+
Assert.Equal(expected, {{ testedClass }}.{{ test.testedMethod }}({{ test.input.n }}));
18+
{{ end -}}
19+
}
20+
{{ end -}}
21+
}
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
1-
using System;
21
using Xunit;
32

43
public class PythagoreanTripletTests
54
{
65
[Fact]
76
public void Triplets_whose_sum_is_12()
87
{
9-
Assert.Equal(new[]
10-
{
8+
(int, int, int)[] expected = [
119
(3, 4, 5)
12-
}, PythagoreanTriplet.TripletsWithSum(12));
10+
];
11+
Assert.Equal(expected, PythagoreanTriplet.TripletsWithSum(12));
1312
}
1413

1514
[Fact(Skip = "Remove this Skip property to run this test")]
1615
public void Triplets_whose_sum_is_108()
1716
{
18-
Assert.Equal(new[]
19-
{
17+
(int, int, int)[] expected = [
2018
(27, 36, 45)
21-
}, PythagoreanTriplet.TripletsWithSum(108));
19+
];
20+
Assert.Equal(expected, PythagoreanTriplet.TripletsWithSum(108));
2221
}
2322

2423
[Fact(Skip = "Remove this Skip property to run this test")]
2524
public void Triplets_whose_sum_is_1000()
2625
{
27-
Assert.Equal(new[]
28-
{
26+
(int, int, int)[] expected = [
2927
(200, 375, 425)
30-
}, PythagoreanTriplet.TripletsWithSum(1000));
28+
];
29+
Assert.Equal(expected, PythagoreanTriplet.TripletsWithSum(1000));
3130
}
3231

3332
[Fact(Skip = "Remove this Skip property to run this test")]
3433
public void No_matching_triplets_for_1001()
3534
{
36-
Assert.Equal(Array.Empty<(int, int, int)>(), PythagoreanTriplet.TripletsWithSum(1001));
35+
Assert.Empty(PythagoreanTriplet.TripletsWithSum(1001));
3736
}
3837

3938
[Fact(Skip = "Remove this Skip property to run this test")]
4039
public void Returns_all_matching_triplets()
4140
{
42-
Assert.Equal(new[]
43-
{
41+
(int, int, int)[] expected = [
4442
(9, 40, 41),
4543
(15, 36, 39)
46-
}, PythagoreanTriplet.TripletsWithSum(90));
44+
];
45+
Assert.Equal(expected, PythagoreanTriplet.TripletsWithSum(90));
4746
}
4847

4948
[Fact(Skip = "Remove this Skip property to run this test")]
5049
public void Several_matching_triplets()
5150
{
52-
Assert.Equal(new[]
53-
{
51+
(int, int, int)[] expected = [
5452
(40, 399, 401),
5553
(56, 390, 394),
5654
(105, 360, 375),
@@ -59,19 +57,20 @@ public void Several_matching_triplets()
5957
(168, 315, 357),
6058
(210, 280, 350),
6159
(240, 252, 348)
62-
}, PythagoreanTriplet.TripletsWithSum(840));
60+
];
61+
Assert.Equal(expected, PythagoreanTriplet.TripletsWithSum(840));
6362
}
6463

6564
[Fact(Skip = "Remove this Skip property to run this test")]
6665
public void Triplets_for_large_number()
6766
{
68-
Assert.Equal(new[]
69-
{
67+
(int, int, int)[] expected = [
7068
(1200, 14375, 14425),
7169
(1875, 14000, 14125),
7270
(5000, 12000, 13000),
7371
(6000, 11250, 12750),
7472
(7500, 10000, 12500)
75-
}, PythagoreanTriplet.TripletsWithSum(30000));
73+
];
74+
Assert.Equal(expected, PythagoreanTriplet.TripletsWithSum(30000));
7675
}
7776
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using Xunit;
3+
4+
public class {{ testClass }}
5+
{
6+
{{- for test in tests }}
7+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
8+
public void {{ test.shortTestMethod }}()
9+
{
10+
{{- if test.property == "create" }}
11+
{{- if test.expected.error }}
12+
Assert.Throws<ArgumentOutOfRangeException>(() => {{ testedClass }}.{{ test.testedMethod }}({{ test.input.queen.position.row }}, {{ test.input.queen.position.column }}));
13+
{{ else }}
14+
var queen = {{ testedClass }}.{{ test.testedMethod }}({{ test.input.queen.position.row }}, {{ test.input.queen.position.column }});
15+
{{ end -}}
16+
{{- else }}
17+
var whiteQueen = {{ testedClass }}.Create({{ test.input.white_queen.position.row }}, {{ test.input.white_queen.position.column }});
18+
var blackQueen = {{ testedClass }}.Create({{ test.input.black_queen.position.row }}, {{ test.input.black_queen.position.column }});
19+
Assert.{{ test.expected ? "True" : "False" }}({{ testedClass }}.{{ test.testedMethod }}(whiteQueen, blackQueen));
20+
{{ end -}}
21+
}
22+
{{ end -}}
23+
}

exercises/practice/queen-attack/QueenAttackTests.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class QueenAttackTests
66
[Fact]
77
public void Queen_with_a_valid_position()
88
{
9-
var actual = QueenAttack.Create(2, 2);
9+
var queen = QueenAttack.Create(2, 2);
1010
}
1111

1212
[Fact(Skip = "Remove this Skip property to run this test")]
@@ -36,64 +36,64 @@ public void Queen_must_have_column_on_board()
3636
[Fact(Skip = "Remove this Skip property to run this test")]
3737
public void Cannot_attack()
3838
{
39-
var whiteQueen = QueenAttack.Create(2,4);
40-
var blackQueen = QueenAttack.Create(6,6);
39+
var whiteQueen = QueenAttack.Create(2, 4);
40+
var blackQueen = QueenAttack.Create(6, 6);
4141
Assert.False(QueenAttack.CanAttack(whiteQueen, blackQueen));
4242
}
4343

4444
[Fact(Skip = "Remove this Skip property to run this test")]
4545
public void Can_attack_on_same_row()
4646
{
47-
var whiteQueen = QueenAttack.Create(2,4);
48-
var blackQueen = QueenAttack.Create(2,6);
47+
var whiteQueen = QueenAttack.Create(2, 4);
48+
var blackQueen = QueenAttack.Create(2, 6);
4949
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
5050
}
5151

5252
[Fact(Skip = "Remove this Skip property to run this test")]
5353
public void Can_attack_on_same_column()
5454
{
55-
var whiteQueen = QueenAttack.Create(4,5);
56-
var blackQueen = QueenAttack.Create(2,5);
55+
var whiteQueen = QueenAttack.Create(4, 5);
56+
var blackQueen = QueenAttack.Create(2, 5);
5757
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
5858
}
5959

6060
[Fact(Skip = "Remove this Skip property to run this test")]
6161
public void Can_attack_on_first_diagonal()
6262
{
63-
var whiteQueen = QueenAttack.Create(2,2);
64-
var blackQueen = QueenAttack.Create(0,4);
63+
var whiteQueen = QueenAttack.Create(2, 2);
64+
var blackQueen = QueenAttack.Create(0, 4);
6565
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
6666
}
6767

6868
[Fact(Skip = "Remove this Skip property to run this test")]
6969
public void Can_attack_on_second_diagonal()
7070
{
71-
var whiteQueen = QueenAttack.Create(2,2);
72-
var blackQueen = QueenAttack.Create(3,1);
71+
var whiteQueen = QueenAttack.Create(2, 2);
72+
var blackQueen = QueenAttack.Create(3, 1);
7373
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
7474
}
7575

7676
[Fact(Skip = "Remove this Skip property to run this test")]
7777
public void Can_attack_on_third_diagonal()
7878
{
79-
var whiteQueen = QueenAttack.Create(2,2);
80-
var blackQueen = QueenAttack.Create(1,1);
79+
var whiteQueen = QueenAttack.Create(2, 2);
80+
var blackQueen = QueenAttack.Create(1, 1);
8181
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
8282
}
8383

8484
[Fact(Skip = "Remove this Skip property to run this test")]
8585
public void Can_attack_on_fourth_diagonal()
8686
{
87-
var whiteQueen = QueenAttack.Create(1,7);
88-
var blackQueen = QueenAttack.Create(0,6);
87+
var whiteQueen = QueenAttack.Create(1, 7);
88+
var blackQueen = QueenAttack.Create(0, 6);
8989
Assert.True(QueenAttack.CanAttack(whiteQueen, blackQueen));
9090
}
9191

9292
[Fact(Skip = "Remove this Skip property to run this test")]
9393
public void Cannot_attack_if_falling_diagonals_are_only_the_same_when_reflected_across_the_longest_falling_diagonal()
9494
{
95-
var whiteQueen = QueenAttack.Create(4,1);
96-
var blackQueen = QueenAttack.Create(2,5);
95+
var whiteQueen = QueenAttack.Create(4, 1);
96+
var blackQueen = QueenAttack.Create(2, 5);
9797
Assert.False(QueenAttack.CanAttack(whiteQueen, blackQueen));
9898
}
9999
}

0 commit comments

Comments
 (0)