#region Apache License 2.0 /* Nuclex .NET Framework Copyright (C) 2002-2024 Markus Ewald / Nuclex Development Labs Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #endregion // Apache License 2.0 using System; using System.ComponentModel; using NUnit.Framework; namespace Nuclex.Support { /// Unit tests for the property change event argument helper [TestFixture] internal class PropertyChangedEventArgsHelperTest { #region class TestViewModel /// Example class on which unit test generates change notifications public class TestViewModel { /// Example property that will be reported to have changed public int SomeProperty { get; set; } } #endregion // class TestViewModel /// /// Verifies that a property change notification matching the property /// passed to the AreAffecting() method is recognized /// [Test] public void MatchingPropertyChangeNotificationIsRecognized() { var arguments = new PropertyChangedEventArgs("SomeProperty"); #pragma warning disable 0618 Assert.IsTrue(arguments.AreAffecting(() => ViewModel.SomeProperty)); #pragma warning restore 0618 Assert.IsTrue(arguments.AreAffecting("SomeProperty")); } /// /// Ensures that a mismatching property change notification will /// not report the property as being affected. /// [Test] public void MismatchingPropertyIsReportedAsUnaffected() { var arguments = new PropertyChangedEventArgs("AnotherProperty"); #pragma warning disable 0618 Assert.IsFalse(arguments.AreAffecting(() => ViewModel.SomeProperty)); #pragma warning restore 0618 Assert.IsFalse(arguments.AreAffecting("SomeProperty")); } /// /// Verifies that any specific property is reported as being affected /// when the property change notification is a null wildcard /// [Test] public void SpecificPropertyIsAffectedByNullWildcard() { var nullArguments = new PropertyChangedEventArgs(null); #pragma warning disable 0618 Assert.IsTrue(nullArguments.AreAffecting(() => ViewModel.SomeProperty)); #pragma warning restore 0618 Assert.IsTrue(nullArguments.AreAffecting("SomeProperty")); } /// /// Verifies that any specific property is reported as being affected /// when the property change notification is an empty wildcard /// [Test] public void SpecificPropertyIsAffectedByEmptyWildcard() { var emptyArguments = new PropertyChangedEventArgs(string.Empty); #pragma warning disable 0618 Assert.IsTrue(emptyArguments.AreAffecting(() => ViewModel.SomeProperty)); #pragma warning disable 0618 Assert.IsTrue(emptyArguments.AreAffecting("SomeProperty")); } /// /// Tests whether the helper can recognize a wildcard property change /// notification using null as the wildcard. /// [Test] public void NullWildcardIsRecognized() { var nullArguments = new PropertyChangedEventArgs(null); Assert.IsTrue(nullArguments.AffectAllProperties()); } /// /// Tests whether the helper can recognize a wildcard property change /// notification using an empty string as the wildcard. /// [Test] public void EmptyWildcardIsRecognized() { var emptyArguments = new PropertyChangedEventArgs(string.Empty); Assert.IsTrue(emptyArguments.AffectAllProperties()); } /// Helper used to construct lambda expressions protected static TestViewModel ViewModel { get; set; } } } // namespace Nuclex.Support