Skip to content
This repository has been archived by the owner on Mar 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #50 from rowanmanning/assert-tests
Browse files Browse the repository at this point in the history
Add commonjs unit testing unit tests
  • Loading branch information
JakeChampion committed Oct 13, 2017
2 parents 2be3044 + adc8450 commit e8cd5c0
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@ components
coverage
node_modules
npm-debug.log
.vscode
3 changes: 2 additions & 1 deletion .jscsrc
Expand Up @@ -50,6 +50,7 @@

"excludeFiles": [
"coverage/**",
"node_modules/**"
"node_modules/**",
"test/unit/commonjs-assert/program.js"
]
}
1 change: 1 addition & 0 deletions .jshintignore
@@ -1,2 +1,3 @@
coverage
node_modules
test/unit/commonjs-assert/program.js
3 changes: 2 additions & 1 deletion .jshintrc
Expand Up @@ -11,7 +11,8 @@
"maxparams": 5,
"maxdepth": 2,
"maxstatements": false,
"maxcomplexity": 13,
"maxcomplexity": 20,
"proto": true,
"node": true,
"strict": true,
"unused": true
Expand Down
12 changes: 11 additions & 1 deletion README.md
Expand Up @@ -123,6 +123,14 @@ Assert that `actual` is deeply equal to `expected`.

Assert that `actual` is not deeply equal to `expected`.

### proclaim.deepStrictEqual( actual, expected, [message] )

Assert that `actual` is deeply equal to `expected`, as determined by the strict equality operator `===`.

### proclaim.notDeepStrictEqual( actual, expected, [message] )

Assert that `actual` is not deeply equal to `expected`, as determined by the strict not equal operator `!==`.


### proclaim.throws( fn, [expected], [message] )

Expand All @@ -131,6 +139,7 @@ Assert that `fn` throws an error. If `expected` is present then the thrown `erro
- If `expected` is a function, assert that `error instanceof expected`
- If `expected` is a string, assert that `error.message === expected`
- If `expected` is a RegExp, assert that `expected.test(error) === true`
- If `expected` is a function which error is not an instance of, assert that `expected.call({}, error) === true`


### proclaim.doesNotThrow( fn, [expected], [message] )
Expand Down Expand Up @@ -381,7 +390,8 @@ If you're opening issues related to these, please mention the version that the i
License
-------

Proclaim is licensed under the [MIT][info-license] license.
Proclaim is licensed under the [MIT][info-license] license.

Copyright © 2015, Rowan Manning


Expand Down
86 changes: 73 additions & 13 deletions lib/proclaim.js
Expand Up @@ -60,9 +60,23 @@
}
};

// Assert that two values are strictly deeply equal
proclaim.deepStrictEqual = function (actual, expected, msg) {
if (!isDeepEqual(actual, expected, true)) {
fail(actual, expected, msg, 'deepStrictEqual', proclaim.deepStrictEqual);
}
};

// Assert that two values are not strictly deeply equal
proclaim.notDeepStrictEqual = function (actual, expected, msg) {
if (isDeepEqual(actual, expected, true)) {
fail(actual, expected, msg, 'notDeepStrictEqual', proclaim.notDeepStrictEqual);
}
};

// Assert that a function throws an error
proclaim.throws = function (fn, expected, msg) {
if (!functionThrows(fn, expected)) {
if (!functionThrows(fn, expected, true)) {
fail(fn, expected, msg, 'throws');
}
};
Expand All @@ -80,7 +94,7 @@

// Assert that a function does not throw an error
proclaim.doesNotThrow = function (fn, expected, msg) {
if (functionThrows(fn, expected)) {
if (functionThrows(fn, expected, false)) {
fail(fn, expected, msg, '!throws');
}
};
Expand Down Expand Up @@ -387,20 +401,36 @@
}

// Utility for deep equality testing of objects
function objectsEqual (obj1, obj2) {
function objectsEqual (obj1, obj2, strict) {
/* jshint eqeqeq: false */

// Check for undefined or null
if (isUndefinedOrNull(obj1) || isUndefinedOrNull(obj2)) {
return false;
}

// Object prototypes must be the same
if (obj1.prototype !== obj2.prototype) {
return false;
if (isPrimitive(obj1) || isPrimitive(obj2)) {
return obj1 === obj2;
}

if (strict) {
// Object prototypes must be the same
var obj1prototype = obj1.prototype || (obj1).__proto__;
var obj2prototype = obj2.prototype || (obj2).__proto__;
if (obj1prototype !== obj2prototype) {
return false;
}
}

// Handle argument objects
var obj1IsArgumentsObject = isArgumentsObject(obj1);
var obj2IsArgumentsObject = isArgumentsObject(obj2);
if (
(obj1IsArgumentsObject && !obj2IsArgumentsObject) ||
(!obj1IsArgumentsObject && obj2IsArgumentsObject)
) {
return false;
}
if (isArgumentsObject(obj1)) {
if (!isArgumentsObject(obj2)) {
return false;
Expand Down Expand Up @@ -445,7 +475,7 @@
// Expensive deep test
for (i = 0; i < len; i += 1) {
key = obj1Keys[i];
if (!isDeepEqual(obj1[key], obj2[key])) {
if (!isDeepEqual(obj1[key], obj2[key], strict)) {
return false;
}
}
Expand All @@ -455,7 +485,7 @@
}

// Utility for deep equality testing
function isDeepEqual (actual, expected) {
function isDeepEqual (actual, expected, strict) {
/* jshint eqeqeq: false */
if (actual === expected) {
return true;
Expand All @@ -472,14 +502,14 @@
actual.ignoreCase === expected.ignoreCase
);
}
if (typeof actual !== 'object' || typeof expected !== 'object') {
return actual == expected;
if (typeof actual !== 'object' && typeof expected !== 'object') {
return strict ? actual === expected : actual == expected;
}
return objectsEqual(actual, expected);
return objectsEqual(actual, expected, strict);
}

// Utility for testing whether a function throws an error
function functionThrows (fn, expected) {
function functionThrows (fn, expected, shouldThrow) {

// Try/catch
var thrown = false;
Expand All @@ -497,6 +527,18 @@
thrown = errorMatches(thrownError, expected);
}

var isUnexpectedException = !shouldThrow && thrownError && !expected;
var isUnwantedException = !shouldThrow && isError(thrownError);

if (isUnexpectedException || (isUnwantedException && thrown)) {
return thrown;
}

if ((shouldThrow && thrownError && expected &&
!errorMatches(thrownError, expected)) || (!shouldThrow && thrownError)) {
throw thrownError;
}

return thrown;
}

Expand All @@ -511,7 +553,8 @@
if (actual instanceof expected) {
return true;
}
return false;

return expected.call({}, actual) === true;
}

// Get a formatted assertion error message
Expand Down Expand Up @@ -543,6 +586,23 @@
return clone;
}

// Utility for detecting if a value is a primitive. A primitive value is a member of one of
// the following built-in types: Undefined, Null, Boolean, Number, String, and Symbol;
function isPrimitive (value) {
return value === null || (typeof value !== 'function' && typeof value !== 'object');
}

// Utility for checking if a value is an Error.
function isError (e) {
return isObject(e) &&
(Object.prototype.toString.call(e) === '[object Error]' || e instanceof Error);
}

// Utility for checking if a value is an Object.
function isObject (arg) {
return typeof arg === 'object' && arg !== null;
}


// Exports
// -------
Expand Down

0 comments on commit e8cd5c0

Please sign in to comment.