def test_substitute_params(self): section = Section("f", 1, "name", { "name": "$(name):pos", "exposure": 1.0 }) params = {"name": "me"} param_dict = section.substitute_params(params) expected = {"name": "me:pos", "exposure": 1.0} assert param_dict == expected
def test_instantiate(self): @method_takes("desc", StringMeta("description"), REQUIRED, "foo", StringMeta("optional thing"), "thing") def f(extra, params): return extra, 2, params.desc, params.foo ca = Mock(CAPart=f) parts = Mock(ca=ca) section = Section("ca.CAPart", dict(desc="my name")) result = section.instantiate({}, parts, "extra") self.assertEqual(result, ("extra", 2, "my name", "thing"))
def test_instantiate(self, mock_import): @add_call_types def f(desc: ADesc, foo: AThing = "thing") -> Any: return 2, desc, foo mock_import.return_value = Mock(MyPart=f) section = Section("f", 1, "mymodule.parts.MyPart", dict(desc="my name")) result = section.instantiate({}) mock_import.assert_called_once_with("malcolm.modules.mymodule.parts") assert result == (2, "my name", "thing")
def test_instantiate(self, mock_import): @method_takes("desc", StringMeta("description"), REQUIRED, "foo", StringMeta("optional thing"), "thing") def f(extra, params): return extra, 2, params.desc, params.foo mock_import.return_value = Mock(MyPart=f) section = Section("f", 1, "mymodule.parts.MyPart", dict(desc="my name")) result = section.instantiate({}, "extra") mock_import.assert_called_once_with("malcolm.modules.mymodule.parts") assert result == ("extra", 2, "my name", "thing")
def test_split_into_sections(self): filename = "/tmp/yamltest.yaml" with open(filename, "w") as f: f.write(""" - builtin.parameters.string: name: something - builtin.defines.docstring: value: My special docstring - builtin.controllers.ManagerController: mri: m """) sections = Section.from_yaml(filename) assert sections == (dict(blocks=[], defines=[ANY], parameters=[ANY], controllers=[ANY], parts=[], includes=[]), "yamltest", "My special docstring") assert sections[0]["parameters"][0].name == "builtin.parameters.string" assert sections[0]["parameters"][0].param_dict == dict( name="something") assert sections[0]["controllers"][ 0].name == "builtin.controllers.ManagerController" assert sections[0]["controllers"][0].param_dict == dict(mri="m") assert sections[0]["defines"][0].name == "builtin.defines.docstring" assert sections[0]["defines"][0].param_dict == dict( value="My special docstring")
def test_split_into_sections(self): text = """ - parameters.string: name: something - controllers.ManagerController: """ sections = Section.from_yaml(text) self.assertEqual( sections, dict(blocks=[], parameters=[ANY], controllers=[ANY], parts=[], includes=[], comms=[])) self.assertEqual(sections["parameters"][0].name, "string") self.assertEqual(sections["parameters"][0].param_dict, {"name": "something"}) self.assertEqual(sections["controllers"][0].name, "ManagerController") self.assertEqual(sections["controllers"][0].param_dict, {})
def test_repr(self): s = Section("f", 1, "ca.CADoublePart", {"name": "me"}) expected = "Section(ca.CADoublePart, {'name': 'me'})" assert repr(s) == expected
def test_repr(self): s = Section("ca.CADoublePart", {"name": "me"}) expected = "Section(ca.CADoublePart, {'name': 'me'})" self.assertEqual(repr(s), expected)
def test_substitute_params(self): section = Section("name", {"name": "$(name):pos", "exposure": 1.0}) params = {"name": "me"} param_dict = section.substitute_params(params) expected = {"name": "me:pos", "exposure": 1.0} self.assertEqual(param_dict, expected)