Skip to content

Commit 7a84a81

Browse files
authored
Add the ignored_fields kwarg to BaseNode.equals (#16)
1 parent 73214e4 commit 7a84a81

File tree

3 files changed

+53
-12
lines changed

3 files changed

+53
-12
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
Nodes are deeply compared on a field by field basis. If possible, False is
88
returned early. When comparing attributes, tags and variants in
99
SelectExpressions, the order doesn't matter. By default, spans are not
10-
taken into account.
10+
taken into account. Other fields may also be ignored if necessary:
11+
12+
message1.equals(message2, ignored_fields=['comment', 'span'])
1113

1214
## fluent 0.4.0 (June 13th, 2017)
1315

fluent/syntax/ast.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ def from_json(value):
2727
return value
2828

2929

30-
def scalars_equal(node1, node2, with_spans=False):
30+
def scalars_equal(node1, node2, ignored_fields):
3131
"""Compare two nodes which are not lists."""
3232

3333
if type(node1) != type(node2):
3434
return False
3535

3636
if isinstance(node1, BaseNode):
37-
return node1.equals(node2, with_spans)
37+
return node1.equals(node2, ignored_fields)
3838

3939
return node1 == node2
4040

@@ -73,7 +73,7 @@ def visit(value):
7373

7474
return fun(node)
7575

76-
def equals(self, other, with_spans=False):
76+
def equals(self, other, ignored_fields=['span']):
7777
"""Compare two nodes.
7878
7979
Nodes are deeply compared on a field by field basis. If possible, False
@@ -85,9 +85,10 @@ def equals(self, other, with_spans=False):
8585
self_keys = set(vars(self).keys())
8686
other_keys = set(vars(other).keys())
8787

88-
if not with_spans:
89-
self_keys.discard('span')
90-
other_keys.discard('span')
88+
if ignored_fields:
89+
for key in ignored_fields:
90+
self_keys.discard(key)
91+
other_keys.discard(key)
9192

9293
if self_keys != other_keys:
9394
return False
@@ -117,10 +118,10 @@ def equals(self, other, with_spans=False):
117118
field2 = sorted(field2, key=sorting)
118119

119120
for elem1, elem2 in zip(field1, field2):
120-
if not scalars_equal(elem1, elem2, with_spans):
121+
if not scalars_equal(elem1, elem2, ignored_fields):
121122
return False
122123

123-
elif not scalars_equal(field1, field2, with_spans):
124+
elif not scalars_equal(field1, field2, ignored_fields):
124125
return False
125126

126127
return True

tests/syntax/test_equals.py

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ def test_equals_with_spans(self):
203203
]
204204

205205
for a, b in messages:
206-
self.assertTrue(a.equals(b, with_spans=True))
206+
self.assertTrue(a.equals(b, ignored_fields=None))
207207

208208
def test_parser_without_spans_equals_with_spans(self):
209209
parser = FluentParser(with_spans=False)
@@ -221,7 +221,7 @@ def test_parser_without_spans_equals_with_spans(self):
221221
]
222222

223223
for a, b in messages:
224-
self.assertTrue(a.equals(b, with_spans=True))
224+
self.assertTrue(a.equals(b, ignored_fields=None))
225225

226226
def test_differ_with_spans(self):
227227
parser = FluentParser()
@@ -237,4 +237,42 @@ def test_differ_with_spans(self):
237237
]
238238

239239
for a, b in messages:
240-
self.assertFalse(a.equals(b, with_spans=True))
240+
self.assertFalse(a.equals(b, ignored_fields=None))
241+
242+
243+
class TestIgnoredFields(unittest.TestCase):
244+
def setUp(self):
245+
self.parser = FluentParser()
246+
247+
def parse_ftl_entry(self, string):
248+
return self.parser.parse_entry(dedent_ftl(string))
249+
250+
def test_ignore_value(self):
251+
a = self.parse_ftl_entry("foo = Foo")
252+
b = self.parse_ftl_entry("foo = Bar")
253+
254+
self.assertTrue(a.equals(b, ignored_fields=['value']))
255+
256+
def test_ignore_value_span(self):
257+
a = self.parse_ftl_entry("foo = Foo")
258+
b = self.parse_ftl_entry("foo = Foobar")
259+
260+
self.assertTrue(a.equals(b, ignored_fields=['span', 'value']))
261+
self.assertFalse(a.equals(b, ignored_fields=['value']))
262+
263+
def test_ignore_comments(self):
264+
a = self.parse_ftl_entry("""\
265+
// Comment A
266+
foo = Foo
267+
""")
268+
b = self.parse_ftl_entry("""\
269+
// Comment B
270+
foo = Foo
271+
""")
272+
c = self.parse_ftl_entry("""\
273+
// Comment CC
274+
foo = Foo
275+
""")
276+
277+
self.assertTrue(a.equals(b, ignored_fields=['comment']))
278+
self.assertFalse(a.equals(c, ignored_fields=['comment']))

0 commit comments

Comments
 (0)