Skip to content

Commit 8341aa5

Browse files
accumulate: add generator
1 parent 3804d8b commit 8341aa5

File tree

2 files changed

+62
-28
lines changed

2 files changed

+62
-28
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{{ func normalize
2+
normalized = string.replace $0 "upcase(x)" "x.ToUpper()"
3+
normalized = string.replace normalized "reverse(x)" "new string(x.Reverse().ToArray())"
4+
string.replace normalized "accumulate([\"1\", \"2\", \"3\"], " "new[] { \"1\", \"2\", \"3\" }.Accumulate("
5+
end }}
6+
7+
{{ func vartype
8+
case (object.typeof (array.first $0))
9+
when "string"
10+
ret "string[]"
11+
when "array"
12+
ret "string[][]"
13+
else
14+
ret "int[]"
15+
end
16+
end }}
17+
using System.Linq;
18+
using Xunit;
19+
20+
public class {{ testClass }}
21+
{
22+
{{- for test in tests }}
23+
[Fact{{ if !for.first }}(Skip = "Remove this Skip property to run this test"){{ end }}]
24+
public void {{ test.testMethod }}()
25+
{
26+
{{ test.input.list | vartype }} input = {{ test.input.list }};
27+
{{ test.expected | vartype }} expected = {{ test.expected }};
28+
Assert.Equal(expected, input.{{ test.testedMethod }}({{ test.input.accumulator | normalize }}));
29+
}
30+
{{ end }}
31+
[Fact(Skip = "Remove this Skip property to run this test")]
32+
public void Accumulate_is_lazy()
33+
{
34+
var counter = 0;
35+
int[] input = [1, 2, 3];
36+
var accumulation = input.Accumulate(x => x * counter++);
37+
Assert.Equal(0, counter);
38+
var _ = accumulation.ToList();
39+
Assert.Equal(3, counter);
40+
}
41+
}
Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,56 @@
1-
using System;
2-
using System.Collections.Generic;
31
using System.Linq;
42
using Xunit;
53

64
public class AccumulateTests
75
{
86
[Fact]
9-
public void Empty_accumulation_produces_empty_accumulation()
7+
public void Accumulate_empty()
108
{
11-
Assert.Equal(new int[0], new int[0].Accumulate(x => x * x));
9+
int[] input = [];
10+
int[] expected = [];
11+
Assert.Equal(expected, input.Accumulate((x) => x * x));
1212
}
1313

1414
[Fact(Skip = "Remove this Skip property to run this test")]
1515
public void Accumulate_squares()
1616
{
17-
Assert.Equal(new[] { 1, 4, 9 }, new[] { 1, 2, 3 }.Accumulate(x => x * x));
17+
int[] input = [1, 2, 3];
18+
int[] expected = [1, 4, 9];
19+
Assert.Equal(expected, input.Accumulate((x) => x * x));
1820
}
1921

2022
[Fact(Skip = "Remove this Skip property to run this test")]
2123
public void Accumulate_upcases()
2224
{
23-
Assert.Equal(new List<string> { "HELLO", "WORLD" }, new List<string> { "hello", "world" }.Accumulate(x => x.ToUpper()));
25+
string[] input = ["Hello", "world"];
26+
string[] expected = ["HELLO", "WORLD"];
27+
Assert.Equal(expected, input.Accumulate((x) => x.ToUpper()));
2428
}
2529

2630
[Fact(Skip = "Remove this Skip property to run this test")]
2731
public void Accumulate_reversed_strings()
2832
{
29-
Assert.Equal("eht kciuq nworb xof cte".Split(' '), "the quick brown fox etc".Split(' ').Accumulate(Reverse));
30-
}
31-
32-
private static string Reverse(string value)
33-
{
34-
var array = value.ToCharArray();
35-
Array.Reverse(array);
36-
return new string(array);
33+
string[] input = ["the", "quick", "brown", "fox", "etc"];
34+
string[] expected = ["eht", "kciuq", "nworb", "xof", "cte"];
35+
Assert.Equal(expected, input.Accumulate((x) => new string(x.Reverse().ToArray())));
3736
}
3837

3938
[Fact(Skip = "Remove this Skip property to run this test")]
40-
public void Accumulate_within_accumulate()
39+
public void Accumulate_recursively()
4140
{
42-
var actual = new[] { "a", "b", "c" }.Accumulate(c =>
43-
string.Join(" ", new[] { "1", "2", "3" }.Accumulate(d => c + d)));
44-
Assert.Equal(new[] { "a1 a2 a3", "b1 b2 b3", "c1 c2 c3" }, actual);
41+
string[] input = ["a", "b", "c"];
42+
string[][] expected = [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]];
43+
Assert.Equal(expected, input.Accumulate((x) => new[] { "1", "2", "3" }.Accumulate((y) => x + y)));
4544
}
4645

4746
[Fact(Skip = "Remove this Skip property to run this test")]
4847
public void Accumulate_is_lazy()
4948
{
5049
var counter = 0;
51-
var accumulation = new[] { 1, 2, 3 }.Accumulate(x => x * counter++);
52-
50+
int[] input = [1, 2, 3];
51+
var accumulation = input.Accumulate(x => x * counter++);
5352
Assert.Equal(0, counter);
54-
accumulation.ToList();
53+
var _ = accumulation.ToList();
5554
Assert.Equal(3, counter);
5655
}
57-
58-
[Fact(Skip = "Remove this Skip property to run this test")]
59-
public void Accumulate_allows_different_return_type()
60-
{
61-
Assert.Equal(new[] { "1", "2", "3" }, new[] { 1, 2, 3 }.Accumulate(x => x.ToString()));
62-
}
63-
}
56+
}

0 commit comments

Comments
 (0)