Skip to content

Instantly share code, notes, and snippets.

public interface IFoo { void MyMethod(Action<int[]> callback); }
[Test]
public void TestCallback()
{
var sub = Substitute.For<IFoo>();
var callback = Substitute.For<Action<int[]>>();
var data = new[] { 1, 2, 3 };
sub.When(x => x.MyMethod(callback))
@dtchepak
dtchepak / gist:665388
Created November 6, 2010 12:47
Fixed undefined local variable or method `docs' for #<DocsToCode:0x358cb30> by closing over local vars
require 'spec'
class DocsToCode
def docs_in(path)
[]
end
def code_in(doc)
""
end
def extract(path)
@dtchepak
dtchepak / test.cs
Created May 9, 2011 12:27
Serializing and .NET mock frameworks
public interface IRoot { string Data { get; set; } }
[Serializable]
public abstract class Root : IRoot { public abstract string Data { get; set; } }
[Test]
public void FailingTest()
{
var root = CreateMock(); // <-- create a mock with your favourite mocking framework for Root or IRoot
var formatter = new BinaryFormatter();
@dtchepak
dtchepak / Form.cs
Created June 28, 2011 03:42
Observing combinations of events where another event does not occur
var mouseDown = Observable.FromEventPattern(firstButton, "MouseDown");
var mouseUp = Observable.FromEventPattern(firstButton, "MouseUp");
var mouseMove = Observable.FromEventPattern(firstButton, "MouseMove");
mouseDown.Subscribe(x => Debug.WriteLine(">>> mouse down"));
mouseUp.Subscribe(x => Debug.WriteLine(">>> mouse up"));
(from down in mouseDown
from up in mouseUp.TakeUntil(mouseMove)
select up)
@dtchepak
dtchepak / NHibernatishExample.cs
Created September 9, 2011 11:45
Trying to replicate reported problem with NSub 1.2 and pure virtual classes
public class NHibernatishExample {
public interface IRepository { SampleEntity Get(int id); }
public class SampleEntity {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class SomeService {
private readonly IRepository _repository;
public SomeService(IRepository repository) { _repository = repository; }
public SampleEntity DoSomething() {
@dtchepak
dtchepak / rodent.txt
Created December 21, 2011 02:50
Rodent-reaching on OS X
* Retrieving a window once "minimised"
* Resuming/suspending Parallels VMs.
* Finder. All of finder, but primarily alternating between favourites on LHS and files on RHS
* Tweetdeck on Chrome [not OS X specific]
* Misson Control (accessing and using): Bind keyboard shortcut, tab to switch apps
* Visual switch between windows in same app: bind keyboard shortcut, tab/~ to switch between apps, arrows to switch windows
* Going Back through system prefs: Cmd + [, Cmd + ]
* Right click (App Key / right Win Key on Windows keyboards)
data Car = Car { make :: String, model :: String } deriving (Eq,Show)
-- Explicit implementation of Ord for Car
instance Ord Car where
compare (Car { make = aMake, model = aModel}) (Car { make = bMake, model = bModel}) =
if makeCompare == EQ then aModel `compare` bModel else makeCompare
where makeCompare = aMake `compare` bMake
-- Implementation over arbitrary number of field in record.
comp :: (Ord a) => [b -> a] -> b -> b -> Ordering
@dtchepak
dtchepak / DoThese.cs
Created February 6, 2012 11:40
Sample for running a sequence of Do actions for NSubstitute When.Do
using System;
using System.Collections.Generic;
using NSubstitute;
using NSubstitute.Core;
using NUnit.Framework;
namespace Sample {
public static class MyTestExtensions {
public static void DoThese<T>(this WhenCalled<T> when, params Action<CallInfo>[] actions) {
var actionQueue = new Queue<Action<CallInfo>>(actions);
@dtchepak
dtchepak / CustomMatchersExample.cs
Created February 17, 2012 04:23
Custom matcher via extension method vs. base class
/* Simplified custom matcher example.
This shows matching args less than a specified int value.
More common use would be checking a few properties on a complex type (e.g. Car make, model, year)
*/
//Arg.Matches.LessThan(2);
public static class Extensions {
public static int LessThan(this Match match, int number) {
return match.WhenNoErrorsFrom(x => {
@dtchepak
dtchepak / argh.hs
Created February 26, 2012 06:10
Playing around with take and fold
data TakeState a = TakeState Int a deriving (Show)
take' n =
let f (TakeState i acc) head = if i==n then (TakeState i acc) else (TakeState (i+1) (acc++[head]))
in foldl f (TakeState 0 [])