Skip to content

Instantly share code, notes, and snippets.

@vosechu
Created May 27, 2013 19:30
Show Gist options
  • Save vosechu/5658705 to your computer and use it in GitHub Desktop.
Save vosechu/5658705 to your computer and use it in GitHub Desktop.
Test-first code to create an interview question object
require 'minitest/autorun'
class TestHQ9F < MiniTest::Unit::TestCase
def test_h
# assert_output takes a block. Anything that is output inside the
# block is captured and matched.
assert_output "Hello World!\n" do
h
end
end
def test_99
# When the output is really long, sometimes we want to capture it
# and use assert_match.
out, err = capture_io do
HQ9F._99(99)
end
assert_match /99 bottles of beer on the wall,\n/, out
end
def test_f
out, err = capture_io do
hq9f = HQ9F.new
hq9f.f(1, 100, {3 => 'Chunky', 7 => 'Bacon', 17 => 'HeartPCS'})
end
# If we want to reason about individual lines we need to split the
# output into lines. String#split will split on spaces, tabs, or
# newlines if there is no argument given.
result = out.split()
assert_equal '1', result.first
assert_equal 'Chunky', result[3-1]
assert_equal 'HeartPCS', result[17-1]
end
def test_each
hq9f = HQ9F.new
10.times do |i|
hq9f << i
end
hq9f.each_with_index do |k,v|
assert_equal k,v
end
end
def test_push_pop
hq9f = HQ9F.new
hq9f.push_lambda lambda { puts 'PCS Rocks' }
assert_output "PCS Rocks\n" do
hq9f.pop_lambda
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment