fix(if): Treat below 0 numbers as negative

This commit is contained in:
Kyle Fuller
2016-11-27 20:11:51 +00:00
parent 9fdbbc99e9
commit 2324808dca
3 changed files with 38 additions and 0 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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"
}
}
}
}