Parse variables as expressions

removed static boolean expressions
added test for rendering template with boolean expression
This commit is contained in:
Ilya Puchka
2017-12-23 22:01:04 +01:00
committed by David Jennes
parent 242bea54c3
commit 6649b7e716
5 changed files with 54 additions and 24 deletions

View File

@@ -115,12 +115,12 @@ final class ExpressionsTests: XCTestCase {
func testNotExpression() {
it("returns truthy for positive expressions") {
let expression = NotExpression(expression: StaticExpression(value: true))
let expression = NotExpression(expression: VariableExpression(variable: Variable("true")))
try expect(expression.evaluate(context: Context())).to.beFalse()
}
it("returns falsy for negative expressions") {
let expression = NotExpression(expression: StaticExpression(value: false))
let expression = NotExpression(expression: VariableExpression(variable: Variable("false")))
try expect(expression.evaluate(context: Context())).to.beTrue()
}
}

View File

@@ -200,8 +200,8 @@ final class IfNodeTests: XCTestCase {
func testRendering() {
it("renders a true expression") {
let node = IfNode(conditions: [
IfCondition(expression: StaticExpression(value: true), nodes: [TextNode(text: "1")]),
IfCondition(expression: StaticExpression(value: true), nodes: [TextNode(text: "2")]),
IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "2")]),
IfCondition(expression: nil, nodes: [TextNode(text: "3")])
])
@@ -210,8 +210,8 @@ final class IfNodeTests: XCTestCase {
it("renders the first true expression") {
let node = IfNode(conditions: [
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "1")]),
IfCondition(expression: StaticExpression(value: true), nodes: [TextNode(text: "2")]),
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("true")), nodes: [TextNode(text: "2")]),
IfCondition(expression: nil, nodes: [TextNode(text: "3")])
])
@@ -220,8 +220,8 @@ final class IfNodeTests: XCTestCase {
it("renders the empty expression when other conditions are falsy") {
let node = IfNode(conditions: [
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "1")]),
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "2")]),
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "2")]),
IfCondition(expression: nil, nodes: [TextNode(text: "3")])
])
@@ -230,8 +230,8 @@ final class IfNodeTests: XCTestCase {
it("renders empty when no truthy conditions") {
let node = IfNode(conditions: [
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "1")]),
IfCondition(expression: StaticExpression(value: false), nodes: [TextNode(text: "2")])
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "1")]),
IfCondition(expression: VariableExpression(variable: Variable("false")), nodes: [TextNode(text: "2")])
])
try expect(try node.render(Context())) == ""

View File

@@ -90,4 +90,22 @@ final class NodeTests: XCTestCase {
try expect(try renderNodes(nodes, self.context)).toThrow(TemplateSyntaxError("Custom Error"))
}
}
func testRenderingBooleans() {
it("can render true & false") {
try expect(Template(templateString: "{{ true }}").render()) == "true"
try expect(Template(templateString: "{{ false }}").render()) == "false"
}
it("can resolve variable") {
let template = Template(templateString: "{{ value == \"known\" }}")
try expect(template.render(["value": "known"])) == "true"
try expect(template.render(["value": "unknown"])) == "false"
}
it("can render a boolean expression") {
try expect(Template(templateString: "{{ 1 > 0 }}").render()) == "true"
try expect(Template(templateString: "{{ 1 == 2 }}").render()) == "false"
}
}
}