UNPKG

unexpected

Version:
1,187 lines (1,102 loc) 166 kB
/*global unexpected*/ // THIS FILE IS AUTOGENERATED! DO NOT CHANGE IT MANUALLY. // It is built based on the examples in the documentation folder // when the documentation site gets build by running "make site-build". it.skipIf = function (condition) { (condition ? it.skip : it).apply(it, Array.prototype.slice.call(arguments, 1)); }; describe("documentation tests", function () { var isBrowser = typeof weknowhow !== 'undefined'; var isPhantom = typeof mochaPhantomJS !== 'undefined'; var expect; beforeEach(function () { expect = unexpected.clone(); expect.output.preferredWidth = 80; }); it("api/UnexpectedError.md contains correct examples", function () { var testPromises = []; expect.addAssertion("array", "to have item satisfying", function (expect, subject) { var args = Array.prototype.slice.call(arguments, 2); var promises = subject.map(function (item) { return expect.promise(function () { return expect.apply(expect, [item].concat(args)); }); }); return expect.promise.settle(promises).then(function () { var failed = promises.every(function (promise) { return promise.isRejected(); }); if (failed) { expect.fail({ diff: function (output, diff, inspect, equal) { var result = { inline: true, diff: output }; promises.forEach(function (promise, index) { if (index > 0) { result.diff.nl(2); } var error = promise.reason(); // the error is connected to the current scope // but we are just interested in the nested error error.errorMode = 'bubble'; result.diff.append(error.getErrorMessage(output)); }); return result; } }); } }); }); try { expect(['foo', 'bar'], 'to have item satisfying', 'to equal', 'bar'); expect(['foo', 'bar'], 'to have item satisfying', 'to equal', 'bAr'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(['foo', 'bar'], 'to have item satisfying', 'to equal', 'bar');").nl(); output.code("expect(['foo', 'bar'], 'to have item satisfying', 'to equal', 'bAr');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected [ 'foo', 'bar' ] to have item satisfying 'to equal', 'bAr'\n" + "\n" + "expected 'foo' to equal 'bAr'\n" + "\n" + "-foo\n" + "+bAr\n" + "\n" + "expected 'bar' to equal 'bAr'\n" + "\n" + "-bar\n" + "+bAr" ); } try { expect.addAssertion('detailed to be', function (expect, subject, value) { expect.errorMode = 'bubble'; expect.withError(function () { expect(subject, 'to be', value); }, function (err) { err.getParents().forEach(function (e) { e.errorMode = 'nested'; }); expect.fail(err); }); }); expect('f00!', 'detailed to be', 'foo!'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.addAssertion('detailed to be', function (expect, subject, value) {").nl(); output.code(" expect.errorMode = 'bubble';").nl(); output.code(" expect.withError(function () {").nl(); output.code(" expect(subject, 'to be', value);").nl(); output.code(" }, function (err) {").nl(); output.code(" err.getParents().forEach(function (e) {").nl(); output.code(" e.errorMode = 'nested';").nl(); output.code(" });").nl(); output.code(" expect.fail(err);").nl(); output.code(" });").nl(); output.code("});").nl(); output.code("").nl(); output.code("expect('f00!', 'detailed to be', 'foo!');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected 'f00!' to be 'foo!'\n" + " expected 'f00!' to equal 'foo!'\n" + " Explicit failure\n" + "\n" + " -f00!\n" + " +foo!" ); } try { expect.addAssertion('to be completely custom', function (expect, subject) { return expect.withError(function () { expect(subject, 'to satisfy', { custom: true }); }, function (err) { var createDiff = err.getDiffMethod(); expect.fail({ diff: function (output, diff, inspect, equal) { output.text('~~~~~~~~~~~~~~').sp().success('custom').sp().text('~~~~~~~~~~~~~~').nl(); var result = createDiff(output, diff, inspect, equal); return result; } }); }); }); expect({ custom: false }, 'to be completely custom'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.addAssertion('to be completely custom', function (expect, subject) {").nl(); output.code(" return expect.withError(function () {").nl(); output.code(" expect(subject, 'to satisfy', { custom: true });").nl(); output.code(" }, function (err) {").nl(); output.code(" var createDiff = err.getDiffMethod();").nl(); output.code(" expect.fail({").nl(); output.code(" diff: function (output, diff, inspect, equal) {").nl(); output.code(" output.text('~~~~~~~~~~~~~~').sp().success('custom').sp().text('~~~~~~~~~~~~~~').nl();").nl(); output.code(" var result = createDiff(output, diff, inspect, equal);").nl(); output.code(" return result;").nl(); output.code(" }").nl(); output.code(" });").nl(); output.code(" });").nl(); output.code("});").nl(); output.code("").nl(); output.code("expect({ custom: false }, 'to be completely custom');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected { custom: false } to be completely custom\n" + "\n" + "~~~~~~~~~~~~~~ custom ~~~~~~~~~~~~~~\n" + "{\n" + " custom: false // should equal true\n" + "}" ); } return expect.promise.all(testPromises); }); it("api/addAssertion.md contains correct examples", function () { var testPromises = []; var errorMode = 'default'; // use to control the error mode later in the example expect.addAssertion('array', '[not] to be (sorted|ordered)', function (expect, subject, cmp) { expect.errorMode = errorMode; expect(subject, '[not] to equal', [].concat(subject).sort(cmp)); }); expect([1,2,3], 'to be sorted'); expect([1,2,3], 'to be ordered'); expect([2,1,3], 'not to be sorted'); expect([2,1,3], 'not to be ordered'); expect([3,2,1], 'to be sorted', function (x, y) { return y - x; }); try { expect.addAssertion('number', 'to be contained by', function (expect, subject, start, finish) { expect.subjectOutput = function (output) { output.text('point ').jsNumber(subject); }; expect.argsOutput = function (output) { output.text('interval ').text('[').appendInspected(start).text(';').appendInspected(finish).text(']'); }; expect(subject >= start && subject <= finish, '[not] to be truthy'); }); expect(4, 'to be contained by', 8, 10); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.addAssertion('number', 'to be contained by', function (expect, subject, start, finish) {").nl(); output.code(" expect.subjectOutput = function (output) {").nl(); output.code(" output.text('point ').jsNumber(subject);").nl(); output.code(" };").nl(); output.code(" expect.argsOutput = function (output) {").nl(); output.code(" output.text('interval ').text('[').appendInspected(start).text(';').appendInspected(finish).text(']');").nl(); output.code(" };").nl(); output.code(" expect(subject >= start && subject <= finish, '[not] to be truthy');").nl(); output.code("});").nl(); output.code("").nl(); output.code("expect(4, 'to be contained by', 8, 10);").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected point 4 to be contained by interval [8;10]" ); } try { expect.addAssertion('number', 'to be similar to', function (expect, subject, value, epsilon) { if (typeof epsilon !== 'number') { epsilon = 1e-9; } expect.argsOutput[2] = function (output) { output.text('(epsilon: ').jsNumber(epsilon.toExponential()).text(')') } expect(Math.abs(subject - value), 'to be less than or equal to', epsilon); }); expect(4, 'to be similar to', 4.0001); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.addAssertion('number', 'to be similar to', function (expect, subject, value, epsilon) {").nl(); output.code(" if (typeof epsilon !== 'number') {").nl(); output.code(" epsilon = 1e-9;").nl(); output.code(" }").nl(); output.code(" expect.argsOutput[2] = function (output) {").nl(); output.code(" output.text('(epsilon: ').jsNumber(epsilon.toExponential()).text(')')").nl(); output.code(" }").nl(); output.code(" expect(Math.abs(subject - value), 'to be less than or equal to', epsilon);").nl(); output.code("});").nl(); output.code("").nl(); output.code("expect(4, 'to be similar to', 4.0001);").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected 4 to be similar to 4.0001, (epsilon: 1e-9)" ); } try { expect([ 1, 3, 2, 4 ], 'to be sorted'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected [ 1, 3, 2, 4 ] to be sorted\n" + "\n" + "[\n" + " 1,\n" + " 3, // should equal 2\n" + " 2, // should equal 3\n" + " 4\n" + "]" ); } try { errorMode = 'bubble'; expect([ 1, 3, 2, 4 ], 'to be sorted'); expect.fail(function (output) { output.error("expected:").nl(); output.code("errorMode = 'bubble';").nl(); output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected [ 1, 3, 2, 4 ] to equal [ 1, 2, 3, 4 ]\n" + "\n" + "[\n" + " 1,\n" + " 3, // should equal 2\n" + " 2, // should equal 3\n" + " 4\n" + "]" ); } try { errorMode = 'nested'; expect([ 1, 3, 2, 4 ], 'to be sorted'); expect.fail(function (output) { output.error("expected:").nl(); output.code("errorMode = 'nested';").nl(); output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected [ 1, 3, 2, 4 ] to be sorted\n" + " expected [ 1, 3, 2, 4 ] to equal [ 1, 2, 3, 4 ]\n" + "\n" + " [\n" + " 1,\n" + " 3, // should equal 2\n" + " 2, // should equal 3\n" + " 4\n" + " ]" ); } try { errorMode = 'defaultOrNested'; expect([ 1, 3, 2, 4 ], 'to be sorted'); expect.fail(function (output) { output.error("expected:").nl(); output.code("errorMode = 'defaultOrNested';").nl(); output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected [ 1, 3, 2, 4 ] to be sorted\n" + "\n" + "[\n" + " 1,\n" + " 3, // should equal 2\n" + " 2, // should equal 3\n" + " 4\n" + "]" ); } try { errorMode = 'diff'; expect([ 1, 3, 2, 4 ], 'to be sorted'); expect.fail(function (output) { output.error("expected:").nl(); output.code("errorMode = 'diff';").nl(); output.code("expect([ 1, 3, 2, 4 ], 'to be sorted');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "[\n" + " 1,\n" + " 3, // should equal 2\n" + " 2, // should equal 3\n" + " 4\n" + "]" ); } function Timelock(value, delay) { this.value = value; this.delay = delay; } Timelock.prototype.getValue = function (cb) { var that = this; setTimeout(function () { cb(that.value); }, this.delay); }; expect.addType({ name: 'Timelock', identify: function (value) { return value && value instanceof Timelock; }, inspect: function (value, depth, output) { output.jsFunctionName('Timelock'); } }); expect.addAssertion('Timelock', 'to satisfy', function (expect, subject, spec) { return expect.promise(function (run) { subject.getValue(run(function (value) { return expect(value, 'to satisfy', spec); })); }); }); testPromises.push(expect.promise(function () { return expect(new Timelock('Hello world!', 5), 'to satisfy', expect.it('not to match', /!/)); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("return expect(new Timelock('Hello world!', 5), 'to satisfy', expect.it('not to match', /!/));").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "expected Timelock to satisfy expect.it('not to match', /!/)\n" + "\n" + "expected 'Hello world!' not to match /!/\n" + "\n" + "Hello world!\n" + " ^" ); })); return expect.promise.all(testPromises); }); it("api/addType.md contains correct examples", function () { var testPromises = []; function Person(name, age) { this.name = name; this.age = age; } expect.addType({ name: 'Person', base: 'object', identify: function (value) { return value instanceof Person; } }); try { expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24)); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected Person({ name: 'John Doe', age: 42 })\n" + "to equal Person({ name: 'Jane Doe', age: 24 })\n" + "\n" + "Person({\n" + " name: 'John Doe', // should equal 'Jane Doe'\n" + " // -John Doe\n" + " // +Jane Doe\n" + " age: 42 // should equal 24\n" + "})" ); } expect = unexpected.clone(); expect.output.preferredWidth = 80; expect.addType({ name: 'Person', base: 'object', identify: function (value) { return value instanceof Person; }, inspect: function (person, depth, output, inspect) { output.text('new Person(') .append(inspect(person.name, depth)) .text(', ') .append(inspect(person.age, depth)) .text(')'); } }); try { expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24)); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected new Person('John Doe', 42) to equal new Person('Jane Doe', 24)\n" + "\n" + "Person({\n" + " name: 'John Doe', // should equal 'Jane Doe'\n" + " // -John Doe\n" + " // +Jane Doe\n" + " age: 42 // should equal 24\n" + "})" ); } expect = unexpected.clone(); expect.output.preferredWidth = 80; expect.addType({ name: 'Person', base: 'object', identify: function (value) { return value instanceof Person; }, inspect: function (person, depth, output, inspect) { output.text('new Person(') .append(inspect(person.name, depth)) .text(', ') .append(inspect(person.age, depth)) .text(')'); }, equal: function (a, b, equal) { return a === b || equal(a.name, b.name); } }); expect = unexpected.clone(); expect.output.preferredWidth = 80; expect.addType({ name: 'Person', base: 'object', identify: function (value) { return value instanceof Person; }, inspect: function (person, depth, output, inspect) { output.text('new Person(') .append(inspect(person.name, depth)) .text(', ') .append(inspect(person.age, depth)) .text(')'); }, equal: function (a, b, equal) { return a === b || equal(a.name, b.name); }, diff: function (actual, expected, output, diff, inspect) { return this.baseType.diff({name: actual.name}, {name: expected.name}, output); } }); try { expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24)); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected new Person('John Doe', 42) to equal new Person('Jane Doe', 24)\n" + "\n" + "{\n" + " name: 'John Doe' // should equal 'Jane Doe'\n" + " // -John Doe\n" + " // +Jane Doe\n" + "}" ); } expect = unexpected.clone(); expect.output.preferredWidth = 80; var inlineDiff = true; // used to change inlining in a later example expect.addType({ name: 'Person', base: 'object', identify: function (value) { return value instanceof Person; }, inspect: function (person, depth, output, inspect) { output.text('new Person(') .append(inspect(person.name, depth)) .text(', ') .append(inspect(person.age, depth)) .text(')'); }, equal: function (a, b, equal) { return a === b || equal(a.name, b.name); }, diff: function (actual, expected, output, diff, inspect) { var nameDiff = diff(actual.name, expected.name); output.text('new Person(') .nl() .indentLines(); if (nameDiff && nameDiff.inline) { output.append(nameDiff.diff); } else { output.i().append(inspect(actual.name)).text(',').sp() .annotationBlock(function () { this.error('should be ').append(inspect(expected.name)); if (nameDiff) { this.nl().append(nameDiff.diff); } }) .nl(); } output.i().append(inspect(actual.age)) .outdentLines() .nl() .text(')'); return { inline: inlineDiff, diff: output }; } }); try { expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24)); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(new Person('John Doe', 42), 'to equal', new Person('Jane Doe', 24));").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected new Person('John Doe', 42) to equal new Person('Jane Doe', 24)\n" + "\n" + "new Person(\n" + " 'John Doe', // should be 'Jane Doe'\n" + " // -John Doe\n" + " // +Jane Doe\n" + " 42\n" + ")" ); } try { inlineDiff = true; expect( {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)}, 'to equal', {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)} ); expect.fail(function (output) { output.error("expected:").nl(); output.code("inlineDiff = true;").nl(); output.code("expect(").nl(); output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)},").nl(); output.code(" 'to equal',").nl(); output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)}").nl(); output.code(");").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected\n" + "{\n" + " 'John Doe': new Person('John Doe', 42),\n" + " 'Jane Doe': new Person('Janie Doe', 24)\n" + "}\n" + "to equal\n" + "{\n" + " 'John Doe': new Person('John Doe', 42),\n" + " 'Jane Doe': new Person('Jane Doe', 24)\n" + "}\n" + "\n" + "{\n" + " 'John Doe': new Person('John Doe', 42),\n" + " 'Jane Doe': new Person(\n" + " 'Janie Doe', // should be 'Jane Doe'\n" + " // -Janie Doe\n" + " // +Jane Doe\n" + " 24\n" + " )\n" + "}" ); } try { inlineDiff = false; expect( {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)}, 'to equal', {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)} ); expect.fail(function (output) { output.error("expected:").nl(); output.code("inlineDiff = false;").nl(); output.code("expect(").nl(); output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Janie Doe', 24)},").nl(); output.code(" 'to equal',").nl(); output.code(" {'John Doe': new Person('John Doe', 42), 'Jane Doe': new Person('Jane Doe', 24)}").nl(); output.code(");").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected\n" + "{\n" + " 'John Doe': new Person('John Doe', 42),\n" + " 'Jane Doe': new Person('Janie Doe', 24)\n" + "}\n" + "to equal\n" + "{\n" + " 'John Doe': new Person('John Doe', 42),\n" + " 'Jane Doe': new Person('Jane Doe', 24)\n" + "}\n" + "\n" + "{\n" + " 'John Doe': new Person('John Doe', 42),\n" + " 'Jane Doe': new Person('Janie Doe', 24) // should equal new Person('Jane Doe', 24)\n" + " // new Person(\n" + " // 'Janie Doe', // should be 'Jane Doe'\n" + " // // -Janie Doe\n" + " // // +Jane Doe\n" + " // 24\n" + " // )\n" + "}" ); } expect.addAssertion('Person', 'to be above legal age', function (expect, subject) { expect(subject.age, 'to be greater than or equal to', 18); }); expect(new Person('Jane Doe', 24), 'to be above legal age'); expect(new Person('Jane Doe', 24), 'to have keys', 'name', 'age'); expect(new Person('Jane Doe', 24), 'to satisfy', { name: expect.it('to be a string').and('not to be empty'), age: expect.it('to be a number').and('not to be negative') }); return expect.promise.all(testPromises); }); it("api/clone.md contains correct examples", function () { var testPromises = []; var originalExpect = expect; expect = expect.clone().addAssertion('to be an integer', function (expect, subject) { expect(subject, 'to be a number'); expect(Math.round(subject), 'to be', subject); }); expect(42, 'to be an integer'); try { originalExpect(42, 'to be an integer'); expect.fail(function (output) { output.error("expected:").nl(); output.code("originalExpect(42, 'to be an integer');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "Unknown assertion 'to be an integer', did you mean: 'to be a number'" ); } return expect.promise.all(testPromises); }); it("api/expect.md contains correct examples", function () { var testPromises = []; try { expect(123, 'to equal', 456); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect(123, 'to equal', 456);").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "expected 123 to equal 456" ); } it('should call the callback', function () { return expect(setImmediate, 'to call the callback'); }); expect('abc', 'to be a string').and('to have length', 3); it('should do the right thing', function () { return expect(setImmediate, 'to be a function').and('to call the callback'); }); return expect.promise.all(testPromises); }); it("api/fail.md contains correct examples", function () { var testPromises = []; try { expect.fail(); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.fail();").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "Explicit failure" ); } try { expect.fail('Custom failure message'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.fail('Custom failure message');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "Custom failure message" ); } try { expect.fail('{0} was expected to be {1}', 0, 'zero'); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.fail('{0} was expected to be {1}', 0, 'zero');").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "0 was expected to be 'zero'" ); } try { var error = new Error('throw me'); expect.fail(new Error(error)); expect.fail(function (output) { output.error("expected:").nl(); output.code("var error = new Error('throw me');").nl(); output.code("expect.fail(new Error(error));").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "Error: throw me" ); } try { expect.fail(function (output) { 'You have been a very bad boy!'.split(/ /).forEach(function (word, index) { if (index > 0) { output.sp(); } var style = index % 2 === 0 ? 'jsPrimitive' : 'jsString'; output[style](word); }); }); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.fail(function (output) {").nl(); output.code(" 'You have been a very bad boy!'.split(/ /).forEach(function (word, index) {").nl(); output.code(" if (index > 0) { output.sp(); }").nl(); output.code(" var style = index % 2 === 0 ? 'jsPrimitive' : 'jsString';").nl(); output.code(" output[style](word);").nl(); output.code(" });").nl(); output.code("});").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "You have been a very bad boy!" ); } try { expect.fail({ message: function (output) { 'You have been a very bad boy!'.split(/ /).forEach(function (word, index) { if (index > 0) { output.sp(); } var style = index % 2 === 0 ? 'jsPrimitive' : 'jsString'; output[style](word); }); }, diff: function (output, diff, inspect, equal) { return diff('You have been a very bad boy!', 'You have been a very mad boy!'); } }); expect.fail(function (output) { output.error("expected:").nl(); output.code("expect.fail({").nl(); output.code(" message: function (output) {").nl(); output.code(" 'You have been a very bad boy!'.split(/ /).forEach(function (word, index) {").nl(); output.code(" if (index > 0) { output.sp(); }").nl(); output.code(" var style = index % 2 === 0 ? 'jsPrimitive' : 'jsString';").nl(); output.code(" output[style](word);").nl(); output.code(" });").nl(); output.code(" },").nl(); output.code(" diff: function (output, diff, inspect, equal) {").nl(); output.code(" return diff('You have been a very bad boy!', 'You have been a very mad boy!');").nl(); output.code(" }").nl(); output.code("});").nl(); output.error("to throw"); }); } catch (e) { expect(e, "to have message", "You have been a very bad boy!\n" + "\n" + "-You have been a very bad boy!\n" + "+You have been a very mad boy!" ); } return expect.promise.all(testPromises); }); it("api/promise-all.md contains correct examples", function () { var testPromises = []; expect.addAssertion('to be a number after a short delay', function (expect, subject) { return expect.promise(function (run) { setTimeout(run(function () { expect(subject, 'to be a number'); }), 1); }); }); testPromises.push(expect.promise(function () { return expect.promise.all({ foo: [ expect(42, 'to be a number after a short delay') ], bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: 1, b: 2 }, 'to have values satisfying', 'to be a number after a short delay') }); })); testPromises.push(expect.promise(function () { return expect.promise.all({ foo: [ expect(42, 'to be a number after a short delay') ], bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '0', b: 1 }, 'to have values satisfying', 'to be a number after a short delay') }); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("return expect.promise.all({").nl(); output.code(" foo: [").nl(); output.code(" expect(42, 'to be a number after a short delay')").nl(); output.code(" ],").nl(); output.code(" bar: expect([0, 1, 2], 'to have items satisfying',").nl(); output.code(" expect.it('to be a number after a short delay')),").nl(); output.code("").nl(); output.code(" baz: expect({ a: '0', b: 1 }, 'to have values satisfying',").nl(); output.code(" 'to be a number after a short delay')").nl(); output.code("});").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "expected { a: '0', b: 1 }\n" + "to have values satisfying 'to be a number after a short delay'\n" + "\n" + "{\n" + " a: '0', // should be a number after a short delay\n" + " b: 1\n" + "}" ); })); return expect.promise.all(testPromises); }); it("api/promise-any.md contains correct examples", function () { var testPromises = []; expect.addAssertion('to be a number after a short delay', function (expect, subject) { return expect.promise(function (run) { setTimeout(run(function () { expect(subject, 'to be a number'); }), 0); }); }); testPromises.push(expect.promise(function () { return expect.promise.any({ foo: [ expect('42', 'to be a number after a short delay') ], bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '1', b: 2 }, 'to have values satisfying', 'to be a number after a short delay') }); })); testPromises.push(expect.promise(function () { return expect.promise.any({ foo: [ expect('42', 'to be a number after a short delay') ], bar: expect([0, '1', 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '0', b: 1 }, 'to have values satisfying', 'to be a number after a short delay') }).caught(function (aggregateError) { // Let's reformat the error a bit expect.fail(function (output) { output.error(aggregateError.message); var errors = []; for (var i = 0; i < aggregateError.length; i += 1) { errors.push(aggregateError[i]); } errors.sort(function (a, b) { // Make the output stable if (a.message < b.message) return -1; if (a.message > b.message) return 1; return 0; }); output.indentLines(); errors.forEach(function (e, i) { output.nl().i().text(i + ': ').block(e.getErrorMessage({ output: output })); }); }); }); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("return expect.promise.any({").nl(); output.code(" foo: [").nl(); output.code(" expect('42', 'to be a number after a short delay')").nl(); output.code(" ],").nl(); output.code(" bar: expect([0, '1', 2], 'to have items satisfying',").nl(); output.code(" expect.it('to be a number after a short delay')),").nl(); output.code("").nl(); output.code(" baz: expect({ a: '0', b: 1 }, 'to have values satisfying',").nl(); output.code(" 'to be a number after a short delay')").nl(); output.code("}).caught(function (aggregateError) {").nl(); output.code(" // Let's reformat the error a bit").nl(); output.code(" expect.fail(function (output) {").nl(); output.code(" output.error(aggregateError.message);").nl(); output.code(" var errors = [];").nl(); output.code(" for (var i = 0; i < aggregateError.length; i += 1) {").nl(); output.code(" errors.push(aggregateError[i]);").nl(); output.code(" }").nl(); output.code("").nl(); output.code(" errors.sort(function (a, b) { // Make the output stable").nl(); output.code(" if (a.message < b.message) return -1;").nl(); output.code(" if (a.message > b.message) return 1;").nl(); output.code(" return 0;").nl(); output.code(" });").nl(); output.code("").nl(); output.code(" output.indentLines();").nl(); output.code(" errors.forEach(function (e, i) {").nl(); output.code(" output.nl().i().text(i + ': ').block(e.getErrorMessage({ output: output }));").nl(); output.code(" });").nl(); output.code(" });").nl(); output.code("});").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "aggregate error\n" + " 0: expected '42' to be a number after a short delay\n" + " 1: expected [ 0, '1', 2 ]\n" + " to have items satisfying expect.it('to be a number after a short delay')\n" + "\n" + " [\n" + " 0,\n" + " '1', // should be a number after a short delay\n" + " 2\n" + " ]\n" + " 2: expected { a: '0', b: 1 }\n" + " to have values satisfying 'to be a number after a short delay'\n" + "\n" + " {\n" + " a: '0', // should be a number after a short delay\n" + " b: 1\n" + " }" ); })); return expect.promise.all(testPromises); }); it("api/promise-settle.md contains correct examples", function () { var testPromises = []; expect.addAssertion('to be a number after a short delay', function (expect, subject) { return expect.promise(function (run) { setTimeout(run(function () { expect(subject, 'to be a number'); }), 1); }); }); testPromises.push(expect.promise(function () { var promises = { foo: expect('42', 'to be a number after a short delay'), bar: expect([0, 1, 2], 'to have items satisfying', expect.it('to be a number after a short delay')), baz: expect({ a: '1', b: 2 }, 'to have values satisfying', 'to be a number after a short delay') }; return expect.promise.all(promises).caught(function () { return expect.promise.settle(promises).then(function () { expect.fail(function (output) { output.text('{').nl(); output.indentLines(); Object.keys(promises).forEach(function (key, index) { output.i().jsKey(key).text(':').sp(); if (promises[key].isFulfilled()) { output.success('✓'); } else { output.error('⨯ ').block(promises[key].reason().getErrorMessage({ output: output })); } output.nl(); }); output.outdentLines(); output.text('}'); }); }); }); }).then(function () { return expect.promise(function () { expect.fail(function (output) { output.error("expected:").nl(); output.code("var promises = {").nl(); output.code(" foo: expect('42', 'to be a number after a short delay'),").nl(); output.code(" bar: expect([0, 1, 2], 'to have items satisfying',").nl(); output.code(" expect.it('to be a number after a short delay')),").nl(); output.code(" baz: expect({ a: '1', b: 2 }, 'to have values satisfying',").nl(); output.code(" 'to be a number after a short delay')").nl(); output.code("};").nl(); output.code("").nl(); output.code("return expect.promise.all(promises).caught(function () {").nl(); output.code(" return expect.promise.settle(promises).then(function () {").nl(); output.code(" expect.fail(function (output) {").nl(); output.code(" output.text('{').nl();").nl(); output.code(" output.indentLines();").nl(); output.code(" Object.keys(promises).forEach(function (key, index) {").nl(); output.code(" output.i().jsKey(key).text(':').sp();").nl(); output.code(" if (promises[key].isFulfilled()) {").nl(); output.code(" output.success('✓');").nl(); output.code(" } else {").nl(); output.code(" output.error('⨯ ').block(promises[key].reason().getErrorMessage({ output: output }));").nl(); output.code(" }").nl(); output.code(" output.nl();").nl(); output.code(" });").nl(); output.code(" output.outdentLines();").nl(); output.code(" output.text('}');").nl(); output.code(" });").nl(); output.code(" });").nl(); output.code("});").nl(); output.error("to throw"); }); }); }).caught(function (e) { expect(e, "to have message", "{\n" + " foo: ⨯ expected '42' to be a number after a short delay\n" + " bar: ✓\n" + " baz: ⨯ expected { a: '1', b: 2 }\n" + " to have values satisfying 'to be a number after a short delay'\n" + "\n" + " {\n" + " a: '1', // should be a number after a short delay\n" + " b: 2\n" + " }\n" + "}" ); })); return expect.promise.all(testPromises); }); it("api/use.md contains correct examples", function () { var testPromises = []; function IntegerInterval(from, to) { this.from = from; this.to = to; } expect.use({ name: 'unexpected-integer-intervals', installInto: function (expect) { expect.addType({ name: 'IntegerInterval', base: 'object', identify: function (value) { return value && value instanceof IntegerInterval; }, inspect: function (value, depth, out