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
|
import unittest
import unittest.mock
from mcstasscript.helper.mcstas_objects import provide_parameter
from mcstasscript.helper.mcstas_objects import write_parameter
class Test_ParameterVariable(unittest.TestCase):
"""
Tests the ParameterVariable class that holds an input parameter
for the instrument.
"""
def test_ParameterVariable_init_basic(self):
"""
Smallest possible initialization
"""
par = provide_parameter("test")
self.assertEqual(par.name, "test")
def test_ParameterVariable_init_basic_type(self):
"""
Initialization with a type
"""
par = provide_parameter("double", "test")
self.assertEqual(par.name, "test")
self.assertEqual(par.type, "double")
def test_ParameterVariable_init_basic_type_value(self):
"""
Initialization with type and value
"""
par = provide_parameter("double", "test", value=518)
self.assertEqual(par.name, "test")
self.assertEqual(par.type, "double")
self.assertEqual(par.value, 518)
def test_ParameterVariable_init_basic_type_value_comment(self):
"""
Initialization with type, value and comment
"""
par = provide_parameter("double", "test", value=518,
comment="test comment /")
self.assertEqual(par.name, "test")
self.assertEqual(par.type, "double")
self.assertEqual(par.value, 518)
self.assertEqual(par.comment, "test comment /")
def test_ParameterVariable_init_basic_value_comment(self):
"""
Initialization with value and comment
"""
par = provide_parameter("test", value=518,
comment="test comment /")
self.assertEqual(par.name, "test")
self.assertEqual(par.type, "")
self.assertEqual(par.value, 518)
self.assertEqual(par.comment, "test comment /")
def test_ParameterVariable_init_options_initialize(self):
"""
Initialization with value and comment
"""
par = provide_parameter("test", value=2,
options=[1, 2, 3.1])
self.assertEqual(par.name, "test")
self.assertEqual(par.type, "")
self.assertEqual(par.value, 2)
self.assertEqual(par.get_options(), [1, 2, 3.1])
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_ParameterVariable_write_basic(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument parameter section. The write file operation is
mocked and check using a patch. Here a simple parameter is
used.
"""
par = provide_parameter("double", "test")
with mock_f('test.txt', 'w') as m_fo:
write_parameter(m_fo, parameter=par, stop_character="")
expected_writes = [unittest.mock.call("double test"),
unittest.mock.call(""),
unittest.mock.call(""),
unittest.mock.call("\n")]
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_has_calls(expected_writes, any_order=False)
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_ParameterVariable_write_complex_float(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument parameter section. The write file operation is
mocked and check using a patch. Here a parameter with a value
is used. (float value)
"""
par = provide_parameter("double", "test", value=5.4,
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
write_parameter(m_fo, parameter=par, stop_character=")")
expected_writes = [unittest.mock.call("double test"),
unittest.mock.call(" = 5.4"),
unittest.mock.call(")"),
unittest.mock.call("// test comment"),
unittest.mock.call("\n")]
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_has_calls(expected_writes, any_order=False)
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_ParameterVariable_write_complex_int(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument parameter section. The write file operation is
mocked and check using a patch. Here a parameter with a value
is used. (integer value)
"""
par = provide_parameter("double", "test", value=5,
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
write_parameter(m_fo, parameter=par, stop_character=")")
expected_writes = [unittest.mock.call("double test"),
unittest.mock.call(" = 5"),
unittest.mock.call(")"),
unittest.mock.call("// test comment"),
unittest.mock.call("\n")]
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_has_calls(expected_writes, any_order=False)
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_ParameterVariable_write_complex_string(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument parameter section. The write file operation is
mocked and check using a patch. Here a parameter with a value
is used. (string value)
"""
par = provide_parameter("double", "test", value="\"Al\"",
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
write_parameter(m_fo, parameter=par, stop_character=",")
expected_writes = [unittest.mock.call("double test"),
unittest.mock.call(" = \"Al\""),
unittest.mock.call(","),
unittest.mock.call("// test comment"),
unittest.mock.call("\n")]
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_has_calls(expected_writes, any_order=False)
if __name__ == '__main__':
unittest.main()
|