Skip to content

Instantly share code, notes, and snippets.

@riffraff
Forked from ryanb/quiz.rb
Created May 16, 2009 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riffraff/112663 to your computer and use it in GitHub Desktop.
Save riffraff/112663 to your computer and use it in GitHub Desktop.
# use a single string object as IO, enforces ordering, use srand to avoid randomness and stubbing/subclassing
class Quiz
def initialize(input = STDIN, output = STDOUT)
@input = input
@output = output
end
def problem
first = rand(10)
second = rand(10)
@output.puts "What is #{first} + #{second}?"
answer = @input.gets
puts answer
if answer.to_i == first + second
@output.puts "Correct!"
else
@output.puts "Incorrect!"
end
end
end
require "test/unit"
class QuizTest < Test::Unit::TestCase
def mkIO(s)
def s.puts(str)
def self.gets
self
end
concat(str)
end
s
end
def test_correct
srand(10)
io=mkIO("13\n")
Quiz.new(io,io).problem
assert_equal("13\nWhat is 9 + 4?Correct!",io)
end
def test_incorrect
srand(10)
io=mkIO("14\n")
Quiz.new(io,io).problem
assert_equal("14\nWhat is 9 + 4?Incorrect!",io)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment