Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save TGOlson/0687728977cc47790a52 to your computer and use it in GitHub Desktop.

Select an option

Save TGOlson/0687728977cc47790a52 to your computer and use it in GitHub Desktop.
Point free assertion dream
var R = require('ramda');
// * -> IO
var report = R.bind(console.log, console);
// * -> IO
var reportSuccess = R.compose(report, R.always('Success'));
// * -> * -> IO
var reportFailure = function(expected, actual) {
report('Failure: Expected ' + expected + ' to be ' + actual);
};
// a -> (a -> b -> IO)
var bindExpectedToMatcher = function(expected) {
return R.partialRight(R.partial, expected);
};
// a -> {(a -> b -> IO)}
var expect = function(expected) {
return R.mapObj(bindExpectedToMatcher(expected), matchers);
};
// (a -> b -> Boolean) -> a -> b -> IO
var createMatcher = R.partialRight(R.ifElse, reportSuccess, reportFailure);
var matchers = {};
// (a -> b -> Boolean) -> String -> IO
var addMatcher = function(cond, name) {
matchers[name] = createMatcher(cond);
};
// {k: (a -> b -> Boolean)} -> {k: (a -> b-> IO)}
var addMatchers = R.mapObjIndexed(addMatcher);
addMatchers({
toBe: R.eq,
toEqual: R.eqDeep
});
expect(factorial(0)).toBe(1);
expect(factorial(1)).toBe(1);
expect(factorial(5)).toBe(120);
expect(factorial(6)).toBe(100); // Expected 720 to be 100
expect({}).toBe({}); // Expected [object Object] to be [object Object]
expect({}).toEqual({});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment