-
Notifications
You must be signed in to change notification settings - Fork 84
/
_matchers.py
295 lines (212 loc) · 11.2 KB
/
_matchers.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
'''Matcher classes used by unit tests in order to set mock expectations.
These work well with our visatype definitions.
'''
import ctypes
import nimodinst._visatype as _visatype
import pprint
pp = pprint.PrettyPrinter(indent=4)
# Base classes
class _ScalarMatcher(object):
def __init__(self, expected_type, expected_value):
self.expected_type = expected_type
self.expected_value = expected_value
def __eq__(self, other):
if not isinstance(other, self.expected_type):
print("Unexpected type. Expected: {0}. Received: {1}".format(self.expected_type, type(other)))
return False
if other.value != self.expected_value:
print("Unexpected value. Expected: {0}. Received: {1}".format(self.expected_value, other.value))
return False
return True
def __repr__(self):
return '{0}({1}, {2})'.format(self.__class__.__name__, pp.pformat(self.expected_type), pp.pformat(self.expected_value))
class _PointerMatcher(object):
def __init__(self, expected_type):
self.expected_type = expected_type
def __eq__(self, other):
if not isinstance(other, ctypes.POINTER(self.expected_type)):
print("Unexpected type. Expected: {0}. Received: {1}".format(ctypes.POINTER(self.expected_type), type(other)))
return False
return True
def __repr__(self):
return '{0}({1})'.format(self.__class__.__name__, pp.pformat(self.expected_type))
class _BufferMatcher(object):
def __init__(self, expected_element_type, expected_size_or_value):
if isinstance(expected_size_or_value, int):
# Were given the size of the buffer
self.expected_value = None
self.expected_size = expected_size_or_value
else:
# Were given a list or something that behaves like a list
self.expected_value = expected_size_or_value
self.expected_size = len(expected_size_or_value)
self.expected_type = expected_element_type * self.expected_size
# Store params for __repr__
self._expected_element_type = expected_element_type
self._expected_size_or_value = expected_size_or_value
def __eq__(self, other):
if not isinstance(other, self.expected_type):
# We try to "dereference" this in case it is a pointer and then do the check again. Only then saying they don't match
try:
other = other.contents
except AttributeError:
pass
# Because of object lifetimes, we may need to mock the other instance and provide lists instead of the actual array
if not isinstance(other, self.expected_type) and not isinstance(other, list):
print("Unexpected type. Expected: {0} or {1}. Received: {2}".format(self.expected_type, list, type(other)))
return False
if self.expected_size != len(other):
print("Unexpected length. Expected: {0}. Received: {1}".format(self.expected_size, len(other)))
return False
if self.expected_value is not None:
# Can't compare the objects directly because they're different types (one is list, another is ctypes array).
# Go element by element, which allows for reporting the first index where different values were found.
for i in range(0, len(self.expected_value)):
if self.expected_value[i] != other[i]:
print("Unexpected value at index {0}. Expected: {1}. Received: {2}".format(i, self.expected_value[i], other[i]))
return False
return True
def __repr__(self):
return '{0}({1}, {2})'.format(self.__class__.__name__, pp.pformat(self._expected_element_type), pp.pformat(self._expected_size_or_value))
def __str__(self):
ret_str = self.__repr__() + '\n'
ret_str += ' expected_type = ' + str(self.expected_type) + '\n'
ret_str += ' expected_value = ' + str(self.expected_value) + '\n'
ret_str += ' expected_size = ' + str(self.expected_size) + '\n'
return ret_str
# Strings
class ViStringMatcher(object):
def __init__(self, expected_string_value):
self.expected_string_value = expected_string_value
def __eq__(self, other):
if not isinstance(other, ctypes.Array):
# We try to "dereference" this in case it is a pointer and then do the check again. Only then saying they don't match
try:
other = other.contents
except AttributeError:
pass
if not isinstance(other, ctypes.Array):
print("Unexpected type. Expected: {0}. Received: {1}".format(self.expected_type, type(other)))
return False
if len(other) < len(self.expected_string_value) + 1: # +1 for NULL terminating character
print("Unexpected length in C string. Expected at least: {0}. Received {1}".format(len(other), len(self.expected_string_value) + 1))
return False
if not isinstance(other[0], bytes):
print("Unexpected type. Not a string. Received: {0}".format(type(other[0])))
return False
if other.value.decode("ascii") != self.expected_string_value:
print("Unexpected value. Expected {0}. Received: {1}".format(self.expected_string_value, other.value.decode))
return False
return True
def __repr__(self):
return '{0}({1})'.format(self.__class__.__name__, pp.pformat(self.expected_string_value))
# Custom Type
def _compare_ctype_structs(expected, actual):
# From https://stackoverflow.com/questions/20986330/print-all-fields-of-ctypes-structure-with-introspection
for field in expected._fields_:
field_name = field[0]
expected_val = getattr(expected, field_name)
actual_val = getattr(actual, field_name)
if expected_val != actual_val:
print("Unexpected value field {0}. Expected: {1}. Received: {2}".format(field_name, expected_val, actual_val))
return False
return True
class CustomTypeMatcher(object):
def __init__(self, expected_type, expected_value):
self.expected_type = expected_type
self.expected_value = expected_value
def __eq__(self, actual):
if not isinstance(actual, self.expected_type):
print("Unexpected type. Expected: {0}. Received: {1}".format(self.expected_type, type(actual)))
return False
return _compare_ctype_structs(self.expected_value, actual)
def __repr__(self):
return '{0}({1}, {2})'.format(self.__class__.__name__, pp.pformat(self.expected_type), pp.pformat(self.expected_value))
class CustomTypeBufferMatcher(object):
def __init__(self, expected_element_type, expected_value):
self.expected_value = expected_value
self.expected_size = len(expected_value)
self.expected_type = expected_element_type * self.expected_size
self.expected_element_type = expected_element_type
def __eq__(self, actual):
if not isinstance(actual, self.expected_type):
print("Unexpected array type. Expected: {0}. Received: {1}".format(self.expected_type, type(actual)))
return False
if self.expected_size != len(actual):
print("Unexpected length. Expected: {0}. Received: {1}".format(self.expected_size, len(actual)))
return False
if self.expected_value is not None:
# Can't compare the objects directly because they're different types (one is list, another is ctypes array).
# Go element by element, which allows for reporting the first index where different values were found.
for a, e in zip(actual, self.expected_value):
if not isinstance(a, self.expected_element_type):
print("Unexpected type. Expected: {0}. Received: {1}".format(self.expected_element_type, type(a)))
return False
if not _compare_ctype_structs(e, a):
return False
return True
def __repr__(self):
expected_val_repr = '[' + ', '.join([x.__repr__() for x in self.expected_value]) + ']'
return '{0}({1}, {2})'.format(self.__class__.__name__, pp.pformat(self.expected_element_type), expected_val_repr)
def __str__(self):
ret_str = self.__repr__() + '\n'
ret_str += ' expected_type = ' + str(self.expected_type) + '\n'
ret_str += ' expected_size = ' + str(self.expected_size) + '\n'
return ret_str
# Scalars
class ViBooleanMatcher(_ScalarMatcher):
def __init__(self, expected_value):
_ScalarMatcher.__init__(self, _visatype.ViBoolean, 1 if expected_value is True else 0)
class ViSessionMatcher(_ScalarMatcher):
def __init__(self, expected_value):
_ScalarMatcher.__init__(self, _visatype.ViSession, expected_value)
class ViInt16Matcher(_ScalarMatcher):
def __init__(self, expected_value):
_ScalarMatcher.__init__(self, _visatype.ViInt16, expected_value)
class ViInt32Matcher(_ScalarMatcher):
def __init__(self, expected_value):
_ScalarMatcher.__init__(self, _visatype.ViInt32, expected_value)
class ViInt64Matcher(_ScalarMatcher):
def __init__(self, expected_value):
_ScalarMatcher.__init__(self, _visatype.ViInt64, expected_value)
class ViReal64Matcher(_ScalarMatcher):
def __init__(self, expected_value):
_ScalarMatcher.__init__(self, _visatype.ViReal64, expected_value)
# Pointers
class ViBooleanPointerMatcher(_PointerMatcher):
def __init__(self):
_PointerMatcher.__init__(self, _visatype.ViBoolean)
class ViSessionPointerMatcher(_PointerMatcher):
def __init__(self):
_PointerMatcher.__init__(self, _visatype.ViSession)
class ViInt16PointerMatcher(_PointerMatcher):
def __init__(self):
_PointerMatcher.__init__(self, _visatype.ViInt16)
class ViInt32PointerMatcher(_PointerMatcher):
def __init__(self):
_PointerMatcher.__init__(self, _visatype.ViInt32)
class ViInt64PointerMatcher(_PointerMatcher):
def __init__(self):
_PointerMatcher.__init__(self, _visatype.ViInt64)
class ViReal64PointerMatcher(_PointerMatcher):
def __init__(self):
_PointerMatcher.__init__(self, _visatype.ViReal64)
# Buffers
class ViBooleanBufferMatcher(_BufferMatcher):
def __init__(self, expected_size_or_value):
_BufferMatcher.__init__(self, _visatype.ViBoolean, expected_size_or_value)
class ViCharBufferMatcher(_BufferMatcher):
def __init__(self, expected_size_or_value):
_BufferMatcher.__init__(self, _visatype.ViChar, expected_size_or_value)
class ViInt16BufferMatcher(_BufferMatcher):
def __init__(self, expected_size_or_value):
_BufferMatcher.__init__(self, _visatype.ViInt16, expected_size_or_value)
class ViInt32BufferMatcher(_BufferMatcher):
def __init__(self, expected_size_or_value):
_BufferMatcher.__init__(self, _visatype.ViInt32, expected_size_or_value)
class ViInt64BufferMatcher(_BufferMatcher):
def __init__(self, expected_size_or_value):
_BufferMatcher.__init__(self, _visatype.ViInt64, expected_size_or_value)
class ViReal64BufferMatcher(_BufferMatcher):
def __init__(self, expected_size_or_value):
_BufferMatcher.__init__(self, _visatype.ViReal64, expected_size_or_value)