1+ using System . Text . Json ;
2+
13using HandlebarsDotNet ;
4+ using HandlebarsDotNet . IO ;
25
36using Humanizer ;
47
8+ using Microsoft . CodeAnalysis . CSharp ;
9+
510namespace Generators ;
611
712internal static class TemplateRenderer
813{
9- static TemplateRenderer ( ) =>
14+ static TemplateRenderer ( )
15+ {
1016 Handlebars . RegisterHelper ( "method_name" , ( writer , context , parameters ) =>
1117 {
1218 var path = parameters . SelectMany ( parameter => parameter as IEnumerable < string > ?? [ parameter . ToString ( ) ! ] ) ;
1319 writer . WriteSafeString ( string . Join ( " " , path ) . Dehumanize ( ) ) ;
1420 } ) ;
21+ Handlebars . Configuration . FormatterProviders . Add ( new JsonElementFormatter ( ) ) ;
22+ }
1523
1624 public static string RenderTests ( Exercise exercise , TestCase [ ] testCases ) =>
1725 CompileTemplate ( exercise ) ( ToTemplateData ( exercise , testCases ) ) ;
@@ -31,4 +39,27 @@ private static Dictionary<string, TestCase[]> GroupTestCasesByProperty(TestCase[
3139 testCases
3240 . GroupBy ( testCase => testCase . Property )
3341 . ToDictionary ( kv => kv . Key , kv => kv . ToArray ( ) ) ;
42+
43+ private sealed class JsonElementFormatter : IFormatter , IFormatterProvider
44+ {
45+ public void Format < T > ( T value , in EncodedTextWriter writer )
46+ {
47+ if ( value is not JsonElement element )
48+ throw new ArgumentException ( "Invalid type" ) ;
49+
50+ writer . Write ( SymbolDisplay . FormatLiteral ( element . ToString ( ) , false ) ) ;
51+ }
52+
53+ public bool TryCreateFormatter ( Type type , out IFormatter formatter )
54+ {
55+ if ( type != typeof ( JsonElement ) )
56+ {
57+ formatter = null ;
58+ return false ;
59+ }
60+
61+ formatter = this ;
62+ return true ;
63+ }
64+ }
3465}
0 commit comments