From 6c1e6bdcf502af4ac8748b84764f08fbb9b49f62 Mon Sep 17 00:00:00 2001 From: Denis Bardadym Date: Mon, 28 Aug 2017 16:55:21 +0300 Subject: [PATCH] Prettier all --- lib/assertion-error.js | 60 ++++---- lib/assertion.js | 15 +- lib/config.js | 4 +- lib/ext/_assert.js | 40 +++--- lib/ext/assert.js | 12 +- lib/ext/bool.js | 12 +- lib/ext/chain.js | 16 ++- lib/ext/contain.js | 107 +++++++++----- lib/ext/eql.js | 53 +++---- lib/ext/error.js | 35 +++-- lib/ext/match.js | 152 ++++++++++++-------- lib/ext/number.js | 58 ++++---- lib/ext/promise.js | 228 ++++++++++++++++------------- lib/ext/property.js | 173 ++++++++++++++-------- lib/ext/string.js | 14 +- lib/ext/type.js | 109 +++++++------- lib/format.js | 4 +- lib/should.js | 51 ++++--- lib/util.js | 10 +- package-lock.json | 6 + package.json | 1 + test/exist.test.js | 52 ++++--- test/ext/assert.test.js | 293 +++++++++++++++++++++++--------------- test/ext/bool.test.js | 28 ++-- test/ext/contain.test.js | 220 ++++++++-------------------- test/ext/eql.test.js | 77 +++++----- test/ext/error.test.js | 159 +++++++++++++-------- test/ext/match.test.js | 185 +++++++++++++----------- test/ext/number.test.js | 67 +++++---- test/ext/promise.test.js | 234 +++++++++++++++++++----------- test/ext/property.test.js | 229 +++++++++++++++-------------- test/ext/string.test.js | 35 +++-- test/ext/type.test.js | 231 +++++++++++++++++------------- test/nested.test.js | 37 +++-- test/not.test.js | 19 ++- test/perf.test.js | 13 +- test/should.test.js | 33 +++-- test/util.js | 21 +-- 38 files changed, 1712 insertions(+), 1381 deletions(-) diff --git a/lib/assertion-error.js b/lib/assertion-error.js index 7ba79af..8a3ab99 100644 --- a/lib/assertion-error.js +++ b/lib/assertion-error.js @@ -4,9 +4,9 @@ * Copyright(c) 2013-2017 Denis Bardadym * MIT Licensed */ -import { merge } from 'should-util'; -import { functionName } from './util'; -import { format } from './format'; +import { merge } from "should-util"; +import { functionName } from "./util"; +import { format } from "./format"; /** * should AssertionError @@ -19,18 +19,17 @@ export default function AssertionError(options) { merge(this, options); if (!options.message) { - Object.defineProperty(this, 'message', { - get: function() { - if (!this._message) { - this._message = this.generateMessage(); - this.generatedMessage = true; - } - return this._message; - }, - configurable: true, - enumerable: false - } - ); + Object.defineProperty(this, "message", { + get: function() { + if (!this._message) { + this._message = this.generateMessage(); + this.generatedMessage = true; + } + return this._message; + }, + configurable: true, + enumerable: false + }); } if (Error.captureStackTrace) { @@ -44,11 +43,11 @@ export default function AssertionError(options) { if (this.stackStartFunction) { // try to strip useless frames var fn_name = functionName(this.stackStartFunction); - var idx = out.indexOf('\n' + fn_name); + var idx = out.indexOf("\n" + fn_name); if (idx >= 0) { // once we have located the function frame // we need to strip out everything before it (and its line) - var next_line = out.indexOf('\n', idx + 1); + var next_line = out.indexOf("\n", idx + 1); out = out.substring(next_line + 1); } } @@ -58,21 +57,19 @@ export default function AssertionError(options) { } } - -var indent = ' '; +var indent = " "; function prependIndent(line) { return indent + line; } function indentLines(text) { - return text.split('\n').map(prependIndent).join('\n'); + return text.split("\n").map(prependIndent).join("\n"); } - // assert.AssertionError instanceof Error AssertionError.prototype = Object.create(Error.prototype, { name: { - value: 'AssertionError' + value: "AssertionError" }, generateMessage: { @@ -81,12 +78,23 @@ AssertionError.prototype = Object.create(Error.prototype, { return this.previous.message; } var actual = format(this.actual); - var expected = 'expected' in this ? ' ' + format(this.expected) : ''; - var details = 'details' in this && this.details ? ' (' + this.details + ')' : ''; + var expected = "expected" in this ? " " + format(this.expected) : ""; + var details = + "details" in this && this.details ? " (" + this.details + ")" : ""; - var previous = this.previous ? '\n' + indentLines(this.previous.message) : ''; + var previous = this.previous + ? "\n" + indentLines(this.previous.message) + : ""; - return 'expected ' + actual + (this.negate ? ' not ' : ' ') + this.operator + expected + details + previous; + return ( + "expected " + + actual + + (this.negate ? " not " : " ") + + this.operator + + expected + + details + + previous + ); } } }); diff --git a/lib/assertion.js b/lib/assertion.js index 4c2e102..b5adbef 100644 --- a/lib/assertion.js +++ b/lib/assertion.js @@ -274,11 +274,22 @@ Assertion.addChain = function(name, onCall) { Assertion.alias = function(from, to) { var desc = Object.getOwnPropertyDescriptor(Assertion.prototype, from); if (!desc) { - throw new Error("Alias " + from + " -> " + to + " could not be created as " + from + " not defined"); + throw new Error( + "Alias " + + from + + " -> " + + to + + " could not be created as " + + from + + " not defined" + ); } Object.defineProperty(Assertion.prototype, to, desc); - var desc2 = Object.getOwnPropertyDescriptor(PromisedAssertion.prototype, from); + var desc2 = Object.getOwnPropertyDescriptor( + PromisedAssertion.prototype, + from + ); if (desc2) { Object.defineProperty(PromisedAssertion.prototype, to, desc2); } diff --git a/lib/config.js b/lib/config.js index 7da637d..04ce77c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -5,8 +5,8 @@ * MIT Licensed */ -import format from 'should-format'; -import { defaultTypeAdaptorStorage } from 'should-type-adaptors'; +import format from "should-format"; +import { defaultTypeAdaptorStorage } from "should-type-adaptors"; var config = { typeAdaptors: defaultTypeAdaptorStorage, diff --git a/lib/ext/_assert.js b/lib/ext/_assert.js index c31e44a..8ca1036 100644 --- a/lib/ext/_assert.js +++ b/lib/ext/_assert.js @@ -27,9 +27,9 @@ // when used in node, this will actually load the util module we depend on // versus loading the builtin util module as happens otherwise // this is a bug in node module loading as far as I am concerned -import { Assertion } from './../assertion'; +import { Assertion } from "./../assertion"; -import _deepEqual from 'should-equal'; +import _deepEqual from "should-equal"; var pSlice = Array.prototype.slice; @@ -86,7 +86,7 @@ assert.fail = fail; */ function ok(value, message) { if (!value) { - fail(value, true, message, '==', assert.ok); + fail(value, true, message, "==", assert.ok); } } assert.ok = ok; @@ -106,7 +106,7 @@ assert.ok = ok; */ assert.equal = function equal(actual, expected, message) { if (actual != expected) { - fail(actual, expected, message, '==', assert.equal); + fail(actual, expected, message, "==", assert.equal); } }; @@ -123,7 +123,7 @@ assert.equal = function equal(actual, expected, message) { */ assert.notEqual = function notEqual(actual, expected, message) { if (actual == expected) { - fail(actual, expected, message, '!=', assert.notEqual); + fail(actual, expected, message, "!=", assert.notEqual); } }; @@ -142,11 +142,10 @@ assert.notEqual = function notEqual(actual, expected, message) { */ assert.deepEqual = function deepEqual(actual, expected, message) { if (_deepEqual(actual, expected).length !== 0) { - fail(actual, expected, message, 'deepEqual', assert.deepEqual); + fail(actual, expected, message, "deepEqual", assert.deepEqual); } }; - // 8. The non-equivalence assertion tests for any deep inequality. // assert.notDeepEqual(actual, expected, message_opt); /** @@ -162,7 +161,7 @@ assert.deepEqual = function deepEqual(actual, expected, message) { */ assert.notDeepEqual = function notDeepEqual(actual, expected, message) { if (_deepEqual(actual, expected).result) { - fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual); + fail(actual, expected, message, "notDeepEqual", assert.notDeepEqual); } }; @@ -179,7 +178,7 @@ assert.notDeepEqual = function notDeepEqual(actual, expected, message) { */ assert.strictEqual = function strictEqual(actual, expected, message) { if (actual !== expected) { - fail(actual, expected, message, '===', assert.strictEqual); + fail(actual, expected, message, "===", assert.strictEqual); } }; @@ -196,7 +195,7 @@ assert.strictEqual = function strictEqual(actual, expected, message) { */ assert.notStrictEqual = function notStrictEqual(actual, expected, message) { if (actual === expected) { - fail(actual, expected, message, '!==', assert.notStrictEqual); + fail(actual, expected, message, "!==", assert.notStrictEqual); } }; @@ -205,7 +204,7 @@ function expectedException(actual, expected) { return false; } - if (Object.prototype.toString.call(expected) == '[object RegExp]') { + if (Object.prototype.toString.call(expected) == "[object RegExp]") { return expected.test(actual); } else if (actual instanceof expected) { return true; @@ -219,7 +218,7 @@ function expectedException(actual, expected) { function _throws(shouldThrow, block, expected, message) { var actual; - if (typeof expected == 'string') { + if (typeof expected == "string") { message = expected; expected = null; } @@ -230,18 +229,25 @@ function _throws(shouldThrow, block, expected, message) { actual = e; } - message = (expected && expected.name ? ' (' + expected.name + ')' : '.') + - (message ? ' ' + message : '.'); + message = + (expected && expected.name ? " (" + expected.name + ")" : ".") + + (message ? " " + message : "."); if (shouldThrow && !actual) { - fail(actual, expected, 'Missing expected exception' + message); + fail(actual, expected, "Missing expected exception" + message); } if (!shouldThrow && expectedException(actual, expected)) { - fail(actual, expected, 'Got unwanted exception' + message); + fail(actual, expected, "Got unwanted exception" + message); } - if ((shouldThrow && actual && expected && !expectedException(actual, expected)) || (!shouldThrow && actual)) { + if ( + (shouldThrow && + actual && + expected && + !expectedException(actual, expected)) || + (!shouldThrow && actual) + ) { throw actual; } } diff --git a/lib/ext/assert.js b/lib/ext/assert.js index 110951b..39130e3 100644 --- a/lib/ext/assert.js +++ b/lib/ext/assert.js @@ -5,9 +5,9 @@ * MIT Licensed */ -import { merge } from 'should-util'; -import assert from './_assert'; -import AssertionError from '../assertion-error'; +import { merge } from "should-util"; +import assert from "./_assert"; +import AssertionError from "../assertion-error"; export default function(should) { var i = should.format; @@ -40,7 +40,8 @@ export default function(should) { should.exist = should.exists = function(obj, msg) { if (null == obj) { throw new AssertionError({ - message: msg || ('expected ' + i(obj) + ' to exist'), stackStartFunction: should.exist + message: msg || "expected " + i(obj) + " to exist", + stackStartFunction: should.exist }); } }; @@ -64,7 +65,8 @@ export default function(should) { should.not.exist = should.not.exists = function(obj, msg) { if (null != obj) { throw new AssertionError({ - message: msg || ('expected ' + i(obj) + ' to not exist'), stackStartFunction: should.not.exist + message: msg || "expected " + i(obj) + " to not exist", + stackStartFunction: should.not.exist }); } }; diff --git a/lib/ext/bool.js b/lib/ext/bool.js index 2cf81c1..45080fa 100644 --- a/lib/ext/bool.js +++ b/lib/ext/bool.js @@ -21,11 +21,11 @@ export default function(should, Assertion) { * * ({ a: 10}).should.not.be.true(); */ - Assertion.add('true', function(message) { + Assertion.add("true", function(message) { this.is.exactly(true, message); }); - Assertion.alias('true', 'True'); + Assertion.alias("true", "True"); /** * Assert given object is exactly `false`. @@ -40,11 +40,11 @@ export default function(should, Assertion) { * (true).should.not.be.false(); * false.should.be.false(); */ - Assertion.add('false', function(message) { + Assertion.add("false", function(message) { this.is.exactly(false, message); }); - Assertion.alias('false', 'False'); + Assertion.alias("false", "False"); /** * Assert given object is truthy according javascript type conversions. @@ -62,8 +62,8 @@ export default function(should, Assertion) { * (10).should.be.ok(); * (0).should.not.be.ok(); */ - Assertion.add('ok', function() { - this.params = { operator: 'to be truthy' }; + Assertion.add("ok", function() { + this.params = { operator: "to be truthy" }; this.assert(this.obj); }); diff --git a/lib/ext/chain.js b/lib/ext/chain.js index 16e4ba2..18923c3 100644 --- a/lib/ext/chain.js +++ b/lib/ext/chain.js @@ -26,7 +26,21 @@ export default function(should, Assertion) { * @alias Assertion#it * @category assertion chaining */ - ['an', 'of', 'a', 'and', 'be', 'been', 'has', 'have', 'with', 'is', 'which', 'the', 'it'].forEach(function(name) { + [ + "an", + "of", + "a", + "and", + "be", + "been", + "has", + "have", + "with", + "is", + "which", + "the", + "it" + ].forEach(function(name) { Assertion.addChain(name); }); } diff --git a/lib/ext/contain.js b/lib/ext/contain.js index 9d1aa7d..69ae143 100644 --- a/lib/ext/contain.js +++ b/lib/ext/contain.js @@ -5,9 +5,15 @@ * MIT Licensed */ -import { isIterable, some, isEmpty, forEach, iterator } from 'should-type-adaptors'; +import { + isIterable, + some, + isEmpty, + forEach, + iterator +} from "should-type-adaptors"; -import eql from 'should-equal'; +import eql from "should-equal"; export default function(should, Assertion) { var i = should.format; @@ -36,23 +42,29 @@ export default function(should, Assertion) { * // throws AssertionError: expected { a: 10, c: { d: 10 } } to contain { b: 10 } * // expected { a: 10, c: { d: 10 } } to have property b */ - Assertion.add('containEql', function(other) { - this.params = { operator: 'to contain ' + i(other) }; + Assertion.add("containEql", function(other) { + this.params = { operator: "to contain " + i(other) }; this.is.not.null().and.not.undefined(); var obj = this.obj; - if (typeof obj == 'string') { + if (typeof obj == "string") { this.assert(obj.indexOf(String(other)) >= 0); } else if (isIterable(obj)) { - this.assert(some(obj, function(v) { - return eql(v, other).length === 0; - })); + this.assert( + some(obj, function(v) { + return eql(v, other).length === 0; + }) + ); } else { - forEach(other, function(value, key) { - should(obj).have.value(key, value); - }, this); + forEach( + other, + function(value, key) { + should(obj).have.value(key, value); + }, + this + ); } }); @@ -75,11 +87,12 @@ export default function(should, Assertion) { * ({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {c: 10}}); * ({ a: 10, b: { c: 10, d: [1, 2, 3] }}).should.containDeepOrdered({b: {d: [1, 3]}}); */ - Assertion.add('containDeepOrdered', function(other) { - this.params = {operator: 'to contain ' + i(other)}; + Assertion.add("containDeepOrdered", function(other) { + this.params = { operator: "to contain " + i(other) }; var obj = this.obj; - if (typeof obj == 'string') {// expect other to be string + if (typeof obj == "string") { + // expect other to be string this.is.equal(String(other)); } else if (isIterable(obj) && isIterable(other)) { var objIterator = iterator(obj); @@ -100,7 +113,13 @@ export default function(should, Assertion) { } this.assert(nextOther.done); - } else if (obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {//TODO compare types object contains object case + } else if ( + obj != null && + other != null && + typeof obj == "object" && + typeof other == "object" + ) { + //TODO compare types object contains object case forEach(other, function(value, key) { should(obj[key]).containDeepOrdered(value); }); @@ -126,33 +145,46 @@ export default function(should, Assertion) { * [ 1, 2, 3].should.containDeep([2, 1]); * [ 1, 2, [ 1, 2, 3 ]].should.containDeep([ 1, [ 3, 1 ]]); */ - Assertion.add('containDeep', function(other) { - this.params = {operator: 'to contain ' + i(other)}; + Assertion.add("containDeep", function(other) { + this.params = { operator: "to contain " + i(other) }; var obj = this.obj; - if (typeof obj == 'string') {// expect other to be string + if (typeof obj == "string") { + // expect other to be string this.is.equal(String(other)); } else if (isIterable(obj) && isIterable(other)) { var usedKeys = {}; - forEach(other, function(otherItem) { - this.assert(some(obj, function(item, index) { - if (index in usedKeys) { - return false; - } - - try { - should(item).containDeep(otherItem); - usedKeys[index] = true; - return true; - } catch (e) { - if (e instanceof should.AssertionError) { - return false; - } - throw e; - } - })); - }, this); - } else if (obj != null && other != null && typeof obj == 'object' && typeof other == 'object') {// object contains object case + forEach( + other, + function(otherItem) { + this.assert( + some(obj, function(item, index) { + if (index in usedKeys) { + return false; + } + + try { + should(item).containDeep(otherItem); + usedKeys[index] = true; + return true; + } catch (e) { + if (e instanceof should.AssertionError) { + return false; + } + throw e; + } + }) + ); + }, + this + ); + } else if ( + obj != null && + other != null && + typeof obj == "object" && + typeof other == "object" + ) { + // object contains object case forEach(other, function(value, key) { should(obj[key]).containDeep(value); }); @@ -165,5 +197,4 @@ export default function(should, Assertion) { this.eql(other); } }); - } diff --git a/lib/ext/eql.js b/lib/ext/eql.js index 3f21bc1..fb8ad25 100644 --- a/lib/ext/eql.js +++ b/lib/ext/eql.js @@ -5,21 +5,21 @@ * MIT Licensed */ -import eql from 'should-equal'; -import getType from 'should-type'; -import { formatProp, format } from '../format'; -import { forEach } from 'should-type-adaptors'; +import eql from "should-equal"; +import getType from "should-type"; +import { formatProp, format } from "../format"; +import { forEach } from "should-type-adaptors"; function formatEqlResult(r, a, b) { - return ((r.path.length > 0 ? 'at ' + r.path.map(formatProp).join(' -> ') : '') + - (r.a === a ? '' : ', A has ' + format(r.a)) + - (r.b === b ? '' : ' and B has ' + format(r.b)) + - (r.showReason ? ' because ' + r.reason : '')).trim(); + return ((r.path.length > 0 + ? "at " + r.path.map(formatProp).join(" -> ") + : "") + + (r.a === a ? "" : ", A has " + format(r.a)) + + (r.b === b ? "" : " and B has " + format(r.b)) + + (r.showReason ? " because " + r.reason : "")).trim(); } export default function(should, Assertion) { - - /** * Deep object equality comparison. For full spec see [`should-equal tests`](https://github.com/shouldjs/equal/blob/master/test.js). * @@ -41,13 +41,15 @@ export default function(should, Assertion) { * ({ a: 10}).should.be.eql({ a: 10 }); * [ 'a' ].should.not.be.eql({ '0': 'a' }); */ - Assertion.add('eql', function(val, description) { - this.params = {operator: 'to equal', expected: val, message: description}; + Assertion.add("eql", function(val, description) { + this.params = { operator: "to equal", expected: val, message: description }; var obj = this.obj; var fails = eql(this.obj, val, should.config); - this.params.details = fails.map(function(fail) { - return formatEqlResult(fail, obj, val); - }).join(', '); + this.params.details = fails + .map(function(fail) { + return formatEqlResult(fail, obj, val); + }) + .join(", "); this.params.showDiff = eql(getType(obj), getType(val)).length === 0; @@ -71,18 +73,18 @@ export default function(should, Assertion) { * * should(null).be.exactly(null); */ - Assertion.add('equal', function(val, description) { - this.params = {operator: 'to be', expected: val, message: description}; + Assertion.add("equal", function(val, description) { + this.params = { operator: "to be", expected: val, message: description }; this.params.showDiff = eql(getType(this.obj), getType(val)).length === 0; this.assert(val === this.obj); }); - Assertion.alias('equal', 'equals'); - Assertion.alias('equal', 'exactly'); - Assertion.alias('eql', 'eqls'); - Assertion.alias('eql', 'deepEqual'); + Assertion.alias("equal", "equals"); + Assertion.alias("equal", "exactly"); + Assertion.alias("eql", "eqls"); + Assertion.alias("eql", "deepEqual"); function addOneOf(name, message, method) { Assertion.add(name, function(vals) { @@ -92,7 +94,7 @@ export default function(should, Assertion) { should(vals).be.Array(); } - this.params = {operator: message, expected: vals}; + this.params = { operator: message, expected: vals }; var obj = this.obj; var found = false; @@ -104,7 +106,7 @@ export default function(should, Assertion) { return false; } catch (e) { if (e instanceof should.AssertionError) { - return;//do nothing + return; //do nothing } throw e; } @@ -126,7 +128,7 @@ export default function(should, Assertion) { * 'ab'.should.be.equalOneOf('a', 10, 'ab'); * 'ab'.should.be.equalOneOf(['a', 10, 'ab']); */ - addOneOf('equalOneOf', 'to be equals one of', 'equal'); + addOneOf("equalOneOf", "to be equals one of", "equal"); /** * Exact comparison using .eql to be one of supplied objects. @@ -140,6 +142,5 @@ export default function(should, Assertion) { * ({a: 10}).should.be.oneOf('a', 10, 'ab', {a: 10}); * ({a: 10}).should.be.oneOf(['a', 10, 'ab', {a: 10}]); */ - addOneOf('oneOf', 'to be one of', 'eql'); - + addOneOf("oneOf", "to be one of", "eql"); } diff --git a/lib/ext/error.js b/lib/ext/error.js index acf8ece..2ecfd8c 100644 --- a/lib/ext/error.js +++ b/lib/ext/error.js @@ -4,8 +4,8 @@ * Copyright(c) 2013-2017 Denis Bardadym * MIT Licensed */ -import { isIterator, isGeneratorFunction } from 'should-util'; -import { functionName } from '../util'; +import { isIterator, isGeneratorFunction } from "should-util"; +import { functionName } from "../util"; export default function(should, Assertion) { var i = should.format; @@ -34,10 +34,10 @@ export default function(should, Assertion) { * yield throwError(); * }).should.throw(); */ - Assertion.add('throw', function(message, properties) { + Assertion.add("throw", function(message, properties) { var fn = this.obj; var err = {}; - var errorInfo = ''; + var errorInfo = ""; var thrown = false; if (isGeneratorFunction(fn)) { @@ -59,11 +59,11 @@ export default function(should, Assertion) { if (thrown) { if (message) { - if ('string' == typeof message) { + if ("string" == typeof message) { errorMatched = message == err.message; } else if (message instanceof RegExp) { errorMatched = message.test(err.message); - } else if ('function' == typeof message) { + } else if ("function" == typeof message) { errorMatched = err instanceof message; } else if (null != message) { try { @@ -79,12 +79,21 @@ export default function(should, Assertion) { } if (!errorMatched) { - if ('string' == typeof message || message instanceof RegExp) { - errorInfo = " with a message matching " + i(message) + ", but got '" + err.message + "'"; - } else if ('function' == typeof message) { - errorInfo = " of type " + functionName(message) + ", but got " + functionName(err.constructor); + if ("string" == typeof message || message instanceof RegExp) { + errorInfo = + " with a message matching " + + i(message) + + ", but got '" + + err.message + + "'"; + } else if ("function" == typeof message) { + errorInfo = + " of type " + + functionName(message) + + ", but got " + + functionName(err.constructor); } - } else if ('function' == typeof message && properties) { + } else if ("function" == typeof message && properties) { try { should(err).match(properties); } catch (e) { @@ -101,11 +110,11 @@ export default function(should, Assertion) { } } - this.params = { operator: 'to throw exception' + errorInfo }; + this.params = { operator: "to throw exception" + errorInfo }; this.assert(thrown); this.assert(errorMatched); }); - Assertion.alias('throw', 'throwError'); + Assertion.alias("throw", "throwError"); } diff --git a/lib/ext/match.js b/lib/ext/match.js index b75dc50..8ac75df 100644 --- a/lib/ext/match.js +++ b/lib/ext/match.js @@ -5,9 +5,9 @@ * MIT Licensed */ -import { formatProp } from '../format'; -import { some, forEach } from 'should-type-adaptors'; -import eql from 'should-equal'; +import { formatProp } from "../format"; +import { some, forEach } from "should-type-adaptors"; +import eql from "should-equal"; export default function(should, Assertion) { var i = should.format; @@ -68,69 +68,89 @@ export default function(should, Assertion) { * (new Error('boom')).should.match(/abc/);//passed because no keys * (new Error('boom')).should.not.match({ message: /abc/ });//check specified property */ - Assertion.add('match', function(other, description) { - this.params = {operator: 'to match ' + i(other), message: description}; + Assertion.add("match", function(other, description) { + this.params = { operator: "to match " + i(other), message: description }; if (eql(this.obj, other).length !== 0) { - if (other instanceof RegExp) { // something - regex - - if (typeof this.obj == 'string') { + if (other instanceof RegExp) { + // something - regex + if (typeof this.obj == "string") { this.assert(other.exec(this.obj)); - } else if (null != this.obj && typeof this.obj == 'object') { - - var notMatchedProps = [], matchedProps = []; - forEach(this.obj, function(value, name) { - if (other.exec(value)) { - matchedProps.push(formatProp(name)); - } else { - notMatchedProps.push(formatProp(name) + ' (' + i(value) + ')'); - } - }, this); + } else if (null != this.obj && typeof this.obj == "object") { + var notMatchedProps = [], + matchedProps = []; + forEach( + this.obj, + function(value, name) { + if (other.exec(value)) { + matchedProps.push(formatProp(name)); + } else { + notMatchedProps.push(formatProp(name) + " (" + i(value) + ")"); + } + }, + this + ); if (notMatchedProps.length) { - this.params.operator += '\n not matched properties: ' + notMatchedProps.join(', '); + this.params.operator += + "\n not matched properties: " + notMatchedProps.join(", "); } if (matchedProps.length) { - this.params.operator += '\n matched properties: ' + matchedProps.join(', '); + this.params.operator += + "\n matched properties: " + matchedProps.join(", "); } this.assert(notMatchedProps.length === 0); - } // should we try to convert to String and exec? - else { + } else { + // should we try to convert to String and exec? this.assert(false); } - } else if (typeof other == 'function') { + } else if (typeof other == "function") { var res; res = other(this.obj); //if we throw exception ok - it is used .should inside - if (typeof res == 'boolean') { + if (typeof res == "boolean") { this.assert(res); // if it is just boolean function assert on it } - } else if (other != null && this.obj != null && typeof other == 'object' && typeof this.obj == 'object') { // try to match properties (for Object and Array) + } else if ( + other != null && + this.obj != null && + typeof other == "object" && + typeof this.obj == "object" + ) { + // try to match properties (for Object and Array) notMatchedProps = []; matchedProps = []; - forEach(other, function(value, key) { - try { - should(this.obj).have.property(key).which.match(value); - matchedProps.push(formatProp(key)); - } catch (e) { - if (e instanceof should.AssertionError) { - notMatchedProps.push(formatProp(key) + ' (' + i(this.obj[key]) + ')'); - } else { - throw e; + forEach( + other, + function(value, key) { + try { + should(this.obj).have.property(key).which.match(value); + matchedProps.push(formatProp(key)); + } catch (e) { + if (e instanceof should.AssertionError) { + notMatchedProps.push( + formatProp(key) + " (" + i(this.obj[key]) + ")" + ); + } else { + throw e; + } } - } - }, this); + }, + this + ); if (notMatchedProps.length) { - this.params.operator += '\n not matched properties: ' + notMatchedProps.join(', '); + this.params.operator += + "\n not matched properties: " + notMatchedProps.join(", "); } if (matchedProps.length) { - this.params.operator += '\n matched properties: ' + matchedProps.join(', '); + this.params.operator += + "\n matched properties: " + matchedProps.join(", "); } this.assert(notMatchedProps.length === 0); @@ -161,12 +181,19 @@ export default function(should, Assertion) { * * { a: 'a', b: 'a', c: 'a' }.should.matchEach(function(value) { value.should.be.eql('a') }); */ - Assertion.add('matchEach', function(other, description) { - this.params = {operator: 'to match each ' + i(other), message: description}; - - forEach(this.obj, function(value) { - should(value).match(other); - }, this); + Assertion.add("matchEach", function(other, description) { + this.params = { + operator: "to match each " + i(other), + message: description + }; + + forEach( + this.obj, + function(value) { + should(value).match(other); + }, + this + ); }); /** @@ -190,23 +217,28 @@ export default function(should, Assertion) { * * { a: 'a', b: 'b', c: 'c' }.should.matchAny(function(value) { value.should.be.eql('a') }); */ - Assertion.add('matchAny', function(other, description) { - this.params = {operator: 'to match any ' + i(other), message: description}; - - this.assert(some(this.obj, function(value) { - try { - should(value).match(other); - return true; - } catch (e) { - if (e instanceof should.AssertionError) { - // Caught an AssertionError, return false to the iterator - return false; + Assertion.add("matchAny", function(other, description) { + this.params = { + operator: "to match any " + i(other), + message: description + }; + + this.assert( + some(this.obj, function(value) { + try { + should(value).match(other); + return true; + } catch (e) { + if (e instanceof should.AssertionError) { + // Caught an AssertionError, return false to the iterator + return false; + } + throw e; } - throw e; - } - })); + }) + ); }); - Assertion.alias('matchAny', 'matchSome'); - Assertion.alias('matchEach', 'matchEvery'); + Assertion.alias("matchAny", "matchSome"); + Assertion.alias("matchEach", "matchEvery"); } diff --git a/lib/ext/number.js b/lib/ext/number.js index 226f490..02b62cd 100644 --- a/lib/ext/number.js +++ b/lib/ext/number.js @@ -6,7 +6,6 @@ */ export default function(should, Assertion) { - /** * Assert given object is NaN * @name NaN @@ -17,8 +16,8 @@ export default function(should, Assertion) { * (10).should.not.be.NaN(); * NaN.should.be.NaN(); */ - Assertion.add('NaN', function() { - this.params = { operator: 'to be NaN' }; + Assertion.add("NaN", function() { + this.params = { operator: "to be NaN" }; this.assert(this.obj !== this.obj); }); @@ -34,12 +33,10 @@ export default function(should, Assertion) { * (10).should.not.be.Infinity(); * NaN.should.not.be.Infinity(); */ - Assertion.add('Infinity', function() { - this.params = { operator: 'to be Infinity' }; + Assertion.add("Infinity", function() { + this.params = { operator: "to be Infinity" }; - this.is.a.Number() - .and.not.a.NaN() - .and.assert(!isFinite(this.obj)); + this.is.a.Number().and.not.a.NaN().and.assert(!isFinite(this.obj)); }); /** @@ -55,8 +52,11 @@ export default function(should, Assertion) { * * (10).should.be.within(0, 20); */ - Assertion.add('within', function(start, finish, description) { - this.params = { operator: 'to be within ' + start + '..' + finish, message: description }; + Assertion.add("within", function(start, finish, description) { + this.params = { + operator: "to be within " + start + ".." + finish, + message: description + }; this.assert(this.obj >= start && this.obj <= finish); }); @@ -74,8 +74,11 @@ export default function(should, Assertion) { * * (9.99).should.be.approximately(10, 0.1); */ - Assertion.add('approximately', function(value, delta, description) { - this.params = { operator: 'to be approximately ' + value + ' ±' + delta, message: description }; + Assertion.add("approximately", function(value, delta, description) { + this.params = { + operator: "to be approximately " + value + " ±" + delta, + message: description + }; this.assert(Math.abs(this.obj - value) <= delta); }); @@ -93,8 +96,8 @@ export default function(should, Assertion) { * * (10).should.be.above(0); */ - Assertion.add('above', function(n, description) { - this.params = { operator: 'to be above ' + n, message: description }; + Assertion.add("above", function(n, description) { + this.params = { operator: "to be above " + n, message: description }; this.assert(this.obj > n); }); @@ -112,14 +115,14 @@ export default function(should, Assertion) { * * (0).should.be.below(10); */ - Assertion.add('below', function(n, description) { - this.params = { operator: 'to be below ' + n, message: description }; + Assertion.add("below", function(n, description) { + this.params = { operator: "to be below " + n, message: description }; this.assert(this.obj < n); }); - Assertion.alias('above', 'greaterThan'); - Assertion.alias('below', 'lessThan'); + Assertion.alias("above", "greaterThan"); + Assertion.alias("below", "lessThan"); /** * Assert given number above `n`. @@ -135,8 +138,11 @@ export default function(should, Assertion) { * (10).should.be.aboveOrEqual(0); * (10).should.be.aboveOrEqual(10); */ - Assertion.add('aboveOrEqual', function(n, description) { - this.params = { operator: 'to be above or equal ' + n, message: description }; + Assertion.add("aboveOrEqual", function(n, description) { + this.params = { + operator: "to be above or equal " + n, + message: description + }; this.assert(this.obj >= n); }); @@ -155,13 +161,15 @@ export default function(should, Assertion) { * (0).should.be.belowOrEqual(10); * (0).should.be.belowOrEqual(0); */ - Assertion.add('belowOrEqual', function(n, description) { - this.params = { operator: 'to be below or equal ' + n, message: description }; + Assertion.add("belowOrEqual", function(n, description) { + this.params = { + operator: "to be below or equal " + n, + message: description + }; this.assert(this.obj <= n); }); - Assertion.alias('aboveOrEqual', 'greaterThanOrEqual'); - Assertion.alias('belowOrEqual', 'lessThanOrEqual'); - + Assertion.alias("aboveOrEqual", "greaterThanOrEqual"); + Assertion.alias("belowOrEqual", "lessThanOrEqual"); } diff --git a/lib/ext/promise.js b/lib/ext/promise.js index 4445c63..0d01458 100644 --- a/lib/ext/promise.js +++ b/lib/ext/promise.js @@ -5,8 +5,8 @@ * MIT Licensed */ -import { functionName } from '../util'; -import { PromisedAssertion } from '../assertion'; +import { functionName } from "../util"; +import { PromisedAssertion } from "../assertion"; export default function(should, Assertion) { /** @@ -21,13 +21,12 @@ export default function(should, Assertion) { * (new Promise(function(resolve, reject) { resolve(10); })).should.be.a.Promise() * (10).should.not.be.a.Promise() */ - Assertion.add('Promise', function() { - this.params = {operator: 'to be promise'}; + Assertion.add("Promise", function() { + this.params = { operator: "to be promise" }; var obj = this.obj; - should(obj).have.property('then') - .which.is.a.Function(); + should(obj).have.property("then").which.is.a.Function(); }); /** @@ -49,23 +48,27 @@ export default function(should, Assertion) { * }); */ Assertion.prototype.fulfilled = function Assertion$fulfilled() { - this.params = {operator: 'to be fulfilled'}; + this.params = { operator: "to be fulfilled" }; should(this.obj).be.a.Promise(); var that = this; - return this.obj.then(function next$onResolve(value) { - if (that.negate) { - that.fail(); - } - return value; - }, function next$onReject(err) { - if (!that.negate) { - that.params.operator += ', but it was rejected with ' + should.format(err); - that.fail(); + return this.obj.then( + function next$onResolve(value) { + if (that.negate) { + that.fail(); + } + return value; + }, + function next$onReject(err) { + if (!that.negate) { + that.params.operator += + ", but it was rejected with " + should.format(err); + that.fail(); + } + return err; } - return err; - }); + ); }; /** @@ -88,26 +91,29 @@ export default function(should, Assertion) { * }); */ Assertion.prototype.rejected = function() { - this.params = {operator: 'to be rejected'}; + this.params = { operator: "to be rejected" }; should(this.obj).be.a.Promise(); var that = this; - return this.obj.then(function(value) { - if (!that.negate) { - that.params.operator += ', but it was fulfilled'; - if (arguments.length != 0) { - that.params.operator += ' with ' + should.format(value); + return this.obj.then( + function(value) { + if (!that.negate) { + that.params.operator += ", but it was fulfilled"; + if (arguments.length != 0) { + that.params.operator += " with " + should.format(value); + } + that.fail(); } - that.fail(); - } - return value; - }, function next$onError(err) { - if (that.negate) { - that.fail(); + return value; + }, + function next$onError(err) { + if (that.negate) { + that.fail(); + } + return err; } - return err; - }); + ); }; /** @@ -131,24 +137,30 @@ export default function(should, Assertion) { * }); */ Assertion.prototype.fulfilledWith = function(expectedValue) { - this.params = {operator: 'to be fulfilled with ' + should.format(expectedValue)}; + this.params = { + operator: "to be fulfilled with " + should.format(expectedValue) + }; should(this.obj).be.a.Promise(); var that = this; - return this.obj.then(function(value) { - if (that.negate) { - that.fail(); - } - should(value).eql(expectedValue); - return value; - }, function next$onError(err) { - if (!that.negate) { - that.params.operator += ', but it was rejected with ' + should.format(err); - that.fail(); + return this.obj.then( + function(value) { + if (that.negate) { + that.fail(); + } + should(value).eql(expectedValue); + return value; + }, + function next$onError(err) { + if (!that.negate) { + that.params.operator += + ", but it was rejected with " + should.format(err); + that.fail(); + } + return err; } - return err; - }); + ); }; /** @@ -178,68 +190,80 @@ export default function(should, Assertion) { * }); */ Assertion.prototype.rejectedWith = function(message, properties) { - this.params = {operator: 'to be rejected'}; + this.params = { operator: "to be rejected" }; should(this.obj).be.a.Promise(); var that = this; - return this.obj.then(function(value) { - if (!that.negate) { - that.fail(); - } - return value; - }, function next$onError(err) { - if (that.negate) { - that.fail(); - } + return this.obj.then( + function(value) { + if (!that.negate) { + that.fail(); + } + return value; + }, + function next$onError(err) { + if (that.negate) { + that.fail(); + } - var errorMatched = true; - var errorInfo = ''; + var errorMatched = true; + var errorInfo = ""; - if ('string' === typeof message) { - errorMatched = message === err.message; - } else if (message instanceof RegExp) { - errorMatched = message.test(err.message); - } else if ('function' === typeof message) { - errorMatched = err instanceof message; - } else if (message !== null && typeof message === 'object') { - try { - should(err).match(message); - } catch (e) { - if (e instanceof should.AssertionError) { - errorInfo = ': ' + e.message; - errorMatched = false; - } else { - throw e; + if ("string" === typeof message) { + errorMatched = message === err.message; + } else if (message instanceof RegExp) { + errorMatched = message.test(err.message); + } else if ("function" === typeof message) { + errorMatched = err instanceof message; + } else if (message !== null && typeof message === "object") { + try { + should(err).match(message); + } catch (e) { + if (e instanceof should.AssertionError) { + errorInfo = ": " + e.message; + errorMatched = false; + } else { + throw e; + } } } - } - if (!errorMatched) { - if ( typeof message === 'string' || message instanceof RegExp) { - errorInfo = ' with a message matching ' + should.format(message) + ", but got '" + err.message + "'"; - } else if ('function' === typeof message) { - errorInfo = ' of type ' + functionName(message) + ', but got ' + functionName(err.constructor); - } - } else if ('function' === typeof message && properties) { - try { - should(err).match(properties); - } catch (e) { - if (e instanceof should.AssertionError) { - errorInfo = ': ' + e.message; - errorMatched = false; - } else { - throw e; + if (!errorMatched) { + if (typeof message === "string" || message instanceof RegExp) { + errorInfo = + " with a message matching " + + should.format(message) + + ", but got '" + + err.message + + "'"; + } else if ("function" === typeof message) { + errorInfo = + " of type " + + functionName(message) + + ", but got " + + functionName(err.constructor); + } + } else if ("function" === typeof message && properties) { + try { + should(err).match(properties); + } catch (e) { + if (e instanceof should.AssertionError) { + errorInfo = ": " + e.message; + errorMatched = false; + } else { + throw e; + } } } - } - that.params.operator += errorInfo; + that.params.operator += errorInfo; - that.assert(errorMatched); + that.assert(errorMatched); - return err; - }); + return err; + } + ); }; /** @@ -263,22 +287,24 @@ export default function(should, Assertion) { * .should.be.finally.equal(10); * }); */ - Object.defineProperty(Assertion.prototype, 'finally', { + Object.defineProperty(Assertion.prototype, "finally", { get: function() { should(this.obj).be.a.Promise(); var that = this; - return new PromisedAssertion(this.obj.then(function(obj) { - var a = should(obj); + return new PromisedAssertion( + this.obj.then(function(obj) { + var a = should(obj); - a.negate = that.negate; - a.anyOne = that.anyOne; + a.negate = that.negate; + a.anyOne = that.anyOne; - return a; - })); + return a; + }) + ); } }); - Assertion.alias('finally', 'eventually'); + Assertion.alias("finally", "eventually"); } diff --git a/lib/ext/property.js b/lib/ext/property.js index ae255de..c49e469 100644 --- a/lib/ext/property.js +++ b/lib/ext/property.js @@ -5,11 +5,16 @@ * MIT Licensed */ -import { convertPropertyName } from '../util'; -import { hasOwnProperty } from 'should-util'; -import { formatProp } from '../format'; -import { isEmpty, has as hasKey, get as getValue, size } from 'should-type-adaptors'; -import eql from 'should-equal'; +import { convertPropertyName } from "../util"; +import { hasOwnProperty } from "should-util"; +import { formatProp } from "../format"; +import { + isEmpty, + has as hasKey, + get as getValue, + size +} from "should-type-adaptors"; +import eql from "should-equal"; var aSlice = Array.prototype.slice; @@ -27,11 +32,16 @@ export default function(should, Assertion) { * * ({ a: 10 }).should.have.propertyWithDescriptor('a', { enumerable: true }); */ - Assertion.add('propertyWithDescriptor', function(name, desc) { - this.params = {actual: this.obj, operator: 'to have own property with descriptor ' + i(desc)}; + Assertion.add("propertyWithDescriptor", function(name, desc) { + this.params = { + actual: this.obj, + operator: "to have own property with descriptor " + i(desc) + }; var obj = this.obj; this.have.ownProperty(name); - should(Object.getOwnPropertyDescriptor(Object(obj), name)).have.properties(desc); + should(Object.getOwnPropertyDescriptor(Object(obj), name)).have.properties( + desc + ); }); function processPropsArgs() { @@ -40,7 +50,7 @@ export default function(should, Assertion) { args.names = aSlice.call(arguments); } else { var arg = arguments[0]; - if (typeof arg === 'string') { + if (typeof arg === "string") { args.names = [arg]; } else if (Array.isArray(arg)) { args.names = arg; @@ -52,11 +62,14 @@ export default function(should, Assertion) { return args; } - Assertion.add('enumerable', function(name, val) { + Assertion.add("enumerable", function(name, val) { name = convertPropertyName(name); this.params = { - operator: "to have enumerable property " + formatProp(name) + (arguments.length > 1 ? " equal to " + i(val): "") + operator: + "to have enumerable property " + + formatProp(name) + + (arguments.length > 1 ? " equal to " + i(val) : "") }; var desc = { enumerable: true }; @@ -66,18 +79,21 @@ export default function(should, Assertion) { this.have.propertyWithDescriptor(name, desc); }); - Assertion.add('enumerables', function(/*names*/) { - var args = processPropsArgs.apply(null, arguments); + Assertion.add( + "enumerables", + function(/*names*/) { + var args = processPropsArgs.apply(null, arguments); - this.params = { - operator: "to have enumerables " + args.names.map(formatProp) - }; + this.params = { + operator: "to have enumerables " + args.names.map(formatProp) + }; - var obj = this.obj; - args.names.forEach(function(name) { - should(obj).have.enumerable(name); - }); - }); + var obj = this.obj; + args.names.forEach(function(name) { + should(obj).have.enumerable(name); + }); + } + ); /** * Asserts given object has property with optionally value. **On success it change given object to be value of property**. @@ -91,7 +107,7 @@ export default function(should, Assertion) { * * ({ a: 10 }).should.have.property('a'); */ - Assertion.add('property', function(name, val) { + Assertion.add("property", function(name, val) { name = convertPropertyName(name); if (arguments.length > 1) { var p = {}; @@ -116,12 +132,12 @@ export default function(should, Assertion) { * ({ a: 10, b: 20 }).should.have.properties([ 'a' ]); * ({ a: 10, b: 20 }).should.have.properties({ b: 20 }); */ - Assertion.add('properties', function(names) { + Assertion.add("properties", function(names) { var values = {}; if (arguments.length > 1) { names = aSlice.call(arguments); } else if (!Array.isArray(names)) { - if (typeof names == 'string' || typeof names == 'symbol') { + if (typeof names == "string" || typeof names == "symbol") { names = [names]; } else { values = names; @@ -129,7 +145,8 @@ export default function(should, Assertion) { } } - var obj = Object(this.obj), missingProperties = []; + var obj = Object(this.obj), + missingProperties = []; //just enumerate properties and check if they all present names.forEach(function(name) { @@ -142,19 +159,27 @@ export default function(should, Assertion) { if (props.length === 0) { props = names.map(formatProp); } else if (this.anyOne) { - props = names.filter(function(name) { - return missingProperties.indexOf(formatProp(name)) < 0; - }).map(formatProp); + props = names + .filter(function(name) { + return missingProperties.indexOf(formatProp(name)) < 0; + }) + .map(formatProp); } - var operator = (props.length === 1 ? - 'to have property ' : 'to have ' + (this.anyOne ? 'any of ' : '') + 'properties ') + props.join(', '); + var operator = + (props.length === 1 + ? "to have property " + : "to have " + (this.anyOne ? "any of " : "") + "properties ") + + props.join(", "); - this.params = {obj: this.obj, operator: operator}; + this.params = { obj: this.obj, operator: operator }; //check that all properties presented //or if we request one of them that at least one them presented - this.assert(missingProperties.length === 0 || (this.anyOne && missingProperties.length != names.length)); + this.assert( + missingProperties.length === 0 || + (this.anyOne && missingProperties.length != names.length) + ); // check if values in object matched expected var valueCheckNames = Object.keys(values); @@ -166,24 +191,35 @@ export default function(should, Assertion) { valueCheckNames.forEach(function(name) { var value = values[name]; if (eql(obj[name], value).length !== 0) { - wrongValues.push(formatProp(name) + ' of ' + i(value) + ' (got ' + i(obj[name]) + ')'); + wrongValues.push( + formatProp(name) + " of " + i(value) + " (got " + i(obj[name]) + ")" + ); } else { - props.push(formatProp(name) + ' of ' + i(value)); + props.push(formatProp(name) + " of " + i(value)); } }); - if ((wrongValues.length !== 0 && !this.anyOne) || (this.anyOne && props.length === 0)) { + if ( + (wrongValues.length !== 0 && !this.anyOne) || + (this.anyOne && props.length === 0) + ) { props = wrongValues; } - operator = (props.length === 1 ? - 'to have property ' : 'to have ' + (this.anyOne ? 'any of ' : '') + 'properties ') + props.join(', '); + operator = + (props.length === 1 + ? "to have property " + : "to have " + (this.anyOne ? "any of " : "") + "properties ") + + props.join(", "); - this.params = {obj: this.obj, operator: operator}; + this.params = { obj: this.obj, operator: operator }; //if there is no not matched values //or there is at least one matched - this.assert(wrongValues.length === 0 || (this.anyOne && wrongValues.length != valueCheckNames.length)); + this.assert( + wrongValues.length === 0 || + (this.anyOne && wrongValues.length != valueCheckNames.length) + ); } }); @@ -200,11 +236,11 @@ export default function(should, Assertion) { * * [1, 2].should.have.length(2); */ - Assertion.add('length', function(n, description) { - this.have.property('length', n, description); + Assertion.add("length", function(n, description) { + this.have.property("length", n, description); }); - Assertion.alias('length', 'lengthOf'); + Assertion.alias("length", "lengthOf"); /** * Asserts given object has own property. **On success it change given object to be value of property**. @@ -219,11 +255,11 @@ export default function(should, Assertion) { * * ({ a: 10 }).should.have.ownProperty('a'); */ - Assertion.add('ownProperty', function(name, description) { + Assertion.add("ownProperty", function(name, description) { name = convertPropertyName(name); this.params = { actual: this.obj, - operator: 'to have own property ' + formatProp(name), + operator: "to have own property " + formatProp(name), message: description }; @@ -232,7 +268,7 @@ export default function(should, Assertion) { this.obj = this.obj[name]; }); - Assertion.alias('ownProperty', 'hasOwnProperty'); + Assertion.alias("ownProperty", "hasOwnProperty"); /** * Asserts given object is empty. For strings, arrays and arguments it checks .length property, for objects it checks keys. @@ -246,10 +282,14 @@ export default function(should, Assertion) { * [].should.be.empty(); * ({}).should.be.empty(); */ - Assertion.add('empty', function() { - this.params = {operator: 'to be empty'}; - this.assert(isEmpty(this.obj)); - }, true); + Assertion.add( + "empty", + function() { + this.params = { operator: "to be empty" }; + this.assert(isEmpty(this.obj)); + }, + true + ); /** * Asserts given object has such keys. Compared to `properties`, `keys` does not accept Object as a argument. @@ -268,7 +308,7 @@ export default function(should, Assertion) { * * json.should.have.only.keys('type', 'version') */ - Assertion.add('keys', function(keys) { + Assertion.add("keys", function(keys) { keys = aSlice.call(arguments); var obj = Object(this.obj); @@ -278,12 +318,15 @@ export default function(should, Assertion) { return !hasKey(obj, key); }); - var verb = 'to have ' + (this.onlyThis ? 'only ': '') + (keys.length === 1 ? 'key ' : 'keys '); + var verb = + "to have " + + (this.onlyThis ? "only " : "") + + (keys.length === 1 ? "key " : "keys "); - this.params = {operator: verb + keys.join(', ')}; + this.params = { operator: verb + keys.join(", ") }; if (missingKeys.length > 0) { - this.params.operator += '\n\tmissing keys: ' + missingKeys.join(', '); + this.params.operator += "\n\tmissing keys: " + missingKeys.join(", "); } this.assert(missingKeys.length === 0); @@ -293,8 +336,7 @@ export default function(should, Assertion) { } }); - - Assertion.add('key', function(key) { + Assertion.add("key", function(key) { this.have.keys(key); this.obj = getValue(this.obj, key); }); @@ -312,7 +354,7 @@ export default function(should, Assertion) { * ({ a: 10 }).should.have.value('a', 10); * (new Map([[1, 2]])).should.have.value(1, 2); */ - Assertion.add('value', function(key, value) { + Assertion.add("value", function(key, value) { this.have.key(key).which.is.eql(value); }); @@ -328,8 +370,8 @@ export default function(should, Assertion) { * ({ a: 10 }).should.have.size(1); * (new Map([[1, 2]])).should.have.size(1); */ - Assertion.add('size', function(s) { - this.params = { operator: 'to have size ' + s }; + Assertion.add("size", function(s) { + this.params = { operator: "to have size " + s }; size(this.obj).should.be.exactly(s); }); @@ -344,7 +386,7 @@ export default function(should, Assertion) { * * ({ a: {b: 10}}).should.have.propertyByPath('a', 'b').eql(10); */ - Assertion.add('propertyByPath', function(properties) { + Assertion.add("propertyByPath", function(properties) { properties = aSlice.call(arguments); var allProps = properties.map(formatProp); @@ -358,12 +400,21 @@ export default function(should, Assertion) { var currentProperty; while (properties.length) { currentProperty = properties.shift(); - this.params = {operator: 'to have property by path ' + allProps.join(', ') + ' - failed on ' + formatProp(currentProperty)}; + this.params = { + operator: + "to have property by path " + + allProps.join(", ") + + " - failed on " + + formatProp(currentProperty) + }; obj = obj.have.property(currentProperty); foundProperties.push(currentProperty); } - this.params = {obj: this.obj, operator: 'to have property by path ' + allProps.join(', ')}; + this.params = { + obj: this.obj, + operator: "to have property by path " + allProps.join(", ") + }; this.obj = obj.obj; }); diff --git a/lib/ext/string.js b/lib/ext/string.js index 4af051a..bd557ab 100644 --- a/lib/ext/string.js +++ b/lib/ext/string.js @@ -17,8 +17,11 @@ export default function(should, Assertion) { * * 'abc'.should.startWith('a'); */ - Assertion.add('startWith', function(str, description) { - this.params = { operator: 'to start with ' + should.format(str), message: description }; + Assertion.add("startWith", function(str, description) { + this.params = { + operator: "to start with " + should.format(str), + message: description + }; this.assert(0 === this.obj.indexOf(str)); }); @@ -34,8 +37,11 @@ export default function(should, Assertion) { * * 'abca'.should.endWith('a'); */ - Assertion.add('endWith', function(str, description) { - this.params = { operator: 'to end with ' + should.format(str), message: description }; + Assertion.add("endWith", function(str, description) { + this.params = { + operator: "to end with " + should.format(str), + message: description + }; this.assert(this.obj.indexOf(str, this.obj.length - str.length) >= 0); }); diff --git a/lib/ext/type.js b/lib/ext/type.js index 5862f72..b2ed344 100644 --- a/lib/ext/type.js +++ b/lib/ext/type.js @@ -5,7 +5,7 @@ * MIT Licensed */ -import { functionName } from '../util'; +import { functionName } from "../util"; export default function(should, Assertion) { /** @@ -14,10 +14,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Number', function() { - this.params = {operator: 'to be a number'}; + Assertion.add("Number", function() { + this.params = { operator: "to be a number" }; - this.have.type('number'); + this.have.type("number"); }); /** @@ -27,13 +27,13 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('arguments', function() { - this.params = {operator: 'to be arguments'}; + Assertion.add("arguments", function() { + this.params = { operator: "to be arguments" }; - this.have.class('Arguments'); + this.have.class("Arguments"); }); - Assertion.alias('arguments', 'Arguments'); + Assertion.alias("arguments", "Arguments"); /** * Assert given object has some type using `typeof` @@ -43,8 +43,8 @@ export default function(should, Assertion) { * @param {string} [description] Optional message * @category assertion types */ - Assertion.add('type', function(type, description) { - this.params = {operator: 'to have type ' + type, message: description}; + Assertion.add("type", function(type, description) { + this.params = { operator: "to have type " + type, message: description }; should(typeof this.obj).be.exactly(type); }); @@ -58,13 +58,16 @@ export default function(should, Assertion) { * @param {string} [description] Optional message * @category assertion types */ - Assertion.add('instanceof', function(constructor, description) { - this.params = {operator: 'to be an instance of ' + functionName(constructor), message: description}; + Assertion.add("instanceof", function(constructor, description) { + this.params = { + operator: "to be an instance of " + functionName(constructor), + message: description + }; this.assert(Object(this.obj) instanceof constructor); }); - Assertion.alias('instanceof', 'instanceOf'); + Assertion.alias("instanceof", "instanceOf"); /** * Assert given object is function @@ -72,10 +75,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Function', function() { - this.params = {operator: 'to be a function'}; + Assertion.add("Function", function() { + this.params = { operator: "to be a function" }; - this.have.type('function'); + this.have.type("function"); }); /** @@ -84,10 +87,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Object', function() { - this.params = {operator: 'to be an object'}; + Assertion.add("Object", function() { + this.params = { operator: "to be an object" }; - this.is.not.null().and.have.type('object'); + this.is.not.null().and.have.type("object"); }); /** @@ -96,10 +99,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('String', function() { - this.params = {operator: 'to be a string'}; + Assertion.add("String", function() { + this.params = { operator: "to be a string" }; - this.have.type('string'); + this.have.type("string"); }); /** @@ -108,10 +111,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Array', function() { - this.params = {operator: 'to be an array'}; + Assertion.add("Array", function() { + this.params = { operator: "to be an array" }; - this.have.class('Array'); + this.have.class("Array"); }); /** @@ -120,10 +123,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Boolean', function() { - this.params = {operator: 'to be a boolean'}; + Assertion.add("Boolean", function() { + this.params = { operator: "to be a boolean" }; - this.have.type('boolean'); + this.have.type("boolean"); }); /** @@ -132,8 +135,8 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Error', function() { - this.params = {operator: 'to be an error'}; + Assertion.add("Error", function() { + this.params = { operator: "to be an error" }; this.have.instanceOf(Error); }); @@ -144,8 +147,8 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('Date', function() { - this.params = {operator: 'to be a date'}; + Assertion.add("Date", function() { + this.params = { operator: "to be a date" }; this.have.instanceOf(Date); }); @@ -157,13 +160,13 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('null', function() { - this.params = {operator: 'to be null'}; + Assertion.add("null", function() { + this.params = { operator: "to be null" }; this.assert(this.obj === null); }); - Assertion.alias('null', 'Null'); + Assertion.alias("null", "Null"); /** * Assert given object has some internal [[Class]], via Object.prototype.toString call @@ -172,13 +175,15 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('class', function(cls) { - this.params = {operator: 'to have [[Class]] ' + cls}; + Assertion.add("class", function(cls) { + this.params = { operator: "to have [[Class]] " + cls }; - this.assert(Object.prototype.toString.call(this.obj) === '[object ' + cls + ']'); + this.assert( + Object.prototype.toString.call(this.obj) === "[object " + cls + "]" + ); }); - Assertion.alias('class', 'Class'); + Assertion.alias("class", "Class"); /** * Assert given object is undefined @@ -187,13 +192,13 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion types */ - Assertion.add('undefined', function() { - this.params = {operator: 'to be undefined'}; + Assertion.add("undefined", function() { + this.params = { operator: "to be undefined" }; this.assert(this.obj === void 0); }); - Assertion.alias('undefined', 'Undefined'); + Assertion.alias("undefined", "Undefined"); /** * Assert given object supports es6 iterable protocol (just check @@ -202,8 +207,8 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion es6 */ - Assertion.add('iterable', function() { - this.params = {operator: 'to be iterable'}; + Assertion.add("iterable", function() { + this.params = { operator: "to be iterable" }; should(this.obj).have.property(Symbol.iterator).which.is.a.Function(); }); @@ -215,10 +220,10 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion es6 */ - Assertion.add('iterator', function() { - this.params = {operator: 'to be iterator'}; + Assertion.add("iterator", function() { + this.params = { operator: "to be iterator" }; - should(this.obj).have.property('next').which.is.a.Function(); + should(this.obj).have.property("next").which.is.a.Function(); }); /** @@ -227,11 +232,11 @@ export default function(should, Assertion) { * @memberOf Assertion * @category assertion es6 */ - Assertion.add('generator', function() { - this.params = {operator: 'to be generator'}; + Assertion.add("generator", function() { + this.params = { operator: "to be generator" }; - should(this.obj).be.iterable - .and.iterator - .and.it.is.equal(this.obj[Symbol.iterator]()); + should(this.obj).be.iterable.and.iterator.and.it.is.equal( + this.obj[Symbol.iterator]() + ); }); } diff --git a/lib/format.js b/lib/format.js index 158bad5..074027a 100644 --- a/lib/format.js +++ b/lib/format.js @@ -1,5 +1,5 @@ -import sformat from 'should-format'; -import config from './config'; +import sformat from "should-format"; +import config from "./config"; export function format(value, opts) { return config.getFormatter(opts).format(value); diff --git a/lib/should.js b/lib/should.js index f6d5e2c..49833e1 100644 --- a/lib/should.js +++ b/lib/should.js @@ -4,28 +4,28 @@ * Copyright(c) 2013-2017 Denis Bardadym * MIT Licensed */ -import t from 'should-type'; -import eq from 'should-equal'; -import defaultFormat from 'should-format'; - -import { isWrapperType } from './util'; -import { format } from './format'; -import AssertionError from './assertion-error'; -import { Assertion } from './assertion'; -import config from './config'; - -import assertExtensions from './ext/assert'; -import chainAssertions from './ext/chain'; -import booleanAssertions from './ext/bool'; -import numberAssertions from './ext/number'; -import typeAssertions from './ext/type'; -import equalityAssertions from './ext/eql'; -import promiseAssertions from './ext/promise'; -import stringAssertions from './ext/string'; -import containAssertions from './ext/contain'; -import propertyAssertions from './ext/property'; -import errorAssertions from './ext/error'; -import matchingAssertions from './ext/match'; +import t from "should-type"; +import eq from "should-equal"; +import defaultFormat from "should-format"; + +import { isWrapperType } from "./util"; +import { format } from "./format"; +import AssertionError from "./assertion-error"; +import { Assertion } from "./assertion"; +import config from "./config"; + +import assertExtensions from "./ext/assert"; +import chainAssertions from "./ext/chain"; +import booleanAssertions from "./ext/bool"; +import numberAssertions from "./ext/number"; +import typeAssertions from "./ext/type"; +import equalityAssertions from "./ext/eql"; +import promiseAssertions from "./ext/promise"; +import stringAssertions from "./ext/string"; +import containAssertions from "./ext/contain"; +import propertyAssertions from "./ext/property"; +import errorAssertions from "./ext/error"; +import matchingAssertions from "./ext/match"; /** * Our function should @@ -38,7 +38,7 @@ import matchingAssertions from './ext/match'; * should('abc').be.a.String(); */ function should(obj) { - return (new Assertion(obj)); + return new Assertion(obj); } should.AssertionError = AssertionError; @@ -100,14 +100,13 @@ export default should; * should.not.exist(Object.prototype.must); */ should.extend = function(propertyName, proto) { - propertyName = propertyName || 'should'; + propertyName = propertyName || "should"; proto = proto || Object.prototype; var prevDescriptor = Object.getOwnPropertyDescriptor(proto, propertyName); Object.defineProperty(proto, propertyName, { - set: function() { - }, + set: function() {}, get: function() { return should(isWrapperType(this) ? this.valueOf() : this); }, diff --git a/lib/util.js b/lib/util.js index a20e3d4..eb5ccf1 100644 --- a/lib/util.js +++ b/lib/util.js @@ -4,17 +4,17 @@ * Copyright(c) 2013-2017 Denis Bardadym * MIT Licensed */ -import sformat from 'should-format'; +import sformat from "should-format"; export function isWrapperType(obj) { - return obj instanceof Number || - obj instanceof String || - obj instanceof Boolean; + return ( + obj instanceof Number || obj instanceof String || obj instanceof Boolean + ); } // XXX make it more strict: numbers, strings, symbols - and nothing else export function convertPropertyName(name) { - return (typeof name === 'symbol') ? name : String(name); + return typeof name === "symbol" ? name : String(name); } export var functionName = sformat.functionName; diff --git a/package-lock.json b/package-lock.json index ef6f2af..b532b02 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4335,6 +4335,12 @@ "integrity": "sha1-gV7R9uvGWSb4ZbMQwHE7yzMVzks=", "dev": true }, + "prettier": { + "version": "1.5.3", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-1.5.3.tgz", + "integrity": "sha1-WdrcaDNF7GuI+IuU7Urn4do5S/4=", + "dev": true + }, "process": { "version": "0.11.10", "resolved": "https://registry.npmjs.org/process/-/process-0.11.10.tgz", diff --git a/package.json b/package.json index d36de4e..327de7f 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,7 @@ "eslint-config-shouldjs": "^1.0.0", "mocha": "latest", "mocha-better-spec-reporter": "latest", + "prettier": "^1.5.3", "rollup": "^0.49.1", "rollup-plugin-node-resolve": "^3.0.0", "zuul": "latest" diff --git a/test/exist.test.js b/test/exist.test.js index 55b1fa8..24c1d33 100644 --- a/test/exist.test.js +++ b/test/exist.test.js @@ -2,12 +2,12 @@ * Module dependencies. */ -var should = require('../'); +var should = require("../"); function err(fn, msg) { try { fn(); - should.fail('expected an error'); + should.fail("expected an error"); } catch (err) { should.equal(msg, err.message); } @@ -15,78 +15,74 @@ function err(fn, msg) { function err_should_exist(obj) { err(function() { - should.exist(obj); - }, - 'expected ' + should.format(obj) + ' to exist'); + should.exist(obj); + }, "expected " + should.format(obj) + " to exist"); } function err_should_not_exist(obj) { err(function() { - should.not.exist(obj); - }, - 'expected ' + should.format(obj) + ' to not exist'); + should.not.exist(obj); + }, "expected " + should.format(obj) + " to not exist"); } -describe('exist', function() { - +describe("exist", function() { // static should.exist() pass, - it('test static should.exist() pass w/ bool', function() { + it("test static should.exist() pass w/ bool", function() { should.exist(false); }); - it('test static should.exist() pass w/ number', function() { + it("test static should.exist() pass w/ number", function() { should.exist(0); }); - it('test static should.exist() pass w/ string', function() { - should.exist(''); + it("test static should.exist() pass w/ string", function() { + should.exist(""); }); - it('test static should.exist() pass w/ object', function() { + it("test static should.exist() pass w/ object", function() { should.exist({}); }); - it('test static should.exist() pass w/ array', function() { + it("test static should.exist() pass w/ array", function() { should.exist([]); }); // static should.exist() fail, - it('test static should.exist() fail w/ null', function() { + it("test static should.exist() fail w/ null", function() { err_should_exist(null); }); - it('test static should.exist() fail w/ undefined', function() { + it("test static should.exist() fail w/ undefined", function() { err_should_exist(undefined); }); // static should.not.exist() pass, - it('test static should.not.exist() pass w/ null', function() { + it("test static should.not.exist() pass w/ null", function() { should.not.exist(null); }); - it('test static should.not.exist() pass w/ undefined', function() { + it("test static should.not.exist() pass w/ undefined", function() { should.not.exist(undefined); }); -// static should.not.exist() fail, - it('test static should.not.exist() fail w/ bool', function() { + // static should.not.exist() fail, + it("test static should.not.exist() fail w/ bool", function() { err_should_not_exist(false); }); - it('test static should.not.exist() fail w/ number', function() { + it("test static should.not.exist() fail w/ number", function() { err_should_not_exist(0); }); - it('test static should.not.exist() fail w/ string', function() { - err_should_not_exist(''); + it("test static should.not.exist() fail w/ string", function() { + err_should_not_exist(""); }); - it('test static should.not.exist() fail w/ object', function() { + it("test static should.not.exist() fail w/ object", function() { err_should_not_exist({}); }); - it('test static should.not.exist() fail w/ array', function() { + it("test static should.not.exist() fail w/ array", function() { err_should_not_exist([]); }); - }); diff --git a/test/ext/assert.test.js b/test/ext/assert.test.js index 408dcce..950e344 100644 --- a/test/ext/assert.test.js +++ b/test/ext/assert.test.js @@ -19,7 +19,7 @@ // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE // USE OR OTHER DEALINGS IN THE SOFTWARE. -var assert = require('../../'); +var assert = require("../../"); var keys = Object.keys; @@ -30,112 +30,156 @@ function makeBlock(f) { }; } -describe('assert', function() { - it('.ok', function() { - assert.throws(makeBlock(assert.ok, false), assert.AssertionError, 'ok(false)'); +describe("assert", function() { + it(".ok", function() { + assert.throws( + makeBlock(assert.ok, false), + assert.AssertionError, + "ok(false)" + ); - assert.doesNotThrow(makeBlock(assert.ok, true), assert.AssertionError, 'ok(true)'); + assert.doesNotThrow( + makeBlock(assert.ok, true), + assert.AssertionError, + "ok(true)" + ); - assert.doesNotThrow(makeBlock(assert.ok, 'test', 'ok(\'test\')')); + assert.doesNotThrow(makeBlock(assert.ok, "test", "ok('test')")); - assert.throws(makeBlock(assert.ok, false), - assert.AssertionError, 'ok(false)'); + assert.throws( + makeBlock(assert.ok, false), + assert.AssertionError, + "ok(false)" + ); - assert.doesNotThrow(makeBlock(assert.ok, true), - assert.AssertionError, 'ok(true)'); + assert.doesNotThrow( + makeBlock(assert.ok, true), + assert.AssertionError, + "ok(true)" + ); - assert.doesNotThrow(makeBlock(assert.ok, 'test'), 'ok(\'test\')'); + assert.doesNotThrow(makeBlock(assert.ok, "test"), "ok('test')"); }); - it('.equal', function() { - assert.throws(makeBlock(assert.equal, true, false), assert.AssertionError, 'equal'); + it(".equal", function() { + assert.throws( + makeBlock(assert.equal, true, false), + assert.AssertionError, + "equal" + ); - assert.doesNotThrow(makeBlock(assert.equal, null, null), 'equal'); + assert.doesNotThrow(makeBlock(assert.equal, null, null), "equal"); - assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), 'equal'); + assert.doesNotThrow(makeBlock(assert.equal, undefined, undefined), "equal"); - assert.doesNotThrow(makeBlock(assert.equal, null, undefined), 'equal'); + assert.doesNotThrow(makeBlock(assert.equal, null, undefined), "equal"); - assert.doesNotThrow(makeBlock(assert.equal, true, true), 'equal'); + assert.doesNotThrow(makeBlock(assert.equal, true, true), "equal"); - assert.doesNotThrow(makeBlock(assert.equal, 2, '2'), 'equal'); + assert.doesNotThrow(makeBlock(assert.equal, 2, "2"), "equal"); - assert.doesNotThrow(makeBlock(assert.notEqual, true, false), 'notEqual'); + assert.doesNotThrow(makeBlock(assert.notEqual, true, false), "notEqual"); - assert.throws(makeBlock(assert.notEqual, true, true), - assert.AssertionError, 'notEqual'); + assert.throws( + makeBlock(assert.notEqual, true, true), + assert.AssertionError, + "notEqual" + ); }); - it('.strictEqual', function() { - assert.throws(makeBlock(assert.strictEqual, 2, '2'), - assert.AssertionError, 'strictEqual'); + it(".strictEqual", function() { + assert.throws( + makeBlock(assert.strictEqual, 2, "2"), + assert.AssertionError, + "strictEqual" + ); - assert.throws(makeBlock(assert.strictEqual, null, undefined), - assert.AssertionError, 'strictEqual'); + assert.throws( + makeBlock(assert.strictEqual, null, undefined), + assert.AssertionError, + "strictEqual" + ); - assert.doesNotThrow(makeBlock(assert.notStrictEqual, 2, '2'), 'notStrictEqual'); + assert.doesNotThrow( + makeBlock(assert.notStrictEqual, 2, "2"), + "notStrictEqual" + ); }); - it('.deepEqual - 7.2', function() { - assert.doesNotThrow(makeBlock(assert.deepEqual, new Date(2000, 3, 14), - new Date(2000, 3, 14)), 'deepEqual date'); + it(".deepEqual - 7.2", function() { + assert.doesNotThrow( + makeBlock(assert.deepEqual, new Date(2000, 3, 14), new Date(2000, 3, 14)), + "deepEqual date" + ); - assert.throws(makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)), + assert.throws( + makeBlock(assert.deepEqual, new Date(), new Date(2000, 3, 14)), assert.AssertionError, - 'deepEqual date'); + "deepEqual date" + ); }); - it('.deepEqual - 7.3', function() { + it(".deepEqual - 7.3", function() { assert.doesNotThrow(makeBlock(assert.deepEqual, /a/, /a/)); assert.doesNotThrow(makeBlock(assert.deepEqual, /a/g, /a/g)); assert.doesNotThrow(makeBlock(assert.deepEqual, /a/i, /a/i)); assert.doesNotThrow(makeBlock(assert.deepEqual, /a/m, /a/m)); - assert.doesNotThrow(makeBlock(assert.deepEqual, /a/igm, /a/igm)); + assert.doesNotThrow(makeBlock(assert.deepEqual, /a/gim, /a/gim)); assert.throws(makeBlock(assert.deepEqual, /ab/, /a/)); assert.throws(makeBlock(assert.deepEqual, /a/g, /a/)); assert.throws(makeBlock(assert.deepEqual, /a/i, /a/)); assert.throws(makeBlock(assert.deepEqual, /a/m, /a/)); - assert.throws(makeBlock(assert.deepEqual, /a/igm, /a/im)); + assert.throws(makeBlock(assert.deepEqual, /a/gim, /a/im)); var re1 = /a/; re1.lastIndex = 3; assert.throws(makeBlock(assert.deepEqual, re1, /a/)); }); - it('.deepEqual - 7.4', function() { - assert.throws(makeBlock(assert.deepEqual, 4, '4'), 'deepEqual == check'); - assert.throws(makeBlock(assert.deepEqual, true, 1), 'deepEqual == check'); - assert.throws(makeBlock(assert.deepEqual, 4, '5'), + it(".deepEqual - 7.4", function() { + assert.throws(makeBlock(assert.deepEqual, 4, "4"), "deepEqual == check"); + assert.throws(makeBlock(assert.deepEqual, true, 1), "deepEqual == check"); + assert.throws( + makeBlock(assert.deepEqual, 4, "5"), assert.AssertionError, - 'deepEqual == check'); + "deepEqual == check" + ); }); - it('.deepEqual - 7.5', function() { + it(".deepEqual - 7.5", function() { // having the same number of owned properties && the same set of keys - assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4}, {a: 4})); - assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '2'}, {a: 4, b: '2'})); - assert.throws(makeBlock(assert.deepEqual, [4], ['4'])); - assert.throws(makeBlock(assert.deepEqual, {a: 4}, {a: 4, b: true}), - assert.AssertionError); - assert.throws(makeBlock(assert.deepEqual, ['a'], {0: 'a'})); + assert.doesNotThrow(makeBlock(assert.deepEqual, { a: 4 }, { a: 4 })); + assert.doesNotThrow( + makeBlock(assert.deepEqual, { a: 4, b: "2" }, { a: 4, b: "2" }) + ); + assert.throws(makeBlock(assert.deepEqual, [4], ["4"])); + assert.throws( + makeBlock(assert.deepEqual, { a: 4 }, { a: 4, b: true }), + assert.AssertionError + ); + assert.throws(makeBlock(assert.deepEqual, ["a"], { 0: "a" })); //(although not necessarily the same order), - assert.doesNotThrow(makeBlock(assert.deepEqual, {a: 4, b: '1'}, {b: '1', a: 4})); + assert.doesNotThrow( + makeBlock(assert.deepEqual, { a: 4, b: "1" }, { b: "1", a: 4 }) + ); var a1 = [1, 2, 3]; var a2 = [1, 2, 3]; - a1.a = 'test'; + a1.a = "test"; a1.b = true; a2.b = true; - a2.a = 'test'; - assert.throws(makeBlock(assert.deepEqual, keys(a1), keys(a2)), - assert.AssertionError); + a2.a = "test"; + assert.throws( + makeBlock(assert.deepEqual, keys(a1), keys(a2)), + assert.AssertionError + ); assert.doesNotThrow(makeBlock(assert.deepEqual, a1, a2)); }); - it('.deepEqual - instances', function() { + it(".deepEqual - instances", function() { // having an identical prototype property var nbRoot = { toString: function() { - return this.first + ' ' + this.last; + return this.first + " " + this.last; } }; @@ -155,30 +199,37 @@ describe('assert', function() { nameBuilder2.prototype = nbRoot; - var nb1 = new nameBuilder('Ryan', 'Dahl'); - var nb2 = new nameBuilder2('Ryan', 'Dahl'); + var nb1 = new nameBuilder("Ryan", "Dahl"); + var nb2 = new nameBuilder2("Ryan", "Dahl"); assert.doesNotThrow(makeBlock(assert.deepEqual, nb1, nb2)); // assert working there only because nb2 prototype is Function - it really does not check prototypes nameBuilder2.prototype = Object; - nb2 = new nameBuilder2('Ryan', 'Dahl'); + nb2 = new nameBuilder2("Ryan", "Dahl"); assert.throws(makeBlock(assert.deepEqual, nb1, nb2), assert.AssertionError); // String literal + object blew up my implementation... - assert.throws(makeBlock(assert.deepEqual, 'a', {}), assert.AssertionError); + assert.throws(makeBlock(assert.deepEqual, "a", {}), assert.AssertionError); }); function thrower(errorConstructor) { - throw new errorConstructor('test'); + throw new errorConstructor("test"); } - it('Testing the throwing', function() { + it("Testing the throwing", function() { makeBlock(thrower, assert.AssertionError); // the basic calls work - assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError, 'message'); - assert.throws(makeBlock(thrower, assert.AssertionError), assert.AssertionError); + assert.throws( + makeBlock(thrower, assert.AssertionError), + assert.AssertionError, + "message" + ); + assert.throws( + makeBlock(thrower, assert.AssertionError), + assert.AssertionError + ); assert.throws(makeBlock(thrower, assert.AssertionError)); // if not passing an error, catch all. @@ -190,11 +241,14 @@ describe('assert', function() { assert.throws(makeBlock(thrower, TypeError), assert.AssertionError); } catch (e) { threw = true; - assert.ok(e instanceof TypeError, 'type'); + assert.ok(e instanceof TypeError, "type"); } - assert.equal(true, threw, - 'a.throws with an explicit error is eating extra errors', - assert.AssertionError); + assert.equal( + true, + threw, + "a.throws with an explicit error is eating extra errors", + assert.AssertionError + ); threw = false; // doesNotThrow should pass through all errors @@ -204,8 +258,11 @@ describe('assert', function() { threw = true; assert.ok(e instanceof TypeError); } - assert.equal(true, threw, - 'a.doesNotThrow with an explicit error is eating extra errors'); + assert.equal( + true, + threw, + "a.doesNotThrow with an explicit error is eating extra errors" + ); // key difference is that throwing our correct error makes an assertion error try { @@ -214,13 +271,16 @@ describe('assert', function() { threw = true; assert.ok(e instanceof assert.AssertionError); } - assert.equal(true, threw, - 'a.doesNotThrow is not catching type matching errors'); + assert.equal( + true, + threw, + "a.doesNotThrow is not catching type matching errors" + ); }); - it('.ifError', function() { + it(".ifError", function() { assert.throws(function() { - assert.ifError(new Error('test error')); + assert.ifError(new Error("test error")); }); assert.doesNotThrow(function() { assert.ifError(null); @@ -230,34 +290,31 @@ describe('assert', function() { }); }); - it('make sure that validating using constructor really works', function() { + it("make sure that validating using constructor really works", function() { var threw = false; try { - assert.throws( - function() { - throw ({}); - }, - Array - ); + assert.throws(function() { + throw {}; + }, Array); } catch (e) { threw = true; } - assert.ok(threw, 'wrong constructor validation'); + assert.ok(threw, "wrong constructor validation"); }); - it(' use a RegExp to validate error message', function() { + it(" use a RegExp to validate error message", function() { assert.throws(makeBlock(thrower, TypeError), /test/); }); - it('se a fn to validate error object', function() { + it("se a fn to validate error object", function() { assert.throws(makeBlock(thrower, TypeError), function(err) { - if ((err instanceof TypeError) && /test/.test(err)) { + if (err instanceof TypeError && /test/.test(err)) { return true; } }); }); - it('Make sure deepEqual doesn\'t loop forever on circular refs', function() { + it("Make sure deepEqual doesn't loop forever on circular refs", function() { var b = {}; b.b = b; @@ -274,40 +331,45 @@ describe('assert', function() { assert.ok(!gotError); }); - - it('test assertion message', function() { + it("test assertion message", function() { function testAssertionMessage(actual, expected) { try { - assert.equal(actual, ''); + assert.equal(actual, ""); } catch (e) { - assert.equal(e.toString(), - ['AssertionError: expected', expected, '==', "''"].join(' ')); + assert.equal( + e.toString(), + ["AssertionError: expected", expected, "==", "''"].join(" ") + ); } } - testAssertionMessage(undefined, 'undefined'); - testAssertionMessage(null, 'null'); - testAssertionMessage(true, 'true'); - testAssertionMessage(false, 'false'); - testAssertionMessage(0, '0'); - testAssertionMessage(100, '100'); - testAssertionMessage(NaN, 'NaN'); - testAssertionMessage(Infinity, 'Infinity'); - testAssertionMessage(-Infinity, '-Infinity'); - testAssertionMessage('', '""'); - testAssertionMessage('foo', "'foo'"); - testAssertionMessage([], 'Array []'); - testAssertionMessage([1, 2, 3], 'Array [ 1, 2, 3 ]'); - testAssertionMessage(/a/, '/a/'); - testAssertionMessage(function f() { - }, 'Function { name: \'f\' }'); - testAssertionMessage({}, 'Object {}'); - testAssertionMessage({a: undefined, b: null}, 'Object { a: undefined, b: null }'); - testAssertionMessage({a: NaN, b: Infinity, c: -Infinity}, - 'Object { a: NaN, b: Infinity, c: -Infinity }'); + testAssertionMessage(undefined, "undefined"); + testAssertionMessage(null, "null"); + testAssertionMessage(true, "true"); + testAssertionMessage(false, "false"); + testAssertionMessage(0, "0"); + testAssertionMessage(100, "100"); + testAssertionMessage(NaN, "NaN"); + testAssertionMessage(Infinity, "Infinity"); + testAssertionMessage(-Infinity, "-Infinity"); + testAssertionMessage("", '""'); + testAssertionMessage("foo", "'foo'"); + testAssertionMessage([], "Array []"); + testAssertionMessage([1, 2, 3], "Array [ 1, 2, 3 ]"); + testAssertionMessage(/a/, "/a/"); + testAssertionMessage(function f() {}, "Function { name: 'f' }"); + testAssertionMessage({}, "Object {}"); + testAssertionMessage( + { a: undefined, b: null }, + "Object { a: undefined, b: null }" + ); + testAssertionMessage( + { a: NaN, b: Infinity, c: -Infinity }, + "Object { a: NaN, b: Infinity, c: -Infinity }" + ); }); - it('regressions from node.js testcase', function() { + it("regressions from node.js testcase", function() { var threw = false; try { @@ -316,20 +378,23 @@ describe('assert', function() { }); } catch (e) { threw = true; - assert.equal(e.message, 'Missing expected exception..'); + assert.equal(e.message, "Missing expected exception.."); } assert.ok(threw); try { assert.equal(1, 2); } catch (e) { - assert.equal(e.toString().split('\n')[0], 'AssertionError: expected 1 == 2'); + assert.equal( + e.toString().split("\n")[0], + "AssertionError: expected 1 == 2" + ); } try { - assert.equal(1, 2, 'oh no'); + assert.equal(1, 2, "oh no"); } catch (e) { - assert.equal(e.toString().split('\n')[0], 'AssertionError: oh no'); + assert.equal(e.toString().split("\n")[0], "AssertionError: oh no"); } }); }); diff --git a/test/ext/bool.test.js b/test/ext/bool.test.js index 5ca306e..e3d4cc8 100644 --- a/test/ext/bool.test.js +++ b/test/ext/bool.test.js @@ -1,14 +1,14 @@ -var err = require('../util').err; -require('../../'); +var err = require("../util").err; +require("../../"); -describe('bool', function() { - it('test true', function() { +describe("bool", function() { + it("test true", function() { true.should.be.true(); false.should.not.be.true(); (1).should.not.be.true(); err(function() { - 'test'.should.be.true(); + "test".should.be.true(); }, "expected 'test' to be true"); err(function() { @@ -16,17 +16,17 @@ describe('bool', function() { }, "expected true not to be true (false negative fail)"); err(function() { - (false).should.be.true('My text'); - }, 'My text'); + false.should.be.true("My text"); + }, "My text"); }); - it('test false', function() { + it("test false", function() { false.should.be.false(); true.should.not.be.false(); (0).should.not.be.false(); err(function() { - ''.should.be.false(); + "".should.be.false(); }, "expected '' to be false"); err(function() { @@ -34,22 +34,22 @@ describe('bool', function() { }, "expected false not to be false (false negative fail)"); err(function() { - (true).should.be.false('My text'); - }, 'My text'); + true.should.be.false("My text"); + }, "My text"); }); - it('test ok', function() { + it("test ok", function() { true.should.be.ok(); false.should.not.be.ok(); (1).should.be.ok(); (0).should.not.be.ok(); err(function() { - ''.should.be.ok(); + "".should.be.ok(); }, "expected '' to be truthy"); err(function() { - 'test'.should.not.be.ok(); + "test".should.not.be.ok(); }, "expected 'test' not to be truthy (false negative fail)"); }); }); diff --git a/test/ext/contain.test.js b/test/ext/contain.test.js index bee1c1d..7369167 100644 --- a/test/ext/contain.test.js +++ b/test/ext/contain.test.js @@ -1,201 +1,107 @@ -var err = require('../util').err; -require('../../'); +var err = require("../util").err; +require("../../"); -describe('contain*', function() { - - it('test containEql', function() { - 'hello boy'.should.containEql('boy'); +describe("contain*", function() { + it("test containEql", function() { + "hello boy".should.containEql("boy"); [1, 2, 3].should.containEql(3); - [ - [1], - [2], - [3] - ].should.containEql([3]); - [ - [1], - [2], - [3, 4] - ].should.not.containEql([3]); - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.containEql({a: 'a'}); - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.not.containEql({b: 'b'}); - - ({}).should.not.containEql({ a: 10 }); - - if (typeof Set === 'function') { + [[1], [2], [3]].should.containEql([3]); + [[1], [2], [3, 4]].should.not.containEql([3]); + [{ a: "a" }, { b: "b", c: "c" }].should.containEql({ a: "a" }); + [{ a: "a" }, { b: "b", c: "c" }].should.not.containEql({ b: "b" }); + + ({}.should.not.containEql({ a: 10 })); + + if (typeof Set === "function") { new Set([1, 2, 3]).should.containEql(new Set([1])); } - ({ b: 10 }).should.containEql({ b: 10 }); + ({ b: 10 }.should.containEql({ b: 10 })); [1, 2, 3].should.containEql(1); - ([1, 2, { a: 10 }]).should.containEql({ a: 10 }); + [1, 2, { a: 10 }].should.containEql({ a: 10 }); [1, 2, 3].should.not.containEql({ a: 1 }); err(function() { [1, 2, 3].should.not.containEql(3); - }, 'expected Array [ 1, 2, 3 ] not to contain 3 (false negative fail)'); + }, "expected Array [ 1, 2, 3 ] not to contain 3 (false negative fail)"); err(function() { [1, 2, 3].should.containEql(4); - }, 'expected Array [ 1, 2, 3 ] to contain 4'); + }, "expected Array [ 1, 2, 3 ] to contain 4"); }); - it('test containDeep', function() { + it("test containDeep", function() { + ({ + a: { b: 10 }, + b: { c: 10, d: 11, a: { b: 10, c: 11 } } + }.should.containDeep({ a: { b: 10 }, b: { c: 10, a: { c: 11 } } })); - ({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}).should - .containDeep({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}}); - - [1, 2, 3, { a: { b: { d: 12 }}}].should.containDeep([ - { a: { b: {d: 12}}} + [1, 2, 3, { a: { b: { d: 12 } } }].should.containDeep([ + { a: { b: { d: 12 } } } ]); - [ - [1, [2, 3], 3], - [2] - ].should.not.containDeep([1, 2]); - - [ - [1], - [2], - [3] - ].should.containDeep([ - [3] - ]); - [ - [1], - [2], - [3, 4] - ].should.containDeep([ - [3] - ]); - [ - [1], - [2], - [3, 4] - ].should.containDeep([ - [1], - [3] - ]); - [ - [1], - [2], - [3, 4] - ].should.containDeep([ - [3], - [1] - ]); + [[1, [2, 3], 3], [2]].should.not.containDeep([1, 2]); + + [[1], [2], [3]].should.containDeep([[3]]); + [[1], [2], [3, 4]].should.containDeep([[3]]); + [[1], [2], [3, 4]].should.containDeep([[1], [3]]); + [[1], [2], [3, 4]].should.containDeep([[3], [1]]); [1, 2, 3].should.not.containDeep([3, 3, 3]); [1, 2, 3].should.containDeep([3]); - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.containDeep([ - {a: 'a'} - ]); + [{ a: "a" }, { b: "b", c: "c" }].should.containDeep([{ a: "a" }]); - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.containDeep([ - {b: 'b'} - ]); + [{ a: "a" }, { b: "b", c: "c" }].should.containDeep([{ b: "b" }]); [1, 2, 3].should.containDeep([3, 2]); - ['code-for-days', 'code'].should.containDeep(['code', 'code-for-days']); - ['code-for-days', 'code-fast'].should.containDeep(['code-fast', 'code-for-days']); + ["code-for-days", "code"].should.containDeep(["code", "code-for-days"]); + ["code-for-days", "code-fast"].should.containDeep([ + "code-fast", + "code-for-days" + ]); err(function() { - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.not.containDeep([ - {b: 'b'} - ]); + [{ a: "a" }, { b: "b", c: "c" }].should.not.containDeep([{ b: "b" }]); }, "expected Array [ Object { a: 'a' }, Object { b: 'b', c: 'c' } ] not to contain Array [ Object { b: 'b' } ] (false negative fail)"); - ({hi: null}).should.containEql({hi: null}); + ({ hi: null }.should.containEql({ hi: null })); - var firstDec = [{date: (new Date('2014-12-01 00:00:00'))}]; - var secondDec = [{date: (new Date('2014-12-02 00:00:00'))}]; + var firstDec = [{ date: new Date("2014-12-01 00:00:00") }]; + var secondDec = [{ date: new Date("2014-12-02 00:00:00") }]; firstDec.should.not.containDeep(secondDec); - [{date: { d: (new Date('2014-12-01 00:00:00'))}}].should.not.containDeep([{date: (new Date('2014-12-02 00:00:00'))}]); + [{ date: { d: new Date("2014-12-01 00:00:00") } }].should.not.containDeep([ + { date: new Date("2014-12-02 00:00:00") } + ]); }); - it('test containDeepOrdered', function() { - - ({ a: { b: 10 }, b: { c: 10, d: 11, a: { b: 10, c: 11} }}).should - .containDeepOrdered({ a: { b: 10 }, b: { c: 10, a: { c: 11 }}}); + it("test containDeepOrdered", function() { + ({ + a: { b: 10 }, + b: { c: 10, d: 11, a: { b: 10, c: 11 } } + }.should.containDeepOrdered({ a: { b: 10 }, b: { c: 10, a: { c: 11 } } })); - [1, 2, 3, { a: { b: { d: 12 }}}].should.containDeepOrdered([ - { a: { b: {d: 12}}} + [1, 2, 3, { a: { b: { d: 12 } } }].should.containDeepOrdered([ + { a: { b: { d: 12 } } } ]); - [ - [1, [2, 3], 3], - [2] - ].should.not.containDeepOrdered([1, 2]); - - [ - [1], - [2], - [3] - ].should.containDeepOrdered([ - [3] - ]); - [ - [1], - [2], - [3, 4] - ].should.containDeepOrdered([ - [3] - ]); - [ - [1], - [2], - [3, 4] - ].should.containDeepOrdered([ - [1], - [3] - ]); - [ - [1], - [2], - [3, 4] - ].should.not.containDeepOrdered([ - [3], - [1] - ]); - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.containDeepOrdered([ - {a: 'a'} - ]); - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.containDeepOrdered([ - {b: 'b'} - ]); + [[1, [2, 3], 3], [2]].should.not.containDeepOrdered([1, 2]); + + [[1], [2], [3]].should.containDeepOrdered([[3]]); + [[1], [2], [3, 4]].should.containDeepOrdered([[3]]); + [[1], [2], [3, 4]].should.containDeepOrdered([[1], [3]]); + [[1], [2], [3, 4]].should.not.containDeepOrdered([[3], [1]]); + [{ a: "a" }, { b: "b", c: "c" }].should.containDeepOrdered([{ a: "a" }]); + [{ a: "a" }, { b: "b", c: "c" }].should.containDeepOrdered([{ b: "b" }]); err(function() { - [ - {a: 'a'}, - {b: 'b', c: 'c'} - ].should.not.containDeepOrdered([ - {b: 'b'} - ]); + [{ a: "a" }, { b: "b", c: "c" }].should.not.containDeepOrdered([ + { b: "b" } + ]); }, "expected Array [ Object { a: 'a' }, Object { b: 'b', c: 'c' } ] not to contain Array [ Object { b: 'b' } ] (false negative fail)"); - ({hi: null}).should.containEql({hi: null}); + ({ hi: null }.should.containEql({ hi: null })); }); }); diff --git a/test/ext/eql.test.js b/test/ext/eql.test.js index a9b7d83..9873705 100644 --- a/test/ext/eql.test.js +++ b/test/ext/eql.test.js @@ -1,10 +1,10 @@ -var err = require('../util').err, - should = require('../../'); +var err = require("../util").err, + should = require("../../"); -describe('eql', function() { - it('test eql(val)', function() { - 'test'.should.eql('test'); - ({foo: 'bar'}).should.eql({foo: 'bar'}); +describe("eql", function() { + it("test eql(val)", function() { + "test".should.eql("test"); + ({ foo: "bar" }.should.eql({ foo: "bar" })); (1).should.eql(1); var memo = []; @@ -12,40 +12,40 @@ describe('eql', function() { memo.push(arguments); } - memorize('a', [1, 2]); - memorize('a', [1, 2]); + memorize("a", [1, 2]); + memorize("a", [1, 2]); memo[0].should.eql(memo[1]); err(function() { (4).should.eql(3); - }, 'expected 4 to equal 3'); + }, "expected 4 to equal 3"); }); - it('test equal(val)', function() { - 'test'.should.equal('test'); + it("test equal(val)", function() { + "test".should.equal("test"); (1).should.equal(1); err(function() { (4).should.equal(3); - }, 'expected 4 to be 3'); + }, "expected 4 to be 3"); err(function() { - '4'.should.equal(4); + "4".should.equal(4); }, "expected '4' to be 4"); - var date = new Date; + var date = new Date(); date.should.equal(date); }); - it('test .equal()', function() { + it("test .equal()", function() { var foo; should.equal(undefined, foo); }); - it('should allow to test with prototypes', function() { + it("should allow to test with prototypes", function() { should.config.checkProtoEql = false; - var b = {a: 2}; + var b = { a: 2 }; var a = Object.create(null); a.a = 2; @@ -55,43 +55,30 @@ describe('eql', function() { err(function() { b.should.be.eql(a); - }, 'expected Object { a: 2 } to equal { a: 2 } (because A and B have different prototypes)'); - - + }, "expected Object { a: 2 } to equal { a: 2 } (because A and B have different prototypes)"); }); - it('should check one of equal', function() { - 'ab'.should.be.equalOneOf('a', 10, 'ab'); - 'ab'.should.be.equalOneOf(['a', 10, 'ab']); - 'ab'.should.not.be.equalOneOf(['a', 10]); - 'ab'.should.not.be.equalOneOf('a', 10); + it("should check one of equal", function() { + "ab".should.be.equalOneOf("a", 10, "ab"); + "ab".should.be.equalOneOf(["a", 10, "ab"]); + "ab".should.not.be.equalOneOf(["a", 10]); + "ab".should.not.be.equalOneOf("a", 10); }); - it('should check one of deep equal', function() { - ({ a: 'ab' }).should.not.be.oneOf('a', 10, 'ab'); - ({ a: 'ab' }).should.not.be.oneOf(['a', 10, 'ab']); - ({ a: 'ab' }).should.be.oneOf('a', 10, 'ab', { a: 'ab' }); - ({ a: 'ab' }).should.be.oneOf(['a', 10, 'ab', { a: 'ab' }]); + it("should check one of deep equal", function() { + ({ a: "ab" }.should.not.be.oneOf("a", 10, "ab")); + ({ a: "ab" }.should.not.be.oneOf(["a", 10, "ab"])); + ({ a: "ab" }.should.be.oneOf("a", 10, "ab", { a: "ab" })); + ({ a: "ab" }.should.be.oneOf(["a", 10, "ab", { a: "ab" }])); }); - it('should correct print arrays in error message', function() { - var toTest = [ - 0, 1, 0, 4, - 0, 0, 0, 3, - 0, 4, 5, 0, - 0, 0, 0, 6, - ]; - - var expected = [ - 0, 1, 0, 3, - 0, 0, 0, 3, - 0, 4, 5, 0, - 0, 0, 0, 6, - ]; + it("should correct print arrays in error message", function() { + var toTest = [0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 5, 0, 0, 0, 0, 6]; + var expected = [0, 1, 0, 3, 0, 0, 0, 3, 0, 4, 5, 0, 0, 0, 0, 6]; err(function() { toTest.should.eql(expected); - }, 'expected Array [ 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 5, 0, 0, 0, 0, 6 ] to equal Array [ 0, 1, 0, 3, 0, 0, 0, 3, 0, 4, 5, 0, 0, 0, 0, 6 ] (at \'3\', A has 4 and B has 3)'); + }, "expected Array [ 0, 1, 0, 4, 0, 0, 0, 3, 0, 4, 5, 0, 0, 0, 0, 6 ] to equal Array [ 0, 1, 0, 3, 0, 0, 0, 3, 0, 4, 5, 0, 0, 0, 0, 6 ] (at '3', A has 4 and B has 3)"); }); }); diff --git a/test/ext/error.test.js b/test/ext/error.test.js index f390a2c..8c4ea61 100644 --- a/test/ext/error.test.js +++ b/test/ext/error.test.js @@ -1,134 +1,169 @@ -var err = require('../util').err; -require('../../'); +var err = require("../util").err; +require("../../"); - -describe('error', function() { - it('test throw()', function() { +describe("error", function() { + it("test throw()", function() { err(function() { - 'a'.should.throw(); - }, 'expected \'a\' to be a function\n expected \'a\' to have type function\n expected \'string\' to be \'function\''); + "a".should.throw(); + }, "expected 'a' to be a function\n expected 'a' to have type function\n expected 'string' to be 'function'"); - (function() {}).should.not.throw(); - (function() { throw new Error('fail'); }).should.throw(); + (function() {}.should.not.throw()); + (function() { + throw new Error("fail"); + }.should.throw()); err(function() { - (function() {}).should.throw(); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throw()); + }, "expected Function { name: '' } to throw exception"); err(function() { (function() { - throw new Error('fail'); - }).should.not.throw(); + throw new Error("fail"); + }.should.not.throw()); }, /expected Function \{ name: '' \} not to throw exception \(got Error \{[\s\S]*message: 'fail'[\s\S]*\}\)/); }); - it('test throw() with regex message', function() { - (function() { throw new Error('fail'); }).should.throw(/fail/); + it("test throw() with regex message", function() { + (function() { + throw new Error("fail"); + }.should.throw(/fail/)); err(function() { - (function() {}).should.throw(/fail/); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throw(/fail/)); + }, "expected Function { name: '' } to throw exception"); err(function() { - (function() { throw new Error('error'); }).should.throw(/fail/); - }, "expected Function { name: \'\' } to throw exception with a message matching /fail/, but got 'error'"); + (function() { + throw new Error("error"); + }.should.throw(/fail/)); + }, "expected Function { name: '' } to throw exception with a message matching /fail/, but got 'error'"); }); - it('test throw() with string message', function() { - (function() { throw new Error('fail'); }).should.throw('fail'); + it("test throw() with string message", function() { + (function() { + throw new Error("fail"); + }.should.throw("fail")); err(function() { - (function() {}).should.throw('fail'); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throw("fail")); + }, "expected Function { name: '' } to throw exception"); err(function() { - (function() { throw new Error('error'); }).should.throw('fail'); - }, "expected Function { name: \'\' } to throw exception with a message matching 'fail', but got 'error'"); + (function() { + throw new Error("error"); + }.should.throw("fail")); + }, "expected Function { name: '' } to throw exception with a message matching 'fail', but got 'error'"); }); - it('test throw() with type', function() { - (function() { throw new Error('fail'); }).should.throw(Error); + it("test throw() with type", function() { + (function() { + throw new Error("fail"); + }.should.throw(Error)); err(function() { - (function() {}).should.throw(Error); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throw(Error)); + }, "expected Function { name: '' } to throw exception"); err(function() { - (function() { throw 'error'; }).should.throw(Error); - }, 'expected Function { name: \'\' } to throw exception of type Error, but got String'); + (function() { + throw "error"; + }.should.throw(Error)); + }, "expected Function { name: '' } to throw exception of type Error, but got String"); }); - it('test throwError()', function() { - (function() {}).should.not.throwError(); - (function() { throw new Error('fail'); }).should.throwError(); + it("test throwError()", function() { + (function() {}.should.not.throwError()); + (function() { + throw new Error("fail"); + }.should.throwError()); err(function() { - (function() {}).should.throwError(); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throwError()); + }, "expected Function { name: '' } to throw exception"); err(function() { (function() { - throw new Error('fail'); - }).should.not.throwError(); + throw new Error("fail"); + }.should.not.throwError()); }, /expected Function \{ name: '' \} not to throw exception \(got Error \{[\s\S]*message: 'fail'[\s\S]*\}\)/); }); - it('test throwError() with regex message', function() { - (function() { throw new Error('fail'); }).should.throwError(/fail/); + it("test throwError() with regex message", function() { + (function() { + throw new Error("fail"); + }.should.throwError(/fail/)); err(function() { - (function() {}).should.throwError(/fail/); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throwError(/fail/)); + }, "expected Function { name: '' } to throw exception"); err(function() { - (function() { throw new Error('error'); }).should.throwError(/fail/); - }, "expected Function { name: \'\' } to throw exception with a message matching /fail/, but got 'error'"); + (function() { + throw new Error("error"); + }.should.throwError(/fail/)); + }, "expected Function { name: '' } to throw exception with a message matching /fail/, but got 'error'"); }); - it('test throwError() with string message', function() { - (function() { throw new Error('fail'); }).should.throwError('fail'); + it("test throwError() with string message", function() { + (function() { + throw new Error("fail"); + }.should.throwError("fail")); err(function() { - (function() {}).should.throwError('fail'); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throwError("fail")); + }, "expected Function { name: '' } to throw exception"); err(function() { - (function() { throw new Error('error'); }).should.throwError('fail'); - }, "expected Function { name: \'\' } to throw exception with a message matching 'fail', but got 'error'"); + (function() { + throw new Error("error"); + }.should.throwError("fail")); + }, "expected Function { name: '' } to throw exception with a message matching 'fail', but got 'error'"); }); - it('test throwError() with type', function() { - (function() { throw new Error('fail'); }).should.throw(Error); + it("test throwError() with type", function() { + (function() { + throw new Error("fail"); + }.should.throw(Error)); err(function() { - (function() {}).should.throw(Error); - }, 'expected Function { name: \'\' } to throw exception'); + (function() {}.should.throw(Error)); + }, "expected Function { name: '' } to throw exception"); err(function() { - (function() { throw 'error'; }).should.throw(Error); + (function() { + throw "error"; + }.should.throw(Error)); }, "expected Function { name: '' } to throw exception of type Error, but got String"); }); - it('test .throw(err, properties) with matching error', function() { + it("test .throw(err, properties) with matching error", function() { var error = new Error(); error.a = 10; - (function() { throw error; }).should.throw(Error, { a: 10 }); + (function() { + throw error; + }.should.throw(Error, { a: 10 })); err(function() { - (function() { throw error; }).should.throw(Error, { a: 11 }); + (function() { + throw error; + }.should.throw(Error, { a: 11 })); }, /expected Function \{ name: '' \} to throw exception: expected Error \{[\s\S]*a: 10,[\s\S]*message: ''[\s\S]*\} to match Object \{ a: 11 \}\n\s{4}not matched properties: a \(10\)/); }); - it('test .throw(properties) with matching error', function() { + it("test .throw(properties) with matching error", function() { var error = new Error(); error.a = 10; - (function() { throw error; }).should.throw({ a: 10 }); + (function() { + throw error; + }.should.throw({ a: 10 })); err(function() { - (function() { throw error; }).should.throw({ a: 11 }); + (function() { + throw error; + }.should.throw({ a: 11 })); }, /expected Function \{ name: '' \} to throw exception: expected Error \{[\s\S]*a: 10,[\s\S]*message: ''[\s\S]*\} to match Object \{ a: 11 \}\n\s{4}not matched properties: a \(10\)/); }); -/* TODO find a way to write tests with es6 features + /* TODO find a way to write tests with es6 features it('should support to catch errors from generators', function() { if(generatorSupported) { var throwsError = function*() { diff --git a/test/ext/match.test.js b/test/ext/match.test.js index 9b6677c..858421f 100644 --- a/test/ext/match.test.js +++ b/test/ext/match.test.js @@ -1,61 +1,60 @@ -var err = require('../util').err; -var should = require('../../'); +var err = require("../util").err; +var should = require("../../"); -describe('match', function() { - - it('test string match(regexp)', function() { - 'foobar'.should.match(/^foo/); - 'foobar'.should.not.match(/^bar/); +describe("match", function() { + it("test string match(regexp)", function() { + "foobar".should.match(/^foo/); + "foobar".should.not.match(/^bar/); err(function() { - 'foobar'.should.match(/^bar/i); + "foobar".should.match(/^bar/i); }, "expected 'foobar' to match /^bar/i"); err(function() { - 'foobar'.should.not.match(/^foo/i); + "foobar".should.not.match(/^foo/i); }, "expected 'foobar' not to match /^foo/i (false negative fail)"); err(function() { - 'foobar'.should.match(/^bar/i, 'foo'); + "foobar".should.match(/^bar/i, "foo"); }, "foo"); err(function() { - 'foobar'.should.not.match(/^foo/i, 'foo'); + "foobar".should.not.match(/^foo/i, "foo"); }, "foo"); }); - it('test object match(regexp)', function() { - ({ a: 'foo', c: 'barfoo' }).should.match(/foo$/); + it("test object match(regexp)", function() { + ({ a: "foo", c: "barfoo" }.should.match(/foo$/)); - ({ a: 'a' }).should.not.match(/^http/); + ({ a: "a" }.should.not.match(/^http/)); should(null).not.match(/bar/); // positive false err(function() { - ({ a: 'foo', c: 'barfoo' }).should.not.match(/foo$/); + ({ a: "foo", c: "barfoo" }.should.not.match(/foo$/)); }, "expected Object { a: 'foo', c: 'barfoo' } not to match /foo$/\n matched properties: a, c (false negative fail)"); // negative true err(function() { - ({ a: 'foo', c: 'barfoo' }).should.match(/^foo$/); + ({ a: "foo", c: "barfoo" }.should.match(/^foo$/)); }, "expected Object { a: 'foo', c: 'barfoo' } to match /^foo$/\n not matched properties: c ('barfoo')\n matched properties: a"); }); - it('test array match(regexp)', function() { - ['a', 'b', 'c'].should.match(/[a-z]/); - ['a', 'b', 'c'].should.not.match(/[d-z]/); + it("test array match(regexp)", function() { + ["a", "b", "c"].should.match(/[a-z]/); + ["a", "b", "c"].should.not.match(/[d-z]/); err(function() { - ['a', 'b', 'c'].should.not.match(/[a-z]/); + ["a", "b", "c"].should.not.match(/[a-z]/); }, "expected Array [ 'a', 'b', 'c' ] not to match /[a-z]/\n matched properties: '0', '1', '2' (false negative fail)"); err(function() { - ['a', 'b', 'c'].should.match(/[d-z]/); - }, "expected Array [ 'a', 'b', 'c' ] to match /[d-z]/\n not matched properties: \'0\' (\'a\'), \'1\' (\'b\'), \'2\' (\'c\')"); + ["a", "b", "c"].should.match(/[d-z]/); + }, "expected Array [ 'a', 'b', 'c' ] to match /[d-z]/\n not matched properties: '0' ('a'), '1' ('b'), '2' ('c')"); }); - it('test match(function)', function() { + it("test match(function)", function() { (5).should.match(function(n) { return n > 0; }); @@ -97,23 +96,32 @@ describe('match', function() { }, "expected 5 not to match Function { name: '' } (false negative fail)"); }); - it('test match(object)', function() { - ({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should - .match({ a: 10, b: /c$/, c: function(it) { - return it.should.have.property('d', 10); - }}); - - ({ nested: null }).should.not.match( { nested: { prop: /foo/ }} ); - - [10, 'abc', { d: 10 }, 0].should - .match({ '0': 10, '1': /c$/, '2': function(it) { - return it.should.have.property('d', 10); - } }); + it("test match(object)", function() { + ({ a: 10, b: "abc", c: { d: 10 }, d: 0 }.should.match({ + a: 10, + b: /c$/, + c: function(it) { + return it.should.have.property("d", 10); + } + })); + + ({ nested: null }.should.not.match({ nested: { prop: /foo/ } })); + + [10, "abc", { d: 10 }, 0].should.match({ + "0": 10, + "1": /c$/, + "2": function(it) { + return it.should.have.property("d", 10); + } + }); - [10, 'abc', { d: 10 }, 0].should - .match([10, /c$/, function(it) { - return it.should.have.property('d', 10); - }]); + [10, "abc", { d: 10 }, 0].should.match([ + 10, + /c$/, + function(it) { + return it.should.have.property("d", 10); + } + ]); var d = new Date(); var ds = d.toString(); @@ -122,88 +130,95 @@ describe('match', function() { ds.should.not.match(d); err(function() { - ({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should - .match({ a: 11, b: /c$/, c: function c(it) { - return it.should.have.property('d', 10); - }}); + ({ a: 10, b: "abc", c: { d: 10 }, d: 0 }.should.match({ + a: 11, + b: /c$/, + c: function c(it) { + return it.should.have.property("d", 10); + } + })); }, "expected Object { a: 10, b: 'abc', c: Object { d: 10 }, d: 0 } to match Object { a: 11, b: /c$/, c: Function { name: 'c' } }\n not matched properties: a (10)\n matched properties: b, c"); err(function() { - ({ a: 10, b: 'abc', c: { d: 10 }, d: 0 }).should.not - .match({ a: 10, b: /c$/, c: function c(it) { - return it.should.have.property('d', 10); - }}); + ({ a: 10, b: "abc", c: { d: 10 }, d: 0 }.should.not.match({ + a: 10, + b: /c$/, + c: function c(it) { + return it.should.have.property("d", 10); + } + })); }, "expected Object { a: 10, b: 'abc', c: Object { d: 10 }, d: 0 } not to match Object { a: 10, b: /c$/, c: Function { name: 'c' } }\n matched properties: a, b, c (false negative fail)"); }); - it('test each property match(function)', function() { + it("test each property match(function)", function() { [10, 11, 12].should.matchEach(function(it) { return it >= 10; }); [10, 10].should.matchEach(10); - ({ a: 10, b: 11, c: 12}).should.matchEach(function(value) { + ({ a: 10, b: 11, c: 12 }.should.matchEach(function(value) { value.should.be.a.Number(); - }); + })); - (['a', 'b', 'c']).should.matchEach(/[a-c]/); + ["a", "b", "c"].should.matchEach(/[a-c]/); err(function() { - (['a', 'b', 'c']).should.not.matchEach(/[a-c]/); + ["a", "b", "c"].should.not.matchEach(/[a-c]/); }, "expected Array [ 'a', 'b', 'c' ] not to match each /[a-c]/ (false negative fail)"); err(function() { [10, 11].should.matchEach(10); - }, 'expected Array [ 10, 11 ] to match each 10\n expected 11 to match 10'); + }, "expected Array [ 10, 11 ] to match each 10\n expected 11 to match 10"); }); - it('test matchAny(obj)', function() { - [{}, {nested: {prop: 'foo'}}].should.matchAny( {nested: {prop: 'foo'}} ); + it("test matchAny(obj)", function() { + [{}, { nested: { prop: "foo" } }].should.matchAny({ + nested: { prop: "foo" } + }); - [ - { nested: undefined }, - { nested: { prop: 'foo' } } - ].should.matchAny({ nested: { prop: /foo/ } }); + [{ nested: undefined }, { nested: { prop: "foo" } }].should.matchAny({ + nested: { prop: /foo/ } + }); }); - it('test matchAny(function)', function() { - [9, 10, 11].should.matchAny(function(it) { - return it >= 10; - }); + it("test matchAny(function)", function() { + [9, 10, 11].should.matchAny(function(it) { + return it >= 10; + }); - [9, 10, 11].should.not.matchAny(function(it) { - return it == 2; - }); + [9, 10, 11].should.not.matchAny(function(it) { + return it == 2; + }); - ({ a: 10, b: 11, c: 12}).should.matchAny(function(value) { - value.should.be.a.Number(); - }); + ({ a: 10, b: 11, c: 12 }.should.matchAny(function(value) { + value.should.be.a.Number(); + })); - ({ a: 10, b: 'eleven', c: 'twelve'}).should.matchAny(function(value) { - value.should.be.a.Number(); - }); + ({ a: 10, b: "eleven", c: "twelve" }.should.matchAny(function(value) { + value.should.be.a.Number(); + })); }); - it('test matchAny(number)', function() { - [10, 11, 12].should.matchAny(10); + it("test matchAny(number)", function() { + [10, 11, 12].should.matchAny(10); - [10, 10].should.matchAny(10); + [10, 10].should.matchAny(10); - [10, 11, 12].should.not.matchAny(2); + [10, 11, 12].should.not.matchAny(2); }); - it('test matchAny(regexp)', function() { - (['a', 'b', 'c']).should.matchAny(/[a-b]/); + it("test matchAny(regexp)", function() { + ["a", "b", "c"].should.matchAny(/[a-b]/); - (['a', 'b', 'c']).should.not.matchAny(/[d-f]/); + ["a", "b", "c"].should.not.matchAny(/[d-f]/); - err(function() { - (['a', 'b', 'c']).should.not.matchAny(/[a-c]/); - }, "expected Array [ 'a', 'b', 'c' ] not to match any /[a-c]/ (false negative fail)"); + err(function() { + ["a", "b", "c"].should.not.matchAny(/[a-c]/); + }, "expected Array [ 'a', 'b', 'c' ] not to match any /[a-c]/ (false negative fail)"); - err(function() { - [8, 9].should.matchAny(10); - }, "expected Array [ 8, 9 ] to match any 10"); + err(function() { + [8, 9].should.matchAny(10); + }, "expected Array [ 8, 9 ] to match any 10"); }); }); diff --git a/test/ext/number.test.js b/test/ext/number.test.js index 3f41fda..734fcfe 100644 --- a/test/ext/number.test.js +++ b/test/ext/number.test.js @@ -1,15 +1,15 @@ -var err = require('../util').err; -require('../../'); +var err = require("../util").err; +require("../../"); -describe('number', function() { - it('test NaN', function() { +describe("number", function() { + it("test NaN", function() { NaN.should.be.NaN(); Infinity.should.not.be.NaN(); (0).should.not.be.NaN(); false.should.not.be.NaN(); - ({}).should.not.be.NaN(); - ''.should.not.be.NaN(); - 'foo'.should.not.be.NaN(); + ({}.should.not.be.NaN()); + "".should.not.be.NaN(); + "foo".should.not.be.NaN(); /^$/.should.not.be.NaN(); err(function() { @@ -21,15 +21,15 @@ describe('number', function() { }, "expected NaN not to be NaN (false negative fail)"); }); - it('test Infinity', function() { + it("test Infinity", function() { NaN.should.not.be.Infinity(); - (1/0).should.be.Infinity(); + (1 / 0).should.be.Infinity(); Infinity.should.be.Infinity(); (0).should.not.be.Infinity(); false.should.not.be.Infinity(); - ({}).should.not.be.Infinity(); - ''.should.not.be.Infinity(); - 'foo'.should.not.be.Infinity(); + ({}.should.not.be.Infinity()); + "".should.not.be.Infinity(); + "foo".should.not.be.Infinity(); /^$/.should.not.be.Infinity(); err(function() { @@ -41,33 +41,33 @@ describe('number', function() { }, "expected Infinity not to be Infinity (false negative fail)"); }); - it('test within(start, finish)', function() { + it("test within(start, finish)", function() { (5).should.be.within(5, 10); - (5).should.be.within(3,6); - (5).should.be.within(3,5); - (5).should.not.be.within(1,3); + (5).should.be.within(3, 6); + (5).should.be.within(3, 5); + (5).should.not.be.within(1, 3); err(function() { - (5).should.not.be.within(4,6); + (5).should.not.be.within(4, 6); }, "expected 5 not to be within 4..6 (false negative fail)"); err(function() { - (10).should.be.within(50,100); + (10).should.be.within(50, 100); }, "expected 10 to be within 50..100"); err(function() { - (5).should.not.be.within(4,6, 'foo'); + (5).should.not.be.within(4, 6, "foo"); }, "foo"); err(function() { - (10).should.be.within(50,100, 'foo'); + (10).should.be.within(50, 100, "foo"); }, "foo"); }); - it('test approximately(number, delta)', function() { + it("test approximately(number, delta)", function() { (1.5).should.be.approximately(1.4, 0.2); - (1.5).should.be.approximately(1.5, 10E-10); - (1.5).should.not.be.approximately(1.4, 1E-2); + (1.5).should.be.approximately(1.5, 10e-10); + (1.5).should.not.be.approximately(1.4, 1e-2); err(function() { (99.99).should.not.be.approximately(100, 0.1); @@ -78,8 +78,7 @@ describe('number', function() { }, "expected 99.99 to be approximately 105 ±0.1"); }); - - it('test above(n)', function() { + it("test above(n)", function() { (5).should.be.above(2); (5).should.be.greaterThan(2); (5).should.not.be.above(5); @@ -94,15 +93,15 @@ describe('number', function() { }, "expected 10 not to be above 6 (false negative fail)"); err(function() { - (5).should.be.above(6, 'foo'); + (5).should.be.above(6, "foo"); }, "foo"); err(function() { - (10).should.not.be.above(6, 'foo'); + (10).should.not.be.above(6, "foo"); }, "foo"); }); - it('test below(n)', function() { + it("test below(n)", function() { (2).should.be.below(5); (2).should.be.lessThan(5); (5).should.not.be.below(5); @@ -117,31 +116,31 @@ describe('number', function() { }, "expected 6 not to be below 10 (false negative fail)"); err(function() { - (6).should.be.below(5, 'foo'); + (6).should.be.below(5, "foo"); }, "foo"); err(function() { - (6).should.not.be.below(10, 'foo'); + (6).should.not.be.below(10, "foo"); }, "foo"); }); - it('test aboveOrEqual(n)', function() { + it("test aboveOrEqual(n)", function() { (5).should.be.aboveOrEqual(2); (5).should.be.aboveOrEqual(5); (5).should.not.be.aboveOrEqual(6); err(function() { (5).should.be.aboveOrEqual(6); - }, 'expected 5 to be above or equal 6'); + }, "expected 5 to be above or equal 6"); }); - it('test belowOrEqual(n)', function() { + it("test belowOrEqual(n)", function() { (2).should.be.belowOrEqual(5); (5).should.be.belowOrEqual(5); (6).should.not.be.belowOrEqual(5); err(function() { (6).should.be.belowOrEqual(5); - }, 'expected 6 to be below or equal 5'); + }, "expected 6 to be below or equal 5"); }); }); diff --git a/test/ext/promise.test.js b/test/ext/promise.test.js index 311537c..d2e854d 100644 --- a/test/ext/promise.test.js +++ b/test/ext/promise.test.js @@ -1,6 +1,6 @@ -var should = require('../../'); +var should = require("../../"); -var Promise = require('bluebird'); +var Promise = require("bluebird"); function promised(value) { return new Promise(function(resolve) { @@ -10,134 +10,206 @@ function promised(value) { function promiseFail() { return new Promise(function(resolve, reject) { - reject(new Error('boom')); + reject(new Error("boom")); }); } function promiseFailTimeout() { return new Promise(function(resolve, reject) { setTimeout(function() { - reject(new Error('boom')); + reject(new Error("boom")); }, 1000); }); } - -it('should return new assertion to hold promise', function() { - var a = promised('abc').should.finally; +it("should return new assertion to hold promise", function() { + var a = promised("abc").should.finally; return a.then.should.be.a.Function(); }); -it('should determine if it is Promise', function() { - promised('a').should.be.a.Promise(); +it("should determine if it is Promise", function() { + promised("a").should.be.a.Promise(); }); -it('should allow to chain calls like with usual assertion', function() { - return promised('abc').should.finally.be.exactly('abc') - .and.be.a.String(); +it("should allow to chain calls like with usual assertion", function() { + return promised("abc").should.finally.be.exactly("abc").and.be.a.String(); }); -it('should allow to use .not and .any', function() { - return promised({a: 10, b: 'abc'}).should.finally.not.have.any.of.properties('c', 'd') - .and.have.property('a', 10); +it("should allow to use .not and .any", function() { + return promised({ a: 10, b: "abc" }).should.finally.not.have.any.of + .properties("c", "d") + .and.have.property("a", 10); }); -it('should treat assertion like promise', function() { +it("should treat assertion like promise", function() { return Promise.all([ promised(10).should.finally.be.a.Number(), - promised('abc').should.finally.be.a.String() + promised("abc").should.finally.be.a.String() ]); }); -it('should propagate .not before .finally', function() { +it("should propagate .not before .finally", function() { return promised(10).should.not.finally.be.a.String(); }); -it('should be possible to use .eventually as an alias for .finally', function() { +it("should be possible to use .eventually as an alias for .finally", function() { return promised(10).should.eventually.be.a.Number(); }); -it('should allow to check if promise fulfilled', function() { +it("should allow to check if promise fulfilled", function() { return Promise.all([ promised(10).should.be.fulfilled(), //positive - promiseFail().should.be.fulfilled().then(function() {//negative - should.fail(); - }, function(err) { - err.should.be.Error().and.match({message: /expected \[Promise\] to be fulfilled, but it was rejected with Error \{[\s\S]*message: 'boom'[\s\S]*\}/}); - }), - promised(10).should.not.be.fulfilled().then(function() {//positive fail - should.fail(); - }, function(err) { - err.should.be.Error().and.match({message: 'expected [Promise] not to be fulfilled'}); - }), - promiseFail().should.not.be.fulfilled()//negative fail + promiseFail().should.be.fulfilled().then( + function() { + //negative + should.fail(); + }, + function(err) { + err.should.be + .Error() + .and.match({ + message: /expected \[Promise\] to be fulfilled, but it was rejected with Error \{[\s\S]*message: 'boom'[\s\S]*\}/ + }); + } + ), + promised(10).should.not.be.fulfilled().then( + function() { + //positive fail + should.fail(); + }, + function(err) { + err.should.be + .Error() + .and.match({ message: "expected [Promise] not to be fulfilled" }); + } + ), + promiseFail().should.not.be.fulfilled() //negative fail ]); }); -it('should be allow to check if promise is fulfilledWith a value', function() { +it("should be allow to check if promise is fulfilledWith a value", function() { return Promise.all([ promised(10).should.be.fulfilledWith(10), //positive - promiseFail().should.be.fulfilledWith(10).then(function() {//negative - should.fail(); - }, function(err) { - err.should.be.Error().and.match({message: /expected \[Promise\] to be fulfilled with 10, but it was rejected with Error \{[\s\S]*message: 'boom\'[\s\S]*\}/}); - }), - promised(10).should.not.be.fulfilledWith(10).then(function() {//positive fail - should.fail(); - }, function(err) { - err.should.be.Error().and.match({message: 'expected [Promise] not to be fulfilled with 10'}); - }), - promiseFail().should.not.be.fulfilledWith(10)//negative fail + promiseFail().should.be.fulfilledWith(10).then( + function() { + //negative + should.fail(); + }, + function(err) { + err.should.be + .Error() + .and.match({ + message: /expected \[Promise\] to be fulfilled with 10, but it was rejected with Error \{[\s\S]*message: 'boom\'[\s\S]*\}/ + }); + } + ), + promised(10).should.not.be.fulfilledWith(10).then( + function() { + //positive fail + should.fail(); + }, + function(err) { + err.should.be + .Error() + .and.match({ + message: "expected [Promise] not to be fulfilled with 10" + }); + } + ), + promiseFail().should.not.be.fulfilledWith(10) //negative fail ]); }); -it('should be allow to check if promise rejected', function() { +it("should be allow to check if promise rejected", function() { return Promise.all([ promiseFail().should.be.rejected(), //positive - promised(10).should.be.rejected().then(function() {//negative - should.fail(); - }, function(err) { - err.should.be.Error().and.match({message: 'expected [Promise] to be rejected, but it was fulfilled with 10'}); - }), - promiseFail().should.not.be.rejected().then(function() {//positive fail - should.fail(); - }, function(err) { - err.should.be.Error().and.match({message: 'expected [Promise] not to be rejected'}); - }), - promised(10).should.not.be.rejected()//negative fail + promised(10).should.be.rejected().then( + function() { + //negative + should.fail(); + }, + function(err) { + err.should.be + .Error() + .and.match({ + message: + "expected [Promise] to be rejected, but it was fulfilled with 10" + }); + } + ), + promiseFail().should.not.be.rejected().then( + function() { + //positive fail + should.fail(); + }, + function(err) { + err.should.be + .Error() + .and.match({ message: "expected [Promise] not to be rejected" }); + } + ), + promised(10).should.not.be.rejected() //negative fail ]); }); -it('should allow to match rejected error', function() { +it("should allow to match rejected error", function() { return Promise.all([ promiseFail().should.be.rejectedWith(Error), - promiseFail().should.be.rejectedWith('boom'), - promiseFail().should.be.rejectedWith('boom1').then(function() { - return should.fail(); - }, function(err) { - return err.should.be.Error().and.match({ message: 'expected [Promise] to be rejected with a message matching \'boom1\', but got \'boom\''}); - }), + promiseFail().should.be.rejectedWith("boom"), + promiseFail().should.be.rejectedWith("boom1").then( + function() { + return should.fail(); + }, + function(err) { + return err.should.be + .Error() + .and.match({ + message: + "expected [Promise] to be rejected with a message matching 'boom1', but got 'boom'" + }); + } + ), promiseFail().should.be.rejectedWith(/boom/), - promiseFail().should.be.rejectedWith(Error, { message: 'boom' }), - promiseFail().should.be.rejectedWith({ message: 'boom' }), - promiseFail().should.not.be.rejectedWith().then(function() {//positive fail - return should.fail(); - }, function(err) { - return err.should.be.Error().and.match({message: 'expected [Promise] not to be rejected'}); - }), - promised(10).should.be.rejectedWith().then(function() {//negative fail - return should.fail(); - }, function(err) { - return err.should.be.Error().and.match({message: 'expected [Promise] to be rejected'}); - }), - promiseFail().should.not.be.rejectedWith(Error).then(function() {//negative fail - return should.fail(); - }, function(err) { - return err.should.be.Error().and.match({message: 'expected [Promise] not to be rejected'}); - }) + promiseFail().should.be.rejectedWith(Error, { message: "boom" }), + promiseFail().should.be.rejectedWith({ message: "boom" }), + promiseFail().should.not.be.rejectedWith().then( + function() { + //positive fail + return should.fail(); + }, + function(err) { + return err.should.be + .Error() + .and.match({ message: "expected [Promise] not to be rejected" }); + } + ), + promised(10).should.be.rejectedWith().then( + function() { + //negative fail + return should.fail(); + }, + function(err) { + return err.should.be + .Error() + .and.match({ message: "expected [Promise] to be rejected" }); + } + ), + promiseFail().should.not.be.rejectedWith(Error).then( + function() { + //negative fail + return should.fail(); + }, + function(err) { + return err.should.be + .Error() + .and.match({ message: "expected [Promise] not to be rejected" }); + } + ) ]); }); -it('should properly check async promise', function() { - return promiseFailTimeout().should.be.rejectedWith(Error, { message: 'boom' }); +it("should properly check async promise", function() { + return promiseFailTimeout().should.be.rejectedWith(Error, { + message: "boom" + }); }); diff --git a/test/ext/property.test.js b/test/ext/property.test.js index 9e6a0f0..7d734b6 100644 --- a/test/ext/property.test.js +++ b/test/ext/property.test.js @@ -1,217 +1,218 @@ -var err = require('../util').err; -require('../../'); - -describe('property', function() { - it('test enumerable(name)', function() { - ({'length': 5}).should.have.enumerable('length'); - (4).should.not.have.enumerable('length'); - - err(function() { - 'asd'.should.have.enumerable('length'); - }, "expected 'asd' to have enumerable property length", - " expected 'asd' to have own property with descriptor Object { enumerable: true }", - " expected Object { value: 3, writable: false, enumerable: false, configurable: false } to have property enumerable of true (got false)"); +var err = require("../util").err; +require("../../"); + +describe("property", function() { + it("test enumerable(name)", function() { + ({ length: 5 }.should.have.enumerable("length")); + (4).should.not.have.enumerable("length"); + + err( + function() { + "asd".should.have.enumerable("length"); + }, + "expected 'asd' to have enumerable property length", + " expected 'asd' to have own property with descriptor Object { enumerable: true }", + " expected Object { value: 3, writable: false, enumerable: false, configurable: false } to have property enumerable of true (got false)" + ); }); - it('test enumerable(name, val)', function() { - ({'length': 5}).should.have.enumerable('length', 5); - - err(function() { - ({'length': 3}).should.have.enumerable('length', 5); - }, "expected Object { length: 3 } to have enumerable property length equal to 5", - " expected Object { length: 3 } to have own property with descriptor Object { enumerable: true, value: 5 }", - " expected Object { value: 3, writable: true, enumerable: true, configurable: true } to have property value of 5 (got 3)"); + it("test enumerable(name, val)", function() { + ({ length: 5 }.should.have.enumerable("length", 5)); + + err( + function() { + ({ length: 3 }.should.have.enumerable("length", 5)); + }, + "expected Object { length: 3 } to have enumerable property length equal to 5", + " expected Object { length: 3 } to have own property with descriptor Object { enumerable: true, value: 5 }", + " expected Object { value: 3, writable: true, enumerable: true, configurable: true } to have property value of 5 (got 3)" + ); }); - it('test enumerables(names)', function() { - var obj = { a: 'a', b: 'b', c: 'c'}; - obj.should.have.enumerables('a', 'b'); - obj.should.have.enumerables(['a', 'b']); + it("test enumerables(names)", function() { + var obj = { a: "a", b: "b", c: "c" }; + obj.should.have.enumerables("a", "b"); + obj.should.have.enumerables(["a", "b"]); }); - it('test property(name)', function() { - 'test'.should.have.property('length'); - (4).should.not.have.property('length'); + it("test property(name)", function() { + "test".should.have.property("length"); + (4).should.not.have.property("length"); err(function() { - 'asd'.should.have.property('foo'); + "asd".should.have.property("foo"); }, "expected 'asd' to have property foo"); }); - it('test property(name, val)', function() { - 'test'.should.have.property('length', 4); - 'asd'.should.have.property('constructor', String); + it("test property(name, val)", function() { + "test".should.have.property("length", 4); + "asd".should.have.property("constructor", String); err(function() { - 'asd'.should.have.property('length', 4); + "asd".should.have.property("length", 4); }, "expected 'asd' to have property length of 4 (got 3)"); err(function() { - 'asd'.should.not.have.property('length', 3); + "asd".should.not.have.property("length", 3); }, "expected 'asd' not to have property length of 3 (false negative fail)"); err(function() { var obj = { f: function f() {} }; var f1 = function f1() {}; f1.a = 1; - obj.should.have.property('f', f1); + obj.should.have.property("f", f1); }, "expected Object { f: Function { name: 'f' } } to have property f of Function { a: 1, name: 'f1' } (got Function { name: 'f' })"); err(function() { - ({a: {b: 1}}).should.have.property('a') - .and.have.property('b', 100); + ({ a: { b: 1 } }.should.have.property("a").and.have.property("b", 100)); }, "expected Object { b: 1 } to have property b of 100 (got 1)"); err(function() { - ({a: {b: 1}}).should.have.property('a') - .and.have.property('c', 100); + ({ a: { b: 1 } }.should.have.property("a").and.have.property("c", 100)); }, "expected Object { b: 1 } to have property c"); err(function() { - ({a: {b: 1}}).should.have.property('a') - .and.have.property('c'); + ({ a: { b: 1 } }.should.have.property("a").and.have.property("c")); }, "expected Object { b: 1 } to have property c"); - }); - it('test length(n)', function() { - 'test'.should.have.length(4); - 'test'.should.have.lengthOf(4); - 'test'.should.not.have.length(3); + it("test length(n)", function() { + "test".should.have.length(4); + "test".should.have.lengthOf(4); + "test".should.not.have.length(3); [1, 2, 3].should.have.length(3); - ({ length: 10}).should.have.length(10); + ({ length: 10 }.should.have.length(10)); err(function() { (4).should.have.length(3); }, "expected 4 to have property length"); err(function() { - 'asd'.should.not.have.length(3); + "asd".should.not.have.length(3); }, "expected 'asd' not to have property length of 3 (false negative fail)"); - }); - it('test ownProperty(name)', function() { - 'test'.should.have.ownProperty('length'); - ({ length: 12 }).should.have.ownProperty('length'); + it("test ownProperty(name)", function() { + "test".should.have.ownProperty("length"); + ({ length: 12 }.should.have.ownProperty("length")); err(function() { - ({ length: 12 }).should.not.have.ownProperty('length'); + ({ length: 12 }.should.not.have.ownProperty("length")); }, "expected Object { length: 12 } not to have own property length (false negative fail)"); err(function() { - ({ length: 12 }).should.not.have.ownProperty('length', 'foo'); + ({ length: 12 }.should.not.have.ownProperty("length", "foo")); }, "foo"); err(function() { - ({ length: 12 }).should.have.ownProperty('foo', 'foo'); + ({ length: 12 }.should.have.ownProperty("foo", "foo")); }, "foo"); }); - it('test ownProperty(name).equal(val)', function() { - ({length: 10}).should.have.ownProperty('length').equal(10); + it("test ownProperty(name).equal(val)", function() { + ({ length: 10 }.should.have.ownProperty("length").equal(10)); }); - it('test properties(name1, name2, ...)', function() { - 'test'.should.have.properties('length', 'indexOf'); - (4).should.not.have.properties('length'); + it("test properties(name1, name2, ...)", function() { + "test".should.have.properties("length", "indexOf"); + (4).should.not.have.properties("length"); err(function() { - 'asd'.should.have.properties('foo'); + "asd".should.have.properties("foo"); }, "expected 'asd' to have property foo"); err(function() { - 'asd'.should.not.have.properties('length', 'indexOf'); + "asd".should.not.have.properties("length", "indexOf"); }, "expected 'asd' not to have properties length, indexOf (false negative fail)"); }); - it('test properties([names])', function() { - 'test'.should.have.properties(['length', 'indexOf']); - (4).should.not.have.properties(['length']); + it("test properties([names])", function() { + "test".should.have.properties(["length", "indexOf"]); + (4).should.not.have.properties(["length"]); err(function() { - 'asd'.should.have.properties(['foo']); + "asd".should.have.properties(["foo"]); }, "expected 'asd' to have property foo"); }); - it('test any of properties', function() { - 'test'.should.have.any.of.properties('length', 'a', 'b'); + it("test any of properties", function() { + "test".should.have.any.of.properties("length", "a", "b"); - 'test'.should.have.any.of.properties('length'); + "test".should.have.any.of.properties("length"); - ({ a: 10 }).should.have.any.of.properties('a', 'b'); + ({ a: 10 }.should.have.any.of.properties("a", "b")); - ({ a: 10 }).should.have.any.of.properties({ a: 10, b: 12 }); + ({ a: 10 }.should.have.any.of.properties({ a: 10, b: 12 })); - ({ a: 10 }).should.not.have.any.of.properties('b', 'c'); + ({ a: 10 }.should.not.have.any.of.properties("b", "c")); - ({ a: 10 }).should.have.any.of.properties(['a', 'b']); + ({ a: 10 }.should.have.any.of.properties(["a", "b"])); err(function() { - ({ a: 10 }).should.not.have.any.of.properties(['a', 'b']); + ({ a: 10 }.should.not.have.any.of.properties(["a", "b"])); }, "expected Object { a: 10 } not to have property a (false negative fail)"); err(function() { - ({ a: 10, b: 10 }).should.not.have.any.of.properties(['a', 'b']); + ({ a: 10, b: 10 }.should.not.have.any.of.properties(["a", "b"])); }, "expected Object { a: 10, b: 10 } not to have any of properties a, b (false negative fail)"); err(function() { - ({ a: 10, b: 10 }).should.not.have.any.of.properties({ a: 10, b: 12 }); + ({ a: 10, b: 10 }.should.not.have.any.of.properties({ a: 10, b: 12 })); }, "expected Object { a: 10, b: 10 } not to have property a of 10 (false negative fail)"); err(function() { - ({ a: 10, b: 10 }).should.not.have.any.of.properties({ a: 10, b: 10 }); + ({ a: 10, b: 10 }.should.not.have.any.of.properties({ a: 10, b: 10 })); }, "expected Object { a: 10, b: 10 } not to have any of properties a of 10, b of 10 (false negative fail)"); err(function() { - ({ a: 11, b: 11 }).should.have.any.of.properties({ a: 10, b: 10 }); + ({ a: 11, b: 11 }.should.have.any.of.properties({ a: 10, b: 10 })); }, "expected Object { a: 11, b: 11 } to have any of properties a of 10 (got 11), b of 10 (got 11)"); }); - it('test keys(array)', function() { - if (typeof Map === 'function') { - (new Map([[1, 2]])).should.have.key(1); + it("test keys(array)", function() { + if (typeof Map === "function") { + new Map([[1, 2]]).should.have.key(1); } - ({ foo: 1 }).should.have.keys('foo'); - ({ foo: 1, bar: 2 }).should.have.keys('foo', 'bar'); - ({}).should.have.keys(); + ({ foo: 1 }.should.have.keys("foo")); + ({ foo: 1, bar: 2 }.should.have.keys("foo", "bar")); + ({}.should.have.keys()); - ({ '1': 'cancelled', '3': 'deleted' }).should.have.keys(1, 3); + ({ "1": "cancelled", "3": "deleted" }.should.have.keys(1, 3)); - ({ a: 10, b: 11 }).should.only.have.keys('a', 'b'); + ({ a: 10, b: 11 }.should.only.have.keys("a", "b")); - ({ a: 10, b: 11, c: 999 }).should.not.only.have.keys('a', 'b'); + ({ a: 10, b: 11, c: 999 }.should.not.only.have.keys("a", "b")); err(function() { - ({ foo: 1 }).should.have.keys('bar'); + ({ foo: 1 }.should.have.keys("bar")); }, "expected Object { foo: 1 } to have key bar\n\tmissing keys: bar"); err(function() { - ({ foo: 1 }).should.have.keys('bar', 'baz'); + ({ foo: 1 }.should.have.keys("bar", "baz")); }, "expected Object { foo: 1 } to have keys bar, baz\n\tmissing keys: bar, baz"); err(function() { - ({ foo: 1 }).should.not.have.keys('foo'); + ({ foo: 1 }.should.not.have.keys("foo")); }, "expected Object { foo: 1 } not to have key foo (false negative fail)"); err(function() { - ({ foo: 1 }).should.not.have.keys('foo'); + ({ foo: 1 }.should.not.have.keys("foo")); }, "expected Object { foo: 1 } not to have key foo (false negative fail)"); err(function() { - ({ foo: 1, bar: 2 }).should.not.have.keys('foo', 'bar'); + ({ foo: 1, bar: 2 }.should.not.have.keys("foo", "bar")); }, "expected Object { foo: 1, bar: 2 } not to have keys foo, bar (false negative fail)"); }); - it('test empty', function() { - ''.should.be.empty(); + it("test empty", function() { + "".should.be.empty(); [].should.be.empty(); - ({}).should.be.empty(); - ({ length: 10 }).should.not.be.empty(); + ({}.should.be.empty()); + ({ length: 10 }.should.not.be.empty()); - if (typeof Map === 'function') { - (new Map([[1, 2]])).should.not.be.empty(); + if (typeof Map === "function") { + new Map([[1, 2]]).should.not.be.empty(); } (function() { @@ -219,51 +220,49 @@ describe('property', function() { })(); err(function() { - ({}).should.not.be.empty(); - }, 'expected Object {} not to be empty (false negative fail)'); + ({}.should.not.be.empty()); + }, "expected Object {} not to be empty (false negative fail)"); err(function() { - ({ length: 10 }).should.be.empty(); - }, 'expected Object { length: 10 } to be empty'); + ({ length: 10 }.should.be.empty()); + }, "expected Object { length: 10 } to be empty"); err(function() { - 'asd'.should.be.empty(); + "asd".should.be.empty(); }, "expected 'asd' to be empty"); err(function() { - ''.should.not.be.empty(); + "".should.not.be.empty(); }, "expected '' not to be empty (false negative fail)"); }); + it("should .propertyByPath lookup properties by name path", function() { + ({ a: { b: 10 } }.should.have.propertyByPath("a", "b")); - it('should .propertyByPath lookup properties by name path', function() { - ({ a: { b: 10}}).should.have.propertyByPath('a', 'b'); + ({ "0": { "0": 10 } }.should.not.have.propertyByPath(0, 0, 1)); - ({ '0': { '0': 10}}).should.not.have.propertyByPath(0, 0, 1); + ({ a: { b: 10 } }.should.have.propertyByPath("a")); - ({ a: { b: 10}}).should.have.propertyByPath('a'); - - ({ a: { b: 10}}).should.not.have.propertyByPath('z'); + ({ a: { b: 10 } }.should.not.have.propertyByPath("z")); // true fail err(function() { - ({ a: { b: 10}}).should.have.propertyByPath('a', 'b', 'c'); + ({ a: { b: 10 } }.should.have.propertyByPath("a", "b", "c")); }, "expected Object { a: Object { b: 10 } } to have property by path a, b, c - failed on c\n expected 10 to have property c"); // true fail err(function() { - ({ a: { b: 10}}).should.have.propertyByPath('z'); + ({ a: { b: 10 } }.should.have.propertyByPath("z")); }, "expected Object { a: Object { b: 10 } } to have property by path z - failed on z\n expected Object { a: Object { b: 10 } } to have property z"); // false positive err(function() { - ({ a: { b: 10}}).should.not.have.propertyByPath('a', 'b'); + ({ a: { b: 10 } }.should.not.have.propertyByPath("a", "b")); }, "expected Object { a: Object { b: 10 } } not to have property by path a, b (false negative fail)"); // false positive err(function() { - ({ a: { b: 10}}).should.not.have.propertyByPath('a'); + ({ a: { b: 10 } }.should.not.have.propertyByPath("a")); }, "expected Object { a: Object { b: 10 } } not to have property by path a (false negative fail)"); - }); }); diff --git a/test/ext/string.test.js b/test/ext/string.test.js index 6481d6e..c2cf0ed 100644 --- a/test/ext/string.test.js +++ b/test/ext/string.test.js @@ -1,47 +1,46 @@ -var err = require('../util').err; -require('../../'); +var err = require("../util").err; +require("../../"); -describe('string', function() { - it('test startWith()', function() { - 'foobar'.should.startWith('foo'); - 'foobar'.should.not.startWith('bar'); +describe("string", function() { + it("test startWith()", function() { + "foobar".should.startWith("foo"); + "foobar".should.not.startWith("bar"); err(function() { - 'foobar'.should.startWith('bar'); + "foobar".should.startWith("bar"); }, "expected 'foobar' to start with 'bar'"); err(function() { - 'foobar'.should.not.startWith('foo'); + "foobar".should.not.startWith("foo"); }, "expected 'foobar' not to start with 'foo' (false negative fail)"); err(function() { - 'foobar'.should.startWith('bar', 'baz'); + "foobar".should.startWith("bar", "baz"); }, "baz"); err(function() { - 'foobar'.should.not.startWith('foo', 'baz'); + "foobar".should.not.startWith("foo", "baz"); }, "baz"); }); - it('test endWith()', function() { - 'foobar'.should.endWith('bar'); - 'foobar'.should.not.endWith('foo'); + it("test endWith()", function() { + "foobar".should.endWith("bar"); + "foobar".should.not.endWith("foo"); err(function() { - 'foobar'.should.endWith('foo'); + "foobar".should.endWith("foo"); }, "expected 'foobar' to end with 'foo'"); err(function() { - 'foobar'.should.not.endWith('bar'); + "foobar".should.not.endWith("bar"); }, "expected 'foobar' not to end with 'bar' (false negative fail)"); err(function() { - 'foobar'.should.endWith('foo', 'baz'); + "foobar".should.endWith("foo", "baz"); }, "baz"); err(function() { - 'foobar'.should.not.endWith('bar', 'baz'); + "foobar".should.not.endWith("bar", "baz"); }, "baz"); }); - }); diff --git a/test/ext/type.test.js b/test/ext/type.test.js index 6ec68d9..4b20534 100644 --- a/test/ext/type.test.js +++ b/test/ext/type.test.js @@ -1,61 +1,70 @@ -var err = require('../util').err, - should = require('../../'); +var err = require("../util").err, + should = require("../../"); -var AssertionError = require('assert').AssertionError; -var util = require('util'); +var AssertionError = require("assert").AssertionError; +var util = require("util"); -describe('type', function() { - it('test arguments', function() { - var args = (function() { return arguments; })(1,2,3); +describe("type", function() { + it("test arguments", function() { + var args = (function() { + return arguments; + })(1, 2, 3); args.should.be.arguments(); [].should.not.be.arguments(); err(function() { - ((function() { return arguments; })(1,2,3)).should.not.be.arguments(); + (function() { + return arguments; + })(1, 2, 3).should.not.be.arguments(); }, "expected Arguments [ 1, 2, 3 ] not to be arguments (false negative fail)"); - err(function() { - ({}).should.be.arguments(); - }, 'expected Object {} to be arguments', - ' expected Object {} to have [[Class]] Arguments'); + err( + function() { + ({}.should.be.arguments()); + }, + "expected Object {} to be arguments", + " expected Object {} to have [[Class]] Arguments" + ); }); - it('test typeof', function() { - 'test'.should.have.type('string'); + it("test typeof", function() { + "test".should.have.type("string"); err(function() { - 'test'.should.not.have.type('string'); + "test".should.not.have.type("string"); }, "expected 'test' not to have type string (false negative fail)"); err(function() { - 'test'.should.not.have.type('string', 'foo'); + "test".should.not.have.type("string", "foo"); }, "foo"); err(function() { - (10).should.have.type('string'); + (10).should.have.type("string"); }, "expected 10 to have type string\n expected 'number' to be 'string'"); - (5).should.have.type('number'); + (5).should.have.type("number"); err(function() { - (5).should.not.have.type('number'); + (5).should.not.have.type("number"); }, "expected 5 not to have type number (false negative fail)"); err(function() { - (5).should.not.have.type('number', 'foo'); + (5).should.not.have.type("number", "foo"); }, "foo"); }); - it('test instanceof', function() { + it("test instanceof", function() { function Foo() {} new Foo().should.be.an.instanceof(Foo); new Date().should.be.an.instanceof(Date); - var tobi = { name: 'Tobi', age: 2 }; + var tobi = { name: "Tobi", age: 2 }; tobi.should.be.an.instanceof(Object); - var getSomething = function() {return "something";}; + var getSomething = function() { + return "something"; + }; getSomething.should.be.an.instanceof(Function); var number = Object(5); @@ -66,7 +75,7 @@ describe('type', function() { (boolean instanceof Boolean).should.be.true(); boolean.should.be.an.instanceof(Boolean); - var string = Object('string'); + var string = Object("string"); (string instanceof String).should.be.true(); string.should.be.an.instanceof(String); @@ -75,24 +84,26 @@ describe('type', function() { }, "expected 3 to be an instance of Foo"); err(function() { - (3).should.an.instanceof(Foo, 'foo'); + (3).should.an.instanceof(Foo, "foo"); }, "foo"); err(function() { - ({}).should.not.be.an.instanceof(Object); + ({}.should.not.be.an.instanceof(Object)); }, "expected Object {} not to be an instance of Object (false negative fail)"); }); - it('test instanceOf (non-reserved)', function() { + it("test instanceOf (non-reserved)", function() { function Foo() {} new Foo().should.be.an.instanceOf(Foo); new Date().should.be.an.instanceOf(Date); - var tobi = { name: 'Tobi', age: 2 }; + var tobi = { name: "Tobi", age: 2 }; tobi.should.be.an.instanceOf(Object); - var getSomething = function() {return "something";}; + var getSomething = function() { + return "something"; + }; getSomething.should.be.an.instanceOf(Function); err(function() { @@ -100,15 +111,17 @@ describe('type', function() { }, "expected 9 to be an instance of Foo"); err(function() { - (9).should.an.instanceOf(Foo, 'foo'); + (9).should.an.instanceOf(Foo, "foo"); }, "foo"); function Foo2() {} - Foo2.prototype.valueOf = function() { return 'foo'; }; + Foo2.prototype.valueOf = function() { + return "foo"; + }; new Foo2().should.be.an.instanceOf(Foo2); }); - it('test Function', function() { + it("test Function", function() { var f = function() {}; f.should.be.a.Function(); @@ -116,127 +129,149 @@ describe('type', function() { Function.should.be.a.Function(); - (new Function("1 * 1")).should.be.a.Function(); + new Function("1 * 1").should.be.a.Function(); - err(function() { - (1).should.be.a.Function(); - }, "expected 1 to be a function", - " expected 1 to have type function", - " expected 'number' to be 'function'"); + err( + function() { + (1).should.be.a.Function(); + }, + "expected 1 to be a function", + " expected 1 to have type function", + " expected 'number' to be 'function'" + ); }); - it('test Object', function() { - ({}).should.be.an.Object(); + it("test Object", function() { + ({}.should.be.an.Object()); Function.should.not.be.an.Object(); - (new Object()).should.be.an.Object(); - (new Date()).should.be.an.Object(); - - err(function() { - (1).should.be.an.Object(); - }, 'expected 1 to be an object', - ' expected 1 to have type object', - ' expected \'number\' to be \'object\''); + new Object().should.be.an.Object(); + new Date().should.be.an.Object(); + + err( + function() { + (1).should.be.an.Object(); + }, + "expected 1 to be an object", + " expected 1 to have type object", + " expected 'number' to be 'object'" + ); }); - it('test String', function() { - ''.should.be.a.String; - ({}).should.not.be.a.String; + it("test String", function() { + "".should.be.a.String; + ({}.should.not.be.a.String); (0).should.not.be.a.String; - (new String("")).should.be.a.String; + new String("").should.be.a.String; //but should(new String("")).not.be.a.String; - err(function() { - (1).should.be.a.String(); - }, 'expected 1 to be a string', - ' expected 1 to have type string', - ' expected \'number\' to be \'string\''); + err( + function() { + (1).should.be.a.String(); + }, + "expected 1 to be a string", + " expected 1 to have type string", + " expected 'number' to be 'string'" + ); }); - it('test Array', function() { + it("test Array", function() { [].should.be.an.Array(); - (new Array(10)).should.be.an.Array(); + new Array(10).should.be.an.Array(); - ''.should.not.be.Array(); + "".should.not.be.Array(); (1).should.not.be.Array(); err(function() { [].should.not.be.Array(); - }, 'expected Array [] not to be an array (false negative fail)'); + }, "expected Array [] not to be an array (false negative fail)"); }); - it('test Number', function() { + it("test Number", function() { (1).should.be.a.Number(); - (new Number(10)).should.be.a.Number(); + new Number(10).should.be.a.Number(); //but should(new Number(10)).should.not.be.a.Number(); NaN.should.be.a.Number(); Infinity.should.be.a.Number(); - ({}).should.not.be.a.Number(); + ({}.should.not.be.a.Number()); - err(function() { - ([]).should.be.a.Number(); - }, 'expected Array [] to be a number', - ' expected Array [] to have type number', - ' expected \'object\' to be \'number\''); + err( + function() { + [].should.be.a.Number(); + }, + "expected Array [] to be a number", + " expected Array [] to have type number", + " expected 'object' to be 'number'" + ); }); - it('test Boolean', function() { - (true).should.be.a.Boolean(); - (false).should.be.a.Boolean(); + it("test Boolean", function() { + true.should.be.a.Boolean(); + false.should.be.a.Boolean(); - (new Boolean(false)).should.be.a.Boolean(); + new Boolean(false).should.be.a.Boolean(); //but should(new Boolean(false)).should.not.be.a.Boolean(); - ({}).should.not.be.a.Boolean(); + ({}.should.not.be.a.Boolean()); - err(function() { - [].should.be.a.Boolean(); - }, "expected Array [] to be a boolean", - " expected Array [] to have type boolean", - " expected 'object' to be 'boolean'"); + err( + function() { + [].should.be.a.Boolean(); + }, + "expected Array [] to be a boolean", + " expected Array [] to have type boolean", + " expected 'object' to be 'boolean'" + ); }); - it('test Error', function() { - (new Error()).should.be.an.Error(); + it("test Error", function() { + new Error().should.be.an.Error(); - ({}).should.not.be.Error(); + ({}.should.not.be.Error()); - var ae = new AssertionError({ actual: 10, operator: 'to fail' }); + var ae = new AssertionError({ actual: 10, operator: "to fail" }); ae.should.be.an.Error(); var AsyncTimeoutError = function AsyncTimeoutError(msg) { msg && (this.message = msg); Error.apply(this, arguments); - Error.captureStackTrace && Error.captureStackTrace(this, AsyncTimeoutError); + Error.captureStackTrace && + Error.captureStackTrace(this, AsyncTimeoutError); }; util.inherits(AsyncTimeoutError, Error); AsyncTimeoutError.prototype.name = AsyncTimeoutError.name; - var e = new AsyncTimeoutError('foo'); + var e = new AsyncTimeoutError("foo"); e.should.be.an.Error(); - err(function() { - ([]).should.be.an.Error(); - }, 'expected Array [] to be an error', - ' expected Array [] to be an instance of Error'); + err( + function() { + [].should.be.an.Error(); + }, + "expected Array [] to be an error", + " expected Array [] to be an instance of Error" + ); }); - it('test Date', function() { - (new Date()).should.be.a.Date(); - (new Date(Date.now())).should.be.a.Date(); - (new Date('foo')).should.be.a.Date(); + it("test Date", function() { + new Date().should.be.a.Date(); + new Date(Date.now()).should.be.a.Date(); + new Date("foo").should.be.a.Date(); - ('foo').should.not.be.a.Date(); + "foo".should.not.be.a.Date(); - err(function() { - ([]).should.be.a.Date(); - }, 'expected Array [] to be a date', - ' expected Array [] to be an instance of Date'); + err( + function() { + [].should.be.a.Date(); + }, + "expected Array [] to be a date", + " expected Array [] to be an instance of Date" + ); }); }); diff --git a/test/nested.test.js b/test/nested.test.js index f171779..cde252a 100644 --- a/test/nested.test.js +++ b/test/nested.test.js @@ -1,26 +1,33 @@ -var should = require('../'); +var should = require("../"); -var util = require('./util'); +var util = require("./util"); -should.Assertion.add('_10', function() { - this.params = {operator: 'to be a number 10'}; +should.Assertion.add( + "_10", + function() { + this.params = { operator: "to be a number 10" }; - this.is.Number().and.equal(10); -}, true); + this.is.Number().and.equal(10); + }, + true +); -it('should throw when used last managed context', function() { +it("should throw when used last managed context", function() { util.err(function() { (11).should.be._10(); - }, 'expected 11 to be a number 10\n expected 11 to be 10'); + }, "expected 11 to be a number 10\n expected 11 to be 10"); - util.err(function() { - ({}).should.be._10(); - }, 'expected Object {} to be a number 10', - ' expected Object {} to be a number', - ' expected Object {} to have type number', - ' expected \'object\' to be \'number\''); + util.err( + function() { + ({}.should.be._10()); + }, + "expected Object {} to be a number 10", + " expected Object {} to be a number", + " expected Object {} to have type number", + " expected 'object' to be 'number'" + ); util.err(function() { (10).should.not.be._10(); - }, 'expected 10 not to be a number 10 (false negative fail)'); + }, "expected 10 not to be a number 10 (false negative fail)"); }); diff --git a/test/not.test.js b/test/not.test.js index 133842b..5adfb71 100644 --- a/test/not.test.js +++ b/test/not.test.js @@ -1,28 +1,27 @@ -var should = require('../'); -var err = require('./util').err; - +var should = require("../"); +var err = require("./util").err; var a = 10; var b = 11; -describe('.not', function() { - it('should not throw when true', function() { +describe(".not", function() { + it("should not throw when true", function() { a.should.be.exactly(a); }); - it('should throw when false', function() { + it("should throw when false", function() { err(function() { a.should.be.exactly(b); - }, 'expected 10 to be 11'); + }, "expected 10 to be 11"); }); - it('should throw when not true (false negative case)', function() { + it("should throw when not true (false negative case)", function() { err(function() { a.should.not.be.exactly(a); - }, 'expected 10 not to be 10 (false negative fail)'); + }, "expected 10 not to be 10 (false negative fail)"); }); - it('should not throw when not false', function() { + it("should not throw when not false", function() { a.should.not.be.exactly(b); }); }); diff --git a/test/perf.test.js b/test/perf.test.js index b1c1c33..60f0cc2 100644 --- a/test/perf.test.js +++ b/test/perf.test.js @@ -1,19 +1,18 @@ -require('../'); +require("../"); function mockComplexObject(prefix, depth, width) { if (depth <= 0) { - return 'value-' + prefix; + return "value-" + prefix; } var obj = {}; for (var i = 0; i < width; i++) { - obj[prefix + depth + '_' + i] = mockComplexObject(prefix, depth-1, width); + obj[prefix + depth + "_" + i] = mockComplexObject(prefix, depth - 1, width); } return obj; } -var a = mockComplexObject('a', 8, 4); -var b = mockComplexObject('b', 8, 4); +var a = mockComplexObject("a", 8, 4); +var b = mockComplexObject("b", 8, 4); - -it('should optimize not.equal for complex objects', function() { +it("should optimize not.equal for complex objects", function() { a.should.be.not.equal(b); // It takes a while }); diff --git a/test/should.test.js b/test/should.test.js index 23b1a1b..cee3649 100644 --- a/test/should.test.js +++ b/test/should.test.js @@ -1,34 +1,33 @@ - /** * Module dependencies. */ -var should = require('../'); -var assert = require('assert'); - +var should = require("../"); +var assert = require("assert"); -describe('should', function() { - it('test assertion', function() { - 'test'.should.be.a.String; - should.equal('foo', 'foo'); +describe("should", function() { + it("test assertion", function() { + "test".should.be.a.String; + should.equal("foo", "foo"); }); - it('test .expected and .actual', function() { + it("test .expected and .actual", function() { try { - 'foo'.should.equal('bar'); + "foo".should.equal("bar"); } catch (err) { - assert('foo' == err.actual, 'err.actual'); - assert('bar' == err.expected, 'err.expected'); + assert("foo" == err.actual, "err.actual"); + assert("bar" == err.expected, "err.expected"); } }); - it('test chaining', function() { - var user = { name: 'tj', pets: ['tobi', 'loki', 'jane', 'bandit'] }; + it("test chaining", function() { + var user = { name: "tj", pets: ["tobi", "loki", "jane", "bandit"] }; - user.should.be.an.instanceOf(Object).and.have.property('name', 'tj'); + user.should.be.an.instanceOf(Object).and.have.property("name", "tj"); - user.should.have.ownProperty('name') + user.should.have + .ownProperty("name") .which.not.have.length(3) - .and.be.equal('tj'); + .and.be.equal("tj"); }); }); diff --git a/test/util.js b/test/util.js index e5d75c6..b397327 100644 --- a/test/util.js +++ b/test/util.js @@ -1,8 +1,8 @@ -var should = require('../'); +var should = require("../"); function err(fn, msg) { if (arguments.length > 2) { - msg = Array.prototype.slice.call(arguments, 1).join('\n'); + msg = Array.prototype.slice.call(arguments, 1).join("\n"); } var ok = true; @@ -10,20 +10,23 @@ function err(fn, msg) { fn(); ok = false; } catch (err) { - if ((typeof msg == 'string' && err.message !== msg) || (msg instanceof RegExp && !msg.test(err.message))) { + if ( + (typeof msg == "string" && err.message !== msg) || + (msg instanceof RegExp && !msg.test(err.message)) + ) { throw new should.AssertionError({ message: [ - 'Expected message does not match', - 'expected: ' + should.format(msg), - ' actual: ' + should.format(err.message) - ].join('\n'), + "Expected message does not match", + "expected: " + should.format(msg), + " actual: " + should.format(err.message) + ].join("\n"), expected: msg, actual: err.message, stackStartFunction: err }); } } - if (!ok) throw new Error('expected an error'); + if (!ok) throw new Error("expected an error"); } -exports.err = err; \ No newline at end of file +exports.err = err;