-
Notifications
You must be signed in to change notification settings - Fork 29
Add BaseNode.equals #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7621d1b
Add BaseNode.equals
stasm 07d9c03
Use sets and for loops instead of any/imap
stasm 9fc9597
scalars_equal is for Nodes and primitives, not lists
stasm 6e13b16
Address @Pike's feedback
stasm cd7e7d4
Make with_spans default to False
stasm 908129a
Add tests and docstrings
stasm 147b70c
Remove izip
stasm 4158edc
Amend the CHANGELOG
stasm db57aa8
Apply @Pike's feedback to tests
stasm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| from __future__ import unicode_literals | ||
| import unittest | ||
| import sys | ||
|
|
||
| sys.path.append('.') | ||
|
|
||
| from tests.syntax import dedent_ftl | ||
| from fluent.syntax.parser import FluentParser | ||
|
|
||
|
|
||
| def identity(node): | ||
| return node | ||
|
|
||
|
|
||
| class TestEntryEqualToSelf(unittest.TestCase): | ||
| def setUp(self): | ||
| self.parser = FluentParser() | ||
|
|
||
| def parse_ftl_entry(self, string): | ||
| return self.parser.parse_entry(dedent_ftl(string)) | ||
|
|
||
| def test_same_simple_message(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = Foo | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_selector_message(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = | ||
| { $num -> | ||
| [one] One | ||
| [two] Two | ||
| [few] Few | ||
| [many] Many | ||
| *[other] Other | ||
| } | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_complex_placeable_message(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = Foo { NUMBER($num, style: "decimal") } Bar | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_message_with_attribute(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo | ||
| .attr = Attr | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_message_with_attributes(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo | ||
| .attr1 = Attr 1 | ||
| .attr2 = Attr 2 | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_message_with_tag(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = Foo | ||
| #tag | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_message_with_tags(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = Foo | ||
| #tag1 | ||
| #tag2 | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
| def test_same_junk(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = Foo { | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message1)) | ||
| self.assertTrue(message1.equals(message1.traverse(identity))) | ||
|
|
||
|
|
||
| class TestOrderEquals(unittest.TestCase): | ||
| def setUp(self): | ||
| self.parser = FluentParser() | ||
|
|
||
| def parse_ftl_entry(self, string): | ||
| return self.parser.parse_entry(dedent_ftl(string)) | ||
|
|
||
| def test_attributes(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo | ||
| .attr1 = Attr1 | ||
| .attr2 = Attr2 | ||
| """) | ||
| message2 = self.parse_ftl_entry("""\ | ||
| foo | ||
| .attr2 = Attr2 | ||
| .attr1 = Attr1 | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message2)) | ||
| self.assertTrue(message2.equals(message1)) | ||
|
|
||
| def test_tags(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = Foo | ||
| #tag1 | ||
| #tag2 | ||
| """) | ||
| message2 = self.parse_ftl_entry("""\ | ||
| foo = Foo | ||
| #tag2 | ||
| #tag1 | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message2)) | ||
| self.assertTrue(message2.equals(message1)) | ||
|
|
||
| def test_variants(self): | ||
| message1 = self.parse_ftl_entry("""\ | ||
| foo = | ||
| { $num -> | ||
| [a] A | ||
| *[b] B | ||
| } | ||
| """) | ||
| message2 = self.parse_ftl_entry("""\ | ||
| foo = | ||
| { $num -> | ||
| *[b] B | ||
| [a] A | ||
| } | ||
| """) | ||
|
|
||
| self.assertTrue(message1.equals(message2)) | ||
| self.assertTrue(message2.equals(message1)) | ||
|
|
||
|
|
||
| class TestEqualWithSpans(unittest.TestCase): | ||
| def test_default_behavior(self): | ||
| parser = FluentParser() | ||
|
|
||
| strings = [ | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = { $arg }", "foo = { $arg }"), | ||
| ] | ||
|
|
||
| messages = [ | ||
| (parser.parse_entry(a), parser.parse_entry(b)) | ||
| for a, b in strings | ||
| ] | ||
|
|
||
| for a, b in messages: | ||
| self.assertTrue(a.equals(b)) | ||
|
|
||
| def test_parser_without_spans(self): | ||
| parser = FluentParser(with_spans=False) | ||
|
|
||
| strings = [ | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = { $arg }", "foo = { $arg }"), | ||
| ] | ||
|
|
||
| messages = [ | ||
| (parser.parse_entry(a), parser.parse_entry(b)) | ||
| for a, b in strings | ||
| ] | ||
|
|
||
| for a, b in messages: | ||
| self.assertTrue(a.equals(b)) | ||
|
|
||
| def test_equals_with_spans(self): | ||
| parser = FluentParser() | ||
|
|
||
| strings = [ | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = { $arg }", "foo = { $arg }"), | ||
| ] | ||
|
|
||
| messages = [ | ||
| (parser.parse_entry(a), parser.parse_entry(b)) | ||
| for a, b in strings | ||
| ] | ||
|
|
||
| for a, b in messages: | ||
| self.assertTrue(a.equals(b, with_spans=True)) | ||
|
|
||
| def test_parser_without_spans_equals_with_spans(self): | ||
| parser = FluentParser(with_spans=False) | ||
|
|
||
| strings = [ | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = { $arg }", "foo = { $arg }"), | ||
| ("foo = { $arg }", "foo = { $arg }"), | ||
| ] | ||
|
|
||
| messages = [ | ||
| (parser.parse_entry(a), parser.parse_entry(b)) | ||
| for a, b in strings | ||
| ] | ||
|
|
||
| for a, b in messages: | ||
| self.assertTrue(a.equals(b, with_spans=True)) | ||
|
|
||
| def test_differ_with_spans(self): | ||
| parser = FluentParser() | ||
|
|
||
| strings = [ | ||
| ("foo = Foo", "foo = Foo"), | ||
| ("foo = { $arg }", "foo = { $arg }"), | ||
| ] | ||
|
|
||
| messages = [ | ||
| (parser.parse_entry(a), parser.parse_entry(b)) | ||
| for a, b in strings | ||
| ] | ||
|
|
||
| for a, b in messages: | ||
| self.assertFalse(a.equals(b, with_spans=True)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to test for equality against the identity traversal, too?
If so, add that to the tests in this class?