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
|
import unittest
import unittest.mock
from mcstasscript.helper.mcstas_objects import DeclareVariable
class Test_DeclareVariable(unittest.TestCase):
"""
Tests the DeclareVariable class that holds a declared variable
that will be written to the McStas declare section.
"""
def test_DeclareVariable_init_basic_type(self):
"""
Initialization with a type
"""
var = DeclareVariable("double", "test")
self.assertEqual(var.name, "test")
self.assertEqual(var.type, "double")
def test_DeclareVariable_init_basic_type_value(self):
"""
Initialization with type and value
"""
var = DeclareVariable("double", "test", value=518)
self.assertEqual(var.name, "test")
self.assertEqual(var.type, "double")
self.assertEqual(var.value, 518)
def test_DeclareVariable_init_basic_type_vector(self):
"""
Initialization with type and value
"""
var = DeclareVariable("double", "test",
array=6, value=[1, 2.2, 3, 3.3, 4, 4.4])
self.assertEqual(var.name, "test")
self.assertEqual(var.type, "double") # space for easier writing
self.assertEqual(var.vector, 6)
self.assertEqual(var.value, [1, 2.2, 3, 3.3, 4, 4.4])
def test_DeclareVariable_init_basic_type_value_comment(self):
"""
Initialization with type, value and comment
"""
var = DeclareVariable("double", "test",
value=518, comment="test comment /")
self.assertEqual(var.name, "test")
self.assertEqual(var.type, "double") # Space for easier writing
self.assertEqual(var.value, 518)
self.assertEqual(var.comment, " // test comment /")
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_DeclareVariable_write_basic(self, mock_f):
"""
Testing that write to file is correct. Here a line is in a
instrument declare section. The write file operation is
mocked and check using a patch. Here a simple declare is
used.
"""
var = DeclareVariable("double", "test")
with mock_f('test.txt', 'w') as m_fo:
var.write_line(m_fo)
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_called_once_with("double test;")
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_DeclareVariable_write_complex_float(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument declare section. The write file operation is
mocked and check using a patch. Here a declare with a value
is used. (float value)
"""
var = DeclareVariable("double",
"test",
value=5.4,
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
var.write_line(m_fo)
expected_write = "double test = 5.4; // test comment"
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_called_once_with(expected_write)
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_DeclareVariable_write_complex_int(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument declare section. The write file operation is
mocked and check using a patch. Here a declare with a value
is used. (integer value)
"""
var = DeclareVariable("double",
"test",
value=5,
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
var.write_line(m_fo)
expected_write = "double test = 5; // test comment"
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_called_once_with(expected_write)
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_DeclareVariable_write_simple_array(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument declare section. The write file operation is
mocked and check using a patch. Here an array is declared.
"""
var = DeclareVariable("double",
"test",
array=29,
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
var.write_line(m_fo)
expected_write = "double test[29]; // test comment"
mock_f.assert_called_with('test.txt', 'w')
handle = mock_f()
handle.write.assert_called_once_with(expected_write)
@unittest.mock.patch('__main__.__builtins__.open',
new_callable=unittest.mock.mock_open)
def test_DeclareVariable_write_complex_array(self, mock_f):
"""
Testing that write to file is correct. Here a line is in an
instrument declare section. The write file operation is
mocked and check using a patch. Here an array is declared and
populated with the selected values.
"""
var = DeclareVariable("double",
"test",
array=3,
value=[5, 4, 3.1],
comment="test comment")
with mock_f('test.txt', 'w') as m_fo:
var.write_line(m_fo)
expected_writes = [unittest.mock.call("double test[3] = {"),
unittest.mock.call("5,"),
unittest.mock.call("4,"),
unittest.mock.call("3.1}; // test comment")]
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()
|