fix(variable): checking values for nil when using mirror (#144)

This commit is contained in:
Ilya Puchka
2017-10-02 10:38:50 +02:00
committed by Kyle Fuller
parent 46f179e3ed
commit e7a0738bda
3 changed files with 22 additions and 0 deletions

View File

@@ -18,6 +18,7 @@
incorrect amount of arguments. incorrect amount of arguments.
- Fixes a potential crash when using incomplete tokens in a template for - Fixes a potential crash when using incomplete tokens in a template for
example, `{%%}` or `{{}}`. example, `{%%}` or `{{}}`.
- Fixes evaluating nil properties as true
## 0.9.0 ## 0.9.0

View File

@@ -105,6 +105,10 @@ public struct Variable : Equatable, Resolvable {
if current == nil { if current == nil {
return nil return nil
// mirror returns non-nil value even for nil-containing properties
// so we have to check if its value is actually nil or not
} else if let current = current, String(describing: current) == "nil" {
return nil
} }
} else { } else {
return nil return nil

View File

@@ -253,5 +253,22 @@ func testIfNode() {
let result = try renderNodes(nodes, Context(dictionary: ["value": "test"])) let result = try renderNodes(nodes, Context(dictionary: ["value": "test"]))
try expect(result) == "true" try expect(result) == "true"
} }
$0.it("evaluates nil properties as false") {
let tokens: [Token] = [
.block(value: "if instance.value"),
.text(value: "true"),
.block(value: "endif")
]
let parser = TokenParser(tokens: tokens, environment: Environment())
let nodes = try parser.parse()
struct SomeType {
let value: String? = nil
}
let result = try renderNodes(nodes, Context(dictionary: ["instance": SomeType()]))
try expect(result) == ""
}
} }
} }