Skip to content

Commit a57ce48

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 a57ce48

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,27 @@ def create_sequence_from_iter(it: Iterable) -> list:
9090
sequence.sequence = True
9191
return sequence
9292

93+
@staticmethod
94+
def is_deep_equal(lhs: Optional[Any], rhs: Optional[Any]) -> bool:
95+
if isinstance(lhs, list) and isinstance(rhs, list):
96+
if len(lhs) != len(rhs):
97+
return False
98+
for ii, _ in enumerate(lhs):
99+
if not Utils.is_deep_equal(lhs[ii], rhs[ii]):
100+
return False
101+
return True
102+
elif isinstance(lhs, dict) and isinstance(rhs, dict):
103+
if lhs.keys() != rhs.keys():
104+
return False
105+
for key in lhs.keys():
106+
if not Utils.is_deep_equal(lhs[key], rhs[key]):
107+
return False
108+
return True
109+
if lhs == rhs and type(lhs) == type(rhs):
110+
return True
111+
112+
return False
113+
93114
class JList(list):
94115
sequence: bool
95116
outer_wrapper: bool

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)