Last active
August 29, 2015 14:16
-
-
Save TGOlson/0687728977cc47790a52 to your computer and use it in GitHub Desktop.
Point free assertion dream
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 characters
| 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