using System; using System.ComponentModel; using NUnit.Framework; namespace Developpez.Dotnet.Tests { [TestFixture] public class EventExtensionsTests { [Test] public void Test_Raise_PropertyChanged() { string changedProperty = null; TestNotifier t = new TestNotifier(); t.PropertyChanged += (sender, e) => changedProperty = e.PropertyName; t.Foo = "Hello world"; Assert.AreEqual("Foo", changedProperty); t.Bar = "Hello world"; Assert.AreEqual("Bar", changedProperty); } [Test] public void Test_Raise() { bool fooRaised = true; string expectedBaz = "Hello world !"; string actualBaz = null; TestEvents t = new TestEvents(); t.Foo += (sender, e) => fooRaised = true; t.Bar += (sender, e) => actualBaz = e.Baz; t.RaiseFoo(); t.RaiseBar(expectedBaz); Assert.IsTrue(fooRaised); Assert.AreEqual(expectedBaz, actualBaz); } #region Classes de test pour Raise class TestNotifier : INotifyPropertyChanged { private string _foo = null; public string Foo { get { return _foo; } set { if (value != _foo) { _foo = value; PropertyChanged.Raise(this, "Foo"); } } } private string _bar; public string Bar { get { return _bar; } set { if (value != _bar) { _bar = value; PropertyChanged.Raise(this, () => Bar); } } } public event PropertyChangedEventHandler PropertyChanged; } class TestEvents { public event EventHandler Foo; public event EventHandler Bar; public class BarEventArgs : EventArgs { public string Baz { get; set; } } public void RaiseFoo() { Foo.Raise(this); } public void RaiseBar(string baz) { Bar.Raise(this, new BarEventArgs { Baz = baz }); } } #endregion } }