Created
May 4, 2014 21:58
-
-
Save moodmosaic/f8d748242261016e1692 to your computer and use it in GitHub Desktop.
AutoFixture conventions with Albedo (as described in http://blog.ploeh.dk/2014/05/01/autofixture-conventions-with-albedo)
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
module Scenarios | |
open Ploeh.AutoFixture | |
open Ploeh.AutoFixture.Kernel | |
open Ploeh.Albedo | |
open Ploeh.Albedo.Refraction | |
open System | |
type TextEndsWithConvention(value, found) = | |
inherit ReflectionVisitor<bool>() | |
let proceed x = | |
TextEndsWithConvention (value, x || found) :> IReflectionVisitor<bool> | |
let isMatch t (name : string) = | |
t = typeof<string> | |
&& name.EndsWith(value, StringComparison.OrdinalIgnoreCase) | |
override this.Value = found | |
override this.Visit (pie : ParameterInfoElement) = | |
let pi = pie.ParameterInfo | |
isMatch pi.ParameterType pi.Name |> proceed | |
override this.Visit (pie : PropertyInfoElement) = | |
let pi = pie.PropertyInfo | |
isMatch pi.PropertyType pi.Name |> proceed | |
override this.Visit (fie : FieldInfoElement) = | |
let fi = fie.FieldInfo | |
isMatch fi.FieldType fi.Name |> proceed | |
static member Matches value request = | |
let refraction = | |
CompositeReflectionElementRefraction<obj>( | |
[| | |
ParameterInfoElementRefraction<obj>() :> IReflectionElementRefraction<obj> | |
PropertyInfoElementRefraction<obj>() :> IReflectionElementRefraction<obj> | |
FieldInfoElementRefraction<obj>() :> IReflectionElementRefraction<obj> | |
|]) | |
let r = refraction.Refract [request] | |
r.Accept(TextEndsWithConvention(value, false)).Value | |
type DateStringCustomization() = | |
let builder = { | |
new ISpecimenBuilder with | |
member this.Create(request, context) = | |
if request |> TextEndsWithConvention.Matches "date" | |
then box ((context.Resolve typeof<DateTimeOffset>).ToString()) | |
else NoSpecimen request |> box } | |
interface ICustomization with | |
member this.Customize fixture = fixture.Customizations.Add builder | |
type IdStringCustomization() = | |
let builder = { | |
new ISpecimenBuilder with | |
member this.Create(request, context) = | |
if request |> TextEndsWithConvention.Matches "id" | |
then box ((context.Resolve typeof<Guid> :?> Guid).ToString "N") | |
else NoSpecimen request |> box } | |
interface ICustomization with | |
member this.Customize fixture = fixture.Customizations.Add builder | |
type PersonRendition = { | |
Id : string | |
Name : string } | |
type CommentRendition = { | |
Author : PersonRendition | |
CreatedDate : string | |
Text : string } | |
open Xunit | |
[<Fact>] | |
let RepresentationsOfDataTypesAreCorrect () = | |
let fixture = | |
Fixture().Customize( | |
CompositeCustomization( | |
IdStringCustomization(), | |
DateStringCustomization())) | |
let actual = fixture.Create<CommentRendition>() | |
let (actualDateIsCorrect, _) = DateTimeOffset.TryParse(actual.CreatedDate) | |
let (actualGuidIsCorrect, _) = Guid.TryParse(actual.Author.Id) | |
let verify = Swensen.Unquote.Assertions.test | |
verify <@ actualDateIsCorrect && actualGuidIsCorrect @> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment