added parent context to ErrorReporterContext and handling errors in include and extend nodes

This commit is contained in:
Ilya Puchka
2017-10-07 21:02:27 +02:00
parent e59609f140
commit 079fdf39b8
9 changed files with 77 additions and 15 deletions

View File

@@ -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)
}
}
}
}

View File

@@ -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!"

View File

@@ -0,0 +1,2 @@
{% block header %}Header{% endblock %}
{% block body %}Body {{ target|unknown }} {% endblock %}

View File

@@ -0,0 +1,3 @@
{% extends "invalid-base.html" %}
{% block body %}Child {{ block.super }}{% endblock %}

View File

@@ -0,0 +1 @@
Hello {{ target|unknown }}!