Skip to content

Commit

Permalink
[api test] Ensure that .hasMeta(), .anyMeta() and .allMeta() ca…
Browse files Browse the repository at this point in the history
…n also optionally take values for keys in `data.meta`.
  • Loading branch information
indexzero committed Mar 13, 2013
1 parent f07502d commit 100747a
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 26 deletions.
33 changes: 27 additions & 6 deletions lib/godot/reactor/has-meta.js
Expand Up @@ -28,16 +28,33 @@ var HasMeta = module.exports = function (type) {
this.type === 'any';
}

this.keys = keys.reduce(function (all, key) {
//
// Create a lookup table of any values provided.
// If no value is provided set the key to `null`
// since we will be checking for `undefined`.
//
this.lookup = keys.reduce(function (all, key) {
if (Array.isArray(key)) {
all = all.concat(key);
key.forEach(function (kkey) {
all[kkey] = null;
});
}
else if (typeof key === 'object') {
Object.keys(key).forEach(function (kkey) {
all[kkey] = key[kkey];
});
}
else {
all.push(key);
all[key] = null;
}

return all;
}, []);
}, {});

//
// Store the list of keys for iterating over later.
//
this.keys = Object.keys(this.lookup);
};

//
Expand All @@ -51,6 +68,8 @@ util.inherits(HasMeta, ReadWriteStream);
// Only filters `data` according to `this.keys`.
//
HasMeta.prototype.write = function (data) {
var self = this;

//
// If there are no tags on the data return
//
Expand All @@ -62,7 +81,9 @@ HasMeta.prototype.write = function (data) {
// Helper function for checking a given `key`.
//
function hasKey(key) {
return data.meta[key] !== undefined;
return data.meta[key] !== undefined
&& (self.lookup[key] === null ||
self.lookup[key] === data.meta[key]);
}

var valid = this.type === 'all'
Expand Down
104 changes: 84 additions & 20 deletions test/reactor/meta-test.js
Expand Up @@ -80,34 +80,98 @@ vows.describe('godot/reactor/meta').addBatch({
}
},
"hasMeta": {
"all": macros.shouldEmitDataSync(
"all": {
"with only keys": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('all', 'a', 'b', 'c'),
'meta',
5
),
"with all values": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('all', { a: 1, b: 1, c: 1 }),
'meta',
5
),
"with keys and values": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('all', { a: 1 }, 'b', 'c'),
'meta',
5
),
},
"any": {
"with only keys": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('any', 'a'),
'meta',
10
),
"with all values": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('any', { a: 1, b: 0, c: 0 }),
'meta',
10
),
"with keys and values": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('any', 'a', { b: 0, c: 0 }),
'meta',
10
),
}
},
"anyMeta": {
"with only keys": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('all', 'a', 'b', 'c'),
.anyMeta('a'),
'meta',
5
10
),
"with all values": macros.shouldEmitDataSync(
godot
.reactor()
.anyMeta({ a: 1, b: 0, c: 0 }),
'meta',
10
),
"any": macros.shouldEmitDataSync(
"with keys and values": macros.shouldEmitDataSync(
godot
.reactor()
.hasMeta('any', 'a'),
.anyMeta('a', { b: 0, c: 0 }),
'meta',
10
)
),
},
"anyMeta": macros.shouldEmitDataSync(
godot
.reactor()
.anyMeta('a'),
'meta',
10
),
"allMeta": macros.shouldEmitDataSync(
godot
.reactor()
.allMeta('a', 'b', 'c'),
'meta',
5
)
"allMeta": {
"with only keys": macros.shouldEmitDataSync(
godot
.reactor()
.allMeta('a', 'b', 'c'),
'meta',
5
),
"with all values": macros.shouldEmitDataSync(
godot
.reactor()
.allMeta({ a: 1, b: 1, c: 1 }),
'meta',
5
),
"with keys and values": macros.shouldEmitDataSync(
godot
.reactor()
.allMeta({ a: 1 }, 'b', 'c'),
'meta',
5
),
}
}
}).export(module);

0 comments on commit 100747a

Please sign in to comment.