From 6b02fccf84a0adb561dc1f51eb39c5f1b03cd5f5 Mon Sep 17 00:00:00 2001 From: Ilya Puchka Date: Thu, 5 Apr 2018 01:22:05 +0100 Subject: [PATCH] feat: added support for ranges in if-in expression (#193) --- CHANGELOG.md | 1 + Sources/Expression.swift | 4 ++++ Tests/StencilTests/ExpressionSpec.swift | 4 ++++ 3 files changed, 9 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index df370d5..d55be11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ - Added `indent` filter - Allow using new lines inside tags - Added support for iterating arrays of tuples +- Added support for ranges in if-in expression ### Bug Fixes diff --git a/Sources/Expression.swift b/Sources/Expression.swift index 1f41afe..c7199fc 100644 --- a/Sources/Expression.swift +++ b/Sources/Expression.swift @@ -105,6 +105,10 @@ final class InExpression: Expression, InfixOperator, CustomStringConvertible { if let lhs = lhsValue as? AnyHashable, let rhs = rhsValue as? [AnyHashable] { return rhs.contains(lhs) + } else if let lhs = lhsValue as? Int, let rhs = rhsValue as? CountableClosedRange { + return rhs.contains(lhs) + } else if let lhs = lhsValue as? Int, let rhs = rhsValue as? CountableRange { + return rhs.contains(lhs) } else if let lhs = lhsValue as? String, let rhs = rhsValue as? String { return rhs.contains(lhs) } else if lhsValue == nil && rhsValue == nil { diff --git a/Tests/StencilTests/ExpressionSpec.swift b/Tests/StencilTests/ExpressionSpec.swift index 4b7958d..c41575f 100644 --- a/Tests/StencilTests/ExpressionSpec.swift +++ b/Tests/StencilTests/ExpressionSpec.swift @@ -287,12 +287,16 @@ func testExpressions() { try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": [1, 2, 3]]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": ["a", "b", "c"]]))).to.beTrue() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "abc"]))).to.beTrue() + try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1...3]))).to.beTrue() + try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": 1..<3]))).to.beTrue() } $0.it("evaluates to false when rhs does not contain lhs") { try expect(expression.evaluate(context: Context(dictionary: ["lhs": 1, "rhs": [2, 3, 4]]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": ["b", "c", "d"]]))).to.beFalse() try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "bcd"]))).to.beFalse() + try expect(expression.evaluate(context: Context(dictionary: ["lhs": 4, "rhs": 1...3]))).to.beFalse() + try expect(expression.evaluate(context: Context(dictionary: ["lhs": 3, "rhs": 1..<3]))).to.beFalse() } } }