Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

'keys' assertion #8

Merged
merged 5 commits into from

2 participants

@reebalazs

No description provided.

@reebalazs

I did not add 'values', and I am not sure if it's needed either. As you can see I print the keys in ${1} and that's all.

I also did not add 'expectation' because I don't find the good English word. Please help me with adding these.

@cjohansen cjohansen merged commit de0251f into busterjs:master
@cjohansen
Owner

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 9, 2013
  1. @reebalazs

    Add a 'keys' assertion

    reebalazs authored
  2. @reebalazs

    remove debug stuff

    reebalazs authored
  3. @reebalazs

    Finish the tests

    reebalazs authored
Commits on Jan 11, 2013
  1. @reebalazs

    Add expectation

    reebalazs authored
  2. @reebalazs
This page is out of date. Refresh to see the latest.
Showing with 72 additions and 0 deletions.
  1. +1 −0  AUTHORS
  2. +27 −0 lib/referee.js
  3. +44 −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
27 lib/referee.js
@@ -454,6 +454,33 @@
values: actualAndTypeOfMessageValues
});
+ function exactKeys(object, keys) {
+ 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}!",
+ expectation: "toHaveKeys"
+ });
+
function captureException(callback) {
try { callback(); } catch (e) { return e; }
return null;
View
44 test/referee-test.js
@@ -1065,6 +1065,50 @@
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("refute", "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() {};
+
+ fail("when keys are exact", {a: 1, b: 2, c: 3}, ['a', 'b', 'c']);
+ pass("when keys are missing", {a: 1, b: 2, c: 3}, ['a', 'b']);
+ pass("when keys are excess", {a: 1, b: 2, c: 3}, ['a', 'b', 'c', 'd']);
+ pass("when keys are not exact", {a: 1, b: 2, c: 3}, ['a', 'b', 'd']);
+ fail("when there are no keys", {}, []);
+ fail("when values are special", {a: -1, b: null, c: undefined}, ['a', 'b', 'c']);
+ fail("and ignore object methods", new Class({a: 1, b: 2, c: 3}), ['a', 'b', 'c']);
+ fail("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 () {});
Something went wrong with that request. Please try again.