Skip to content

Commit

Permalink
subtract command
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminplee committed Mar 25, 2010
1 parent 25e7e82 commit 4778820
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions QuickPietJS/lib/commands.js
Expand Up @@ -45,6 +45,14 @@ Commands = {

stack.push(stack.pop() + stack.pop())
},

subtract : function(stack) {
this._enforce_non_empty_stack(stack, 'SUBTRACT')

var top = stack.pop()
var second_top = stack.pop()
stack.push(second_top - top)
},

_enforce_non_empty_stack : function(stack, method_name) {
if(stack.length == 0) {
Expand Down
23 changes: 22 additions & 1 deletion QuickPietJS/spec/unit/commands.spec.js
Expand Up @@ -137,14 +137,15 @@ describe 'Commands'
describe 'add'
before_each
stack.push(100)
stack.push(5)
stack.push(10)
end
it 'should pop top 2 values and push result of their addition'
Commands.add(stack)
stack.should.eql [15]
stack.should.eql [100, 15]
end
it 'should error if the stack is empty'
Expand All @@ -159,7 +160,27 @@ describe 'Commands'
end
describe 'subtract'
before_each
stack.push(10)
stack.push(200)
stack.push(30)
end
it 'should pop top 2 values and push result of their subtraction (second top - top)'
Commands.subtract(stack)
stack.should.eql [10, 170]
end
it 'should error if the stack is empty'
stack = []
-{ Commands.subtract(stack) }.should.throw_error EvalError, 'Can not SUBTRACT from empty stack'
end
it 'should return undefined'
Commands.subtract(stack).should.eql undefined
end
end
describe 'multiply'
Expand Down

0 comments on commit 4778820

Please sign in to comment.