Sunday, September 20, 2015

The Cloud-Capped Towers OR AutoFixture

"The cloud-capped towers, the gorgeous palaces,
The solemn temples, the great globe itself,
Yea, all which it inherit, shall dissolve,
And, like this insubstantial pageant faded,

Leave not a rack behind. We are such stuff
"
-- Shakespeare, The Tempest
Act IV, Scene I, Lines 152 -156

In my day-to-day work I do a lot of .Net programming.  It seem at some point in each of the applications I am either enhancing or creating I ended including Mark Seemann's AutoFixture (if it is not already in use).  AutoFixture is an easy way to create a fixture object.  A fixture object is an object which centralizes your helper methods in your test code, like methods which create your system under test and help generate test data.

Fixture objects are great and I often find myself wanting one in my day-to-day work, but I am lazy.  Since I am lazy I do not want to go to all the trouble of creating my own fixture object, to quote Homer Simpson, "Can't someone else do it".  Luckily in the .Net realm someone already has, Mark Seemann.  AutoFixture lets you get the best of all worlds, you get a fixture object and you do not have to write the framework around it!  (working with it for a few years now, I can say it is well thought out and not a big hair ball, see also Simple Made Easy for the full reference)

How about some examples?  (taken from the AutoFixture cheat sheet and rewritten using xUnit)

using System;
using System.Collections.Generic;
using System.Linq;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.Xunit2;
using Xunit;
namespace AutoFixtureDemo
{
public class Demo
{
public class MyClass
{
public int Echo(int expected)
{
return expected;
}
}
[Fact]
public void IntroTetFromGitHub()
{
var fixture = new Fixture();
var expected = fixture.Create<int>();
var sut = fixture.Create<MyClass>();
var actual = sut.Echo(expected);
Assert.Equal(expected, actual);
}
[Theory, AutoData]
public void IntroXunitTetFromGitHub(int expected, MyClass sut)
{
var actual = sut.Echo(expected);
Assert.Equal(expected, actual);
}
[Theory, AutoData]
public void CheatSheetTestsForStrings(Fixture fixture)
{
Assert.IsType<string>(fixture.Create<string>());
var seed = "seed";
Assert.Contains(seed, fixture.Create(seed));
}
[Theory, AutoData]
public void CheatSheetTestsForNumbers(Fixture fixture)
{
Assert.IsType<int>(fixture.Create<int>());
}
public class ComplexType
{
public string Name;
public int Number;
}
[Theory, AutoData]
public void CheatSheetTestsForComplexTypes(ComplexType complexType)
{
Assert.NotNull(complexType.Name);
Assert.NotNull(complexType.Number);
Assert.Contains("Name", complexType.Name);
}
public abstract class AbstractType {}
public class MyFakeAbstract : AbstractType { }
public interface IInterface { }
public class MyFakeInterface : IInterface { }
[Theory, AutoData]
public void CheatSheetTestsForAbstractTypes(Fixture fixture)
{
fixture.Register<AbstractType>(() => new MyFakeAbstract());
fixture.Register<IInterface>(() => new MyFakeInterface());
Assert.IsType<MyFakeAbstract>(fixture.Create<AbstractType>());
Assert.IsType<MyFakeInterface>(fixture.Create<IInterface>());
}
[Theory, AutoData]
public void CheatSheetTestsForReplacingDefaultAlgorithms(string replacement, Fixture fixture)
{
Assert.NotEqual(replacement, fixture.Create<string>());
fixture.Register<string>(() => replacement);
var actual = fixture.Create<string>();
Assert.Equal(replacement, actual);
}
[Theory, AutoData]
public void CheatSheetTestsSequences(Fixture fixture)
{
Assert.Equal(3, fixture.CreateMany<string>().Count());
Assert.Equal(3, fixture.CreateMany<int>().Count());
Assert.Equal(3, fixture.CreateMany<int>().Distinct().Count());
const int expected = 100;
Assert.Equal(expected, fixture.CreateMany<int>(expected).Count());
Assert.Equal(3, fixture.CreateMany<MyClass>().Count());
}
[Theory, AutoData]
public void CheatSheetTestsAddManyTo(Fixture fixture)
{
var list = new List<int>();
Assert.Equal(0, list.Count);
fixture.AddManyTo(list);
Assert.Equal(3, list.Count);
}
public class MyClassWithProperties
{
public int Number;
public string SomeString { get; set; }
}
[Theory, AutoData]
public void CheatSheetTestsSetProperty(Fixture fixture)
{
var expected = "my string";
var actual = fixture
.Build<MyClassWithProperties>()
.With(w => w.SomeString, expected)
.Create();
Assert.Equal(expected, actual.SomeString);
actual = fixture
.Build<MyClassWithProperties>()
.Without(w => w.SomeString)
.Create();
Assert.Null(actual.SomeString);
var counter = 0;
fixture
.Build<MyClass>()
.Do(_ => ++counter)
.Create();
Assert.Equal(1, counter);
}
[Theory, AutoData]
public void CheatSheetTestsCustomizeTypes(Fixture fixture)
{
var expected = fixture.Create<int>();
var counter = 0;
fixture
.Customize<MyClassWithProperties>(c => c
.Do(_ => ++counter)
.WithAutoProperties()
.With(w => w.Number, expected));
var myClass = fixture.Create<MyClassWithProperties>();
Assert.Equal(1, counter);
Assert.Equal(expected, myClass.Number);
}
}
}
view raw Demo.cs hosted with ❤ by GitHub


We see in the above lots of wonderful things.
  • We can walk up to the fixture object and ask it for some test data.  
  • We can use the AutoData attribute and ask for test data.  
  • We can register implementation for abstract types.  
  • We can create collections of test data.
  • We can build specific test data saying what attributes we care about and letting the fixture object set up the rest.
  • We can even have a do method to allow for modification outside of the object we are having the fixture object create (this is not good design but sometimes it is needed).
As I work more with AutoFixture I find more and more uses for it.

Another framework I use a lot in my day-to-day .Net programming is Moq.  Guess what, AutoFixture can be uses as an auto-mocking container with Moq (and it has plugins for other mocking frameworks too).

Yet another example.  (using MS Test taken from an overview of AutoFixture I did at work recently)

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoMoq;
namespace AutoFixtureDemo.MSTest
{
public class Echoer
{
private ILogger _logger;
private readonly ISaver _saver;
public Echoer(ILogger logger, ISaver saver)
{
_logger = logger;
_saver = saver;
}
public string Echo(string value)
{
_logger.Log(value);
var result = _saver.Save(value);
if (result.Result) _logger.Log("Successfully saved!");
return value;
}
}
[TestClass]
public class Demo
{
private IFixture _fixture;
private Echoer _sut;
private Mock<ILogger> _spyLogger;
private Mock<ISaver> _spySaver;
[TestInitialize]
public void BeforeEach()
{
_fixture = new Fixture().Customize(new AutoMoqCustomization());
_spyLogger = _fixture.Freeze<Mock<ILogger>>();
_spySaver = _fixture.Freeze<Mock<ISaver>>();
_sut = _fixture.Create<Echoer>();
}
[TestMethod]
public void GivenStringItMustEchoIt()
{
var expected = _fixture.Create<string>();
var actual = _sut.Echo(expected);
Assert.AreEqual(expected, actual);
}
[TestMethod]
public void GivenNullItMustReturnNull()
{
var actual = _sut.Echo(null);
Assert.IsNull(actual);
}
[TestMethod]
public void GivenStringItMustLogIt()
{
var expected = _fixture.Create<string>();
_sut.Echo(expected);
_spyLogger.Verify(v => v.Log(expected));
}
[TestMethod]
public void GivenStringItMustSaveIt()
{
var expected = _fixture.Create<string>();
_sut.Echo(expected);
_spySaver.Verify(v => v.Save(expected));
}
[TestMethod]
public void GivenStringAndSuccessfulSaveItMustLogSuccess()
{
_spySaver
.Setup(s => s.Save(It.IsAny<string>()))
.Returns(_fixture.Build<SaverResult>().With(w => w.Result, true).Create());
_sut.Echo(_fixture.Create<string>());
_spyLogger.Verify(v => v.Log("Successfully saved!"));
}
}
public interface ILogger
{
void Log(string message);
}
public interface ISaver
{
SaverResult Save(string value);
}
public class SaverResult
{
public bool Result { get; set; }
}
}
view raw Demo.cs hosted with ❤ by GitHub


We see in this example that we had a simple class called Echo which got top hatted into having logging and a backup added to it.  The interactions with the logger and back-upper need to be tested, luckily we can tell the fixture object that we would like to get spy objects for the logger and back-upper.  These spies from AutoFixture are Moq mocks which allows us to verify that the behaviors we want.

By using AutoFixture and Moq we can meet all the "needs" of Top Hats everywhere.

(The term Top Hats comes from Uncle Bob's Clean Coder series episode 7, in which there is a scene with an Architect talking about choosing an IDE and Database for a project hence the term Top Hat and top hatting to describe this type of "architecture".)

I find that AutoFixture allows me to simplify my test code (simple as discussed in Simple Made Easy) and allows me to stay focus on what I am actually trying to test.

Sunday, September 13, 2015

Filter, Guarding You From Fold

"You and your ways; whose wraths to guard you from,"
-- Shakespeare, The Tempest

Filter


Filter is a lot like a higher order functional bouncer, it prevents undesirable elements from getting into the resulting collection.  


A simple example would be a filtering with a function which says if a value is odd or not.




In Dr. Hutton's excellent paper, "A tutorial on the universality and expressiveness of fold", he gives the following definition of Filter in terms of Fold:

filter :: (α → Bool) → ([α] → [α])
filter p = fold (λx xs → if p x then x : xs else xs) [ ]

From the definition above, we see that Filter is just Map but with a predicate clause stating if the element should be placed in the resulting collection or not.


Let us look at an example using my name Mike and a function which will check if a character is lower case.

(similar to Map last time)


First Memoize has nothing and X has M.


Next Memoize still has nothing (since M is upper case) and X has i.


Now Memoize has i and X has k.


Then Memoize has i and k while X has e.


Now the Filter will return the result of "ike".

Now what we all came to see, some code examples.

Clojure


(ns fold-filter)
(defn fold-filter
"filter p = fold (λx xs → if p x then x : xs else xs) [ ]"
[pred coll]
(reduce #(if (pred %2) (conj %1 %2) %1) [] coll))
view raw fold_filter.clj hosted with ❤ by GitHub
(ns fold-filter.tests
(require
[clojure.test :refer [deftest testing is are run-tests]]
[fold-filter :as sut]))
(deftest fold-filter-tests
(testing "Given empty collection it must return empty"
(is (empty? (sut/fold-filter (constantly true) []))))
(testing "Given always true predicate it must return collection given"
(are [coll] (= coll (sut/fold-filter (constantly true) coll))
[]
[1]
[1 2 3]
[1 1 2 3 5 8 13 21 34]
["Mike" "Harris" "is" "a" "clojure" "programmer"]))
(testing "Given integer collection and related predicates it must return same as filter"
(are [pred]
(are [coll] (= (filter pred coll) (sut/fold-filter pred coll))
[]
[1]
[1 2 3]
[1 1 2 3 5 8 13 21 34])
even?
odd?
zero?
pos?
(partial identical? 2)
(partial > 7)))
(testing "Given string collection and related predicates it must return same as filter"
(are [pred]
(are [coll] (= (filter pred coll) (sut/fold-filter pred coll))
["" "" ""]
["Hello"]
["Mike" "Harris" "is" "a" "clojure" "programmer"]
["mIxEd" "CaSe"]
["all" "lower" "case"])
clojure.string/blank?
#(.endsWith "r" %)
#(= (clojure.string/lower-case %) %)
#(< (count %) 5)))
)
(run-tests)

In Clojure we can use the reduce function to move through the collection checking the predicate condition with the if macro guard against undesirable elements.

C#


using System;
using System.Collections.Generic;
using System.Linq;
namespace Folder
{
public class Filters
{
public static IEnumerable<T> Filter<T>(
Func<T, bool> pred, IEnumerable<T> coll)
{
return coll.Aggregate(
new List<T>(),
(m, x) =>
{
if (pred(x)) m.Add(x);
return m;
});
}
}
}
view raw Filters.cs hosted with ❤ by GitHub
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
using Folder;
using static System.Char;
namespace FolderTests
{
public class FiltersTests
{
[Fact]
public void GivenEmptyCollectionItMustReturnEmpty()
{
Assert.Empty(Filters.Filter(_ => true, new List<int>()));
}
[Theory]
[InlineData(new[] { 1 })]
[InlineData(new[] { 1, 2, 3 })]
[InlineData(new[] { 1, 1, 2, 3, 5, 8, 13, 21, 34 })]
public void GivenAlwaysTrueItMustReturnCollectionGiven(int[] coll)
{
Assert.Equal(coll, Filters.Filter(_ => true, coll));
}
[Theory]
[InlineData(new[] {1})]
[InlineData(new[] {1, 2, 3})]
[InlineData(new[] {1, 1, 2, 3, 5, 8, 13, 21, 34})]
public void GivenIntegersAndPredicateItMustReturnSameASWhere(int[] coll)
{
var predicates = new List<Func<int, bool>>
{
x => x%2 == 0,
x => x%2 != 0,
x => x == x*x,
x => x > 5
};
predicates.ForEach(f => Assert.Equal(
coll.Where(f),
Filters.Filter(f, coll)));
}
[Fact]
public void GivenStringsAndPredicateItMustReturnSameAsWhere()
{
var colls = new List<string[]>
{
new[] {"Hello"},
new[] {"Mike", "Harris", "is", "a", "C#", "programmer"},
new[] {"mIxEd", "CaSe"},
new[] {"all", "lower", "case"}
};
var predicates = new List<Func<string, bool>>
{
s => s.EndsWith("r"),
s => s.All(IsLower),
s => s.Length < 5
};
colls.ForEach(coll =>
predicates.ForEach(f => Assert.Equal(
coll.Where(f),
Filters.Filter(f, coll))));
}
}
}
view raw FiltersTests.cs hosted with ❤ by GitHub

With the C# code we'll need to create some type of collection with the seed value which will allow us to add elements to it, we'll use the collection class of List.  We'll simply iterate through the collection using Aggregate and add elements to the memoize List if they pass the predicate clause.

ECMAScript 2015


let _ = require("lodash");
exports.foldFilter = (pred, coll) =>
_.foldl(coll, (m, x) => { if(pred(x)) m.push(x); return m; }, []);
view raw foldFilter.js hosted with ❤ by GitHub
let sut = require("../src/foldFilter"),
expect = require("expect.js"),
_ = require("lodash");
describe("foldFilter", () => {
it("Given an empty collection it must return empty", () => {
expect(sut.foldFilter(_.constant(true), [])).to.be.empty();
}),
it("Given always true predicate it must return collection given", () => {
let alwaysTrue = _.partial(sut.foldFilter, _.constant(true));
_.map([
[],
[1],
[1, 2, 3],
[1, 1, 2, 3, 5, 8, 13, 21, 34],
["Mike", "Harris", "is", "a", "ECMAScript 2015", "programmer"]
],
coll => expect(alwaysTrue(coll)).to.eql(coll));
}),
it("Given integer collection and related predicates it must return same as filter", () => {
_.map([
x => x % 2 === 0,
x => x % 2 !== 0,
_.partial(_.eq, 0),
_.partial(_.gt, 0),
x => x === 2,
_.partial(_.gt, 7)
],
pred => _.map([
[],
[1],
[1, 2, 3],
[1, 1, 2, 3, 5, 8, 13, 21, 34]
],
coll =>
expect(sut.foldFilter(pred, coll))
.to.eql(_.filter(coll, pred))));
}),
it("Given string collection and related predicates it must return same as filter", () => {
_.map([
_.partial(_.eq, ""),
s => _.endsWith(s, "r"),
s => s.toLowerCase() === s,
s => s.length === 5
],
pred => _.map([
["", "", ""],
["Hello"],
["Mike", "Harris", "is", "a", "ECMAScript 2015", "programmer"],
["mIxEd", "CaSe"],
["all", "lower", "case"]
],
coll =>
expect(sut.foldFilter(pred, coll))
.to.eql(_.filter(coll, pred))));
});
});

In the code above that we'll use the build in JavaScript array method of push to add an element which gets past the guarding predicate function to the array at the end.  We are simply move through the collection we are filtering, pushing elements into the memoize array.

Fin


There you have it we are able to keep the riffraff elements out using Fold. 

Monday, September 7, 2015

Folding into Map

"I have forgot the map."
-- Shakespeare, Henry IV Part I
Act III, Scene I, Line 5

Map


Map is the first of the big three Higher Order Function we will looking at Folding using Dr. Hutton's excellent paper, "A tutorial on the universality and expressiveness of fold", as a guide.  The idea of a Map is simple, you have a collection which you wish to apply some translation function against every member of the collection.

We can look at a few simple examples, the first of which would be the identity function.


 Next we can look at a constant function which maps turtle for any value given to it.


Last we can look at a function which shifts everything given to it.


All of this these functions have something in common, they all apply a function against every single member of the collection they are given, thus the resulting collection is the exact same size as the collection given to it.

We can now look at how we would implement Map using a Fold.  We see that Dr. Hutton gives Map the following definition:

map :: (α → β) → ([α] → [β])
map f = fold (λx xs → f x : xs) [ ]

Folding a Map we'll need a collection to seed with then we just apply the given function against each member concatenating it with the seed.


(similar to Reverse from last time)

Let us look at an example of upper casing Mike.


First Memoize has nothing and X has M.


Second Memoize has M and X has i.


Third time Memoize has MI and X has k.


Finally Memoize has MIK and X has e.


Giving the final result of MIKE.  Now let us look at some code examples.

Clojure


(ns fold-map)
(defn fold-map
"map f = fold (λx xs → f x : xs) [ ]"
[f coll]
(reduce #(conj %1 (f %2)) [] coll))
view raw fold_map.clj hosted with ❤ by GitHub
(ns fold-map.tests
(require
[clojure.test :refer [deftest testing is are run-tests]]
[fold-map :as sut]))
(deftest fold-map-tests
(testing "Given an empty collection it must return an empty collection"
(is (empty? (sut/fold-map identity []))))
(testing "Given the identity function it must return collection given"
(are [coll] (= coll (sut/fold-map identity coll))
[]
[1]
[1 2 3 4 5]
["Mike" "Harris" "clojure" "programmer"]
[\a \b \c]))
(testing "Given function and an int collection it must return the same as applying map"
(are [f]
(are [coll] (= (map f coll) (sut/fold-map f coll))
[]
[1]
[1 2 3 4 5]
[1 1 2 3 5 8 13 21 34])
identity
inc
#(* %1 %1)
dec))
(testing "Given function and string collection it must return same as applying map"
(are [f]
(are [coll] (= (map f coll) (sut/fold-map f coll))
[]
["Kelsey"]
["Jack"]
["Mike" "Harris" "clojure" "programmer"]
["mIxEd CaSe"])
identity
clojure.string/lower-case
clojure.string/upper-case
clojure.string/capitalize)))
(run-tests)


In Clojure we use the reduce function to Fold.  We give the reduce a seed of an empty collection and use conj to join applying the function given with the resulting collection.

C#


using System;
using System.Collections.Generic;
using System.Linq;
namespace Folder
{
public class Mapper
{
public static IEnumerable<T> Map<T>(
IEnumerable<T> coll, Func<T, T> func)
{
return coll.Aggregate(
new List<T>(),
(m, x) =>
{
m.Add(func(x));
return m;
});
}
}
}
view raw Mapper.cs hosted with ❤ by GitHub
using System;
using System.Collections.Generic;
using System.Linq;
using Folder;
using Xunit;
namespace FolderTests
{
public class MapperTests
{
[Fact]
public void GivenEmptyCollectionItMustReturnEmptyCollection()
{
Assert.Empty(Mapper.Map<int>(new List<int>(), x => x));
}
[Fact]
public void GivenIdentityFunctionItMustReturnSameCollection()
{
Func<int, int> identity = x => x;
var expected = new List<int> { 1, 2, 3, 4, 5 };
Assert.Equal(expected, Mapper.Map<int>(expected, identity));
}
[Fact]
public void GivenIncrementFunctionItMustReturnNextValuesInCollection()
{
Func<int, int> increment = x => x + 1;
var collection = new List<int> { 1, 2, 3, 4, 5, 5, 6, 42 };
Assert.Equal(
collection.Select(increment),
Mapper.Map<int>(collection, increment));
}
[Fact]
public void GivenFunctionItMustReturnSameAsSelect()
{
var funcs = new List<Func<int, int>>
{
x => x,
x => x + 1,
x => x*x,
x => x - 1
};
var collection = new List<int> { 1, 1, 2, 3, 5, 8, 13, 21, 34 };
funcs.ForEach(f => Assert.Equal(
collection.Select(f),
Mapper.Map(collection, f)));
}
}
}
view raw MapperTests.cs hosted with ❤ by GitHub


With C# we use the aggregate LINQ function to Fold.  We give the aggregate a List and add the result of applying each member of the given collection against the function we are mapping with.

ECMAScript 2015


let _ = require("lodash");
exports.foldMap = (f, coll) =>
_.foldl(coll, (m, x) => { m.push(f(x)); return m; }, []);
view raw foldMap.js hosted with ❤ by GitHub
let sut = require("../src/foldMap"),
expect = require("expect.js"),
_ = require("lodash");
describe("foldMap", () => {
it("Given an empty collection it must return an empty collection", () => {
expect(sut.foldMap(_.identity, [])).to.be.empty();
}),
it("Given identity function it must return given collection", () => {
let identity = _.partial(sut.foldMap, _.identity),
expectSame = (f, coll) => expect(f(coll)).to.eql(coll);
expect(identity([])).to.be.empty();
expectSame(identity, [1]);
expectSame(identity, [1, 2, 3, 4, 5]);
expectSame(identity,
["Mike", "Harris", "ECMAScript 2015", "programmer"]);
expectSame(identity, ['a', 'b', 'c']);
}),
it("Given function and an int collection it must return the same as applying map", () => {
let expectEql = (f, coll) =>
expect(sut.foldMap(f, coll)).to.eql(_.map(coll, f));
_.map([_.identity, x => x + 1, x => x * x, x => x - 1],
f => {
expectEql(f, []);
expectEql(f, [1]);
expectEql(f, [1, 2, 3, 4, 5]);
expectEql(f, [1, 1, 2, 3, 5, 8, 13, 21, 34]);
});
}),
it("Given function and string collection it must return same as applying map", () => {
let expectEql = (f, coll) =>
expect(sut.foldMap(f, coll)).to.eql(_.map(coll, f));
_.map([_.identity, _.camelCase, _.capitalize, _.snakeCase],
f => {
expectEql(f, []);
expectEql(f, ["Kelsey"]);
expectEql(f, ["Jack"]);
expectEql(f, ["Mike", "Harris", "ECMAScript 2015", "programmer"]);
expectEql(f, ["mIxEd CaSe"]);
});
});
});
view raw foldMapSpec.js hosted with ❤ by GitHub


Using ECMAScript 2015 (aka JavaScript), we use lodash's foldl to Fold.  We give the foldl an empty array and push the result of applying the function given against the member we are mapping against.

To End All


There you have it by folding with an empty collection and applying the given function against each member adding the result against the seed and you have a Map.