Skip to content

Commit

Permalink
broke up test into separate exports
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Aug 27, 2010
1 parent c452103 commit 92046a4
Showing 1 changed file with 65 additions and 44 deletions.
109 changes: 65 additions & 44 deletions test/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,96 @@
var sys = require('sys');
var Hash = require('traverse/hash');

exports['hash traversal'] = function (assert) {
var ref1 = { a : 1, b : 2 };
var hash1 = Hash(ref1).map(function (v) { return v + 1 }).end;
assert.equal(ref1.a, 1);
assert.equal(ref1.b, 2);
assert.equal(hash1.a, 2);
assert.equal(hash1.b, 3);

var ref2 = { foo : [1,2], bar : [4,5] };
var hash2 = Hash(ref2).clone.map(
exports.map = function (assert) {
var ref = { a : 1, b : 2 };
var hash = Hash(ref).map(function (v) { return v + 1 }).end;
assert.equal(ref.a, 1);
assert.equal(ref.b, 2);
assert.equal(hash.a, 2);
assert.equal(hash.b, 3);
};

exports['cloned map'] = function (assert) {
var ref = { foo : [1,2], bar : [4,5] };
var hash = Hash(ref).clone.map(
function (v) { v.unshift(v[0] - 1); return v }
).items;
assert.equal(ref2.foo.join(' '), '1 2');
assert.equal(ref2.bar.join(' '), '4 5');
assert.equal(hash2.foo.join(' '), '0 1 2');
assert.equal(hash2.bar.join(' '), '3 4 5');
assert.equal(ref.foo.join(' '), '1 2');
assert.equal(ref.bar.join(' '), '4 5');
assert.equal(hash.foo.join(' '), '0 1 2');
assert.equal(hash.bar.join(' '), '3 4 5');
};

exports.reduce = function (assert) {
var ref = { foo : [1,2], bar : [4,5] };

var sum1 = Hash(ref2).reduce(function (acc, v) {
var sum1 = Hash(ref).reduce(function (acc, v) {
return acc + v.length
}, 0);
assert.equal(sum1, 4);

var sum2 = Hash.reduce(ref2, function (acc, v) {
var sum2 = Hash.reduce(ref, function (acc, v) {
return acc + v.length
}, 0);
assert.equal(sum2, 4);

var ref3 = { a : 5, b : 2, c : 7, 1337 : 'leet' };
var f1 = Hash.filter(ref3, function (v, k) { return v > 5 || k > 5 });
assert.equal(Object.keys(f1).sort().join(' '), '1337 c');

assert.equal(Hash(ref3).keys.sort().join(' '), '1337 a b c');
assert.equal(Hash(ref3).values.sort().join(' '), '2 5 7 leet');

assert.equal(Hash(ref3).length, 4);
};

exports['filter and values'] = function (assert) {
var ref = { a : 5, b : 2, c : 7, 1337 : 'leet' };
var hash = Hash(ref).filter(function (v, k) { return v > 5 || k > 5 });
assert.equal(hash.keys.sort().join(' '), '1337 c');
assert.equal(Hash(ref).keys.sort().join(' '), '1337 a b c');
assert.equal(Hash(ref).values.sort().join(' '), '2 5 7 leet');
assert.equal(Hash(ref).length, 4);
};

exports.concat = function (assert) {
var ref1 = { a : 1, b : 2 };
var ref2 = { foo : 100, bar : 200 };

assert.equal(
Object.keys(Hash.concat([ ref1, ref2 ])).sort().join(' '),
'a b bar foo'
);

var hash3 = Hash(ref1).clone;
var updated = hash3.update({ c : 3, a : 0 });
};

exports.update = function (assert) {
var ref = { a : 1, b : 2 };
var hash = Hash(ref).clone;
var updated = hash.update({ c : 3, a : 0 });
assert.equal(updated.keys.sort().join(' '), 'a b c');
assert.equal(updated.items.a, 0);
assert.equal(updated.items.b, 2);
assert.equal(updated.items.c, 3);
assert.equal(Object.keys(hash3.items).sort().join(' '), 'a b c');
assert.equal(hash3.items.a, 0);
assert.equal(hash3.items.b, 2);
assert.equal(hash3.items.c, 3);
assert.equal(Object.keys(ref1).sort().join(' '), 'a b');
assert.equal(ref1.a, 1);
assert.equal(ref1.b, 2);

var merged = hash3.merge({ c : 3.14, d : 4 });
assert.equal(merged.items.a, 0);
assert.equal(hash.keys.sort().join(' '), 'a b c');
assert.equal(hash.items.a, 0);
assert.equal(hash.items.b, 2);
assert.equal(hash.items.c, 3);
assert.equal(Object.keys(ref).sort().join(' '), 'a b');
assert.equal(ref.a, 1);
assert.equal(ref.b, 2);
};

exports.merge = function (assert) {
var ref = { a : 1, b : 2 };
var hash = Hash(ref).clone;
var merged = hash.merge({ c : 3.14, d : 4 });
assert.equal(merged.items.a, 1);
assert.equal(merged.items.b, 2);
assert.equal(merged.items.c, 3.14);
assert.equal(merged.items.d, 4);
assert.equal(merged.length, 4);
assert.equal(hash3.items.c, 3);
assert.equal(hash3.length, 3);

var extracted = hash3.extract(['a','b']);
assert.equal(hash.keys.sort().join(' '), 'a b');
assert.equal(hash.length, 2);
};

exports.extract = function (assert) {
var hash = Hash({ a : 1, b : 2, c : 3 }).clone;
var extracted = hash.extract(['a','b']);
assert.equal(extracted.length, 2);
assert.equal(extracted.items.a, 0);
assert.equal(extracted.items.a, 1);
assert.equal(extracted.items.b, 2);

assert.equal(hash3.valuesAt(['a','b']).join(' '), '0 2');
assert.equal(hash.valuesAt(['a','b']).join(' '), '1 2');
};

0 comments on commit 92046a4

Please sign in to comment.