Skip to content

Commit

Permalink
implemented rpoplpush
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Feb 7, 2012
1 parent 709ed76 commit cd93db4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/eventvat.js
Expand Up @@ -1010,7 +1010,27 @@
// Remove the last element in a list, append it to another list and return it
//
EventVat.prototype.rpoplpush = function(source, destination) {
throw new Error('Not implemented.');
if (this.type(source) === 'list') {
var dtype = this.type(destination);
var dest;

if (dtype === 'none') {
dest = [];
this.hash[destination] = { value: dest, type: 'list' };
} else if (dtype !== 'list') {
return false;
} else {
dest = this.hash[destination].value;
}

var value = this.hash[source].value.pop();
dest.unshift(value);
this.emit('rpoplpush ' + source, destination, value);
this.emit('rpoplpush', source, destination, value);
return value;
} else {
return false;
}
};

//
Expand Down
25 changes: 25 additions & 0 deletions test/events-test.js
Expand Up @@ -1150,5 +1150,30 @@ module.exports = simpleEvents({
vat.die();
test.done();
},
'Raise event on `rpoplpush` method invokation': function(test) {

var vat = EventVat();

vat.on('rpoplpush', function(source, destination, value) {
test.equal(source, 'mylist');
test.equal(destination, 'mylist2');
test.equal(value, 'two');
});

vat.on('rpoplpush mylist', function(destination, value) {
test.equal(destination, 'mylist2');
test.equal(value, 'two');
});

vat.rpush('mylist', 'one');
vat.rpush('mylist', 'two');
vat.rpush('mylist2', 'three');
vat.rpoplpush('mylist', 'mylist2');

test.expect(5);
vat.die();
test.done();

},

});
24 changes: 24 additions & 0 deletions test/methods-test.js
Expand Up @@ -841,4 +841,28 @@ this.methodSuite = {
vat.die();
test.done();
},
'Invoke `rpoplpush` method and report return value and stored values': function(test) {

var vat = EventVat();

vat.rpush('mylist', 'one');
vat.rpush('mylist', 'two');
vat.rpush('mylist2', 'three');

test.equal(vat.lindex('mylist', 0), 'one');
test.equal(vat.lindex('mylist', 1), 'two');
test.equal(vat.lindex('mylist2', 0), 'three');
test.equal(vat.lindex('mylist2', 1), false);

test.equal(vat.rpoplpush('mylist', 'mylist2'), 'two');

test.equal(vat.lindex('mylist', 0), 'one');
test.equal(vat.lindex('mylist', 1), false);
test.equal(vat.lindex('mylist2', 0), 'two');
test.equal(vat.lindex('mylist2', 1), 'three');

vat.die();
test.done();

},
};

0 comments on commit cd93db4

Please sign in to comment.