Files
linqpad/Fluent Assertion IAssertionRule Test.linq

115 lines
4.2 KiB
C#

<Query Kind="Program">
<Reference>&lt;ProgramFilesX64&gt;\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll</Reference>
<NuGetReference>Amazon.Lambda.S3Events</NuGetReference>
<NuGetReference>AWSSDK.Core</NuGetReference>
<NuGetReference>AWSSDK.S3</NuGetReference>
<NuGetReference>FluentAssertions</NuGetReference>
<Namespace>Amazon</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy.ActionIdentifiers</Namespace>
<Namespace>Amazon.Internal</Namespace>
<Namespace>Amazon.Lambda.S3Events</Namespace>
<Namespace>Amazon.MissingTypes</Namespace>
<Namespace>Amazon.Runtime</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement.Internal</Namespace>
<Namespace>Amazon.Runtime.EventStreams</Namespace>
<Namespace>Amazon.Runtime.EventStreams.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal.Auth</Namespace>
<Namespace>Amazon.Runtime.Internal.Settings</Namespace>
<Namespace>Amazon.Runtime.Internal.Transform</Namespace>
<Namespace>Amazon.Runtime.Internal.Util</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces.Internal</Namespace>
<Namespace>Amazon.S3</Namespace>
<Namespace>Amazon.S3.Encryption</Namespace>
<Namespace>Amazon.S3.Encryption.Internal</Namespace>
<Namespace>Amazon.S3.Internal</Namespace>
<Namespace>Amazon.S3.IO</Namespace>
<Namespace>Amazon.S3.Model</Namespace>
<Namespace>Amazon.S3.Model.Internal.MarshallTransformations</Namespace>
<Namespace>Amazon.S3.Transfer</Namespace>
<Namespace>Amazon.S3.Util</Namespace>
<Namespace>Amazon.Util</Namespace>
<Namespace>Amazon.Util.Internal</Namespace>
<Namespace>Amazon.Util.Internal.PlatformServices</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Converters</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
<Namespace>Newtonsoft.Json.Schema</Namespace>
<Namespace>Newtonsoft.Json.Serialization</Namespace>
<Namespace>System</Namespace>
<Namespace>System.Diagnostics</Namespace>
<Namespace>System.Diagnostics.Tracing</Namespace>
<Namespace>System.IO.Compression</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1.Utilities</Namespace>
<Namespace>ThirdParty.BouncyCastle.Math</Namespace>
<Namespace>ThirdParty.BouncyCastle.OpenSsl</Namespace>
<Namespace>ThirdParty.BouncyCastle.Utilities.IO.Pem</Namespace>
<Namespace>ThirdParty.Ionic.Zlib</Namespace>
<Namespace>ThirdParty.Json.LitJson</Namespace>
<Namespace>ThirdParty.MD5</Namespace>
<Namespace>FluentAssertions.Equivalency</Namespace>
<Namespace>FluentAssertions.Execution</Namespace>
<Namespace>FluentAssertions</Namespace>
</Query>
void Main()
{
var lt = LoginType.Something;
Action act1 = () =>
{
var ltDto = LoginTypeDto.Something;
ltDto.Should().BeEquivalentTo(lt, options => options.Using(new LoginTypeRule()));
};
Action act2 = () =>
{
var ltDto = LoginTypeDto.SomethingElse;
ltDto.Should().BeEquivalentTo(lt, options => options.Using(new LoginTypeRule()));
};
act1.Should().NotThrow<AssertionFailedException>();
act2.Should().Throw<AssertionFailedException>();
}
// Define other methods and classes here
public class LoginTypeRule : IAssertionRule
{
public bool AssertEquality(IEquivalencyValidationContext context)
{
var subjectValue = GetValueForEnum(context.Subject);
var expectationValue = GetValueForEnum(context.Expectation);
if (subjectValue == null || expectationValue == null)
{
// rule not applicable
return false;
}
Execute.Assertion
.BecauseOf(context.Because, context.BecauseArgs)
.ForCondition(subjectValue == expectationValue)
.FailWith("Expected {context:string} to be {0}{reason}, but found {1}",
context.Subject, context.Expectation);
// equality assertion handled
return true;
}
private int? GetValueForEnum(object enumValue)
{
if (enumValue is LoginType || enumValue is LoginTypeDto)
{
return (int)enumValue;
}
return null;
}
}
enum LoginType { Something, SomethingElse, Other };
enum LoginTypeDto { Something, SomethingElse, Other }