Skip to content

Commit 3be816e

Browse files
Use newtonsoft.system.json
1 parent 6f0a3d7 commit 3be816e

File tree

11 files changed

+36
-53
lines changed

11 files changed

+36
-53
lines changed

exercises/practice/acronym/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class AcronymTests
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
77
public void {{method_name path}}()
88
{
9-
Assert.Equal({{expected}}, Acronym.Abbreviate({{input.phrase}}));
9+
Assert.Equal({{literal expected}}, Acronym.Abbreviate({{literal input.phrase}}));
1010
}
1111
{{/test_cases}}
1212
}

exercises/practice/hamming/.meta/Generator.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public class HammingTests
77
public void {{method_name path}}()
88
{
99
{{#if error}}
10-
Assert.Throws<ArgumentException>(() => Hamming.Distance({{input.strand1}}, {{input.strand2}}));
10+
Assert.Throws<ArgumentException>(() => Hamming.Distance({{literal input.strand1}}, {{literal input.strand2}}));
1111
{{else}}
12-
Assert.Equal({{expected}}, Hamming.Distance({{input.strand1}}, {{input.strand2}}));
12+
Assert.Equal({{expected}}, Hamming.Distance({{literal input.strand1}}, {{literal input.strand2}}));
1313
{{/if}}
1414
}
1515
{{/test_cases}}

exercises/practice/isogram/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class IsogramTests
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
77
public void {{method_name path}}()
88
{
9-
Assert.{{expected}}(Isogram.IsIsogram({{input.phrase}}));
9+
Assert.{{expected}}(Isogram.IsIsogram({{literal input.phrase}}));
1010
}
1111
{{/test_cases}}
1212
}

exercises/practice/pangram/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class PangramTests
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
77
public void {{method_name path}}()
88
{
9-
Assert.{{expected}}(Pangram.IsPangram({{input.sentence}}));
9+
Assert.{{literal expected}}(Pangram.IsPangram({{literal input.sentence}}));
1010
}
1111
{{/test_cases}}
1212
}

exercises/practice/sieve/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SieveTests
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
77
public void {{method_name path}}()
88
{
9-
Assert.Equal({{expected}}, Sieve.Primes({{input.limit}}));
9+
Assert.Equal([{{expected}}], Sieve.Primes({{input.limit}}));
1010
}
1111
{{/test_cases}}
1212
}

exercises/practice/space-age/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class SpaceAgeTests
77
public void {{method_name path}}()
88
{
99
var sut = new SpaceAge({{input.seconds}});
10-
Assert.Equal({{expected}}, sut.On{{raw input.planet}}(), precision: 2);
10+
Assert.Equal({{expected}}, sut.On{{input.planet}}(), precision: 2);
1111
}
1212
{{/test_cases}}
1313
}

exercises/practice/sum-of-multiples/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class SumOfMultiplesTests
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
77
public void {{method_name path}}()
88
{
9-
Assert.Equal({{expected}}, SumOfMultiples.Sum({{input.factors}}, {{input.limit}}));
9+
Assert.Equal({{expected}}, SumOfMultiples.Sum([{{input.factors}}], {{input.limit}}));
1010
}
1111
{{/test_cases}}
1212
}

exercises/practice/two-fer/.meta/Generator.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public class TwoFerTests
66
[Fact{{#unless @first}}(Skip = "Remove this Skip property to run this test"){{/unless}}]
77
public void {{method_name path}}()
88
{
9-
Assert.Equal({{expected}}, TwoFer.Speak({{#if input.name}}{{input.name}}{{/if}}));
9+
Assert.Equal({{literal expected}}, TwoFer.Speak({{#if input.name}}{{literal input.name}}{{/if}}));
1010
}
1111
{{/test_cases}}
1212
}

generators.new/CanonicalData.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Collections.Immutable;
2-
using System.Text.Json;
3-
using System.Text.Json.Nodes;
2+
using System.Dynamic;
43

54
using LibGit2Sharp;
65

6+
using Newtonsoft.Json;
7+
using Newtonsoft.Json.Linq;
8+
79
namespace Generators;
810

911
internal record TestCase(
@@ -17,41 +19,40 @@ internal record TestCase(
1719

1820
internal static class CanonicalData
1921
{
20-
private static readonly JsonSerializerOptions SerializerOptions = new() { PropertyNameCaseInsensitive = true };
21-
2222
static CanonicalData() => ProbSpecs.Sync();
2323

2424
internal static TestCase[] Parse(Exercise exercise)
2525
{
2626
var json = File.ReadAllText(Paths.CanonicalDataFile(exercise));
27-
var jsonNode = JsonNode.Parse(json)!.AsObject();
28-
return Parse(jsonNode, ImmutableQueue<string>.Empty)
27+
var jsonObject = JObject.Parse(json);
28+
return Parse(jsonObject, ImmutableQueue<string>.Empty)
2929
.ToArray();
3030
}
3131

32-
private static IEnumerable<TestCase> Parse(JsonObject jsonObject, ImmutableQueue<string> path)
32+
private static IEnumerable<TestCase> Parse(JObject jsonObject, ImmutableQueue<string> path)
3333
{
34-
var updatedPath = jsonObject.TryGetPropertyValue("description", out var description)
35-
? path.Enqueue(description.Deserialize<string>()!)
34+
var updatedPath = jsonObject.TryGetValue("description", out var description)
35+
? path.Enqueue(description.Value<string>()!)
3636
: path;
37+
38+
if (jsonObject.TryGetValue("cases", out var cases))
39+
return ((JArray)cases).Cast<JObject>().SelectMany(child => Parse(child, updatedPath));
3740

38-
return jsonObject.TryGetPropertyValue("cases", out var cases)
39-
? cases!.AsArray().SelectMany(child => Parse(child!.AsObject(), updatedPath))
40-
: [ToTestCase(jsonObject, updatedPath)];
41+
return [ToTestCase(jsonObject, updatedPath)];
4142
}
4243

43-
private static TestCase ToTestCase(JsonObject testCaseJson, IEnumerable<string> path)
44+
private static TestCase ToTestCase(JObject testCaseJson, IEnumerable<string> path)
4445
{
45-
testCaseJson["path"] = JsonValue.Create(path);
46+
testCaseJson["path"] = JArray.FromObject(path);
4647
testCaseJson["error"] = ToError(testCaseJson);
47-
return testCaseJson.Deserialize<TestCase>(SerializerOptions)!;
48+
return JsonConvert.DeserializeObject<TestCase>(testCaseJson.ToString())!;
4849
}
4950

50-
private static JsonNode? ToError(JsonObject testCaseJson)
51+
private static JToken? ToError(JObject testCaseJson)
5152
{
52-
if (testCaseJson["expected"] is JsonObject expectedJson &&
53-
expectedJson.TryGetPropertyValue("error", out var error))
54-
return error!.DeepClone();
53+
if (testCaseJson["expected"] is JObject expectedJson &&
54+
expectedJson.TryGetValue("error", out var error))
55+
return error.DeepClone();
5556

5657
return null;
5758
}

generators.new/Generators.new.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
<ItemGroup>
1111
<PackageReference Include="CommandLineParser" Version="2.9.1" />
1212
<PackageReference Include="Handlebars.Net" Version="2.1.6" />
13+
<PackageReference Include="Handlebars.Net.Extension.NewtonsoftJson" Version="1.0.3" />
1314
<PackageReference Include="Handlebars.Net.Helpers" Version="2.4.10" />
1415
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
1516
<PackageReference Include="LibGit2Sharp" Version="0.31.0" />
1617
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.12.0" />
18+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
1719
<PackageReference Include="Tomlyn" Version="0.18.0" />
1820
</ItemGroup>
1921

0 commit comments

Comments
 (0)