public void AdditiveDelegates() { SimpleDelegateTestObject source = new SimpleDelegateTestObject(); SimpleDelegateTestObject target = new SimpleDelegateTestObject(); SimpleDelegateTestObject neutral = new SimpleDelegateTestObject(); neutral.ListenTo(target); { // Does the neutral object receive events? target.FireEvent(); Assert.IsTrue(neutral.PopEventReceived()); source.DeepCopyTo(target); // Does it still receive them after copying the source onto it? target.FireEvent(); Assert.IsTrue(neutral.PopEventReceived()); } { source.ListenTo(source); // Does the source receive its own events? source.FireEvent(); Assert.IsTrue(source.PopEventReceived()); Assert.IsFalse(source.PopEventReceived()); source.DeepCopyTo(target); // Do both neutral object and target itself receive both target events? target.FireEvent(); Assert.IsTrue(neutral.PopEventReceived()); Assert.IsFalse(neutral.PopEventReceived()); Assert.IsTrue(target.PopEventReceived()); Assert.IsFalse(target.PopEventReceived()); Assert.IsFalse(source.PopEventReceived()); } }
[Test] public void StaticDelegates() { SimpleDelegateTestObject source = new SimpleDelegateTestObject(); SimpleDelegateTestObject target; bool staticEventReceived = false; source.SomeEvent += delegate (object sender, EventArgs e) { staticEventReceived = true; }; // Does the static event work as expected? source.FireEvent(); Assert.IsTrue(staticEventReceived); staticEventReceived = false; target = source.DeepClone(); // We expect static events to not be cloned due to conceptual ownership inversion in delegates target.FireEvent(); Assert.IsFalse(staticEventReceived); // The source should still trigger it though. source.FireEvent(); Assert.IsTrue(staticEventReceived); }
[Test] public void SimpleDelegates() { SimpleDelegateTestObject source = new SimpleDelegateTestObject(); SimpleDelegateTestObject target; source.ListenTo(source); // Does the event work as expected? source.FireEvent(); Assert.IsTrue(source.PopEventReceived()); target = source.DeepClone(); // Does the cloned event work the same? target.FireEvent(); Assert.IsFalse(source.PopEventReceived()); Assert.IsTrue(target.PopEventReceived()); }