stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.35k stars 225 forks source link

Attribute name "id" not working #100

Closed foldericon closed 7 years ago

foldericon commented 7 years ago

So let's say I have an object in my context called "company" and that company has the property "id". {{ company.id }} would return nothing and {% if company.id == 1 %} is always true, no matter what the actual value of id is or what I compare it with.

foldericon commented 7 years ago

So apparently my problem was, that I use NSNumber here, I guess there is no way to convert that to int in my template?

kylef commented 7 years ago

Hi @foldericon, I'm unable to reproduce any problems comparing NSNumber/Int etc.

I wrote the following test cases that all pass on master/0.8.0:

diff --git a/Tests/StencilTests/ExpressionSpec.swift b/Tests/StencilTests/ExpressionSpec.swift
index a7b7241..1ace170 100644
--- a/Tests/StencilTests/ExpressionSpec.swift
+++ b/Tests/StencilTests/ExpressionSpec.swift
@@ -1,3 +1,4 @@
+import Foundation
 import Spectre
 @testable import Stencil

@@ -89,6 +90,16 @@ func testExpressions() {
         let context = Context(dictionary: ["value": UInt(0)])
         try expect(try expression.evaluate(context: context)).to.beFalse()
       }
+
+      $0.it("evaluates to false when NSNumber is false") {
+        let context = Context(dictionary: ["value": NSNumber(value: false)])
+        try expect(try expression.evaluate(context: context)).to.beFalse()
+      }
+
+      $0.it("evaluates to false when NSNumber is 0") {
+        let context = Context(dictionary: ["value": NSNumber(value: 0)])
+        try expect(try expression.evaluate(context: context)).to.beFalse()
+      }
     }

     $0.describe("NotExpression") {
@@ -175,10 +186,18 @@ func testExpressions() {
           try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.0]))).to.beTrue()
         }

+        $0.it("evaluates to true with NSNumber and numbers") {
+          try expect(expression.evaluate(context: Context(dictionary: ["lhs": NSNumber(value: 1), "rhs": 1.0]))).to.beTrue()
+        }
+
         $0.it("evaluates to false with non equal numbers") {
           try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1.1]))).to.beFalse()
         }

+        $0.it("evaluates to false with non equal NSNumber and numbers") {
+          try expect(expression.evaluate(context: Context(dictionary: ["lhs": NSNumber(value: 1), "rhs": 1.1]))).to.beFalse()
+        }
+
         $0.it("evaluates to true with booleans") {
           try expect(expression.evaluate(context: Context(dictionary: ["lhs": true, "rhs": true]))).to.beTrue()
         }

Would you be able to provide a larger example or even a failing test case that could demonstrate this problem?