From b7934e58dfd9b344197bcc901d42005a93e5dada Mon Sep 17 00:00:00 2001 From: Ben Quarmby Date: Sun, 7 Aug 2016 14:37:22 +1000 Subject: [PATCH] Contravariant expectations. --- .../{Expectation`1.cs => Expectation.cs} | 33 +++++++------------ .../ExpectationDictionaryExtensions.cs | 3 +- .../ExpectationEnumerableExtensions.cs | 12 +++---- .../Expectations/ExpectationExtensions.cs | 12 +++++++ .../JSLintNet.QualityTools/Expectations/I.cs | 17 +++++++--- .../{IExpectation`1.cs => IExpectation.cs} | 16 +++------ ...ctation`1.cs => IReversibleExpectation.cs} | 6 ++-- .../JSLintNet.QualityTools.csproj | 7 ++-- .../Tasks/JSLintTaskUnit.cs | 11 +++---- 9 files changed, 57 insertions(+), 60 deletions(-) rename specifications/JSLintNet.QualityTools/Expectations/{Expectation`1.cs => Expectation.cs} (65%) create mode 100644 specifications/JSLintNet.QualityTools/Expectations/ExpectationExtensions.cs rename specifications/JSLintNet.QualityTools/Expectations/{IExpectation`1.cs => IExpectation.cs} (65%) rename specifications/JSLintNet.QualityTools/Expectations/{IReversibleExpectation`1.cs => IReversibleExpectation.cs} (64%) diff --git a/specifications/JSLintNet.QualityTools/Expectations/Expectation`1.cs b/specifications/JSLintNet.QualityTools/Expectations/Expectation.cs similarity index 65% rename from specifications/JSLintNet.QualityTools/Expectations/Expectation`1.cs rename to specifications/JSLintNet.QualityTools/Expectations/Expectation.cs index 8cc0249..3b48505 100644 --- a/specifications/JSLintNet.QualityTools/Expectations/Expectation`1.cs +++ b/specifications/JSLintNet.QualityTools/Expectations/Expectation.cs @@ -3,14 +3,14 @@ /// /// An expectation that can be reversed to a negative state. /// - /// Any type. - public class Expectation : IReversibleExpectation + /// Any type. + public class Expectation : IReversibleExpectation { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The value. - public Expectation(T actual) + public Expectation(TActual actual) { this.Actual = actual; this.Positive = true; @@ -22,10 +22,10 @@ public Expectation(T actual) /// /// The actual value. /// - public T Actual { get; private set; } + public TActual Actual { get; private set; } /// - /// Gets a value indicating whether this is positive. + /// Gets a value indicating whether this is positive. /// /// /// true if positive; otherwise, false. @@ -38,7 +38,7 @@ public Expectation(T actual) /// /// The negative expectation. /// - public IExpectation Not + public IExpectation Not { get { @@ -47,17 +47,6 @@ public IExpectation Not } } - /// - /// Matcher that determines whether the actual and expected values are equal. - /// - /// The expected value. - public void ToBe(T expected) - { - var pass = this.Actual == null ? expected == null : this.Actual.Equals(expected); - - ExpectationHelper.PassFail(pass, this, expected); - } - /// /// Matcher that determines whether the actual value is null. /// @@ -69,12 +58,12 @@ public void ToBeNull() /// /// Matcher that determines whether the actual value type is the same as the given type. /// - /// The type to compare. - public void ToBeOfType() + /// The type to compare. + public void ToBeOfType() { - var typeOfS = typeof(S); + var expectedType = typeof(TExpected); - ExpectationHelper.PassFail(this.Actual.GetType() == typeOfS, this, typeOfS.ToString()); + ExpectationHelper.PassFail(this.Actual.GetType() == expectedType, this, expectedType.ToString()); } } } diff --git a/specifications/JSLintNet.QualityTools/Expectations/ExpectationDictionaryExtensions.cs b/specifications/JSLintNet.QualityTools/Expectations/ExpectationDictionaryExtensions.cs index 5c18d52..2062f70 100644 --- a/specifications/JSLintNet.QualityTools/Expectations/ExpectationDictionaryExtensions.cs +++ b/specifications/JSLintNet.QualityTools/Expectations/ExpectationDictionaryExtensions.cs @@ -4,8 +4,7 @@ public static class ExpectationDictionaryExtensions { - public static void ToContainKey(this IExpectation expectation, object key) - where E : IDictionary + public static void ToContainKey(this IExpectation expectation, object key) { ExpectationHelper.PassFail(expectation.Actual.Contains(key), expectation, key.ToString()); } diff --git a/specifications/JSLintNet.QualityTools/Expectations/ExpectationEnumerableExtensions.cs b/specifications/JSLintNet.QualityTools/Expectations/ExpectationEnumerableExtensions.cs index 935e240..cfa51c2 100644 --- a/specifications/JSLintNet.QualityTools/Expectations/ExpectationEnumerableExtensions.cs +++ b/specifications/JSLintNet.QualityTools/Expectations/ExpectationEnumerableExtensions.cs @@ -1,26 +1,22 @@ namespace JSLintNet.QualityTools.Expectations { using System; - using System.Collections; using System.Collections.Generic; using System.Linq; public static class ExpectationEnumerableExtensions { - public static void ToBeEmpty(this IExpectation expectation) - where E : IEnumerable + public static void ToBeEmpty(this IExpectation> expectation) { - ExpectationHelper.PassFail(!expectation.Actual.GetEnumerator().MoveNext(), expectation); + ExpectationHelper.PassFail(!expectation.Actual.Any(), expectation); } - public static void ToContain(this IExpectation expectation, T expected) - where E : IEnumerable + public static void ToContain(this IExpectation> expectation, TItem expected) { ExpectationHelper.PassFail(expectation.Actual.Contains(expected), expectation, expected.ToString()); } - public static void ToContain(this IExpectation expectation, Func predicate) - where E : IEnumerable + public static void ToContain(this IExpectation> expectation, Func predicate) { ExpectationHelper.PassFail(expectation.Actual.Any(predicate), expectation, predicate.ToString()); } diff --git a/specifications/JSLintNet.QualityTools/Expectations/ExpectationExtensions.cs b/specifications/JSLintNet.QualityTools/Expectations/ExpectationExtensions.cs new file mode 100644 index 0000000..b17b967 --- /dev/null +++ b/specifications/JSLintNet.QualityTools/Expectations/ExpectationExtensions.cs @@ -0,0 +1,12 @@ +namespace JSLintNet.QualityTools.Expectations +{ + public static class ExpectationExtensions + { + public static void ToBe(this IExpectation expectation, TActual expected) + { + var pass = expectation.Actual == null ? expected == null : expectation.Actual.Equals(expected); + + ExpectationHelper.PassFail(pass, expectation, expected); + } + } +} diff --git a/specifications/JSLintNet.QualityTools/Expectations/I.cs b/specifications/JSLintNet.QualityTools/Expectations/I.cs index 1cb5554..6d57523 100644 --- a/specifications/JSLintNet.QualityTools/Expectations/I.cs +++ b/specifications/JSLintNet.QualityTools/Expectations/I.cs @@ -10,19 +10,26 @@ public static class I /// /// Creates a new expectation. /// - /// The type of the actual value. + /// The type of the actual value. /// The actual value. /// /// The expectation. /// - public static IReversibleExpectation Expect(T actual) + public static IReversibleExpectation Expect(TActual actual) { - return new Expectation(actual); + return new Expectation(actual); } - public static IReversibleExpectation Expect(Action actual) + /// + /// Creates a new delegate expectation. + /// + /// The action. + /// + /// The expectation. + /// + public static IReversibleExpectation Expect(Action action) { - return new Expectation(actual); + return new Expectation(action); } } } diff --git a/specifications/JSLintNet.QualityTools/Expectations/IExpectation`1.cs b/specifications/JSLintNet.QualityTools/Expectations/IExpectation.cs similarity index 65% rename from specifications/JSLintNet.QualityTools/Expectations/IExpectation`1.cs rename to specifications/JSLintNet.QualityTools/Expectations/IExpectation.cs index 5f231c5..3e12d61 100644 --- a/specifications/JSLintNet.QualityTools/Expectations/IExpectation`1.cs +++ b/specifications/JSLintNet.QualityTools/Expectations/IExpectation.cs @@ -3,8 +3,8 @@ /// /// An expectation. /// - /// Any type. - public interface IExpectation + /// Any type. + public interface IExpectation { /// /// Gets the actual value. @@ -12,7 +12,7 @@ public interface IExpectation /// /// The actual value. /// - T Actual { get; } + TActual Actual { get; } /// /// Gets a value indicating whether this is positive. @@ -22,12 +22,6 @@ public interface IExpectation /// bool Positive { get; } - /// - /// Matcher that determines whether the actual and expected values are the same. - /// - /// The expected value. - void ToBe(T expected); - /// /// Matcher that determines whether the actual value is null. /// @@ -36,7 +30,7 @@ public interface IExpectation /// /// Matcher that determines whether the actual value type is the same as the given type. /// - /// The type to compare. - void ToBeOfType(); + /// The type to compare. + void ToBeOfType(); } } diff --git a/specifications/JSLintNet.QualityTools/Expectations/IReversibleExpectation`1.cs b/specifications/JSLintNet.QualityTools/Expectations/IReversibleExpectation.cs similarity index 64% rename from specifications/JSLintNet.QualityTools/Expectations/IReversibleExpectation`1.cs rename to specifications/JSLintNet.QualityTools/Expectations/IReversibleExpectation.cs index 922c141..e45525b 100644 --- a/specifications/JSLintNet.QualityTools/Expectations/IReversibleExpectation`1.cs +++ b/specifications/JSLintNet.QualityTools/Expectations/IReversibleExpectation.cs @@ -3,8 +3,8 @@ /// /// An expectation that can be reversed to a negative state. /// - /// Any type. - public interface IReversibleExpectation : IExpectation + /// Any type. + public interface IReversibleExpectation : IExpectation { /// /// Gets the negative expectation. @@ -12,6 +12,6 @@ public interface IReversibleExpectation : IExpectation /// /// The negative expectation. /// - IExpectation Not { get; } + IExpectation Not { get; } } } diff --git a/specifications/JSLintNet.QualityTools/JSLintNet.QualityTools.csproj b/specifications/JSLintNet.QualityTools/JSLintNet.QualityTools.csproj index 16a93a9..61f985c 100644 --- a/specifications/JSLintNet.QualityTools/JSLintNet.QualityTools.csproj +++ b/specifications/JSLintNet.QualityTools/JSLintNet.QualityTools.csproj @@ -80,13 +80,14 @@ + - + - - + + diff --git a/specifications/JSLintNet.Specifications/Tasks/JSLintTaskUnit.cs b/specifications/JSLintNet.Specifications/Tasks/JSLintTaskUnit.cs index eece1f2..10fc92f 100644 --- a/specifications/JSLintNet.Specifications/Tasks/JSLintTaskUnit.cs +++ b/specifications/JSLintNet.Specifications/Tasks/JSLintTaskUnit.cs @@ -11,7 +11,6 @@ using JSLintNet.QualityTools.Expectations; using JSLintNet.QualityTools.Fakes; using JSLintNet.Settings; - using Microsoft.Build.Framework; using Moq; using Xunit; @@ -89,7 +88,7 @@ public void Spec16() testable.Instance.Execute(); - I.Expect(testable.BuildEngine.ErrorEvents).ToContain((BuildErrorEventArgs x) => x.Message == string.Format(Resources.ErrorLimitReachedFormat, JSLintNetSettings.DefaultErrorLimit)); + I.Expect(testable.BuildEngine.ErrorEvents).ToContain(x => x.Message == string.Format(Resources.ErrorLimitReachedFormat, JSLintNetSettings.DefaultErrorLimit)); } } @@ -110,7 +109,7 @@ public void Spec17() testable.Instance.Execute(); - I.Expect(testable.BuildEngine.ErrorEvents).ToContain((BuildErrorEventArgs x) => x.Message == string.Format(Resources.ExceptionLimitReachedFormat, JSLintNetSettings.ExceptionLimit)); + I.Expect(testable.BuildEngine.ErrorEvents).ToContain(x => x.Message == string.Format(Resources.ExceptionLimitReachedFormat, JSLintNetSettings.ExceptionLimit)); } } @@ -126,7 +125,7 @@ public void Spec18() testable.Instance.Execute(); - I.Expect(testable.BuildEngine.ErrorEvents).ToContain((BuildErrorEventArgs x) => x.Message == string.Format(Resources.FileLimitReachedFormat, JSLintNetSettings.DefaultFileLimit)); + I.Expect(testable.BuildEngine.ErrorEvents).ToContain(x => x.Message == string.Format(Resources.FileLimitReachedFormat, JSLintNetSettings.DefaultFileLimit)); } } @@ -141,7 +140,7 @@ public void Spec19() testable.Instance.Execute(); - I.Expect(testable.BuildEngine.ErrorEvents).ToContain((BuildErrorEventArgs x) => x.Message == string.Format(Resources.ErrorLimitReachedFormat, 11)); + I.Expect(testable.BuildEngine.ErrorEvents).ToContain(x => x.Message == string.Format(Resources.ErrorLimitReachedFormat, 11)); } } @@ -160,7 +159,7 @@ public void Spec20() testable.Instance.Execute(); - I.Expect(testable.BuildEngine.ErrorEvents).ToContain((BuildErrorEventArgs x) => x.Message == string.Format(Resources.FileLimitReachedFormat, 10)); + I.Expect(testable.BuildEngine.ErrorEvents).ToContain(x => x.Message == string.Format(Resources.FileLimitReachedFormat, 10)); } }