Skip to content

Commit

Permalink
Added tests for using the "throwOnUnresolvedVariable" parameter with …
Browse files Browse the repository at this point in the history
…filters and tags
  • Loading branch information
acecilia committed Jan 12, 2020
1 parent 1739076 commit 45239a6
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Tests/StencilTests/EnvironmentSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,40 @@ final class EnvironmentTests: XCTestCase {
}
}

func testUnresolvedVariableWithDefaultFilter() {
self.template = Template(templateString: """
Hello {{ human.name|default:"World" }}
""")
self.environment = Environment(throwOnUnresolvedVariable: true)

it("does not throw when a filter provides a default value, even though the variable is undefined") {
let result = try self.environment.render(template: self.template, context: [:])
try expect(result) == "Hello World"
}
}

func testUnresolvedVariableWithTags() {
self.template = Template(templateString: """
{% if human.name %}Hello {{ human.name }}{% else %}No hello for you{% endif %}
""")
self.environment = Environment(throwOnUnresolvedVariable: true)

it("does not throw when using tags with defined variables") {
let result = try self.environment.render(template: self.template, context: ["human": ["name": "John"]])
try expect(result) == "Hello John"
}

it("throws when using tags with undefined variables") {
try self.expectError(context: [:], reason: "Variable could not be resolved", token: "if human.name")
}

it("does not throw when using empty variables instead of undefined ones") {
let result = try self.environment.render(template: self.template, context: ["human": ["name": ""]])
try expect(result) == "No hello for you"
}
}


private func expectError(
context: [String: Any] = ["names": ["Bob", "Alice"], "name": "Bob"],
reason: String,
Expand Down

0 comments on commit 45239a6

Please sign in to comment.