added parent context to ErrorReporterContext and handling errors in include and extend nodes
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import Spectre
|
||||
import PathKit
|
||||
@testable import Stencil
|
||||
|
||||
|
||||
@@ -124,6 +125,34 @@ func testEnvironment() {
|
||||
}
|
||||
}
|
||||
|
||||
$0.context("given related templates") {
|
||||
let path = Path(#file) + ".." + "fixtures"
|
||||
let loader = FileSystemLoader(paths: [path])
|
||||
let environment = Environment(loader: loader)
|
||||
|
||||
$0.it("reports syntax error in included template") {
|
||||
let template: Template = "{% include \"invalid-include.html\"%}"
|
||||
environment.errorReporter.context = ErrorReporterContext(template: template)
|
||||
|
||||
let context = Context(dictionary: ["target": "World"], environment: environment)
|
||||
|
||||
let includedTemplate = try environment.loadTemplate(name: "invalid-include.html")
|
||||
let error = expectedSyntaxError(token: "target|unknown", template: includedTemplate, description: "Unknown filter 'unknown'")
|
||||
|
||||
try expect(try template.render(context)).toThrow(error)
|
||||
}
|
||||
|
||||
$0.it("reports syntax error in extended template") {
|
||||
let template = try environment.loadTemplate(name: "invalid-child-super.html")
|
||||
let context = Context(dictionary: ["target": "World"], environment: environment)
|
||||
|
||||
let baseTemplate = try environment.loadTemplate(name: "invalid-base.html")
|
||||
let error = expectedSyntaxError(token: "target|unknown", template: baseTemplate, description: "Unknown filter 'unknown'")
|
||||
|
||||
try expect(try template.render(context)).toThrow(error)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ func testInclude() {
|
||||
|
||||
$0.describe("rendering") {
|
||||
$0.it("throws an error when rendering without a loader") {
|
||||
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
||||
let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown))
|
||||
|
||||
do {
|
||||
_ = try node.render(Context())
|
||||
@@ -41,7 +41,7 @@ func testInclude() {
|
||||
}
|
||||
|
||||
$0.it("throws an error when it cannot find the included template") {
|
||||
let node = IncludeNode(templateName: Variable("\"unknown.html\""))
|
||||
let node = IncludeNode(templateName: Variable("\"unknown.html\""), token: .block(value: "", at: .unknown))
|
||||
|
||||
do {
|
||||
_ = try node.render(Context(environment: environment))
|
||||
@@ -51,7 +51,7 @@ func testInclude() {
|
||||
}
|
||||
|
||||
$0.it("successfully renders a found included template") {
|
||||
let node = IncludeNode(templateName: Variable("\"test.html\""))
|
||||
let node = IncludeNode(templateName: Variable("\"test.html\""), token: .block(value: "", at: .unknown))
|
||||
let context = Context(dictionary: ["target": "World"], environment: environment)
|
||||
let value = try node.render(context)
|
||||
try expect(value) == "Hello World!"
|
||||
|
||||
2
Tests/StencilTests/fixtures/invalid-base.html
Normal file
2
Tests/StencilTests/fixtures/invalid-base.html
Normal file
@@ -0,0 +1,2 @@
|
||||
{% block header %}Header{% endblock %}
|
||||
{% block body %}Body {{ target|unknown }} {% endblock %}
|
||||
3
Tests/StencilTests/fixtures/invalid-child-super.html
Normal file
3
Tests/StencilTests/fixtures/invalid-child-super.html
Normal file
@@ -0,0 +1,3 @@
|
||||
{% extends "invalid-base.html" %}
|
||||
{% block body %}Child {{ block.super }}{% endblock %}
|
||||
|
||||
1
Tests/StencilTests/fixtures/invalid-include.html
Normal file
1
Tests/StencilTests/fixtures/invalid-include.html
Normal file
@@ -0,0 +1 @@
|
||||
Hello {{ target|unknown }}!
|
||||
Reference in New Issue
Block a user