forked from hardbyte/python-can
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessage_helper.py
More file actions
49 lines (40 loc) · 1.95 KB
/
message_helper.py
File metadata and controls
49 lines (40 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env python
# coding: utf-8
"""
This module contains a helper for writing test cases that need to compare messages.
"""
from __future__ import absolute_import, print_function
from copy import copy
class ComparingMessagesTestCase(object):
"""
Must be extended by a class also extending a unittest.TestCase.
.. note:: This class does not extend unittest.TestCase so it does not get
run as a test itself.
"""
def __init__(self, allowed_timestamp_delta=0.0, preserves_channel=True):
"""
:param float or int or None allowed_timestamp_delta: directly passed to :meth:`can.Message.equals`
:param bool preserves_channel: if True, checks that the channel attribute is preserved
"""
self.allowed_timestamp_delta = allowed_timestamp_delta
self.preserves_channel = preserves_channel
def assertMessageEqual(self, message_1, message_2):
"""
Checks that two messages are equal, according to the given rules.
"""
if message_1.equals(message_2, timestamp_delta=self.allowed_timestamp_delta):
return
elif self.preserves_channel:
print("Comparing: message 1: {!r}".format(message_1))
print(" message 2: {!r}".format(message_2))
self.fail("messages are unequal with allowed timestamp delta {}".format(self.allowed_timestamp_delta))
else:
message_2 = copy(message_2) # make sure this method is pure
message_2.channel = message_1.channel
if message_1.equals(message_2, timestamp_delta=self.allowed_timestamp_delta):
return
else:
print("Comparing: message 1: {!r}".format(message_1))
print(" message 2: {!r}".format(message_2))
self.fail("messages are unequal with allowed timestamp delta {} even when ignoring channels" \
.format(self.allowed_timestamp_delta))