Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions source/Handlebars.Test/IssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,37 @@ public void UnrecognisedExpressionThrowsOutOfMemoryException()
Assert.Throws<HandlebarsCompilerException>(()=> Handlebars.Compile(source));
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/545
// Partial registered on an instance should be found during render on that same instance
[Fact]
public void Issue545_PartialRegisteredOnInstanceIsFoundDuringRender()
{
// This is the CORRECT pattern — register and compile on same instance
var handlebars = Handlebars.Create();
handlebars.RegisterTemplate("content", "<p>{{message}}</p>");
var template = handlebars.Compile("<div>{{> content}}</div>");
var result = template(new { message = "Hello" });
Assert.Equal("<div><p>Hello</p></div>", result);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/545
// Mixing static registration with instance compilation should give a helpful error message
[Fact]
public void Issue545_MixingStaticAndInstanceGivesHelpfulError()
{
// Register on static instance
Handlebars.RegisterTemplate("staticContent545", "<p>static</p>");

// Compile on a different instance — should give a useful error
var handlebars = Handlebars.Create();
var template = handlebars.Compile("<div>{{> staticContent545}}</div>");
var ex = Assert.Throws<HandlebarsRuntimeException>(() => template(new { }));
// The error should mention the partial name clearly
Assert.Contains("staticContent545", ex.Message);
// The error should hint that the user may have registered on the wrong instance
Assert.Contains("static Handlebars class", ex.Message);
}

// Issue: https://github.com/Handlebars-Net/Handlebars.Net/issues/521
// Hashtable with an uppercase key should be accessible using the same-cased expression.
// The IDictionary accessor was incorrectly lowercasing the lookup key.
Expand Down
4 changes: 2 additions & 2 deletions source/Handlebars.Test/PartialTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public void RecursionUnboundedBlockPartialWithSpecialNamedPartial()
ex = Assert.IsType<HandlebarsRuntimeException>(ex.InnerException);
Assert.Equal("Runtime error while rendering partial 'myPartial', see inner exception for more information", ex.Message);
ex = Assert.IsType<HandlebarsRuntimeException>(ex.InnerException);
Assert.Equal("Referenced partial name @partial-block could not be resolved", ex.Message);
Assert.StartsWith("Referenced partial name '@partial-block' could not be resolved", ex.Message);
Assert.Null(ex.InnerException);
}

Expand All @@ -694,7 +694,7 @@ public void TemplateWithSpecialNamedPartial()
var data = new {};

var ex = Assert.Throws<HandlebarsRuntimeException>(() => template(data));
Assert.Equal("Referenced partial name @partial-block could not be resolved", ex.Message);
Assert.StartsWith("Referenced partial name '@partial-block' could not be resolved", ex.Message);
}

public class TestMissingPartialTemplateHandler : IMissingPartialTemplateHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ private static void InvokePartialWithFallback(
if (context.PartialBlockTemplate == null)
{
if (configuration.MissingPartialTemplateHandler == null)
throw new HandlebarsRuntimeException($"Referenced partial name {partialName} could not be resolved");

throw new HandlebarsRuntimeException($"Referenced partial name '{partialName}' could not be resolved. If you registered the partial on the static Handlebars class, make sure to also compile and render using the same static class (or register the partial on the IHandlebars instance you are using to compile).");
configuration.MissingPartialTemplateHandler.Handle(configuration, partialName, writer);
return;
}
Expand Down
Loading