Forked from bartelink/PostProcessWhereIsACustomization.cs
Created
January 7, 2020 12:47
-
-
Save FoC-/a00c30a2e76ab471b779513eadfd1bc4 to your computer and use it in GitHub Desktop.
Revisions
-
bartelink revised this gist
Mar 21, 2013 . 1 changed file with 16 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -37,7 +37,7 @@ ISpecimenBuilder ISpecimenBuilderTransformation.Transform( ISpecimenBuilder buil return new SpecimenBuilderPostProcessor( builder, _applyActionIfSpecimenIsOfTypeT ); } class SpecimenBuilderPostProcessor : ISpecimenBuilderNode { readonly ISpecimenBuilder _builder; readonly Action<object> _postProcess; @@ -54,6 +54,21 @@ object ISpecimenBuilder.Create( object request, ISpecimenContext context ) _postProcess( specimen ); return specimen; } ISpecimenBuilderNode ISpecimenBuilderNode.Compose( System.Collections.Generic.IEnumerable<ISpecimenBuilder> builders ) { return new SpecimenBuilderPostProcessor( new CompositeSpecimenBuilder( builders ), _postProcess ); } System.Collections.Generic.IEnumerator<ISpecimenBuilder> System.Collections.Generic.IEnumerable<ISpecimenBuilder>.GetEnumerator() { yield return _builder; } System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { yield return _builder; } } } } -
bartelink created this gist
Jul 20, 2012 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,60 @@ namespace Ploeh.AutoFixture { using Kernel; using System; public class PostProcessWhereIsACustomization<T> : ICustomization where T : class { readonly PostProcessSpecimensBehavior _behavior; public PostProcessWhereIsACustomization( Action<T> action ) { _behavior = new PostProcessSpecimensBehavior( action ); } void ICustomization.Customize( IFixture fixture ) { fixture.Behaviors.Add( _behavior ); } class PostProcessSpecimensBehavior : ISpecimenBuilderTransformation { readonly Action<object> _applyActionIfSpecimenIsOfTypeT; public PostProcessSpecimensBehavior( Action<T> action ) { _applyActionIfSpecimenIsOfTypeT = x => { var asT = x as T; if ( asT != null ) action( asT ); }; } ISpecimenBuilder ISpecimenBuilderTransformation.Transform( ISpecimenBuilder builder ) { return new SpecimenBuilderPostProcessor( builder, _applyActionIfSpecimenIsOfTypeT ); } class SpecimenBuilderPostProcessor : ISpecimenBuilder { readonly ISpecimenBuilder _builder; readonly Action<object> _postProcess; public SpecimenBuilderPostProcessor( ISpecimenBuilder builder, Action<object> initializationAction ) { _postProcess = initializationAction; _builder = builder; } object ISpecimenBuilder.Create( object request, ISpecimenContext context ) { var specimen = _builder.Create( request, context ); _postProcess( specimen ); return specimen; } } } } }