Skip to content
Merged

Gens #2376

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions exercises/practice/accumulate/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{{ func normalize
normalized = string.replace $0 "upcase(x)" "x.ToUpper()"
normalized = string.replace normalized "reverse(x)" "new string(x.Reverse().ToArray())"
string.replace normalized "accumulate([\"1\", \"2\", \"3\"], " "new[] { \"1\", \"2\", \"3\" }.Accumulate("
end }}

{{ func vartype
case (object.typeof (array.first $0))
when "string"
ret "string[]"
when "array"
ret "string[][]"
else
ret "int[]"
end
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 }}()
{
{{ test.input.list | vartype }} input = {{ test.input.list }};
{{ test.expected | vartype }} expected = {{ test.expected }};
Assert.Equal(expected, input.{{ test.testedMethod }}({{ test.input.accumulator | normalize }}));
}
{{ end }}
[Fact(Skip = "Remove this Skip property to run this test")]
public void Accumulate_is_lazy()
{
var counter = 0;
int[] input = [1, 2, 3];
var accumulation = input.Accumulate(x => x * counter++);
Assert.Equal(0, counter);
var _ = accumulation.ToList();
Assert.Equal(3, counter);
}
}
49 changes: 21 additions & 28 deletions exercises/practice/accumulate/AccumulateTests.cs
Original file line number Diff line number Diff line change
@@ -1,63 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

public class AccumulateTests
{
[Fact]
public void Empty_accumulation_produces_empty_accumulation()
public void Accumulate_empty()
{
Assert.Equal(new int[0], new int[0].Accumulate(x => x * x));
int[] input = [];
int[] expected = [];
Assert.Equal(expected, input.Accumulate((x) => x * x));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Accumulate_squares()
{
Assert.Equal(new[] { 1, 4, 9 }, new[] { 1, 2, 3 }.Accumulate(x => x * x));
int[] input = [1, 2, 3];
int[] expected = [1, 4, 9];
Assert.Equal(expected, input.Accumulate((x) => x * x));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Accumulate_upcases()
{
Assert.Equal(new List<string> { "HELLO", "WORLD" }, new List<string> { "hello", "world" }.Accumulate(x => x.ToUpper()));
string[] input = ["Hello", "world"];
string[] expected = ["HELLO", "WORLD"];
Assert.Equal(expected, input.Accumulate((x) => x.ToUpper()));
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Accumulate_reversed_strings()
{
Assert.Equal("eht kciuq nworb xof cte".Split(' '), "the quick brown fox etc".Split(' ').Accumulate(Reverse));
}

private static string Reverse(string value)
{
var array = value.ToCharArray();
Array.Reverse(array);
return new string(array);
string[] input = ["the", "quick", "brown", "fox", "etc"];
string[] expected = ["eht", "kciuq", "nworb", "xof", "cte"];
Assert.Equal(expected, input.Accumulate((x) => new string(x.Reverse().ToArray())));
}

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

[Fact(Skip = "Remove this Skip property to run this test")]
public void Accumulate_is_lazy()
{
var counter = 0;
var accumulation = new[] { 1, 2, 3 }.Accumulate(x => x * counter++);

int[] input = [1, 2, 3];
var accumulation = input.Accumulate(x => x * counter++);
Assert.Equal(0, counter);
accumulation.ToList();
var _ = accumulation.ToList();
Assert.Equal(3, counter);
}

[Fact(Skip = "Remove this Skip property to run this test")]
public void Accumulate_allows_different_return_type()
{
Assert.Equal(new[] { "1", "2", "3" }, new[] { 1, 2, 3 }.Accumulate(x => x.ToString()));
}
}
}
22 changes: 22 additions & 0 deletions exercises/practice/clock/.meta/Generator.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{ func clock
$"new Clock({$0.hour}, {$0.minute})"
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.shortTestMethod }}()
{
{{- if test.property == "equal" }}
Assert.{{ test.expected ? "Equal" : "NotEqual" }}({{ test.input.clock1 | clock }}, {{ test.input.clock2 | clock }});
{{- else if test.property == "create" }}
Assert.Equal({{ test.expected | string.literal }}, {{ test.input | clock }}.ToString());
{{- else }}
Assert.Equal({{ test.expected | string.literal }}, {{ test.input | clock }}.{{ test.testedMethod }}({{ test.input.value }}).ToString());
{{ end -}}
}
{{ end -}}
}
Loading
Loading