Created
March 22, 2017 11:47
-
-
Save MikeMKH/f7f91c0e734bbd8ead51fa2a70d5bdb7 to your computer and use it in GitHub Desktop.
Basic example of folktale core.check with mocha, expect.js, and jsverify
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
| const jsc = require('jsverify'); | |
| const expect = require('expect.js'); | |
| const check = require('core.check'); | |
| describe('core.check', () => { | |
| describe('given the right type it must pass', () => { | |
| jsc.property('given an int when an int is expected it must pass', jsc.int16, | |
| n => check.assert(check.Number(n)) === n | |
| ), | |
| jsc.property('given a string when an string is expected it must pass', jsc.string, | |
| s => check.assert(check.String(s)) === s | |
| ), | |
| jsc.property('given either a string or int when either a string or int is expected it must pass', jsc.bool, jsc.string, jsc.int16, | |
| (pred, s, n) => { | |
| const value = pred ? s : n; | |
| return check.assert(check.Or([check.String, check.Number])(value)) === value; | |
| } | |
| ), | |
| jsc.property('given a string when one is expected it must pass', jsc.string, | |
| s => check.String(s).cata({ | |
| Failure: e => "Something went very wrong!", | |
| Success: r => r === s | |
| }) | |
| ), | |
| jsc.property('given a string and int when it is expected a string and int it must pass', jsc.string, jsc.int32, | |
| (s, n) => { | |
| const pattern = check.Seq([check.String, check.Number]); | |
| const value = [s, n]; | |
| return check.assert(pattern(value)) === value; | |
| }), | |
| jsc.property('given a string and int with others when it is expected a string and int it must pass', jsc.string, jsc.int32, jsc.bool, jsc.asciichar, | |
| (s, n, b, c) => { | |
| const pattern = check.Seq([check.String, check.Number]); | |
| const value = [s, n, b, c]; | |
| return check.assert(pattern(value)) === value; | |
| }); | |
| }), | |
| describe('given the wrong type it must fail', () => { | |
| it('given an int when a string is expected it must fail', () => { | |
| expect(() => check.assert(check.String(1))).to.throwError(); | |
| }), | |
| it('given a string when an int is expected it must fail', () => { | |
| expect(() => check.assert(check.Number("1"))).to.throwError(); | |
| }), | |
| it('given neither a string nor an int when it expects either then it must fail', () => { | |
| expect(() => check.assert(check.Or([check.String, check.Number])({something: "else"}))); | |
| }), | |
| it('given a bool when it expected a string it must fail', () => { | |
| expect(check.String(false).cata({ | |
| Failure: e => "Ummm that is a boolean", | |
| Success: s => "How the hell did we get here?" | |
| })).to.eql("Ummm that is a boolean"); | |
| }), | |
| it('given a string and int when it expects only strings it must fail', () => { | |
| expect(() => check.assert(check.Seq([check.String, check.String])(["pass", 1]))).to.throwError(); | |
| }); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment