Permalink
Browse files

index: Add equality methods (equal, deepEqual, etc.)

Closes #5.
  • Loading branch information...
1 parent cb83cc1 commit 558822fa66aabd0a603ef9329e22c7e514d8d9db @stephenmathieson stephenmathieson committed with jonathanong Dec 10, 2014
Showing with 143 additions and 1 deletion.
  1. +28 −1 index.js
  2. +1 −0 package.json
  3. +114 −0 test/test.js
View
@@ -1,6 +1,33 @@
var createError = require('http-errors');
+var eql = require('deep-equal');
-module.exports = function (value, status, msg, opts) {
+module.exports = assert;
+
+function assert(value, status, msg, opts) {
if (value) return;
throw createError(status, msg, opts);
}
+
+assert.equal = function(a, b, status, msg, opts) {
+ assert(a == b, status, msg, opts);
+};
+
+assert.notEqual = function(a, b, status, msg, opts) {
+ assert(a != b, status, msg, opts);
+};
+
+assert.strictEqual = function(a, b, status, msg, opts) {
+ assert(a === b, status, msg, opts);
+};
+
+assert.notStrictEqual = function(a, b, status, msg, opts) {
+ assert(a !== b, status, msg, opts);
+};
+
+assert.deepEqual = function(a, b, status, msg, opts) {
+ assert(eql(a, b), status, msg, opts);
+};
+
+assert.notDeepEqual = function(a, b, status, msg, opts) {
+ assert(!eql(a, b), status, msg, opts);
+};
View
@@ -21,6 +21,7 @@
"http"
],
"dependencies": {
+ "deep-equal": "^0.2.1",
"http-errors": "~1.2.0"
}
}
View
@@ -66,3 +66,117 @@ describe('assert()', function () {
ok(!err.expose);
});
})
+
+describe('assert.equal()', function() {
+ it('should throw when things aren\'t equal', function(){
+ var err;
+ try {
+ assert.equal('a', 'b', 401, 'fail');
+ } catch (e) {
+ err = e;
+ }
+
+ ok(err);
+ ok(err.status == 401);
+ ok(err.message == 'fail');
+ })
+
+ it('should not throw when things are equal', function() {
+ assert.equal(1, '1', 401, 'fail');
+ })
+})
+
+describe('assert.notEqual()', function() {
+ it('should throw when things are equal', function(){
+ var err;
+ try {
+ assert.notEqual('a', 'a', 401, 'fail');
+ } catch (e) {
+ err = e;
+ }
+
+ ok(err);
+ ok(err.status == 401);
+ ok(err.message == 'fail');
+ })
+
+ it('should not throw when things aren\'t equal', function() {
+ assert.notEqual(1, 11, 401, 'fail');
+ })
+})
+
+describe('assert.strictEqual()', function() {
+ it('should throw when things aren\'t equal', function(){
+ var err;
+ try {
+ assert.strictEqual(1, '1', 401, 'fail');
+ } catch (e) {
+ err = e;
+ }
+
+ ok(err);
+ ok(err.status == 401);
+ ok(err.message == 'fail');
+ })
+
+ it('should not throw when things are equal', function() {
+ assert.strictEqual(1, 1, 401, 'fail');
+ })
+})
+
+describe('assert.notStrictEqual()', function() {
+ it('should throw when things are equal', function(){
+ var err;
+ try {
+ assert.notStrictEqual(1, 1, 401, 'fail');
+ } catch (e) {
+ err = e;
+ }
+
+ ok(err);
+ ok(err.status == 401);
+ ok(err.message == 'fail');
+ })
+
+ it('should not throw when things aren\'t equal', function() {
+ assert.notStrictEqual(1, '1', 401, 'fail');
+ })
+})
+
+describe('assert.deepEqual()', function() {
+ it('should throw when things aren\'t deeply equal', function(){
+ var err;
+ try {
+ assert.deepEqual({ a: 'a' }, { b: 'b' }, 401, 'fail');
+ } catch (e) {
+ err = e;
+ }
+
+ ok(err);
+ ok(err.status == 401);
+ ok(err.message == 'fail');
+ });
+
+ it('should not throw when things are deeply equal', function() {
+ assert.deepEqual({ a: 'a' }, { a: 'a' }, 401, 'fail');
+ })
+})
+
+describe('assert.notDeepEqual()', function() {
+ it('should throw when things aren\'t deeply equal', function(){
+ var err;
+ try {
+ assert.notDeepEqual({ a: 'a' }, { a: 'a' }, 401, 'fail');
+ } catch (e) {
+ err = e;
+ }
+
+ ok(err);
+ ok(err.status == 401);
+ ok(err.message == 'fail');
+ });
+
+ it('should not throw when things are deeply equal', function() {
+ assert.notDeepEqual({ a: 'a' }, { b: 'b' }, 401, 'fail');
+ })
+})

0 comments on commit 558822f

Please sign in to comment.