Skip to content
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

Type check when comparing nodes #98

Merged
merged 3 commits into from Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions parsimonious/nodes.py
Expand Up @@ -90,8 +90,10 @@ def __str__(self):

def __eq__(self, other):
"""Support by-value deep comparison with other nodes for testing."""
return (other is not None and
self.expr_name == other.expr_name and
if not isinstance(other, Node):
return NotImplemented

return (self.expr_name == other.expr_name and
self.full_text == other.full_text and
self.start == other.start and
self.end == other.end and
Expand Down
8 changes: 7 additions & 1 deletion parsimonious/tests/test_nodes.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from nose import SkipTest
from nose.tools import eq_, assert_raises
from nose.tools import eq_, ok_, assert_raises

from parsimonious import Grammar, NodeVisitor, VisitationError, rule
from parsimonious.nodes import Node
Expand Down Expand Up @@ -142,3 +142,9 @@ def visit_greeting(self, thing, visited_children):
raise PrimalScream('This should percolate up!')

assert_raises(PrimalScream, Screamer().parse, 'howdy')


def test_node_inequality():
node = Node('text', 'o hai', 0, 5)
ok_(node != 5)
ok_(node != None)