Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a 'keys' assertion

  • Loading branch information...
commit 0290e504e0108c9b0a8a2c179be65d859e117893 1 parent 5e03f43
@reebalazs reebalazs authored
Showing with 51 additions and 0 deletions.
  1. +1 −0  AUTHORS
  2. +28 −0 lib/referee.js
  3. +22 −0 test/referee-test.js
View
1  AUTHORS
@@ -3,3 +3,4 @@ August Lilleaas, august.lilleaas@gmail.com
Dave Geddes davidcgeddes@gmail.com
Malcolm Locke malc@wholemeal.co.nz
Stein Magnus Jodal stein.magnus@jodal.no
+Balazs Ree ree@greenfinity.hu
View
28 lib/referee.js
@@ -454,6 +454,34 @@
values: actualAndTypeOfMessageValues
});
+ function exactKeys(object, keys) {
+ throw new Error('xxx');
+ buster.log('XXXXXX CxxxxxXXX XXXx');
+ var keyMap = {};
+ var keyCnt = 0;
+ for (var i=0; i < keys.length; i++) {
+ keyMap[keys[i]] = true;
+ keyCnt += 1;
+ }
+ for (var key in object) {
+ if (object.hasOwnProperty(key)) {
+ if (! keyMap[key]) {
+ return false;
+ }
+ keyCnt -= 1;
+ }
+ }
+ return keyCnt === 0;
+ }
+
+ referee.add('keys', {
+ assert: function (actual, keys) {
+ return exactKeys(actual, keys);
+ },
+ assertMessage: "Expected ${0} to have exact keys ${1}!",
+ refuteMessage: "Expected ${0} not to have exact keys ${1}!"
+ });
+
function captureException(callback) {
try { callback(); } catch (e) { return e; }
return null;
View
22 test/referee-test.js
@@ -1065,6 +1065,28 @@
pass("for mis-ordered array 'subset'", [1, 2, 3], [1, 3]);
});
+ testHelper.assertionTests("=>assert", "keys", function (pass, fail, msg) {
+ function Class(o) {
+ for (var key in o) {
+ if (o.hasOwnProperty(key)) {
+ this[key] = o[key];
+ }
+ }
+ }
+ Class.prototype.methodA = function() {};
+ Class.prototype.methodB = function() {};
+
+ pass("when keys are exact", {a: 1, b: 2, c: 3}, ['a', 'b', 'c']);
+ //fail("when keys are missing", {a: 1, b: 2, c: 3}, ['a', 'b']);
+ //fail("when keys are excess", {a: 1, b: 2, c: 3}, ['a', 'b', 'c', 'd']);
+ //fail("when keys are not exact", {a: 1, b: 2, c: 3}, ['a', 'b', 'd']);
+ //pass("when there are no keys", {}, []);
+ //pass("when values are special", {a: -1, b: null, c: undefined}, ['a', 'b', 'c']);
+ //pass("and ignore object methods", new Class({a: 1, b: 2, c: 3}), ['a', 'b', 'c']);
+ //pass("and allow overwriting object methods",
+ // new Class({a: 1, methodA: 2}), ['a', 'methodA']);
+ });
+
testHelper.assertionTests("assert", "exception", function (pass, fail, msg) {
pass("when callback throws", function () { throw new Error(); });
fail("when callback does not throw", function () {});
Please sign in to comment.
Something went wrong with that request. Please try again.