Skip to content

Commit

Permalink
implemented rpop
Browse files Browse the repository at this point in the history
  • Loading branch information
fent committed Feb 7, 2012
1 parent a931934 commit 709ed76
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/eventvat.js
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,14 @@
// Remove and get the last element in a list
//
EventVat.prototype.rpop = function(key) {
throw new Error('Not implemented.');
if (this.type(key) === 'list') {
var value = this.hash[key].value.pop();
this.emit('rpop ' + key, value);
this.emit('rpop', key, value);
return value;
} else {
return false;
}
};

//
Expand Down
21 changes: 21 additions & 0 deletions test/events-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1129,5 +1129,26 @@ module.exports = simpleEvents({
vat.die();
test.done();
},
'Raise event on `rpop` method invokation': function(test) {

var vat = EventVat();

vat.on('rpop', function(key, value) {
test.equal(key, 'mylist');
test.equal(value, 'two');
});

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

vat.rpush('mylist', 'one');
vat.rpush('mylist', 'two');
vat.rpop('mylist');

test.expect(3);
vat.die();
test.done();
},

});
17 changes: 17 additions & 0 deletions test/methods-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,23 @@ this.methodSuite = {
test.equal(vat.lindex('mylist', 0), 'two');
test.equal(vat.lindex('mylist', 1), false);

vat.die();
test.done();
},
'Invoke `rpop` method and report return value and stored values': function(test) {

var vat = EventVat();

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

test.equal(vat.lindex('mylist', 0), 'one');
test.equal(vat.lindex('mylist', 1), 'two');
test.equal(vat.rpop('mylist'), 'two');

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

vat.die();
test.done();
},
Expand Down

0 comments on commit 709ed76

Please sign in to comment.