Skip to content

Commit bf03e28

Browse files
Extract naming
1 parent af77825 commit bf03e28

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

exercises/practice/difference-of-squares/.meta/Generator.tpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@ public class DifferenceOfSquaresTests
44
{
55
{{#test_cases_by_property.squareOfSum}}
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
7-
public void {{method_name description}}()
7+
public void {{short_test_method_name}}()
88
{
99
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateSquareOfSum({{input.number}}));
1010
}
1111
{{/test_cases_by_property.squareOfSum}}
1212

1313
{{#test_cases_by_property.sumOfSquares}}
1414
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
15-
public void {{method_name description}}()
15+
public void {{short_test_method_name}}()
1616
{
1717
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateSumOfSquares({{input.number}}));
1818
}
1919
{{/test_cases_by_property.sumOfSquares}}
2020

2121
{{#test_cases_by_property.differenceOfSquares}}
2222
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
23-
public void {{method_name description}}()
23+
public void {{short_test_method_name}}()
2424
{
2525
Assert.Equal({{expected}}, DifferenceOfSquares.CalculateDifferenceOfSquares({{input.number}}));
2626
}

generators.new/Naming.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Humanizer;
2+
3+
namespace Generators;
4+
5+
internal static class Naming
6+
{
7+
internal static string ToMethodName(params object[] path)
8+
{
9+
var stringPath = path.Select(obj => obj.ToString()!).ToArray();
10+
11+
// Fix method names that start with a number
12+
if (char.IsNumber(stringPath[0][0]))
13+
stringPath[0] = NumberToWord(stringPath[0]);
14+
15+
return string.Join(" ", stringPath).Dehumanize();
16+
}
17+
18+
private static string NumberToWord(string str)
19+
{
20+
var parts = str.Split(' ');
21+
var word = Convert.ToInt32(parts[0]).ToWords();
22+
return string.Join(" ", [word, ..parts[1..]]);
23+
}
24+
25+
internal static string ToTestClassName(Exercise exercise) => $"{exercise.Name}Tests";
26+
internal static string ToTestedClassName(Exercise exercise) => exercise.Name;
27+
}

generators.new/Templates.cs

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,7 @@ internal static class Templates
1919
static Templates()
2020
{
2121
HandlebarsHelpers.Register(HandlebarsContext, options => { options.UseCategoryPrefix = false; });
22-
2322
HandlebarsContext.Configuration.FormatProvider = CultureInfo.InvariantCulture;
24-
HandlebarsContext.RegisterHelper("method_name", (writer, context, parameters) =>
25-
{
26-
var path = parameters.SelectMany(parameter => parameter as IEnumerable<string> ?? [parameter.ToString()!]).ToArray();
27-
28-
// Fix method names that start with a number
29-
if (char.IsNumber(path[0][0]))
30-
{
31-
var parts = path[0].Split(' ');
32-
path[0] = string.Join(" ", [Convert.ToInt32(parts[0]).ToWords(), ..parts[1..]]);
33-
}
34-
35-
writer.WriteSafeString(string.Join(" ", path).Dehumanize());
36-
});
3723
HandlebarsContext.RegisterHelper("raw", (writer, context, parameters) =>
3824
{
3925
writer.WriteSafeString(parameters.First().ToString());
@@ -68,8 +54,8 @@ internal static Dictionary<string, object> ForCanonicalData(CanonicalData canoni
6854
private static ExpandoObject Create(JToken testCase)
6955
{
7056
dynamic testData = testCase.ToObject<ExpandoObject>()!;
71-
testData.test_method_name = ToMethodName(testData.path.ToArray());
72-
testData.short_test_method_name = ToMethodName(testData.property);
57+
testData.test_method_name = Naming.ToMethodName(testData.path.ToArray());
58+
testData.short_test_method_name = Naming.ToMethodName(testData.description);
7359

7460
return testData;
7561
}
@@ -79,27 +65,13 @@ private static Dictionary<string, object> Create(Exercise exercise) =>
7965
{
8066
["slug"] = exercise.Slug,
8167
["name"] = exercise.Name,
82-
["test_class_name"] = $"{exercise.Name}Tests",
83-
["tested_class_name"] = $"{exercise.Name}"
68+
["test_class_name"] = Naming.ToTestClassName(exercise),
69+
["tested_class_name"] = Naming.ToTestedClassName(exercise)
8470
};
8571

8672
private static Dictionary<string, dynamic[]> GroupTestCasesByProperty(IEnumerable<dynamic> testCases) =>
8773
testCases
8874
.GroupBy(testCase => (string)testCase.property)
8975
.ToDictionary(kv => kv.Key, kv => kv.ToArray());
90-
91-
private static string ToMethodName(params object[] path)
92-
{
93-
var stringPath = path.Select(obj => obj.ToString()!).ToArray();
94-
95-
// Fix method names that start with a number
96-
if (char.IsNumber(stringPath[0][0]))
97-
{
98-
var parts = stringPath[0].Split(' ');
99-
stringPath[0] = string.Join(" ", [Convert.ToInt32(parts[0]).ToWords(), ..parts[1..]]);
100-
}
101-
102-
return string.Join(" ", stringPath).Dehumanize();
103-
}
10476
}
10577
}

0 commit comments

Comments
 (0)