feat(if): Support inequality operator
This commit is contained in:
@@ -20,8 +20,8 @@
|
|||||||
- Variable lookup now supports introspection of Swift types. You can now lookup
|
- Variable lookup now supports introspection of Swift types. You can now lookup
|
||||||
values of Swift structures and classes inside a Context.
|
values of Swift structures and classes inside a Context.
|
||||||
|
|
||||||
- If tags can now use prefix and infix operators such as `not`, `and`, `or` and
|
- If tags can now use prefix and infix operators such as `not`, `and`, `or`,
|
||||||
`==`.
|
`==`, and `!=`.
|
||||||
|
|
||||||
```html+django
|
```html+django
|
||||||
{% if one or two and not three %}
|
{% if one or two and not three %}
|
||||||
|
|||||||
@@ -134,11 +134,11 @@ final class AndExpression: Expression, InfixOperator, CustomStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
final class EqualityExpression: Expression, InfixOperator, CustomStringConvertible {
|
class EqualityExpression: Expression, InfixOperator, CustomStringConvertible {
|
||||||
let lhs: Expression
|
let lhs: Expression
|
||||||
let rhs: Expression
|
let rhs: Expression
|
||||||
|
|
||||||
init(lhs: Expression, rhs: Expression) {
|
required init(lhs: Expression, rhs: Expression) {
|
||||||
self.lhs = lhs
|
self.lhs = lhs
|
||||||
self.rhs = rhs
|
self.rhs = rhs
|
||||||
}
|
}
|
||||||
@@ -170,6 +170,17 @@ final class EqualityExpression: Expression, InfixOperator, CustomStringConvertib
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class InequalityExpression: EqualityExpression {
|
||||||
|
override var description: String {
|
||||||
|
return "(\(lhs) != \(rhs))"
|
||||||
|
}
|
||||||
|
|
||||||
|
override func evaluate(context: Context) throws -> Bool {
|
||||||
|
return try !super.evaluate(context: context)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func toNumber(value: Any) -> Float80? {
|
func toNumber(value: Any) -> Float80? {
|
||||||
if let value = value as? Float {
|
if let value = value as? Float {
|
||||||
return Float80(value)
|
return Float80(value)
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ let operators: [Operator] = [
|
|||||||
.infix("and", 7, AndExpression.self),
|
.infix("and", 7, AndExpression.self),
|
||||||
.prefix("not", 8, NotExpression.self),
|
.prefix("not", 8, NotExpression.self),
|
||||||
.infix("==", 10, EqualityExpression.self),
|
.infix("==", 10, EqualityExpression.self),
|
||||||
|
.infix("!=", 10, InequalityExpression.self),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -162,7 +162,7 @@ func testExpressions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$0.it("evaluates to false with non equal lhs/rhs") {
|
$0.it("evaluates to false with non equal lhs/rhs") {
|
||||||
print(expression)
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beFalse()
|
||||||
}
|
}
|
||||||
|
|
||||||
$0.it("evaluates to true with nils") {
|
$0.it("evaluates to true with nils") {
|
||||||
@@ -190,6 +190,18 @@ func testExpressions() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$0.describe("inequality expression") {
|
||||||
|
let expression = try! parseExpression(components: ["lhs", "!=", "rhs"])
|
||||||
|
|
||||||
|
$0.it("evaluates to true with inequal lhs/rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "a", "rhs": "b"]))).to.beTrue()
|
||||||
|
}
|
||||||
|
|
||||||
|
$0.it("evaluates to false with equal lhs/rhs") {
|
||||||
|
try expect(expression.evaluate(context: Context(dictionary: ["lhs": "b", "rhs": "b"]))).to.beFalse()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$0.describe("multiple expression") {
|
$0.describe("multiple expression") {
|
||||||
let expression = try! parseExpression(components: ["one", "or", "two", "and", "not", "three"])
|
let expression = try! parseExpression(components: ["one", "or", "two", "and", "not", "three"])
|
||||||
|
|
||||||
|
|||||||
@@ -106,6 +106,17 @@ Will be treated as:
|
|||||||
|
|
||||||
.. note:: The equality operator only supports numerical, string and boolean types.
|
.. note:: The equality operator only supports numerical, string and boolean types.
|
||||||
|
|
||||||
|
``!=`` operator
|
||||||
|
"""""""""""""""
|
||||||
|
|
||||||
|
.. code-block:: html+django
|
||||||
|
|
||||||
|
{% if value != other_value %}
|
||||||
|
value is not equal to other_value
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
.. note:: The inequality operator only supports numerical, string and boolean types.
|
||||||
|
|
||||||
``ifnot``
|
``ifnot``
|
||||||
~~~~~~~~~
|
~~~~~~~~~
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user