1- using System . Dynamic ;
2- using System . Globalization ;
1+ using System . Text . Json ;
2+ using System . Text . Json . Nodes ;
33
4- using HandlebarsDotNet ;
5- using HandlebarsDotNet . Helpers ;
4+ using Microsoft . CodeAnalysis . CSharp ;
65
7- using Newtonsoft . Json . Linq ;
6+ using Scriban ;
7+ using Scriban . Runtime ;
88
99namespace Generators ;
1010
1111internal static class Templates
1212{
13- private static readonly IHandlebars HandlebarsContext = Handlebars . Create ( ) ;
14-
15- static Templates ( )
13+ public static string RenderTestsCode ( CanonicalData canonicalData )
1614 {
17- HandlebarsHelpers . Register ( HandlebarsContext , options => { options . UseCategoryPrefix = false ; } ) ;
18- HandlebarsContext . Configuration . FormatProvider = CultureInfo . InvariantCulture ;
19-
20- HandlebarsContext . RegisterHelper ( "lit" , ( writer , context , parameters ) =>
21- writer . WriteSafeString ( Formatting . FormatLiteral ( parameters . First ( ) ) ) ) ;
22-
23- HandlebarsContext . RegisterHelper ( "equals" , ( output , options , context , arguments ) =>
24- {
25- if ( arguments . Length != 2 ) throw new HandlebarsException ( "{{#equals}} helper must have exactly two arguments" ) ;
26-
27- if ( arguments [ 0 ] ! . Equals ( arguments [ 1 ] ! ) )
28- options . Template ( output , context ) ;
29- else
30- options . Inverse ( output , context ) ;
31- } ) ;
15+ var template = Template . Parse ( File . ReadAllText ( Paths . TemplateFile ( canonicalData . Exercise ) ) ) ;
16+ return template . Render ( TemplateData . ForCanonicalData ( canonicalData ) ) ;
3217 }
3318
34- public static string RenderTestsCode ( CanonicalData canonicalData ) =>
35- CompileTemplate ( canonicalData . Exercise ) ( TemplateData . ForCanonicalData ( canonicalData ) ) ;
36-
37- private static HandlebarsTemplate < object , object > CompileTemplate ( Exercise exercise ) =>
38- HandlebarsContext . Compile ( File . ReadAllText ( Paths . TemplateFile ( exercise ) ) ) ;
39-
4019 private static class TemplateData
4120 {
42- internal static Dictionary < string , object > ForCanonicalData ( CanonicalData canonicalData )
21+ internal static JsonElement ForCanonicalData ( CanonicalData canonicalData )
4322 {
4423 var testCases = canonicalData . TestCases . Select ( Create ) . ToArray ( ) ;
24+ var testCasesByProperty = GroupTestCasesByProperty ( testCases ) ;
4525
46- return new ( )
47- {
48- [ "test_cases" ] = testCases . ToArray ( ) ,
49- [ "test_cases_by_property" ] = GroupTestCasesByProperty ( testCases )
50- } ;
26+ return JsonSerializer . SerializeToElement ( new { testCases , testCasesByProperty } ) ;
5127 }
5228
53- private static ExpandoObject Create ( JToken testCase )
29+ private static JsonElement Create ( JsonNode testCase )
5430 {
55- dynamic testData = testCase . ToObject < ExpandoObject > ( ) ! ;
56- testData . test_method_name = Naming . ToMethodName ( testData . path . ToArray ( ) ) ;
57- testData . short_test_method_name = Naming . ToMethodName ( testData . description ) ;
58-
59- if ( testCase [ "expected" ] is JArray expected )
60- {
61- testData . expected = expected . Select ( e => e . ToString ( ) ) . ToArray ( ) ;
62- }
31+ testCase [ "testMethodName" ] = Naming . ToMethodName ( testCase [ "path" ] ! . AsArray ( ) . GetValues < string > ( ) . ToArray ( ) ) ;
32+ testCase [ "shortMethodName" ] = Naming . ToMethodName ( testCase [ "description" ] ! . GetValue < string > ( ) ) ;
6333
64- return testData ;
34+ return JsonSerializer . SerializeToElement ( testCase ) ;
6535 }
6636
67- private static Dictionary < string , dynamic [ ] > GroupTestCasesByProperty ( IEnumerable < dynamic > testCases ) =>
37+ private static Dictionary < string , JsonElement [ ] > GroupTestCasesByProperty ( IEnumerable < JsonElement > testCases ) =>
6838 testCases
69- . GroupBy ( testCase => ( string ) testCase . property )
39+ . GroupBy ( testCase => testCase . GetProperty ( " property" ) . GetString ( ) ! )
7040 . ToDictionary ( kv => kv . Key , kv => kv . ToArray ( ) ) ;
7141 }
72- }
42+ }
0 commit comments