/// <summary> /// Creates an <see cref="Assertion" /> that ensure object properties/fields of the specified instance did not change, except the specified ones, /// in the Act part of your test. /// </summary> /// <typeparam name="T">The <see cref="Type"/> of the expression that can change.</typeparam> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="expression">The expression of an instance and a property/field.</param> /// <param name="kind">The kind of members and visibility to check for changes. Default value is <see cref="NotChangedKind.PublicProperties"/>.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="SmartTestException"> /// If any property/field checked (see <paramref name="kind" />) of the instance in <paramref name="expression"/>, except the /// property/field in <paramref name="expression" /> has changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// Nothing special. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If a property/field of the instance in <paramref name="expression"/> changed during the Act (except the property/field in <paramref name="expression" />), a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no property nor field of <c>other</c>, except <c>CopyCount</c>, /// changed while calling <c>CopyPropertiesFrom</c>. /// <code> /// [Test] /// public void CopyPropertiesFromTest() /// { /// var mc = new MyClass(); /// var other = new MyClass(); /// /// RunTest( ValidValue.IsValid, /// () => mc.CopyPropertiesFrom( other ), /// SmartAssert.NotChangedExcept( () => other.CopyCount, NotChangedKind.All ) ); /// } /// </code> /// </example> public static Assertion NotChangedExcept <T>(this SmartAssertPlaceHolder @this, Expression <Func <T> > expression, NotChangedKind kind = NotChangedKind.PublicProperties) { object instance; PropertyInfo property; GetInstanceAndProperty(expression, out instance, out property); return(new NotChangedAssertion(instance, kind, new[] { property.Name })); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an asynchronous call from the Act is done before continuing Smart /// Assertions validations. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="evt">The <see cref="WaitHandle" /> to wait for before continuing Smart Assertions validations.</param> /// <param name="timeout">The maximum milliseconds to wait for the handle to be set.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> If <paramref name="evt" /> is <c>null</c>.</exception> /// <exception cref="ArgumentOutOfRangeException"> If <paramref name="timeout" /> is less than or equal to <c>0</c>.</exception> /// <exception cref="SmartTestException"> /// If the <paramref name="evt" /> is not set within <paramref name="timeout" />. /// </exception> /// <exception cref="BadTestException"> /// If you call <see cref="SetHandle" />, as you provided a <paramref name="evt" /> to /// wait for. /// </exception> /// <example> /// <para> In this example, the Smart Assertion verifies that the <c>MyMethod</c> call launches a callback within 1s.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// var handle = new ManualResetEvent( false ); /// /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod( () => handle.set() ), /// SmartAssert.Wait( handle, TimeSpan.FromSeconds( 1 ) ) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Wait(this SmartAssertPlaceHolder _, WaitHandle evt, TimeSpan timeout) { if (evt == null) { throw new ArgumentNullException(nameof(evt)); } return(new WaitAssertion(evt, timeout)); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer did not change in the Act /// part of your test. /// </summary> /// <typeparam name="T">The type of the property/indexer that should not change.</typeparam> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which the property/indexer should change.</param> /// <param name="propertyNames">The names of the property/indexer that should not change.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="instance" /> is <c>null</c>. /// </exception> /// <exception cref="SmartTestException"> /// <para> /// If the <see cref="INotifyPropertyChanged" /> event is raised for the specified /// <paramref name="propertyNames" />. /// </para> /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Nothing special.</para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is not raised for any of /// the specified <paramref name="propertyNames" />. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para>If the event is raised, a <see cref="SmartTestException" /> is thrown.</para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is not raised for <c>MyProperty</c> nor <c>OtherProperty</c>, in any order. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotRaised_PropertyChanged( mc, nameof(MyClass.MyProperty), nameof(MyClass.OtherProperty) ) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotRaised_PropertyChanged <T>(this SmartAssertPlaceHolder _, T instance, params string[] propertyNames) where T : INotifyPropertyChanged { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } return(new RaisePropertyChangedAssertion(instance, false, propertyNames)); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer changed to a different value in the Act /// part of your test. /// </summary> /// <typeparam name="T">The type of the property/indexer that should change.</typeparam> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="expression">The expression that will change, directly or indirectly, the property/indexer.</param> /// <param name="expectedValue">The expected value of the property/indexer once the Act part is done.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// <para>If <paramref name="expression" /> is not a <see cref="MemberExpression" /> with a property.</para> /// <para> /// If the involved instance of <paramref name="expression" /> is not an <see cref="INotifyPropertyChanged" /> /// instance. /// </para> /// <para>If the member is not a Property.</para> /// <para>If the value of the <paramref name="expression" /> is <paramref name="expectedValue" /> before the Act.</para> /// </exception> /// <exception cref="SmartTestException"> /// <para>If the <see cref="INotifyPropertyChanged" /> event is not raised.</para> /// If <see cref="PropertyChangedEventArgs.PropertyName" /> is not the name of the property/indexer in the Act. /// <para> /// If the current value of the property is not <paramref name="expectedValue" /> in the event handler and after /// the Act. /// </para> /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The value of the property/indexer IS NOT <paramref name="expectedValue" />; otherwise a /// <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is raised. /// </para> /// <para> /// The <see cref="PropertyChangedEventArgs.PropertyName" /> is the one in the Act; /// otherwise a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer in the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// handler is <paramref name="expectedValue" />; otherwise a <see cref="SmartTestException" /> is /// thrown /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer is <paramref name="expectedValue" />; otherwise a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// <para> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is raised for the property <c>MyProperty</c> and the value is <c>10</c>. /// </para> /// <para>It also ensures that the value is still <c>10</c> after the Act.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Raised_PropertyChanged( () => mc.MyProperty, 10 ) ); /// }</code> /// </example> public static Assertion Raised_PropertyChanged <T>(this SmartAssertPlaceHolder @this, Expression <Func <T> > expression, T expectedValue) { INotifyPropertyChanged sender; PropertyInfo property; GetInstanceAndProperty(expression, out sender, out property); return(new RaisePropertyChangedAssertion(sender, true, property.Name, expectedValue)); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer did not change in the Act /// part of your test. /// </summary> /// <typeparam name="T">The type of the property/indexer that should not change.</typeparam> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="expression">The expression involving an instance an a property/indexer that should not change.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="expression" /> is <c>null</c>. /// </exception> /// <exception cref="SmartTestException"> /// If the <see cref="INotifyPropertyChanged" /> event is raised for the property from <paramref name="expression" />. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Nothing special.</para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is not raised for /// the specified property in <paramref name="expression" />. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para>If the event is raised, a <see cref="SmartTestException" /> is thrown.</para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is not raised for <c>MyProperty</c>. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotRaised_PropertyChanged( () => mc.MyProperty ) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotRaised_PropertyChanged <T>(this SmartAssertPlaceHolder _, Expression <Func <T> > expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } GetInstanceAndProperty(expression, out var sender, out var property); return(new RaisePropertyChangedAssertion(sender, false, new[] { property.Name })); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer changed to a different value in the Act /// part of your test. /// </summary> /// <typeparam name="T">The type of the property/indexer that should change.</typeparam> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which the property/indexer should change.</param> /// <param name="propertyName">The name of the property/indexer that should change.</param> /// <param name="expectedValue">The expected value of the property/indexer once the Act part is done.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="instance" /> or <paramref name="propertyName" /> are <c>null</c>. /// </exception> /// <exception cref="BadTestException"> /// <para>If the value of the property is <paramref name="expectedValue" /> before the Act.</para> /// </exception> /// <exception cref="SmartTestException"> /// <para>If the <see cref="INotifyPropertyChanged" /> event is not raised.</para> /// <para> /// If <see cref="PropertyChangedEventArgs.PropertyName" /> is not <paramref name="propertyName" /> in the event /// handler /// </para> /// <para> /// If the current value of the property is not <paramref name="expectedValue" /> in the event handler and after /// the Act. /// </para> /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The value of the property/indexer IS NOT <paramref name="expectedValue" />; otherwise a /// <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is raised. /// </para> /// <para> /// The <see cref="PropertyChangedEventArgs.PropertyName" /> is <paramref name="propertyName" />; /// otherwise a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer in the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// handler is <paramref name="expectedValue" />; otherwise a <see cref="SmartTestException" /> is /// thrown /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer is <paramref name="expectedValue" />; otherwise a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// <para> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is raised for the property <c>MyProperty</c> and the value is <c>10</c>. /// </para> /// <para>It also ensures that the value is still <c>10</c> after the Act.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Raised_PropertyChanged( mc, nameof(MyClass.MyProperty), 10 ) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Raised_PropertyChanged <T>(this SmartAssertPlaceHolder _, T instance, string propertyName, object expectedValue) where T : INotifyPropertyChanged { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (propertyName == null) { throw new ArgumentNullException(nameof(propertyName)); } return(new RaisePropertyChangedAssertion(instance, true, propertyName, expectedValue)); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure the Act is done within a specific duration. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="maximumDuration">The maximum allowed duration of the Act</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="SmartTestException"> /// If the Act part of your test is longer than <paramref name="maximumDuration" /> ms. /// </exception> /// <remarks>For this assertion to be accurate, you have to put it as the first Smart Assertion.</remarks> /// <example> /// <para> In this example, the Smart Assertion verifies that the <c>MyMethod</c> call takes no longer than 10ms.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Within( TimeSpan.FromMilliseconds( 10 ) ) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Within(this SmartAssertPlaceHolder _, TimeSpan maximumDuration) => new WithinAssertion((long)maximumDuration.TotalMilliseconds);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure the Act is done within a specific duration. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="maximumDuration">The maximum allowed duration of the Act.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="SmartTestException"> /// If the Act part of your test is longer than <paramref name="maximumDuration" /> ms. /// </exception> /// <remarks>For this assertion to be accurate, you have to put it as the first Smart Assertion.</remarks> /// <example> /// <para> In this example, the Smart Assertion verifies that the <c>MyMethod</c> call takes no longer than 10ms.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Within(10) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Within(this SmartAssertPlaceHolder _, long maximumDuration) => new WithinAssertion(maximumDuration);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an asynchronous call from the Act is done before continuing Smart /// Assertions validations. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="timeout">The maximum milliseconds to wait for the handle to be set.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentOutOfRangeException"> If <paramref name="timeout" /> is less than or equal to <c>0</c>.</exception> /// <example> /// <para> /// In this example, the <c>WaitContextHandle</c> wait for the implicit wait handle set by /// <see cref="SetHandle" />. /// </para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(300); /// /// RunTest( AnyValue.IsValid, /// ctx => mc.Method( ctx.SetHandle ), /// SmartAssert.Within( 100 ), /// SmartAssert.WaitContextHandle( TimeSpan.FromSeconds( 1 ) ) ); /// /// Assert.IsTrue( mc.Done ); // The method runs the parallel code (ctx.SetHandle) to its end. /// Assert.IsNull( mc.Exception ); // There was no exception in the parallel code thread. /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion WaitContextHandle(this SmartAssertPlaceHolder _, TimeSpan timeout) => new WaitAssertion(null, timeout);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure object public properties of the specified instance did not change, except the specified ones, /// in the Act part of your test. /// </summary> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which to test public property changes.</param> /// <param name="exceptions">The names of the public properties that can change during the Act.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// If <paramref name="exceptions" /> are not public properties of <paramref name="instance"/>. /// </exception> /// <exception cref="SmartTestException"> /// If any public property of <paramref name="instance"/>, except the <paramref name="exceptions" /> have changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// If names in <paramref name="exceptions" /> are not public properties of <paramref name="instance"/>, a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If a public property of <paramref name="instance"/> changed during the Act (except <paramref name="exceptions" />), a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no public property of <c>other</c>, except <c>CopyCount</c>, /// changed while calling <c>CopyPropertiesFrom</c>. /// <code> /// [Test] /// public void CopyPropertiesFromTest() /// { /// var mc = new MyClass(); /// var other = new MyClass(); /// /// RunTest( ValidValue.IsValid, /// () => mc.CopyPropertiesFrom( other ), /// SmartAssert.NotChangedExcept( other, "CopyCount" ) ); /// } /// </code> /// </example> public static Assertion NotChangedExcept(this SmartAssertPlaceHolder @this, object instance, params string[] exceptions) => new NotChangedAssertion(instance, NotChangedKind.PublicProperties, exceptions);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an asynchronous call from the Act is done before continuing Smart /// Assertions validations. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="timeout">The maximum milliseconds to wait for the handle to be set.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentOutOfRangeException"> If <paramref name="timeout" /> is less than or equal to <c>0</c>.</exception> /// <example> /// <para> /// In this example, the <c>WaitContextHandle</c> wait for the implicit wait handle set by /// <see cref="SetHandle" />. /// </para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(300); /// /// RunTest( AnyValue.IsValid, /// ctx => mc.Method( ctx.SetHandle ), /// SmartAssert.Within( 100 ), /// SmartAssert.WaitContextHandle( 1000 ) ); /// /// Assert.IsTrue( mc.Done ); // The method runs the parallel code (ctx.SetHandle) to its end. /// Assert.IsNull( mc.Exception ); // There was no exception in the parallel code thread. /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion WaitContextHandle(this SmartAssertPlaceHolder _, double timeout) => new WaitAssertion(null, TimeSpan.FromMilliseconds(timeout));
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an standard event is raised in the Act part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="expectedEventName">The name of the event that should raise.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="expectedEventName" /> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="expectedEventName" /> is empty. /// </exception> /// <exception cref="BadTestException"> /// If the <paramref name="expectedEventName" /> is not a valid event of the type of the instance involved in the Act. /// </exception> /// <exception cref="SmartTestException"> /// If the <paramref name="expectedEventName" /> event is not raised. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The <paramref name="expectedEventName" /> exists in the type of instance involved in tha Act type; /// otherwise a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <c>MyEvent</c> event is raised for <c>mc</c>. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Raised( "MyEvent" ) ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Raised(this SmartAssertPlaceHolder _, string expectedEventName) => new RaiseAssertion(null, true, expectedEventName);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an standard event is not raised in the Act part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="eventName">The name of the event that should not raise.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="eventName" /> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="eventName" /> is empty. /// </exception> /// <exception cref="BadTestException"> /// If the <paramref name="eventName" /> is not a valid event of the type of the instance involved in the Act. /// </exception> /// <exception cref="SmartTestException"> /// If the <paramref name="eventName" /> event is raised. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The <paramref name="eventName" /> exists in the type of the involved instance of the Act; /// otherwise a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <c>MyEvent</c> event is not raised for <c>mc</c>. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotRaised( "MyEvent" ) ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotRaised(this SmartAssertPlaceHolder _, string eventName) => new RaiseAssertion(null, false, eventName);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an standard event is raised in the Act part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which the event should raise.</param> /// <param name="expectedEventName">The name of the event that should raise.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="expectedEventName" /> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="expectedEventName" /> is empty. /// </exception> /// <exception cref="BadTestException"> /// If the <paramref name="expectedEventName" /> is not a valid event of the type of the <paramref name="instance" />. /// </exception> /// <exception cref="SmartTestException"> /// If the <paramref name="expectedEventName" /> event is not raised. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The <paramref name="expectedEventName" /> exists in the type of <paramref name="instance" /> type; /// otherwise a /// <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <c>MyEvent</c> event is raised for <c>mc.InnerObject</c> /// inner instance. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Raised( mc.InnerObject, "MyEvent" ) ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Raised(this SmartAssertPlaceHolder _, object instance, string expectedEventName) => new RaiseAssertion(instance, true, expectedEventName);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure object public properties did not change, except the specified ones, /// in /// the Act part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="exceptions">The names of the public properties that can change during the Act.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// If <paramref name="exceptions" /> are not public properties of the instance involve in the Act. /// </exception> /// <exception cref="SmartTestException"> /// If any public property, of the instance involved in the Act, except the <paramref name="exceptions" /> /// have changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// If names in <paramref name="exceptions" /> are not public properties of the instance involved in /// the Act, a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If a public property changed during the Act (except <paramref name="exceptions" />), a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no public property of <c>mc</c>, except <c>MyProperty</c> and /// <c>OtherProperty</c> /// changed while calling <c>MyMethod</c>. /// <code> /// [Test] /// public void CopyFromTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotChangedExcept("MyProperty", "OtherProperty") ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotChangedExcept(this SmartAssertPlaceHolder _, params string[] exceptions) => new NotChangedAssertion(null, NotChangedKind.PublicProperties, exceptions);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure object properties/fields did not change in the Act part of your /// test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which the properties/fields should not change.</param> /// <param name="kind"> /// The kind of tested members and their visibility. Default value is /// <see cref="NotChangedKind.PublicProperties" /> /// </param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="SmartTestException"> /// If any member, of <paramref name="instance" />, described by <paramref name="kind" /> has changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Nothing special.</para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If any of the specified members (using <paramref name="kind" />) has changed, a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no property nor field of <c>other</c> changed while invoking /// <c>CopyFrom</c>. /// <code> /// [Test] /// public void CopyFromTest() /// { /// var mc = new MyClass(); /// var other = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.CopyFrom( other ), /// SmartAssert.NotChanged( other, NotChangedKind.All ) ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotChanged(this SmartAssertPlaceHolder _, object instance, NotChangedKind kind = NotChangedKind.PublicProperties) => new NotChangedAssertion(instance, kind, null);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer changed in the Act part of /// your test. /// </summary> /// <typeparam name="T">The <see cref="Type" /> of the property/indexer.</typeparam> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="after"> /// The expression involving a property/indexer whose value should change in the Act part of your test. /// </param> /// <param name="value">The value to assign to the property/indexer.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException">if the property/indexer value BEFORE the Act is <paramref name="value" />.</exception> /// <exception cref="SmartTestException">if the property/indexer value AFTER the Act is not <paramref name="value" />.</exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Evaluates the <paramref name="after"/> expression and raises a <see cref="BadTestException"/> if it is <paramref name="value"/>.</para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para>Evaluates the <paramref name="after"/> expression and raises a <see cref="SmartTestException"/> if it is not <paramref name="value"/>.</para> /// </description> /// </item> /// </list> /// If you want to test changes of property/indexer and value involved in the Act, prefer using <see cref="ChangedTo" /> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that <c>!Equals(mc.MyProperty,10)</c> before the Act part of your /// test (otherwise a <see cref="BadTestException" /> is raised) and that <c>Equals(mc.MyProperty,10)</c> after the Act /// (otherwise a <see cref="SmartTestException" /> is raised). /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// /// RunTest(AnyValue.Valid, /// () => mc.MyMethod(), /// SmartAssert.ChangedTo(() => mc.MyProperty, 10)); /// }</code> /// </example> /// <seealso cref="Assertion" /> /// <seealso cref="SmartTest.SmartAssert" /> /// <seealso cref="SmartTest" /> public static Assertion ChangedTo <T>(this SmartAssertPlaceHolder @this, Expression <Func <T> > after, T value) => new ChangedToAssertion <T>(after, value);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer did not change at all in the Act /// part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// If the involved instance in the Act is not an <see cref="INotifyPropertyChanged" /> instance. /// </exception> /// <exception cref="SmartTestException"> /// <para>If the <see cref="INotifyPropertyChanged" /> event is raised for any property.</para> /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Nothing special.</para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is not raised for any /// property. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para>If the event is raised, a <see cref="SmartTestException" /> is thrown.</para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is not raised for any property of <c>mc</c>. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotRaised_PropertyChanged() ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotRaised_PropertyChanged(this SmartAssertPlaceHolder _) { return(new RaisePropertyChangedAssertion(null, false, null)); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer changed to a different value in the Act /// part of your test. /// </summary> /// <typeparam name="T">The type of the property/indexer that should change.</typeparam> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="expression">The expression that will change, directly or indirectly, the property/indexer.</param> /// <param name="expectedValue">The expected value of the property/indexer once the Act part is done.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// <para>If <paramref name="expression" /> is not a <see cref="MemberExpression" /> with a property.</para> /// <para> /// If the involved instance of <paramref name="expression" /> is not an <see cref="INotifyPropertyChanged" /> /// instance. /// </para> /// <para>If the member is not a Property.</para> /// <para>If the value of the <paramref name="expression" /> is <paramref name="expectedValue" /> before the Act.</para> /// </exception> /// <exception cref="SmartTestException"> /// <para>If the <see cref="INotifyPropertyChanged" /> event is not raised.</para> /// If <see cref="PropertyChangedEventArgs.PropertyName" /> is not the name of the property/indexer in the Act. /// <para> /// If the current value of the property is not <paramref name="expectedValue" /> in the event handler and after /// the Act. /// </para> /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The value of the property/indexer IS NOT <paramref name="expectedValue" />; otherwise a /// <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is raised. /// </para> /// <para> /// The <see cref="PropertyChangedEventArgs.PropertyName" /> is the one in the Act; /// otherwise a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer in the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// handler is <paramref name="expectedValue" />; otherwise a <see cref="SmartTestException" /> is /// thrown /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer is <paramref name="expectedValue" />; otherwise a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// <para> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is raised for the property <c>MyProperty</c> and the value is <c>10</c>. /// </para> /// <para>It also ensures that the value is still <c>10</c> after the Act.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Raised_PropertyChanged( () => mc.MyProperty, 10 ) ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Raised_PropertyChanged <T>(this SmartAssertPlaceHolder _, Expression <Func <T> > expression, T expectedValue) { GetInstanceAndProperty(expression, out var sender, out var property); return(new RaisePropertyChangedAssertion(sender, true, property.Name, expectedValue)); }
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer changed in the Act part of /// your test. /// </summary> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException">if the property/indexer value BEFORE the Act is the assigned value.</exception> /// <exception cref="SmartTestException">if the property/indexer value AFTER the Act is not the assigned value.</exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Evaluates the property involved in the Act and raises a <see cref="BadTestException"/> if it is the same value as the one involved in the Act.</para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para>Evaluates the property involved in the Act and raises a <see cref="SmartTestException"/> if it is not the same value as the one involved in the Act.</para> /// </description> /// </item> /// </list> /// If you want to test changes of another property/indexer or value, prefer using <see cref="ChangedTo{T}" /> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that <c>!Equals(mc.MyProperty,10)</c> before the Act part of your /// test (otherwise a <see cref="BadTestException" /> is raised) and that <c>Equals(mc.MyProperty,10)</c> after the Act /// (otherwise a <see cref="SmartTestException" /> is raised). /// <code> /// [Test] /// public void MyPropertyTest_Set() /// { /// var mc = new MyClass(); /// /// RunTest( AnyValue.Valid, /// Assign( () => mc.MyProperty, 10 ), /// SmartAssert.ChangedTo() ); /// }</code> /// </example> /// <seealso cref="Assertion" /> /// <seealso cref="SmartTest.SmartAssert" /> /// <seealso cref="SmartTest" /> public static Assertion ChangedTo(this SmartAssertPlaceHolder @this) => new ChangedToAssertion();
public static Assertion NotChangedExcept(this SmartAssertPlaceHolder _) => new NotChangedAssertion(true);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an object property/indexer changed to a different value in the Act /// part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// <para>If the instance is <c>null</c> or the property/indexer cannot be inferred from the Act.</para> /// <para>If the value of the property before the Act is the value involved in the Act.</para> /// </exception> /// <exception cref="SmartTestException"> /// <para>If the <see cref="INotifyPropertyChanged" /> event is not raised.</para> /// <para>If <see cref="PropertyChangedEventArgs.PropertyName" /> is not the involved property in Act.</para> /// <para>If the current value of the property is not the involved value in Act during the Act or after the Act.</para> /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The value of the property/indexer IS NOT the value of the Act; otherwise a /// <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Ensures the <see cref="INotifyPropertyChanged.PropertyChanged" /> event is raised. /// </para> /// <para> /// The <see cref="PropertyChangedEventArgs.PropertyName" /> is the name of the property of the Act; /// otherwise a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer in the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// handler is the one involved in the Act; otherwise a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// <para> /// The value of the property/indexer is the one involved in the Act; otherwise a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// <para> /// In this example, the Smart Assertion verifies that the <see cref="INotifyPropertyChanged.PropertyChanged" /> /// event is raised for the property <c>MyProperty</c> and the value is <c>10</c>. /// </para> /// <para>It also ensures that the value is still <c>10</c> after the Act.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// Assign( () => mc.MyProperty, 10 ), /// SmartAssert.Raised_PropertyChanged() ); /// }</code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Raised_PropertyChanged(this SmartAssertPlaceHolder _) => new RaisePropertyChangedAssertion(null, true, null, null);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure object properties/fields of the specified instance did not change, /// except the specified ones, /// in the Act part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which to test public property changes.</param> /// <param name="kind">The kind of members and visibility to check for changes.</param> /// <param name="exceptions">The names of the properties/fields that can change during the Act.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// If <paramref name="exceptions" /> are not properties/fields of <paramref name="instance" />. /// </exception> /// <exception cref="SmartTestException"> /// If any property/field checked (see <paramref name="kind" />) of <paramref name="instance" />, except the /// <paramref name="exceptions" /> have changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// If names in <paramref name="exceptions" /> are not properties/fields of /// <paramref name="instance" />, a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If a property/field of <paramref name="instance" /> changed during the Act (except /// <paramref name="exceptions" />), a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no property nor field of <c>other</c>, except <c>CopyCount</c>, /// changed while calling <c>CopyPropertiesFrom</c>. /// <code> /// [Test] /// public void CopyFromTest() /// { /// var mc = new MyClass(); /// var other = new MyClass(); /// /// RunTest( ValidValue.IsValid, /// () => mc.CopyPropertiesFrom( other ), /// SmartAssert.NotChangedExcept( other, NotChangedKind.All, "CopyCount" ) ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotChangedExcept(this SmartAssertPlaceHolder _, object instance, NotChangedKind kind, params string[] exceptions) => new NotChangedAssertion(instance, kind, exceptions);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure the provided exception type is thrown. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="verify">Code to add verifications on the exception.</param> /// <typeparam name="T">The expected exception type</typeparam> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <remarks>Should be the first assertion.</remarks> // ReSharper disable once UnusedParameter.Global public static Assertion Throw <T>(this SmartAssertPlaceHolder _, Action <T> verify = null) where T : Exception => new ThrowAssertion <T>(verify);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an standard event is raised in the Act part of your test. /// </summary> /// <typeparam name="T">The type of the <see cref="EventHandler{TEventArgs}" /> of the event that should raise.</typeparam> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which the event should raise.</param> /// <param name="expectedEventName">The name of the event that should raise.</param> /// <param name="assert">The handler that must be run in Act part to do specific assertions.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="expectedEventName" /> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="expectedEventName" /> is empty. /// </exception> /// <exception cref="BadTestException"> /// If the <paramref name="expectedEventName" /> is not a valid event of the type of the <paramref name="instance" />. /// </exception> /// <exception cref="SmartTestException"> /// If the <paramref name="expectedEventName" /> event is not raised. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The <paramref name="expectedEventName" /> exists in the type of <paramref name="instance" /> type; /// otherwise a /// <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>During the Act</term> /// <description> /// <para> /// Invoke the <paramref name="assert" /> handler, to test specific assertions. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was not raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// <para> /// In this example, the Smart Assertion verifies that the <c>MyEvent</c> event is raised for <c>mc.InnerObject</c> /// inner instance. /// </para> /// <para>It also ensure anything you want in the specified handler.</para> /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.Raised( mc.InnerObject, /// "MyEvent", /// ( sender, args ) => { /// // Check anything you want when MyEvent is raised. /// })); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion Raised <T>(this SmartAssertPlaceHolder _, object instance, string expectedEventName, EventHandler <T> assert) where T : EventArgs => assert != null ? new RaiseAssertion <T>(instance, true, expectedEventName, assert) : new RaiseAssertion(instance, true, expectedEventName);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure the provided exception type is thrown for the /// <paramref name="parameterName" /> parameter. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="parameterName">The name of the parameter for which the exception is thrown.</param> /// <param name="message"> /// The expectedMessage of the exception, without the last line (ParameterName: XXX) because it depends on /// the current culture. /// </param> /// <param name="verify">Code to add verifications on the exception.</param> /// <typeparam name="T">The expected exception type</typeparam> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <remarks>Should be the first assertion.</remarks> // ReSharper disable once UnusedParameter.Global public static Assertion Throw <T>(this SmartAssertPlaceHolder _, string parameterName, string message, Action <T> verify = null) where T : ArgumentException => new ThrowArgumentAssertion <T>(parameterName, message, verify);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure an standard event is not raised in the Act part of your test. /// </summary> /// <param name="_">The dummy place holder for all Smart Assertions.</param> /// <param name="instance">The instance for which the event should not raise.</param> /// <param name="eventName">The name of the event that should not raise.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="ArgumentNullException"> /// If <paramref name="eventName" /> is <c>null</c>. /// </exception> /// <exception cref="ArgumentException"> /// If <paramref name="eventName" /> is empty. /// </exception> /// <exception cref="BadTestException"> /// If the <paramref name="eventName" /> is not a valid event of the type of the <paramref name="instance" />. /// </exception> /// <exception cref="SmartTestException"> /// If the <paramref name="eventName" /> event is raised. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// The <paramref name="eventName" /> exists in the type of <paramref name="instance" /> type; /// otherwise a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If the event was raised, a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that the <c>MyEvent</c> event is not raised for <c>mc</c>. /// <code> /// [Test] /// public void MyMethodTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotRaised( mc.InnerObject, "MyEvent" ) ); /// } /// </code> /// </example> // ReSharper disable once UnusedParameter.Global public static Assertion NotRaised(this SmartAssertPlaceHolder _, object instance, string eventName) => new RaiseAssertion(instance, false, eventName);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure object properties/fields did not change in the Act part of your /// test. /// </summary> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="kind"> /// The kind of tested members and their visibility. Default value is /// <see cref="NotChangedKind.PublicProperties" /> /// </param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="SmartTestException"> /// If any member, of the instance involved in the Act, described by <paramref name="kind" /> has changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Nothing special.</para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If any of the specified members (using <paramref name="kind" />) has changed, a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no property nor field of <c>mc</c> changed while invoking /// <c>MyMethod</c>. /// <code> /// [Test] /// public void CopyFromTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotChanged( NotChangedKind.All ) ); /// } /// </code> /// </example> public static Assertion NotChanged(this SmartAssertPlaceHolder @this, NotChangedKind kind = NotChangedKind.PublicProperties) => new NotChangedAssertion(null, kind, null);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure relative changes to a property/indexer of an object. /// </summary> /// <typeparam name="T">The <see cref="Type" /> of the property/indexer.</typeparam> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="after"> /// The expressionn involving a property/indexer whose value should be equal to the value of the member /// only after the Act. /// </param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="SmartTestException"> /// if the property/indexer value AFTER the Act is not the value of the expression /// <paramref name="after" />. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para>Evaluates the <paramref name="after" /> expression.</para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// Evaluates the <paramref name="after" /> expression stripped of any computation so that there is /// only a property/indexer call. /// </para> /// <para> /// This value should be equal to the previously computed value (<paramref name="after" /> before the /// Act); otherwise a <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In the following example, we expect that the <c>mc.MyList.Count</c> value after the Act is the value of the same /// property before the Act plus 1. /// <code> /// [Test] /// public void AddItemTest() /// { /// var mc = new MyClass(); /// /// RunTest( AnyValue.Valid, /// () => mc.AddItem(), /// SmartAssert.Change( () => mc.MyList.Count + 1 ) ); /// }</code> /// </example> /// <seealso cref="Assertion" /> /// <seealso cref="SmartTest.SmartAssert" /> /// <seealso cref="SmartTest" /> public static Assertion Change <T>(this SmartAssertPlaceHolder @this, Expression <Func <T> > after) => new ChangeAssertion <T>(after);
/// <summary> /// Creates an <see cref="Assertion" /> that ensure object properties/fields did not change, except the specified ones, /// in the Act part of your test. /// </summary> /// <param name="this">The dummy place holder for all Smart Assertions.</param> /// <param name="kind">The kind of members and visibility to check for changes.</param> /// <param name="exceptions">The names of the properties/fields that can change during the Act.</param> /// <returns>The newly created <see cref="Assertion" />.</returns> /// <exception cref="BadTestException"> /// If <paramref name="exceptions" /> are not properties/fields of the instance involve in the Act. /// </exception> /// <exception cref="SmartTestException"> /// If any property/field checked (see <paramref name="kind" />), of the instance involved in the Act, except the /// <paramref name="exceptions" /> /// have changed. /// </exception> /// <remarks> /// This <see cref="Assertion" /> ensures that: /// <list type="number"> /// <item> /// <term>Before the Act</term> /// <description> /// <para> /// If names in <paramref name="exceptions" /> are not properties/fields of the instance involved in /// the Act, a <see cref="BadTestException" /> is thrown. /// </para> /// </description> /// </item> /// <item> /// <term>After the Act</term> /// <description> /// <para> /// If a property/field changed during the Act (except <paramref name="exceptions" />), a /// <see cref="SmartTestException" /> is thrown. /// </para> /// </description> /// </item> /// </list> /// </remarks> /// <example> /// In this example, the Smart Assertion verifies that no property nor field of <c>mc</c>, except <c>MyProperty</c> and /// <c>OtherProperty</c> /// changed while calling <c>MyMethod</c>. /// <code> /// [Test] /// public void CopyFromTest() /// { /// var mc = new MyClass(); /// RunTest( ValidValue.IsValid, /// () => mc.MyMethod(), /// SmartAssert.NotChangedExcept( NotChangedKind.All, "MyProperty", "OtherProperty" ) ); /// } /// </code> /// </example> public static Assertion NotChangedExcept(this SmartAssertPlaceHolder @this, NotChangedKind kind, params string[] exceptions) => new NotChangedAssertion(null, kind, exceptions);