Skip to content

Commit

Permalink
Convert performance tests to warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jnm2 committed Dec 11, 2017
1 parent 192fe0a commit 5c45bef
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 30 deletions.
48 changes: 25 additions & 23 deletions src/NUnitFramework/tests/Assertions/CollectionAssertTest.cs
Expand Up @@ -25,6 +25,9 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Compatibility;
using NUnit.Framework.Constraints;
using NUnit.TestUtilities;
using NUnit.TestUtilities.Collections;
using NUnit.TestUtilities.Comparers;
Expand Down Expand Up @@ -130,16 +133,15 @@ public void UniqueFailure_WithTwoNulls()
new List<string>(RANGE.Select(v => v.ToString()))
};

[MaxTime(100)]
[TestCaseSource(nameof(PerformanceData))]
public void PerformanceTests(IEnumerable values)
{
CollectionAssert.AllItemsAreUnique(values);
Warn.Unless(() => CollectionAssert.AllItemsAreUnique(values), HelperConstraints.HasMaxTime(100));
}

#endregion
#endregion

#region AreEqual
#region AreEqual

[Test]
public void AreEqual()
Expand Down Expand Up @@ -307,9 +309,9 @@ public void AreEqual_IEquatableImplementationIsIgnored()
CollectionAssert.AreEqual(y, x, "CollectionAssert 2");
}

#endregion
#endregion

#region AreEquivalent
#region AreEquivalent

[Test]
public void Equivalent()
Expand Down Expand Up @@ -360,9 +362,9 @@ public void AreEquivalentHandlesNull()

CollectionAssert.AreEquivalent(set1,set2);
}
#endregion
#endregion

#region AreNotEqual
#region AreNotEqual

[Test]
public void AreNotEqual()
Expand Down Expand Up @@ -417,9 +419,9 @@ public void AreNotEqual_IEquatableImplementationIsIgnored()
CollectionAssert.AreNotEqual(y, x, "CollectionAssert 2");
}

#endregion
#endregion

#region AreNotEquivalent
#region AreNotEquivalent

[Test]
public void NotEquivalent()
Expand Down Expand Up @@ -452,9 +454,9 @@ public void NotEquivalentHandlesNull()

CollectionAssert.AreNotEquivalent(set1,set2);
}
#endregion
#endregion

#region Contains
#region Contains
[Test]
public void Contains_IList()
{
Expand Down Expand Up @@ -534,9 +536,9 @@ public void ContainsNull_ICollection()
var ca = new SimpleObjectCollection(new object[] { 1, 2, 3, null, 4, 5 });
CollectionAssert.Contains( ca, null );
}
#endregion
#endregion

#region DoesNotContain
#region DoesNotContain
[Test]
public void DoesNotContain()
{
Expand All @@ -563,9 +565,9 @@ public void DoesNotContain_Fails()
var ex = Assert.Throws<AssertionException>(() => CollectionAssert.DoesNotContain(list,"y"));
Assert.That(ex.Message, Is.EqualTo(expectedMessage));
}
#endregion
#endregion

#region IsSubsetOf
#region IsSubsetOf
[Test]
public void IsSubsetOf()
{
Expand Down Expand Up @@ -599,9 +601,9 @@ public void IsSubsetOfHandlesNull()
CollectionAssert.IsSubsetOf(set2,set1);
Assert.That(set2, Is.SubsetOf(set1));
}
#endregion
#endregion

#region IsNotSubsetOf
#region IsNotSubsetOf
[Test]
public void IsNotSubsetOf()
{
Expand Down Expand Up @@ -634,9 +636,9 @@ public void IsNotSubsetOfHandlesNull()

CollectionAssert.IsNotSubsetOf(set1,set2);
}
#endregion
#endregion

#region IsOrdered
#region IsOrdered

[Test]
public void IsOrdered()
Expand Down Expand Up @@ -701,9 +703,9 @@ public void IsOrdered_Handles_custom_comparison2()
CollectionAssert.IsOrdered(list, new TestComparer());
}

#endregion
#endregion

#region Equals
#region Equals

[Test]
public void EqualsFailsWhenUsed()
Expand All @@ -718,7 +720,7 @@ public void ReferenceEqualsFailsWhenUsed()
var ex = Assert.Throws<InvalidOperationException>(() => CollectionAssert.ReferenceEquals(string.Empty, string.Empty));
Assert.That(ex.Message, Does.StartWith("CollectionAssert.ReferenceEquals should not be used for Assertions"));
}
#endregion
#endregion

}
}
Expand Up @@ -8,10 +8,10 @@
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Expand Down Expand Up @@ -68,14 +68,16 @@ public void HonorsIgnoreCase( IEnumerable actual )
new TestCaseData(new List<string>(RANGE.Select(v => v.ToString())), true)
};

[MaxTime(100)]
[TestCaseSource(nameof(PerformanceData))]
public void PerformanceTests(IEnumerable values, bool ignoreCase)
{
if (ignoreCase)
Assert.That(values, Is.Unique.IgnoreCase);
else
Assert.That(values, Is.Unique);
Warn.Unless(() =>
{
if (ignoreCase)
Assert.That(values, Is.Unique.IgnoreCase);
else
Assert.That(values, Is.Unique);
}, HelperConstraints.HasMaxTime(100));
}
}
}
102 changes: 102 additions & 0 deletions src/NUnitFramework/tests/HelperConstraints.cs
@@ -0,0 +1,102 @@
// ***********************************************************************
// Copyright (c) 2017 Charlie Poole, Rob Prouse
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// ***********************************************************************

using System;
using System.Reflection;
using NUnit.Compatibility;
using NUnit.Framework.Constraints;

namespace NUnit.Framework
{
public sealed class HelperConstraints
{
public static Constraint HasMaxTime(int milliseconds)
{
return new HasMaxTimeConstraint(milliseconds);
}

private sealed class HasMaxTimeConstraint : Constraint
{
private readonly int _milliseconds;

public HasMaxTimeConstraint(int milliseconds)
{
_milliseconds = milliseconds;
}

protected override object GetTestObject<TActual>(ActualValueDelegate<TActual> del)
{
return del;
}

public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
var @delegate = actual as Delegate;
if (@delegate == null)
throw new ArgumentException("Actual value must be a delegate.", nameof(actual));

var invokeMethod = @delegate.GetType().GetTypeInfo().GetMethod("Invoke");
if (invokeMethod.GetParameters().Length != 0)
throw new ArgumentException("Delegate must be parameterless.", nameof(actual));

var stopwatch = new System.Diagnostics.Stopwatch();

#if ASYNC
if (Internal.AsyncInvocationRegion.IsAsyncOperation(@delegate))
{
using (var async = Internal.AsyncInvocationRegion.Create(@delegate))
{
stopwatch.Start();
async.WaitForPendingOperationsToComplete(@delegate.DynamicInvoke());
stopwatch.Stop();
}
}
else
#endif
{
stopwatch.Start();
@delegate.DynamicInvoke();
stopwatch.Stop();
}

return new Result(this, stopwatch.ElapsedMilliseconds);
}

private sealed class Result : ConstraintResult
{
private readonly HasMaxTimeConstraint _constraint;

public Result(HasMaxTimeConstraint constraint, long actualMilliseconds)
: base(constraint, actualMilliseconds, actualMilliseconds <= constraint._milliseconds)
{
_constraint = constraint;
}

public override void WriteMessageTo(MessageWriter writer)
{
writer.Write($"Elapsed time of {ActualValue}ms exceeds maximum of {_constraint._milliseconds}ms");
}
}
}
}
}

0 comments on commit 5c45bef

Please sign in to comment.