def test_put_changes_value(self): b = Block() b.on_changed = Mock(wraps=b.on_changed) c = CounterController(Mock(), b, 'block') b.on_changed.reset_mock() c.counter.put(32) self.assertEqual(c.counter.value, 32) c.block.on_changed.assert_called_once_with([["counter", "value"], 32], True)
def test_add_attribute(self): b = Block() b.name = 'block' b.on_changed = MagicMock(side_effect=b.on_changed) attr = MagicMock() b.add_attribute("attr", attr) attr.set_parent.assert_called_once_with(b, "attr") self.assertEqual({"attr":attr}, b.attributes) self.assertIs(attr, b.attr) b.on_changed.assert_called_with( [[attr.name], attr.to_dict.return_value], True)
def test_add_method_registers(self): b = Block() b.on_changed = MagicMock(side_effect=b.on_changed) m = MagicMock() b.add_method("mymethod", m) self.assertEqual(list(b.methods), ["mymethod"]) self.assertFalse(m.called) b.on_changed.assert_called_with([[m.name], m.to_dict.return_value], True) m.return_value = 42 self.assertEqual(b.mymethod(), 42) m.assert_called_once_with()
def test_add_attribute(self): b = Block() b.name = 'block' b.on_changed = MagicMock(side_effect=b.on_changed) attr = MagicMock() b.add_attribute("attr", attr) attr.set_parent.assert_called_once_with(b, "attr") self.assertEqual({"attr": attr}, b.attributes) self.assertIs(attr, b.attr) b.on_changed.assert_called_with( [[attr.name], attr.to_dict.return_value], True)
def test_replace_children(self): b = Block() b.name = "blockname" b.methods["m1"] = 2 b.attributes["a1"] = 3 setattr(b, "m1", 2) setattr(b, "a1", 3) attr_meta = StringMeta(description="desc") attr = Attribute(attr_meta) b.add_attribute('attr', attr) method = Method(description="desc") b.add_method('method', method) b.on_changed = MagicMock(wrap=b.on_changed) b.replace_children({'attr': attr, 'method': method}) self.assertEqual(b.attributes, dict(attr=attr)) self.assertEqual(b.methods, dict(method=method)) b.on_changed.assert_called_once_with([[], b.to_dict()], True) self.assertFalse(hasattr(b, "m1")) self.assertFalse(hasattr(b, "a1"))
def test_replace_children(self): b = Block() b.name = "blockname" b.methods["m1"] = 2 b.attributes["a1"] = 3 setattr(b, "m1", 2) setattr(b, "a1", 3) attr_meta = StringMeta(description="desc") attr = Attribute(attr_meta) b.add_attribute('attr', attr) method = Method(description="desc") b.add_method('method', method) b.on_changed = MagicMock(wrap=b.on_changed) b.replace_children({'attr':attr, 'method':method}) self.assertEqual(b.attributes, dict(attr=attr)) self.assertEqual(b.methods, dict(method=method)) b.on_changed.assert_called_once_with( [[], b.to_dict()], True) self.assertFalse(hasattr(b, "m1")) self.assertFalse(hasattr(b, "a1"))