Skip to content

Commit acbd601

Browse files
committed
Add test cases for boolean equality evaluation
Create is_deep_equal in utils based off of https://github.com/jsonata-js/jsonata/blob/2bb258933b509b97ed173a48e0d5661033b1df19/src/utils.js#L132
1 parent c3b9aa9 commit acbd601

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/jsonata/jsonata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,9 +800,9 @@ def evaluate_equality_expression(self, lhs: Optional[Any], rhs: Optional[Any], o
800800

801801
result = None
802802
if op == "=":
803-
result = lhs == rhs # isDeepEqual(lhs, rhs);
803+
result = utils.Utils.is_deep_equal(lhs, rhs)
804804
elif op == "!=":
805-
result = lhs != rhs # !isDeepEqual(lhs, rhs);
805+
result = not utils.Utils.is_deep_equal(lhs, rhs)
806806
return result
807807

808808
#

src/jsonata/utils.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#
22
# Copyright Robert Yokota
3-
#
3+
#
44
# Licensed under the Apache License, Version 2.0 (the "License")
55
# you may not use this file except in compliance with the License.
66
# You may obtain a copy of the License at
@@ -67,6 +67,7 @@ def is_array_of_numbers(v: Optional[Any]) -> bool:
6767
@staticmethod
6868
def is_function(o: Optional[Any]) -> bool:
6969
from jsonata import jsonata
70+
7071
return isinstance(o, jsonata.Jsonata.JFunctionCallable)
7172

7273
NONE = object()
@@ -90,6 +91,27 @@ def create_sequence_from_iter(it: Iterable) -> list:
9091
sequence.sequence = True
9192
return sequence
9293

94+
@staticmethod
95+
def is_deep_equal(lhs: Optional[Any], rhs: Optional[Any]) -> bool:
96+
if isinstance(lhs, list) and isinstance(rhs, list):
97+
if len(lhs) != len(rhs):
98+
return False
99+
for ii, _ in enumerate(lhs):
100+
if not Utils.is_deep_equal(lhs[ii], rhs[ii]):
101+
return False
102+
return True
103+
elif isinstance(lhs, dict) and isinstance(rhs, dict):
104+
if lhs.keys() != rhs.keys():
105+
return False
106+
for key in lhs.keys():
107+
if not Utils.is_deep_equal(lhs[key], rhs[key]):
108+
return False
109+
return True
110+
if lhs == rhs and type(lhs) == type(rhs):
111+
return True
112+
113+
return False
114+
93115
class JList(list):
94116
sequence: bool
95117
outer_wrapper: bool
@@ -176,3 +198,4 @@ def recurse(val: Optional[Any]) -> None:
176198
def convert_nulls(res: Optional[Any]) -> Optional[Any]:
177199
Utils.recurse(res)
178200
return Utils.convert_value(res)
201+
False

tests/types_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,10 @@ def test_ignore(self):
4040
expr2.set_validate_input(False)
4141
with pytest.raises(TypeError):
4242
expr2.evaluate({"a": a_set})
43+
44+
def test_fix_issue_21(self):
45+
"""
46+
https://github.com/rayokota/jsonata-python/issues/21
47+
"""
48+
assert jsonata.Jsonata("true = 1").evaluate({}) is False
49+
assert jsonata.Jsonata("false = 0").evaluate({}) is False

0 commit comments

Comments
 (0)