Created
May 31, 2013 12:23
-
-
Save mikestok/5684646 to your computer and use it in GitHub Desktop.
Simple (incomplete) stack with a command pop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class Stack | |
def initialize(container_class=Array) | |
@container = container_class.new | |
end | |
def empty? | |
@container.empty? | |
end | |
def push(value) | |
@container.push value | |
return | |
end | |
def pop | |
value = @container.pop | |
yield value if block_given? | |
return | |
end | |
end | |
require 'minitest/autorun' | |
describe Stack do | |
before do | |
@stack = Stack.new | |
end | |
describe "when created" do | |
it "must be empty" do | |
assert_empty @stack | |
end | |
end | |
describe "when a value is pushed onto the stack" do | |
it "must not be empty" do | |
@stack.push 'foo' | |
refute_empty @stack | |
end | |
it "must be able to pop a value off the stack" do | |
pushed_value = "Foo" | |
@stack.push pushed_value | |
@stack.pop { |popped_value| | |
assert_same pushed_value, popped_value | |
} | |
assert_empty @stack | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment