-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
236 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System; | ||
using NUnit.Framework; | ||
using NExpect; | ||
using NExpect.Exceptions; | ||
using static NExpect.Expectations; | ||
using static PeanutButter.RandomGenerators.RandomValueGen; | ||
|
||
namespace NExpect.Tests | ||
{ | ||
[TestFixture] | ||
public class TestEnumMatchers | ||
{ | ||
[Test] | ||
public void ShouldBeAbleToAssertEnumValueHasFlag() | ||
{ | ||
// Arrange | ||
var value = Numbers.One | Numbers.Three; | ||
// Act | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(value) | ||
.To.Have.Flag(Numbers.One) | ||
.And | ||
.To.Have.Flag(Numbers.Three); | ||
Expect(value) | ||
.Not.To.Have.Flag(Numbers.Two) | ||
.And | ||
.Not.To.Have.Flag(Numbers.Four); | ||
}, | ||
Throws.Nothing | ||
); | ||
|
||
Assert.That( | ||
() => | ||
{ | ||
Expect(value) | ||
.To.Have.Flag(Numbers.Two); | ||
}, | ||
Throws.Exception.InstanceOf<UnmetExpectationException>() | ||
.With.Message.Contains( | ||
$"Expected ({value}) to have flag ({Numbers.Two})" | ||
) | ||
); | ||
|
||
Assert.That( | ||
() => | ||
{ | ||
Expect(value) | ||
.Not.To.Have.Flag(Numbers.One); | ||
}, | ||
Throws.Exception.InstanceOf<UnmetExpectationException>() | ||
.With.Message.Contains( | ||
$"Expected ({value}) not to have flag ({Numbers.One})" | ||
) | ||
); | ||
|
||
// Assert | ||
} | ||
|
||
[Test] | ||
public void ShouldAlwaysFailForNonFlagsEnums() | ||
{ | ||
// Arrange | ||
var value = NotFlags.Strange; | ||
// Act | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(value) | ||
.To.Have.Flag(NotFlags.Strange); | ||
}, | ||
Throws.Exception.InstanceOf<UnmetExpectationException>() | ||
.With.Message.Contains("not decorated with [Flags]") | ||
); | ||
Assert.That( | ||
() => | ||
{ | ||
Expect(value) | ||
.Not.To.Have.Flag(NotFlags.Strange); | ||
}, | ||
Throws.Exception.InstanceOf<UnmetExpectationException>() | ||
.With.Message.Contains("not decorated with [Flags]") | ||
); | ||
// Assert | ||
} | ||
|
||
public enum NotFlags | ||
{ | ||
Unknown, | ||
Up, | ||
Down, | ||
Strange, | ||
Charm, | ||
Bottom, | ||
Top | ||
} | ||
|
||
[Flags] | ||
public enum Numbers | ||
{ | ||
Zero = 0, | ||
One = 1, | ||
Two = 2, | ||
Three = 4, | ||
Four = 8 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using Imported.PeanutButter.Utils; | ||
using NExpect.Interfaces; | ||
using NExpect.MatcherLogic; | ||
using static NExpect.Implementations.MessageHelpers; | ||
|
||
namespace NExpect; | ||
|
||
/// <summary> | ||
/// Provides matchers for enum values | ||
/// </summary> | ||
public static class EnumMatchers | ||
{ | ||
/// <summary> | ||
/// Verifies that the [Flag]-decorated enum | ||
/// has the provided flag | ||
/// </summary> | ||
/// <param name="have"></param> | ||
/// <param name="flag">The flag being tested for</param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public static IMore<T> Flag<T>( | ||
this IHave<T> have, | ||
T flag | ||
) where T : struct | ||
{ | ||
return have.Flag( | ||
flag, | ||
NULL_STRING | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that the [Flag]-decorated enum | ||
/// has the provided flag | ||
/// </summary> | ||
/// <param name="have"></param> | ||
/// <param name="flag">The flag being tested for</param> | ||
/// <param name="customMessage">A custom error message</param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public static IMore<T> Flag<T>( | ||
this IHave<T> have, | ||
T flag, | ||
string customMessage | ||
) where T : struct | ||
{ | ||
return have.Flag( | ||
flag, | ||
() => customMessage | ||
); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that the [Flag]-decorated enum | ||
/// has the provided flag | ||
/// </summary> | ||
/// <param name="have"></param> | ||
/// <param name="flag">The flag being tested for</param> | ||
/// <param name="customMessageGenerator">Generates a custom error message</param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public static IMore<T> Flag<T>( | ||
this IHave<T> have, | ||
T flag, | ||
Func<string> customMessageGenerator | ||
) where T : struct | ||
{ | ||
return have.AddMatcher( | ||
actual => | ||
{ | ||
if (!typeof(T).HasAttribute<FlagsAttribute>()) | ||
{ | ||
return new EnforcedMatcherResult( | ||
false, | ||
FinalMessageFor( | ||
() => | ||
$"{typeof(T)} is not decorated with [Flags]. Are you sure you should be using it like a Flags type?", | ||
customMessageGenerator | ||
) | ||
); | ||
} | ||
|
||
var passed = actual.HasFlag(flag); | ||
|
||
return new MatcherResult( | ||
passed, | ||
FinalMessageFor( | ||
() => $"Expected ({actual}) {passed.AsNot()}to have flag ({flag})", | ||
customMessageGenerator | ||
) | ||
); | ||
} | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule PeanutButter
updated
46 files