def test_make_valid_connections(self): """ Makes and brakes some valid connections and tests if the values are passed correctly through the connections. """ self.obj1.SetValue(1) self.obj2.SetValue(2) sumpf.connect(self.obj1.GetValue, self.obj2.SetValue) self.assertEqual(self.obj2.GetValue(), 1) # by making the connection, the value should have been passed automatically self.obj1.SetValue(3) self.assertEqual(self.obj2.GetValue(), 3) # the value should have been passed through the connection automatically sumpf.disconnect(self.obj1.GetValue, self.obj2.SetValue) self.obj1.SetValue(4) self.assertEqual(self.obj2.GetValue(), 3) # after disconnection the passing should have stopped sumpf.connect(self.obj2.SetValue, self.obj1.GetValue) self.obj1.SetValue(5) self.assertEqual(self.obj2.GetValue(), 5) # changing the order of arguments in the connect-call should have worked aswell self.obj1.SetValueNoUpdate(6) self.assertEqual(self.obj2.GetValue(), 5) # a decorator with no automatic passing should be possible aswell sumpf.disconnect(self.obj2.SetValue, self.obj1.GetValue) self.obj1.SetValue(7) self.assertEqual(self.obj2.GetValue(), 5) # changing the order of arguments in the disconnect-call should have worked aswell sumpf.connect(self.obj1.GetValue, self.obj2.SetValue) sumpf.connect(self.obj1.GetText, self.obj2.SetText) self.obj1.ComputeValueAndText(8) self.assertEqual(self.obj2.GetValue(), 8) # ComputeValueAndText should have triggered both connections. GetValue... self.assertEqual(self.obj2.GetText(), "8") # ... and GetText self.obj2.triggered = False sumpf.connect(self.obj1.GetText, self.obj2.Trigger) # multiple connections from an output should be possible sumpf.connect(self.obj1.GetValue, self.obj2.Trigger) # multiple connections to a Trigger should be possible self.assertFalse(self.obj2.triggered) # Triggers should not be triggered on connection self.obj1.SetValue(9) self.assertTrue(self.obj2.triggered) # Triggers should work as well self.obj1.SetText(text="Hallo Welt") # Keyword arguments should be possible
def test_replacing_multi_input(self): """ Testing every aspect of a MultiInput is more complex, so it is done in separately in this and the next method. This method tests the MultiInput that replace updated data with a proper replacing method. This should maintain the order of the output data. """ id0 = self.obj2.AddItemReplace(0) id1 = self.obj2.AddItemReplace(1) self.assertEqual(self.obj2.GetItems(), [0, 1]) # adding items manually should work self.obj1.SetValue(2) sumpf.connect(self.obj1.GetValue, self.obj2.AddItemReplace) self.assertEqual(self.obj2.GetItems(), [0, 1, 2]) # adding items through connections should work self.obj1.SetValue(3) self.assertEqual(self.obj2.GetItems(), [0, 1, 3]) # a connection should only update its own value sumpf.connect(self.obj1.GetValue2, self.obj2.AddItemReplace) self.assertEqual(self.obj2.GetItems(), [0, 1, 3, 6]) # multiple connections must be possible sumpf.connect(self.obj2.GetItems, self.obj1.Trigger) self.obj1.triggered = False sumpf.disconnect(self.obj1.GetValue, self.obj2.AddItemReplace) self.assertTrue(self.obj1.triggered) # disconnecting should have triggered the Trigger self.assertEqual(self.obj2.GetItems(), [0, 1, 6]) # disconnecting should remove the item from the list self.obj2.RemoveItem(id1) self.assertEqual(self.obj2.GetItems(), [0, 6]) # removing items manually should work as well self.obj2.AddItemReplace(7) self.obj1.SetValue2(2) self.assertEqual(self.obj2.GetItems(), [0, 4, 7]) # the connection should still work, even after many changes
def DisconnectAll(self): """ Please do not use this method as it might be changed in future versions. Use the disconnect_all function instead. """ while len(self._connections) > 0: sumpf.disconnect(self, self._connections[0])
def ShowRecent(self, show=True): if show: if not self.__showrecent: self.__progress_indicator.AddMethod(self.__merge_utf.AddInput) sumpf.connect(self.__relabel.GetOutput, self.__merge_utf.AddInput) self.__showrecent = True else: if self.__showrecent and self.__kept_ids != []: self.__progress_indicator.AddMethod(self.__merge_utf.AddInput) sumpf.disconnect(self.__relabel.GetOutput, self.__merge_utf.AddInput) self.__showrecent = False
def test_make_invalid_connections(self): """ Tries to make and brake some invalid connections and tests if the mistakes are correctly detected. """ self.assertRaises(TypeError, sumpf.connect, *(self.obj1.GetText, self.obj2.SetValue2)) # connecting should fail if the types of the connectors are different self.assertRaises(TypeError, sumpf.connect, *(self.obj1.GetValue, self.obj2.GetValue)) # connecting should fail if both connectors are outputs self.assertRaises(TypeError, sumpf.connect, *(self.obj1.SetValue, self.obj2.SetValue)) # connecting should fail if both connectors are inputs self.assertRaises(ValueError, sumpf.connect, *(self.obj1.GetValue, self.obj1.SetValue)) # connecting should fail if the output is connected to an input that updates the output automatically, because that would cause an infinite loop sumpf.connect(self.obj1.GetValue, self.obj1.SetValueNoUpdate) self.assertRaises(ValueError, sumpf.connect, *(self.obj1.GetValue, self.obj1.SetValueNoUpdate)) # connecting should fail if the connection already exists self.assertRaises(ValueError, sumpf.connect, *(self.obj1.GetValue2, self.obj1.SetValueNoUpdate)) # connecting should fail if input is already connected self.assertRaises(ValueError, sumpf.disconnect, *(self.obj1.GetValue, self.obj2.SetValue)) # disconnecting should fail if connection does not exist sumpf.disconnect(self.obj1.GetValue, self.obj1.SetValueNoUpdate) self.assertRaises(ValueError, sumpf.disconnect, *(self.obj1.GetValue, self.obj1.SetValueNoUpdate)) # disconnecting should fail if connection does not exist. Even if it has existed before
def test_order(self): """ Tests if connections are updated in the correct order. """ sumpf.connect(self.obj1.GetValue, self.obj2.SetValue) sumpf.connect(self.obj1.GetValue, self.obj2.SetValue2) sumpf.connect(self.obj1.GetValue, self.obj2.SetValueNoUpdate) self.obj2.order = [] self.obj1.SetValue(1) self.assertEqual(self.obj2.order, ["SetValue", "SetValue2", "SetValueNoUpdate"]) # All items should appear in the correct order sumpf.disconnect(self.obj1.GetValue, self.obj2.SetValue2) sumpf.connect(self.obj1.GetValue, self.obj2.SetValue2) self.obj2.order = [] self.obj1.SetValue(2) self.assertEqual(self.obj2.order, ["SetValue", "SetValueNoUpdate", "SetValue2"]) # All items should appear in the correct order
def test_connections(self): """ Tests if the connections work """ tester1 = ConnectionTester() tester2 = ConnectionTester() sumpf.connect(self.merger.GetOutput, tester1.Trigger) sumpf.connect(tester1.GetSpectrum, self.merger.AddInput) self.assertTrue(tester1.triggered) # connecting to input should work and it should trigger the output self.assertEqual(len(self.merger.GetOutput().GetChannels()), 2) # after adding one connection there should be the two channels from result of the connected output in the output spectrum tester1.SetSamplingRate(22050) self.assertEqual(self.merger.GetOutput().GetResolution(), 1.0 / 22050) # changing the resolution of the only spectrum in the merger should be possible tester1.SetSamplingRate(48000) sumpf.connect(tester2.GetSpectrum, self.merger.AddInput) self.assertEqual(len(self.merger.GetOutput().GetChannels()), 4) # after adding a second connection there should be four channels in the output spectrum tester1.ChangeChannels() self.assertEqual(self.merger.GetOutput().GetChannels()[0:2], tester1.GetSpectrum().GetChannels()) # the order of the channels should remain, even if the output of tester1 has changed tester1.triggered = False sumpf.disconnect(tester1.GetSpectrum, self.merger.AddInput) self.assertTrue(tester1.triggered) # disconnecting from input should should trigger the output as well self.assertEqual(len(self.merger.GetOutput().GetChannels()), 2) # after removing one connection there should be one channel left in the output spectrum
def test_connector_types(self): """ Tests around the typing of connectors """ # single data_type sumpf.connect(self.obj1.GetValue, self.obj2.SetValue) # this Input shall be connectable to integers ... sumpf.disconnect(self.obj1.GetValue, self.obj2.SetValue) self.assertRaises(TypeError, sumpf.connect, *(self.obj1.GetFloat, self.obj2.SetValue)) # ... and only to integers # an explicit list of multiple data_types sumpf.connect(self.obj1.GetFloat, self.obj2.SetValue2) # this Input shall be connectable to both floats... sumpf.disconnect(self.obj1.GetFloat, self.obj2.SetValue2) sumpf.connect(self.obj1.GetValue, self.obj2.SetValue2) # ... and integers. sumpf.disconnect(self.obj1.GetValue, self.obj2.SetValue2) # arbitrary data_types sumpf.connect(self.obj1.GetText, self.obj2.SetArbitrary) # this Input shall be connectable to Outputs with any data type sumpf.disconnect(self.obj1.GetText, self.obj2.SetArbitrary) sumpf.connect(self.obj1.GetFloat, self.obj2.SetArbitrary) sumpf.disconnect(self.obj1.GetFloat, self.obj2.SetArbitrary) sumpf.connect(self.obj1.GetArbitrary, self.obj2.SetValue) # this Output shall be connectable to Inputs with any data type sumpf.disconnect(self.obj1.GetArbitrary, self.obj2.SetValue) sumpf.connect(self.obj1.GetArbitrary, self.obj2.SetText) sumpf.disconnect(self.obj1.GetArbitrary, self.obj2.SetText) sumpf.connect(self.obj1.GetArbitrary, self.obj2.AddItemReplace) sumpf.disconnect(self.obj1.GetArbitrary, self.obj2.AddItemReplace)