Skip to content

Commit 307c8a2

Browse files
hymccordErikSchierboom
authored andcommitted
Add Etl generator (#462)
1 parent 3110183 commit 307c8a2

File tree

5 files changed

+165
-53
lines changed

5 files changed

+165
-53
lines changed

exercises/etl/ETLTest.cs

Lines changed: 0 additions & 51 deletions
This file was deleted.

exercises/etl/Etl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33

4-
public static class ETL
4+
public static class Etl
55
{
66
public static IDictionary<string, int> Transform(IDictionary<int, IList<string>> old)
77
{

exercises/etl/EtlTest.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// This file was auto-generated based on version 1.0.0 of the canonical data.
2+
3+
using Xunit;
4+
using System.Collections.Generic;
5+
6+
public class EtlTest
7+
{
8+
[Fact]
9+
public void A_single_letter()
10+
{
11+
var input = new Dictionary<int, IList<string>>
12+
{
13+
[1] = new[] { "A" }
14+
};
15+
var expected = new Dictionary<string, int>
16+
{
17+
["a"] = 1
18+
};
19+
Assert.Equal(expected, Etl.Transform(input));
20+
}
21+
22+
[Fact(Skip = "Remove to run test")]
23+
public void Single_score_with_multiple_letters()
24+
{
25+
var input = new Dictionary<int, IList<string>>
26+
{
27+
[1] = new[] { "A", "E", "I", "O", "U" }
28+
};
29+
var expected = new Dictionary<string, int>
30+
{
31+
["a"] = 1,
32+
["e"] = 1,
33+
["i"] = 1,
34+
["o"] = 1,
35+
["u"] = 1
36+
};
37+
Assert.Equal(expected, Etl.Transform(input));
38+
}
39+
40+
[Fact(Skip = "Remove to run test")]
41+
public void Multiple_scores_with_multiple_letters()
42+
{
43+
var input = new Dictionary<int, IList<string>>
44+
{
45+
[1] = new[] { "A", "E" },
46+
[2] = new[] { "D", "G" }
47+
};
48+
var expected = new Dictionary<string, int>
49+
{
50+
["a"] = 1,
51+
["d"] = 2,
52+
["e"] = 1,
53+
["g"] = 2
54+
};
55+
Assert.Equal(expected, Etl.Transform(input));
56+
}
57+
58+
[Fact(Skip = "Remove to run test")]
59+
public void Multiple_scores_with_differing_numbers_of_letters()
60+
{
61+
var input = new Dictionary<int, IList<string>>
62+
{
63+
[1] = new[] { "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" },
64+
[2] = new[] { "D", "G" },
65+
[3] = new[] { "B", "C", "M", "P" },
66+
[4] = new[] { "F", "H", "V", "W", "Y" },
67+
[5] = new[] { "K" },
68+
[8] = new[] { "J", "X" },
69+
[10] = new[] { "Q", "Z" }
70+
};
71+
var expected = new Dictionary<string, int>
72+
{
73+
["a"] = 1,
74+
["b"] = 3,
75+
["c"] = 3,
76+
["d"] = 2,
77+
["e"] = 1,
78+
["f"] = 4,
79+
["g"] = 2,
80+
["h"] = 4,
81+
["i"] = 1,
82+
["j"] = 8,
83+
["k"] = 5,
84+
["l"] = 1,
85+
["m"] = 3,
86+
["n"] = 1,
87+
["o"] = 1,
88+
["p"] = 3,
89+
["q"] = 10,
90+
["r"] = 1,
91+
["s"] = 1,
92+
["t"] = 1,
93+
["u"] = 1,
94+
["v"] = 4,
95+
["w"] = 4,
96+
["x"] = 8,
97+
["y"] = 4,
98+
["z"] = 10
99+
};
100+
Assert.Equal(expected, Etl.Transform(input));
101+
}
102+
}

exercises/etl/Example.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Collections.Generic;
22

3-
public static class ETL
3+
public static class Etl
44
{
55
public static IDictionary<string, int> Transform(IDictionary<int, IList<string>> old)
66
{

generators/Exercises/Etl.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using Generators.Input;
2+
using Generators.Output;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace Generators.Exercises
7+
{
8+
public class Etl : Exercise
9+
{
10+
protected override void UpdateCanonicalData(CanonicalData canonicalData)
11+
{
12+
foreach (var canonicalDataCase in canonicalData.Cases)
13+
{
14+
canonicalDataCase.UseVariablesForInput = true;
15+
canonicalDataCase.UseVariableForExpected = true;
16+
// Make sure to convert keys to ints as stated in canonical data
17+
canonicalDataCase.Properties["input"] = ConvertInput(canonicalDataCase.Properties["input"]);
18+
canonicalDataCase.Expected = ConvertExpected(canonicalDataCase.Expected);
19+
}
20+
}
21+
22+
protected override string RenderTestMethodBodyArrange(TestMethodBody testMethodBody)
23+
{
24+
// The ValueFormatter doesn't handle Dictionary<int, IList<string>>. Need to format manually.
25+
Dictionary<int, string[]> inputDictionary = testMethodBody.CanonicalDataCase.Properties["input"];
26+
var newInput = FormatMultiLineEnumerable(
27+
inputDictionary.Keys.Select((key, i) => $"[{ValueFormatter.Format(key)}] = {ValueFormatter.Format(inputDictionary[key])}" + (i < inputDictionary.Keys.Count - 1 ? "," : "")),
28+
"input", "new Dictionary<int, IList<string>>");
29+
newInput = AddTrailingSemicolon(newInput);
30+
31+
var arrangeVariables = testMethodBody.Data.Variables.ToList();
32+
arrangeVariables.RemoveAt(0);
33+
arrangeVariables.InsertRange(0, newInput);
34+
testMethodBody.ArrangeTemplateParameters = new { Variables = arrangeVariables };
35+
36+
return base.RenderTestMethodBodyArrange(testMethodBody);
37+
}
38+
39+
private static dynamic ConvertExpected(dynamic expected)
40+
=> ((Dictionary<string, object>)expected).ToDictionary(kv => kv.Key, kv => int.Parse($"{kv.Value}"));
41+
42+
private static dynamic ConvertInput(dynamic input)
43+
=> ((Dictionary<string, object>)input).ToDictionary(kv => int.Parse(kv.Key), kv => (string[])kv.Value);
44+
45+
private static string[] FormatMultiLineEnumerable(IEnumerable<string> enumerable, string name, string constructor = null)
46+
=> FormatMultiLineVariable(enumerable.Prepend("{").Append("}"), name, constructor);
47+
48+
private static string[] FormatMultiLineVariable(IEnumerable<string> enumerable, string name, string constructor = null)
49+
=> enumerable.Select(line => line == "{" || line == "}" ? line : line.Indent())
50+
.Prepend($"var {name} = {constructor}")
51+
.ToArray();
52+
53+
private static string[] AddTrailingSemicolon(string[] array)
54+
{
55+
array[array.Length - 1] += ";";
56+
return array;
57+
}
58+
59+
protected override HashSet<string> AddAdditionalNamespaces() => new HashSet<string> { typeof(Dictionary<string, int>).Namespace };
60+
}
61+
}

0 commit comments

Comments
 (0)