@@ -206,6 +206,47 @@ public async Task ShouldEvaluateStringValue()
206206 Assert . Equal ( "abc" , result ) ;
207207 }
208208
209+ [ Fact ]
210+ public async Task ShouldRenderNullValueFromContext ( )
211+ {
212+ _parser . TryParse ( "{{ x }}" , out var template , out var error ) ;
213+ var context = new TemplateContext ( ) ;
214+ context . SetValue ( "x" , ( object ) null ) ;
215+
216+ var result = await template . RenderAsync ( context ) ;
217+ Assert . Equal ( string . Empty , result ) ;
218+ }
219+
220+ [ Fact ]
221+ public async Task ShouldRenderNullValueFromProperty ( )
222+ {
223+ _parser . TryParse ( "{{ c.Value }}" , out var template , out var error ) ;
224+
225+ var options = new TemplateOptions ( ) ;
226+ options . MemberAccessStrategy . Register < NullStringContainer > ( ) ;
227+
228+ var context = new TemplateContext ( options ) ;
229+ context . SetValue ( "c" , new NullStringContainer ( ) ) ;
230+
231+ var result = await template . RenderAsync ( context ) ;
232+ Assert . Equal ( string . Empty , result ) ;
233+ }
234+
235+ [ Fact ]
236+ public async Task ShouldRenderNullValueFromToString ( )
237+ {
238+ _parser . TryParse ( "{{ c }}" , out var template , out var error ) ;
239+
240+ var options = new TemplateOptions ( ) ;
241+ options . MemberAccessStrategy . Register < NullStringContainer > ( ) ;
242+
243+ var context = new TemplateContext ( options ) ;
244+ context . SetValue ( "c" , new NullStringContainer ( ) ) ;
245+
246+ var result = await template . RenderAsync ( context ) ;
247+ Assert . Equal ( string . Empty , result ) ;
248+ }
249+
209250 [ Fact ]
210251 public async Task ShouldEvaluateNumberValue ( )
211252 {
@@ -401,6 +442,13 @@ public async Task FirstLastSizeShouldUseGetValue()
401442 Assert . Equal ( "123 456 789" , result ) ;
402443 }
403444
445+ private sealed class NullStringContainer
446+ {
447+ public string Value => null ;
448+
449+ public override string ToString ( ) => null ;
450+ }
451+
404452 private class PersonValue : ObjectValueBase
405453 {
406454 public PersonValue ( Person value ) : base ( value )
@@ -1309,5 +1357,7 @@ public async Task InlineCommentShouldWorkBetweenTags()
13091357 Assert . Contains ( "Success" , result ) ;
13101358 Assert . DoesNotContain ( "This is between if tags" , result ) ;
13111359 }
1360+
1361+
13121362 }
13131363}
0 commit comments