From 2d73c58df6f7a09a74f4ad8746f0f08bd140a33e Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Wed, 20 Jul 2016 02:35:21 +0900 Subject: [PATCH 1/3] [IfNodeSpec] Add a failing test for bool expression --- Tests/Nodes/IfNodeSpec.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Tests/Nodes/IfNodeSpec.swift b/Tests/Nodes/IfNodeSpec.swift index 156031d..278699d 100644 --- a/Tests/Nodes/IfNodeSpec.swift +++ b/Tests/Nodes/IfNodeSpec.swift @@ -73,14 +73,26 @@ func testIfNode() { } $0.describe("rendering") { - let context = Context(dictionary: ["items": true]) - $0.it("renders the truth when expression evaluates to true") { + let context = Context(dictionary: ["items": true]) let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) try expect(try node.render(context)) == "true" } $0.it("renders the false when expression evaluates to false") { + let context = Context(dictionary: ["items": false]) + let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try node.render(context)) == "false" + } + + $0.it("renders the truth when expression is not nil") { + let context = Context(dictionary: ["known": "known"]) + let node = IfNode(variable: "known", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try node.render(context)) == "true" + } + + $0.it("renders the false when expression is nil") { + let context = Context(dictionary: [:]) let node = IfNode(variable: "unknown", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) try expect(try node.render(context)) == "false" } From 5007ba2c9abdf07bc65998f32e4b74ba40b57b35 Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Wed, 20 Jul 2016 02:39:01 +0900 Subject: [PATCH 2/3] [IfNode] Accept and evaluate a `Bool` value as a valid expression --- Sources/IfTag.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/IfTag.swift b/Sources/IfTag.swift index 9b80c2f..538ef70 100644 --- a/Sources/IfTag.swift +++ b/Sources/IfTag.swift @@ -63,6 +63,8 @@ public class IfNode : NodeType { truthy = !result.isEmpty } else if let result = result as? [String:Any] { truthy = !result.isEmpty + } else if let result = result as? Bool { + truthy = result } else if result != nil { truthy = true } From 7bfb69cc8210fb988396a2aed83b4c693b4b375a Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Wed, 20 Jul 2016 02:39:50 +0900 Subject: [PATCH 3/3] [IfNode] Fix the `ifnot` error message --- Sources/IfTag.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/IfTag.swift b/Sources/IfTag.swift index 538ef70..8dae896 100644 --- a/Sources/IfTag.swift +++ b/Sources/IfTag.swift @@ -29,7 +29,7 @@ public class IfNode : NodeType { public class func parse_ifnot(parser:TokenParser, token:Token) throws -> NodeType { let components = token.components() guard components.count == 2 else { - throw TemplateSyntaxError("'ifnot' statements should use the following 'if condition' `\(token.contents)`.") + throw TemplateSyntaxError("'ifnot' statements should use the following 'ifnot condition' `\(token.contents)`.") } let variable = components[1] var trueNodes = [NodeType]()