-
-
Save riffraff/112663 to your computer and use it in GitHub Desktop.
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
# 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