Skip to content

Commit c81a5b4

Browse files
committed
Add Token.__eq__ to ease unit testing.
1 parent c13f014 commit c81a5b4

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

src/robot/parsing/lexer/tokens.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ def __repr__(self):
123123
return 'Token(%s, %r, %s, %s)' % (self.type, self.value,
124124
self.lineno, self.col_offset)
125125

126+
def __eq__(self, other):
127+
if not isinstance(other, Token):
128+
return False
129+
return (self.type == other.type and
130+
self.value == other.value and
131+
self.lineno == other.lineno and
132+
self.col_offset == other.col_offset and
133+
self.error == other.error)
134+
135+
def __ne__(self, other):
136+
return not self == other
137+
126138

127139
class EOS(Token):
128140
__slots__ = []

utest/parsing/test_lexer.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,7 @@ def assert_tokens(source, expected, get_tokens=get_tokens, data_only=False):
1919
% (len(expected), expected, len(tokens), tokens),
2020
values=False)
2121
for act, exp in zip(tokens, expected):
22-
exp = Token(*exp)
23-
assert_equal(act.type, exp.type)
24-
assert_equal(act.value, exp.value, formatter=repr)
25-
assert_equal(act.lineno, exp.lineno)
26-
assert_equal(act.col_offset, exp.col_offset)
27-
assert_equal(act.end_col_offset, exp.col_offset + len(exp.value))
22+
assert_equal(act, Token(*exp), formatter=repr)
2823

2924

3025
class TestName(unittest.TestCase):

utest/parsing/test_splitter.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ def verify_split(string, *expected_statements, **config):
2626
for act, exp in zip(tokens, expected):
2727
if exp[0] == DATA:
2828
expected_data[-1].append(exp)
29-
exp = Token(*exp)
30-
assert_equal(act.type, exp.type)
31-
assert_equal(act.value, exp.value)
32-
assert_equal(act.lineno, exp.lineno)
33-
assert_equal(act.col_offset, exp.col_offset)
34-
assert_equal(act.end_col_offset, exp.col_offset + len(exp.value))
29+
assert_equal(act, Token(*exp), formatter=repr)
3530
if not config:
3631
verify_split(string, *expected_data, data_only=True)
3732

0 commit comments

Comments
 (0)