diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ba0080..2aa8dc4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ - Templates can now extend templates that extend other templates. [#60](https://github.com/kylef/Stencil/issues/60) +- If comparisons will now treat 0 and below numbers as negative. + ## 0.6.0 diff --git a/Sources/IfTag.swift b/Sources/IfTag.swift index d9c1b45..1084936 100644 --- a/Sources/IfTag.swift +++ b/Sources/IfTag.swift @@ -65,6 +65,12 @@ class IfNode : NodeType { truthy = !result.isEmpty } else if let result = result as? Bool { truthy = result + } else if let result = result as? Int { + truthy = result > 0 + } else if let result = result as? Float { + truthy = result > 0 + } else if let result = result as? Double { + truthy = result > 0 } else if result != nil { truthy = true } diff --git a/Tests/StencilTests/IfNodeSpec.swift b/Tests/StencilTests/IfNodeSpec.swift index 3b60209..c048864 100644 --- a/Tests/StencilTests/IfNodeSpec.swift +++ b/Tests/StencilTests/IfNodeSpec.swift @@ -123,6 +123,36 @@ func testIfNode() { let node = IfNode(variable: "items", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) try expect(try node.render(arrayContext)) == "false" } + + $0.it("renders the false when integer is below 1") { + let context = Context(dictionary: ["value": 0]) + let node = IfNode(variable: "value", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try node.render(context)) == "false" + + let negativeContext = Context(dictionary: ["value": -5]) + let negativeNode = IfNode(variable: "value", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try negativeNode.render(negativeContext)) == "false" + } + + $0.it("renders the false when float is below 1") { + let context = Context(dictionary: ["value": Float(0)]) + let node = IfNode(variable: "value", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try node.render(context)) == "false" + + let negativeContext = Context(dictionary: ["value": Float(-5)]) + let negativeNode = IfNode(variable: "value", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try negativeNode.render(negativeContext)) == "false" + } + + $0.it("renders the false when double is below 1") { + let context = Context(dictionary: ["value": Double(0)]) + let node = IfNode(variable: "value", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try node.render(context)) == "false" + + let negativeContext = Context(dictionary: ["value": Double(-5)]) + let negativeNode = IfNode(variable: "value", trueNodes: [TextNode(text: "true")], falseNodes: [TextNode(text: "false")]) + try expect(try negativeNode.render(negativeContext)) == "false" + } } } }