Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Embed ASSERT-PARANOID-EQUAL into the tests support

  • Loading branch information...
commit 0fcbfbd0da1909933c5da308f33d800d972d9a53 1 parent 4b4f67a
@dervus dervus authored
View
3  package.json
@@ -24,7 +24,6 @@
"scripts" : { "test": "make test" },
"dependencies" : { "argparse": "~ 0.1.3" },
- "devDependencies" : { "mocha": ">= 1.7.4",
- "assert-paranoid-equal": "git://github.com/dervus/assert-paranoid-equal.git" },
+ "devDependencies" : { "mocha": ">= 1.7.4" },
"engines" : { "node": ">= 0.6.0" }
}
View
2  test/functional/constructor.js
@@ -1,7 +1,7 @@
'use strict';
-var paranoidEqual = require('assert-paranoid-equal').paranoidEqual;
+var paranoidEqual = require('../support/assert-paranoid-equal').paranoidEqual;
var jsyaml = require('../../lib/js-yaml');
var _functional = require('../support/functional');
var TestLoader = require('../support/test-loader');
View
23 test/support/assert-paranoid-equal.js
@@ -0,0 +1,23 @@
+'use strict';
+
+
+var assert = require('assert');
+var inspectors = require('./assert-paranoid-equal/inspectors');
+var Context = require('./assert-paranoid-equal/context');
+
+
+function paranoidEqual(value1, value2) {
+ inspectors.ensureEqual(new Context(value1, value2), value1, value2);
+}
+
+
+function notParanoidEqual(value1, value2) {
+ assert.throws(
+ function () { paranoidEqual(value1, value2); },
+ assert.AssertionError,
+ 'Given objects are equal, witch is not expected.');
+}
+
+
+module.exports.paranoidEqual = paranoidEqual;
+module.exports.notParanoidEqual = notParanoidEqual;
View
16 test/support/assert-paranoid-equal/context.js
@@ -0,0 +1,16 @@
+'use strict';
+
+
+function Context(actual, expected, path) {
+ this.actual = actual;
+ this.expected = expected;
+ this.path = path || [];
+}
+
+
+Context.prototype.descent = function descent(key) {
+ return new Context(this.actual, this.expected, this.path.concat(key));
+};
+
+
+module.exports = Context;
View
116 test/support/assert-paranoid-equal/inspectors.js
@@ -0,0 +1,116 @@
+'use strict';
+/*jshint latedef:false */
+
+
+var AssertionError = require('assert').AssertionError;
+var ownUtils = require('./utilities');
+var Report = require('./report');
+
+
+function ensureEqual(context, actual, expected) {
+ ensureEqualValues(
+ context,
+ typeof actual,
+ typeof expected,
+ 'types');
+
+ if (ownUtils.isObject(actual) &&
+ ownUtils.isObject(expected)) {
+
+ ensureEqualValues(
+ context,
+ Object.getPrototypeOf(actual),
+ Object.getPrototypeOf(expected),
+ 'prototypes');
+
+ if (ownUtils.isInstanceOf(Date, actual, expected)) {
+ ensureEqualDates(context, actual, expected);
+
+ } else if (ownUtils.isInstanceOf(Buffer, actual, expected)) {
+ ensureEqualObjects(context, actual, expected, {
+ exclude: [ 'parent', 'offset' ]
+ });
+
+ } else {
+ ensureEqualObjects(context, actual, expected);
+ }
+
+ } else {
+ ensureEqualValues(context, actual, expected, 'values');
+ }
+}
+
+
+function ensureEqualValues(context, actual, expected, reason) {
+ if (!(Number.isNaN(actual) && Number.isNaN(expected))) {
+ if (actual !== expected) {
+ throw new AssertionError({
+ actual: context.actual,
+ expected: context.expected,
+ operator: 'is paranoidly equal to',
+ message: new Report(context, actual, expected, reason)
+ });
+ }
+ }
+}
+
+
+function ensureEqualDates(context, actual, expected) {
+ ensureEqualValues(
+ context,
+ actual.getTime(),
+ expected.getTime(),
+ 'date_timestamps');
+
+ ensureEqualObjects(
+ context,
+ actual,
+ expected);
+}
+
+
+function ensureEqualObjects(context, actual, expected, options) {
+ var actualKeys, expectedKeys, includeKeys, excludeKeys,
+ index, length, currentKey;
+
+ if (!ownUtils.isNothing(options)) {
+ includeKeys = options.include;
+ excludeKeys = options.exclude;
+ }
+
+ actualKeys = ownUtils.collectKeys(actual, includeKeys, excludeKeys);
+ expectedKeys = ownUtils.collectKeys(expected, includeKeys, excludeKeys);
+
+ ensureEqualValues(
+ context,
+ actualKeys.length,
+ expectedKeys.length,
+ 'object_property_amounts');
+
+ actualKeys.sort();
+ expectedKeys.sort();
+
+ length = actualKeys.length;
+
+ for (index = 0; index < length; index += 1) {
+ ensureEqualValues(
+ context,
+ actualKeys[index],
+ expectedKeys[index],
+ 'object_property_names');
+ }
+
+ for (index = 0; index < length; index += 1) {
+ currentKey = actualKeys[index];
+ ensureEqual(
+ context.descent(currentKey),
+ actual[currentKey],
+ expected[currentKey]);
+ }
+}
+
+
+module.exports.ensureEqual = ensureEqual;
+module.exports.ensureEqualValues = ensureEqualValues;
+module.exports.ensureEqualDates = ensureEqualDates;
+module.exports.ensureEqualObjects = ensureEqualObjects;
View
55 test/support/assert-paranoid-equal/report.js
@@ -0,0 +1,55 @@
+'use strict';
+
+
+var nodeUtils = require('util');
+
+
+var REASONS = {
+ 'values' : 'Given objects are not equal',
+ 'types' : 'Given objects are of different types',
+ 'prototypes' : 'Given objects has different prototypes',
+ 'object_property_amounts' : 'Given objects has different key amounts',
+ 'object_property_names' : 'Given objects has different key sets',
+ 'date_timestamps' : 'Given Date objects has different timestamps',
+};
+
+
+function Report(context, actual, expected, reason) {
+ this.context = context;
+ this.actual = actual;
+ this.expected = expected;
+ this.reason = reason;
+
+ if (!REASONS.hasOwnProperty(this.reason)) {
+ throw new Error(
+ 'Unknown reason for the report: ' +
+ nodeUtils.inspect(this.reason));
+ }
+}
+
+
+Report.prototype.toString = function toString() {
+ var result = '',
+ index,
+ path = this.context.path,
+ length = path.length;
+
+ result += REASONS[this.reason];
+
+ if (length > 0) {
+ result += ' at ';
+
+ for (index = 0; index < length; index += 1) {
+ result += '[' + JSON.stringify(path[index]) + ']';
+ }
+ }
+
+ result +=
+ ' (actual: ' + nodeUtils.inspect(this.actual) +
+ ', expected: ' + nodeUtils.inspect(this.expected) + ')';
+
+ return result;
+};
+
+
+module.exports = Report;
View
56 test/support/assert-paranoid-equal/utilities.js
@@ -0,0 +1,56 @@
+'use strict';
+
+
+function isNothing(subject) {
+ return (undefined === subject) || (null === subject);
+}
+
+
+function isObject(subject) {
+ return ('object' === typeof subject) && (null !== subject);
+}
+
+
+function isInstanceOf(constructor /*, subjects... */) {
+ var index,
+ length = arguments.length;
+
+ if (length < 2) {
+ return false;
+ }
+
+ for (index = 1; index < length; index += 1) {
+ if (!(arguments[index] instanceof constructor)) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
+
+function collectKeys(subject, include, exclude) {
+ var result = Object.getOwnPropertyNames(subject);
+
+ if (!isNothing(include)) {
+ include.forEach(function (key) {
+ if (0 > result.indexOf(key)) {
+ result.push(key);
+ }
+ });
+ }
+
+ if (!isNothing(exclude)) {
+ result = result.filter(function (key) {
+ return 0 > exclude.indexOf(key);
+ });
+ }
+
+ return result;
+}
+
+
+module.exports.isNothing = isNothing;
+module.exports.isObject = isObject;
+module.exports.isInstanceOf = isInstanceOf;
+module.exports.collectKeys = collectKeys;
Please sign in to comment.
Something went wrong with that request. Please try again.