Allow conditions in variable node (#243)
* use condition in variable node * added support for else expression * addressing code review comments
This commit is contained in:
@@ -337,4 +337,23 @@ func testVariable() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
describe("inline if expression") {
|
||||
|
||||
$0.it("can conditionally render variable") {
|
||||
let template: Template = "{{ variable if variable|uppercase == \"A\" }}"
|
||||
try expect(template.render(Context(dictionary: ["variable": "a"]))) == "a"
|
||||
try expect(template.render(Context(dictionary: ["variable": "b"]))) == ""
|
||||
}
|
||||
|
||||
$0.it("can render with else expression") {
|
||||
let template: Template = "{{ variable if variable|uppercase == \"A\" else fallback|uppercase }}"
|
||||
try expect(template.render(Context(dictionary: ["variable": "b", "fallback": "c"]))) == "C"
|
||||
}
|
||||
|
||||
$0.it("throws when used invalid condition") {
|
||||
let template: Template = "{{ variable if variable \"A\" }}"
|
||||
try expect(template.render(Context(dictionary: ["variable": "a"]))).toThrow()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user