Skip to content

Instantly share code, notes, and snippets.

View bartelink's full-sized avatar

Ruben Bartelink bartelink

View GitHub Profile
public static TRes Match<T,TRes>(this FSharpOption<T> opt, Func<T,TRes> some, Func<TRes> none)
{
if (FSharpOption<T>.get_IsSome(opt))
return some.Invoke(opt.Value);
else
return none.Invoke();
}
@bartelink
bartelink / WarmupCustomization.fs
Created February 5, 2014 14:13
F# wrapper for having AutoFixture perform post-processing on AutoFixture Specimens of a given type
type TypeThatIsARequestSpecification<'T>() =
interface IRequestSpecification with
member this.IsSatisfiedBy request =
match request with
| :? System.Type as requestType -> typeof<'T>.IsAssignableFrom( requestType)
| _ -> false
type PrepareSpecimenCommand<'T>( action) =
interface ISpecimenCommand with
member this.Execute( specimen, context) =
let instance = specimen :?> 'T
module WebApi2Helpers
// Port of answer by @nikosbaxevanis :- http://stackoverflow.com/a/19954215/11635
open System
open Ploeh.AutoFixture
open Ploeh.AutoFixture.Kernel
open Ploeh.AutoFixture.Xunit
open Ploeh.AutoFixture.AutoFoq
open System.Web.Http.Hosting
@bartelink
bartelink / ThesisExample.cs
Created November 5, 2010 12:52
Demonstrates bitbucket.org/johannesrudolph/subspec/overview
1. uses a ContextFixture (which requires Disposal)
2. keeps all Context/ContextFixture+Do+Assert/Observation in a single Test Method at all costs -- lambdas get very yucky the minute you factor calls to any of these into test methods
3. The TheoryDataProvider junk at the end is also applicable to xUnit [Theory] stuff (and you use the same [ClassData] and friends as one uses for other xunit.net stuff and just removes some cruft from writing up test cases)
4. uses sut/result/exception standard names
5. This is a sample and has stuff that doesnt scale up that I wouldnt do. e.g. dont use Conditional Logic in tests with care - something as simple as a should succeed / should fail set of cases should generally be two separate Thesis methods
Go look in bitbucket - there's a good set of tests that serve as a better set of samples.
This snippet will self destruct when someone tells me that the acceptance tests and samples in Johannes' fork are clearer and more complete
@bartelink
bartelink / SutContext.cs
Created May 16, 2011 15:27
The consumption piece for HackedDisposableTrackingCustomization.cs
static class SutContext
{
public static SutContext<T> Create<T>()
{
T sut;
return Create<T>( out sut );
}
public static SutContext<T> Create<T>( out T sut )
{
@bartelink
bartelink / Frozen.cs
Created May 16, 2011 15:14
AutoFixture Frozen extension relying on Injected Fixture
public class Frozen<T>
{
readonly T _value;
private Frozen( T t )
{
_value = t;
}
public Frozen( Fixture fixture )
@bartelink
bartelink / HackedDisposableTrackingCustomization.cs
Last active September 25, 2015 19:47
My 'teardown in reverse order' fork of AutoFixture.Kernel.DisposableTrackingCustomization (Current version works against v3.x, initial revision against 2.x)
namespace Ploeh.AutoFixture.Forked
{
class HackedDisposableTrackingCustomization : ICustomization, IDisposable
{
private readonly DisposableTrackingBehavior behavior;
public HackedDisposableTrackingCustomization()
{
this.behavior = new DisposableTrackingBehavior();
}
@bartelink
bartelink / Get-GrepWinClipboardFiles.ps1
Created July 4, 2012 11:41
Get-GrepWinClipboardFiles. Example: Get-GrepWinClipboardFiles| %{ tf checkout $_ }
function Get-ClipboardText {
$command = {
add-type -an system.windows.forms
[System.Windows.Forms.Clipboard]::GetText()
}
powershell -sta -noprofile -command $command
}
function Get-GrepWinClipboardFiles() {
get-ClipboardText | ConvertFrom-Csv -del "`t" | %{ $_.Path+"\"+$_.Name }
@bartelink
bartelink / ClassAutoData.cs
Created July 17, 2012 13:17
ClassAutoData spiking
[Theory]
[ClassAutoData( typeof( XProvider ) )]
public static void Scenario( X x, string y )
{
Console.WriteLine( x + "," + y );
}
public class ClassAutoDataAttribute : CompositeDataAttribute
{
public ClassAutoDataAttribute( Type @class )
static class Program
{
static void Main()
{
var x = new { a=0, b=1 };
f( x );
}
static void f(dynamic x)
{
Console.WriteLine(x.b);