forked from lightwave-lab/lightlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_instrument_overloading.py
174 lines (128 loc) · 4.79 KB
/
test_instrument_overloading.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
''' Testing instrument getattr, setattr, delattr overloading. '''
import pytest
from lightlab.laboratory.instruments import Instrument
from lightlab.equipment.lab_instruments import VISAInstrumentDriver
class DerivedInstrument(Instrument):
normal_variable = 'default'
_private_variable = 'default_private'
__superprivate_variable = 'default_superprivate'
@property
def private_variable(self):
return self._private_variable
@private_variable.setter
def private_variable(self, value):
self._private_variable = value
@private_variable.deleter
def private_variable(self):
del self._private_variable
@property
def superprivate_variable(self):
return self.__superprivate_variable
@superprivate_variable.setter
def superprivate_variable(self, value):
self.__superprivate_variable = value
@superprivate_variable.deleter
def superprivate_variable(self):
del self.__superprivate_variable
class ReDerivedInstrument(DerivedInstrument):
pass
class NormalClass(object):
normal_variable = 'default'
_private_variable = 'default_private'
__superprivate_variable = 'default_superprivate'
@property
def private_variable(self):
return self._private_variable
@private_variable.setter
def private_variable(self, value):
self._private_variable = value
@private_variable.deleter
def private_variable(self):
del self._private_variable
@property
def superprivate_variable(self):
return self.__superprivate_variable
@superprivate_variable.setter
def superprivate_variable(self, value):
self.__superprivate_variable = value
@superprivate_variable.deleter
def superprivate_variable(self):
del self.__superprivate_variable
class DerivedClass(NormalClass):
pass
class BogusDriver(VISAInstrumentDriver):
instrument_category = DerivedInstrument
_write = ''
def open(self):
self.mbSession = 'open'
def close(self):
self.mbSession = None
def test_init():
d = BogusDriver()
assert d.__class__ == DerivedInstrument
assert d._driver_class == BogusDriver
assert d.driver.address is None
def test_address_change():
d = BogusDriver(address='123')
assert d.address == '123'
assert d.driver.address == '123'
d.driver.open()
assert d.driver.mbSession == 'open'
old_driver = d.driver
d.address = '1234'
assert d.driver is old_driver
assert d.address == '1234'
assert d.driver.address == '1234'
assert d.driver.mbSession is None
# ### Compare DerivedInstrument's behavior with normal class
klasses = [NormalClass, DerivedClass, DerivedInstrument, ReDerivedInstrument]
@pytest.mark.parametrize("Klass", klasses)
def test_normal_variable(Klass):
d = Klass()
assert d.normal_variable == 'default'
d.normal_variable = 123
assert d.normal_variable == 123
del d.normal_variable
assert d.normal_variable == 'default'
@pytest.mark.parametrize("Klass", klasses)
def test_private_variable(Klass):
d = Klass()
assert d._private_variable == 'default_private'
d._private_variable = 123
assert d._private_variable == 123
del d._private_variable
assert d._private_variable == 'default_private'
@pytest.mark.parametrize("Klass", klasses)
def test_superprivate_variable(Klass):
d = Klass()
with pytest.raises(AttributeError): # , message='should not be able to access __variable'
assert d.__superprivate_variable == 'default_superprivate'
d.__superprivate_variable = 123
assert d.__superprivate_variable == 123
del d.__superprivate_variable
with pytest.raises(AttributeError): # , message='should not be able to access __variable'
assert d.__superprivate_variable == 'default_superprivate'
@pytest.mark.parametrize("Klass", klasses)
def test_unknownsuperprivate_variable(Klass):
d = Klass()
d.__unknownsuperprivate_variable = 123
assert d.__unknownsuperprivate_variable == 123
del d.__unknownsuperprivate_variable
with pytest.raises(AttributeError): # , message='should not be able to access __variable'
assert print(d.__unknownsuperprivate_variable)
@pytest.mark.parametrize("Klass", klasses)
def test_property_private_variable(Klass):
d = Klass()
assert d.private_variable == 'default_private'
d.private_variable = 123
assert d.private_variable == 123
del d.private_variable
assert d.private_variable == 'default_private'
@pytest.mark.parametrize("Klass", klasses)
def test_property_superprivate_variable(Klass):
d = Klass()
assert d.superprivate_variable == 'default_superprivate'
d.superprivate_variable = 123
assert d.superprivate_variable == 123
del d.superprivate_variable
assert d.superprivate_variable == 'default_superprivate'