def test_bad_output(self): """ output consumer is not callable, does not have value property """ controller = Controller() try: controller.wire_output(Operand(1.23), "Fred") self.fail("expected exception") except RuntimeError: pass
def test_simple(self): controller = Controller() a = CustomPollableSupplier() controller.poll_input(a) self.assertFalse(a.called) controller.update() self.assertTrue(a.called)
def test_poll_input_no_poll(self): """ input supplier does not have poll property """ controller = Controller() try: controller.poll_input(BadSupplierNotCallable()) controller.update() self.fail("expected exception") except RuntimeError: pass
def test_poll_input_not_callable(self): """ input supplier has poll property but not callable """ controller = Controller() try: controller.poll_input(BadSupplierNotCallable()) controller.update() self.fail("expected exception") except RuntimeError: pass
def test_poll_input_poll_error(self): """ input supplier poll function throws error """ controller = Controller() try: controller.poll_input(BadSupplierPollError()) controller.update() self.fail("expected exception") except RuntimeError: self.fail("Did not expect RuntimeError") except TypeError: pass
def test_end_to_end(self): global function_output_value controller = Controller() vi = CustomValueSupplier() fi = good_input_function op1 = kabuki.node_from_object(vi) op2 = kabuki.node_from_function(fi) cvo = CustomValueConsumer() controller.wire_output(op1, cvo) controller.wire_output(op2, output_setter_function) # operators calculate values right away self.assertEqual(3.5, op1.value, "should be default value of CustomValueInput") self.assertEqual("yay!", op2.value, "should be value returned by good_input_function") # outputs need call to update() first self.assertEqual( None, function_output_value, "should be initial value because update() hasn't been called yet") self.assertEqual( None, cvo.value, "should be initial value because update() hasn't been called yet") # we need to call update to move values to outputs controller.update() self.assertEqual(3.5, cvo.value, "should be default value of CustomValueInput") self.assertEqual("yay!", function_output_value, "should be value returned by good_input_function")
def test_simple(self): controller = Controller() n1 = Operand(value=2) n2 = Operand(value=4) n3 = n1.add(n2) out = CustomValueConsumer() controller.wire_output(n3, out) controller.update() self.assertEqual(6, out.value) n1._value = 3 self.assertEqual(6, out.value) controller.update() self.assertEqual(7, out.value)
def test_end_to_end(self): global function_output_value controller = Controller() vi = CustomValueSupplier() fi = good_input_function op1 = kabuki.node_from_object(vi) op2 = kabuki.node_from_function(fi) cvo = CustomValueConsumer() controller.wire_output(op1, cvo) controller.wire_output(op2, output_setter_function) # operators calculate values right away self.assertEqual(3.5, op1.value, "should be default value of CustomValueInput") self.assertEqual("yay!", op2.value, "should be value returned by good_input_function") # outputs need call to update() first self.assertEqual(None, function_output_value, "should be initial value because update() hasn't been called yet") self.assertEqual(None, cvo.value, "should be initial value because update() hasn't been called yet") # we need to call update to move values to outputs controller.update() self.assertEqual(3.5, cvo.value, "should be default value of CustomValueInput") self.assertEqual("yay!", function_output_value, "should be value returned by good_input_function")
from kabuki.controller import Controller, FunctionInput, ValueInput from kabuki.operators import Operand """ Provide a default controller and façade methods. """ _default_controller = Controller() def node_from_function(supplier): """ Create a node with a value supplied by a function. :param supplier: A function that returns a value. :return: A node that can be operated on. """ return FunctionInput(supplier) def node_from_object(supplier): """ create a node with a value supplied from an object :param supplier: an object with a value property :return: a node that can be operated on """ return ValueInput(supplier) def node_from_value(value): """ create a node from a literal value :param value: a literal value :return: a node that can be operated on """ return Operand(value=value)