-
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #361 from JosephWoodward/master
Merged pull request #355 from bangsholt:AddEnumHaveFlag
- Loading branch information
Showing
21 changed files
with
432 additions
and
0 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,26 @@ | ||
ShouldHaveFlag/NotHaveFlag | ||
=========== | ||
|
||
``ShouldHaveFlag`` allows you to assert whether an object is an enum and has a flag specified. | ||
|
||
Conversely ``ShouldNotHaveFlag`` allows you to assert the opposite; that an object is an enum but does not have a flag specified. | ||
|
||
ShouldHaveFlag | ||
------- | ||
|
||
.. literalinclude:: /../src/DocumentationExamples/CodeExamples/ShouldHaveFlagNotHaveFlagExamples/ShouldHaveFlag.codeSample.approved.txt | ||
:language: c# | ||
|
||
**Exception** | ||
|
||
.. literalinclude:: /../src/DocumentationExamples/CodeExamples/ShouldHaveFlagNotHaveFlagExamples/ShouldHaveFlag.exceptionText.approved.txt | ||
|
||
ShouldNotHaveFlag | ||
------- | ||
|
||
.. literalinclude:: /../src/DocumentationExamples/CodeExamples/ShouldHaveFlagNotHaveFlagExamples/ShouldNotHaveFlag.codeSample.approved.txt | ||
:language: c# | ||
|
||
**Exception** | ||
|
||
.. literalinclude:: /../src/DocumentationExamples/CodeExamples/ShouldHaveFlagNotHaveFlagExamples/ShouldNotHaveFlag.exceptionText.approved.txt |
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
3 changes: 3 additions & 0 deletions
3
src/DocumentationExamples/CodeExamples/ShouldHaveFlag.codeSample.approved.txt
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,3 @@ | ||
var actual = TestEnum.FlagTwo; | ||
var value = TestEnum.FlagOne; | ||
actual.ShouldHaveFlag(value); |
5 changes: 5 additions & 0 deletions
5
src/DocumentationExamples/CodeExamples/ShouldHaveFlag.exceptionText.approved.txt
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,5 @@ | ||
shouldMethod | ||
should have flag | ||
TestEnum.FlagOne | ||
but had | ||
TestEnum.FlagTwo |
3 changes: 3 additions & 0 deletions
3
...les/CodeExamples/ShouldHaveFlagNotHaveFlagExamples.ShouldHaveFlag.codeSample.approved.txt
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,3 @@ | ||
var actual = TestEnum.FlagTwo; | ||
var value = TestEnum.FlagOne; | ||
actual.ShouldHaveFlag(value); |
5 changes: 5 additions & 0 deletions
5
.../CodeExamples/ShouldHaveFlagNotHaveFlagExamples.ShouldHaveFlag.exceptionText.approved.txt
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,5 @@ | ||
shouldMethod | ||
should have flag | ||
TestEnum.FlagOne | ||
but had | ||
TestEnum.FlagTwo |
3 changes: 3 additions & 0 deletions
3
.../CodeExamples/ShouldHaveFlagNotHaveFlagExamples.ShouldNotHaveFlag.codeSample.approved.txt
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,3 @@ | ||
var actual = TestEnum.FlagOne; | ||
var value = TestEnum.FlagOne; | ||
actual.ShouldNotHaveFlag(value); |
5 changes: 5 additions & 0 deletions
5
...deExamples/ShouldHaveFlagNotHaveFlagExamples.ShouldNotHaveFlag.exceptionText.approved.txt
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,5 @@ | ||
shouldMethod | ||
should not have flag | ||
TestEnum.FlagOne | ||
but it had | ||
TestEnum.FlagOne |
3 changes: 3 additions & 0 deletions
3
src/DocumentationExamples/CodeExamples/ShouldNotHaveFlag.codeSample.approved.txt
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,3 @@ | ||
var actual = TestEnum.FlagOne; | ||
var value = TestEnum.FlagOne; | ||
actual.ShouldNotHaveFlag(value); |
5 changes: 5 additions & 0 deletions
5
src/DocumentationExamples/CodeExamples/ShouldNotHaveFlag.exceptionText.approved.txt
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,5 @@ | ||
shouldMethod | ||
should not have flag | ||
TestEnum.FlagOne | ||
but it had | ||
TestEnum.FlagOne |
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
55 changes: 55 additions & 0 deletions
55
src/DocumentationExamples/ShouldHaveFlagNotHaveFlagExamples.cs
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,55 @@ | ||
using System; | ||
using Shouldly.ShouldlyExtensionMethods; | ||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace DocumentationExamples | ||
{ | ||
public class ShouldHaveFlagNotHaveFlagExamples | ||
{ | ||
readonly ITestOutputHelper _testOutputHelper; | ||
|
||
public ShouldHaveFlagNotHaveFlagExamples(ITestOutputHelper testOutputHelper) | ||
{ | ||
_testOutputHelper = testOutputHelper; | ||
} | ||
|
||
[Fact] | ||
public void ShouldHaveFlag() | ||
{ | ||
DocExampleWriter.Document(() => | ||
{ | ||
var actual = TestEnum.FlagTwo; | ||
var value = TestEnum.FlagOne; | ||
actual.ShouldHaveFlag(value); | ||
}, _testOutputHelper); | ||
} | ||
|
||
|
||
[Fact] | ||
public void ShouldNotHaveFlag() | ||
{ | ||
DocExampleWriter.Document(() => | ||
{ | ||
var actual = TestEnum.FlagOne; | ||
var value = TestEnum.FlagOne; | ||
actual.ShouldNotHaveFlag(value); | ||
}, _testOutputHelper); | ||
} | ||
|
||
[Flags] | ||
public enum TestEnum | ||
{ | ||
FlagOne = 1, | ||
FlagTwo = 2, | ||
FlagThree = 4 | ||
} | ||
|
||
public enum TestEnumWithoutFlagAttribute | ||
{ | ||
FlagOne, | ||
FlagTwo, | ||
FlagThree | ||
} | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Shouldly.Shared/MessageGenerators/ShouldHaveFlagMessageGenerator.cs
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,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Shouldly.MessageGenerators | ||
{ | ||
internal class ShouldHaveFlagMessageGenerator : ShouldlyMessageGenerator | ||
{ | ||
public override bool CanProcess(IShouldlyAssertionContext context) | ||
{ | ||
return context.ShouldMethod == "ShouldHaveFlag"; | ||
} | ||
|
||
public override string GenerateErrorMessage(IShouldlyAssertionContext context) | ||
{ | ||
var codePart = context.CodePart; | ||
var expectedValue = context.Expected.ToStringAwesomely(); | ||
|
||
var actual = context.Actual.ToStringAwesomely(); | ||
var actualString = codePart == actual ? " did not" : $@" had | ||
{actual}"; | ||
|
||
return $@"{codePart} | ||
should have flag | ||
{expectedValue} | ||
but{actualString}"; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/Shouldly.Shared/MessageGenerators/ShouldNotHaveFlagMessageGenerator.cs
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,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace Shouldly.MessageGenerators | ||
{ | ||
internal class ShouldNotHaveFlagMessageGenerator : ShouldlyMessageGenerator | ||
{ | ||
public override bool CanProcess(IShouldlyAssertionContext context) | ||
{ | ||
return context.ShouldMethod == "ShouldNotHaveFlag"; | ||
} | ||
|
||
public override string GenerateErrorMessage(IShouldlyAssertionContext context) | ||
{ | ||
var codePart = context.CodePart; | ||
var expectedValue = context.Expected.ToStringAwesomely(); | ||
|
||
var actual = context.Actual.ToStringAwesomely(); | ||
var actualString = codePart == actual ? " had" : $@" it had | ||
{actual}"; | ||
|
||
return $@"{codePart} | ||
should not have flag | ||
{expectedValue} | ||
but{actualString}"; | ||
} | ||
} | ||
} |
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
81 changes: 81 additions & 0 deletions
81
src/Shouldly.Shared/ShouldlyExtensionMethods/ShouldBeEnumExtensions.cs
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,81 @@ | ||
using System; | ||
using JetBrains.Annotations; | ||
|
||
#if PORTABLE | ||
using System.Reflection; | ||
#endif | ||
|
||
namespace Shouldly.ShouldlyExtensionMethods | ||
{ | ||
public static class ShouldHaveEnumExtensions | ||
{ | ||
public static void ShouldHaveFlag(this Enum actual, Enum expectedFlag) | ||
=> ShouldHaveFlag(actual, expectedFlag, () => null); | ||
|
||
public static void ShouldHaveFlag(this Enum actual, Enum expectedFlag, string customMessage) | ||
=> ShouldHaveFlag(actual, expectedFlag, () => customMessage); | ||
|
||
public static void ShouldHaveFlag(this Enum actual, Enum expectedFlag, [InstantHandle] Func<string> customMessage) | ||
{ | ||
CheckEnumHasFlagAttribute(actual); | ||
if (!actual.HasFlag(expectedFlag)) | ||
{ | ||
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expectedFlag, actual, customMessage).ToString()); | ||
} | ||
} | ||
|
||
public static void ShouldNotHaveFlag(this Enum actual, Enum expectedFlag) | ||
=> ShouldNotHaveFlag(actual, expectedFlag, () => null); | ||
|
||
public static void ShouldNotHaveFlag(this Enum actual, Enum expectedFlag, string customMessage) | ||
=> ShouldNotHaveFlag(actual, expectedFlag, () => customMessage); | ||
|
||
public static void ShouldNotHaveFlag(this Enum actual, Enum expectedFlag, | ||
[InstantHandle] Func<string> customMessage) | ||
{ | ||
CheckEnumHasFlagAttribute(actual); | ||
if (actual.HasFlag(expectedFlag)) | ||
{ | ||
throw new ShouldAssertException(new ExpectedActualShouldlyMessage(expectedFlag, actual, customMessage).ToString()); | ||
} | ||
} | ||
|
||
static void CheckEnumHasFlagAttribute(Enum actual) | ||
{ | ||
#if PORTABLE | ||
if (!actual.GetType().GetTypeInfo().IsDefined(typeof(FlagsAttribute), false)) | ||
#else | ||
if (!actual.GetType().IsDefined(typeof(FlagsAttribute), false)) | ||
#endif | ||
{ | ||
throw new ArgumentException("Enum doesn't have Flags attribute", nameof(actual)); | ||
} | ||
} | ||
|
||
#if net35 | ||
/* If the .NET Framework doesn't have a HasFlag, patch in our own version. | ||
Made by decompiling the HasFlag function on enum in .NET Framework version 4.5.1 */ | ||
static bool HasFlag(this Enum enumeration, Enum value) | ||
{ | ||
if (enumeration == null) | ||
{ | ||
return false; | ||
} | ||
|
||
if (value == null) | ||
{ | ||
throw new ArgumentException(nameof(value)); | ||
} | ||
|
||
if (!Enum.IsDefined(enumeration.GetType(), value)) | ||
{ | ||
throw new ArgumentException($"Enumeration type mismatch. The flag is of type '{value.GetType()}', was expecting {enumeration.GetType()}"); | ||
} | ||
|
||
var longValue = Convert.ToUInt64(value); | ||
|
||
return (Convert.ToUInt64(enumeration) & longValue) == longValue; | ||
} | ||
#endif | ||
} | ||
} |
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
78 changes: 78 additions & 0 deletions
78
src/Shouldly.Tests.Shared/ShouldHaveFlag/ShouldHaveFlagScenario.cs
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,78 @@ | ||
using System; | ||
using Shouldly.ShouldlyExtensionMethods; | ||
using Shouldly.Tests.Strings; | ||
using Xunit; | ||
|
||
namespace Shouldly.Tests.Shared.ShouldHaveFlag | ||
{ | ||
public class ShouldHaveFlagScenario | ||
{ | ||
[Fact] | ||
public void FlagScenarioShouldFail() | ||
{ | ||
var actual = TestEnum.FlagTwo; | ||
var value = TestEnum.FlagOne; | ||
|
||
Verify.ShouldFail(() => actual.ShouldHaveFlag(value, "Some additional context"), | ||
errorWithSource: | ||
@"Verify | ||
should have flag | ||
TestEnum.FlagOne | ||
but had | ||
TestEnum.FlagTwo | ||
Additional Info: | ||
Some additional context", | ||
errorWithoutSource: | ||
@"TestEnum.FlagTwo | ||
should have flag | ||
TestEnum.FlagOne | ||
but did not | ||
Additional Info: | ||
Some additional context"); | ||
} | ||
|
||
[Fact] | ||
public void ShouldThrowException() | ||
{ | ||
var actual = TestEnumWithoutFlagAttribute.FlagOne; | ||
var value = TestEnumWithoutFlagAttribute.FlagOne; | ||
|
||
Should.Throw<ArgumentException>(() => actual.ShouldHaveFlag(value)); | ||
} | ||
|
||
[Fact] | ||
public void ShouldPassOneFlagSet() | ||
{ | ||
var actual = TestEnum.FlagOne; | ||
var value = TestEnum.FlagOne; | ||
|
||
actual.ShouldHaveFlag(value); | ||
} | ||
|
||
[Fact] | ||
public void ShouldPassTwoFlagsSet() | ||
{ | ||
var actual = TestEnum.FlagOne | TestEnum.FlagTwo; | ||
var value = TestEnum.FlagTwo; | ||
|
||
actual.ShouldHaveFlag(value); | ||
} | ||
} | ||
|
||
[Flags] | ||
public enum TestEnum | ||
{ | ||
FlagOne = 1, | ||
FlagTwo = 2, | ||
FlagThree = 4 | ||
} | ||
|
||
public enum TestEnumWithoutFlagAttribute | ||
{ | ||
FlagOne, | ||
FlagTwo, | ||
FlagThree | ||
} | ||
} |
Oops, something went wrong.