Skip to content

Instantly share code, notes, and snippets.

@idlehands
Forked from dbc-challenges/card.rb
Created October 24, 2012 23:29
Show Gist options
  • Save idlehands/3949585 to your computer and use it in GitHub Desktop.
Save idlehands/3949585 to your computer and use it in GitHub Desktop.
FlashCardinator
class Card
attr_reader :term, :definition
def initialize(term, definition)
@term = term
@definition = definition
end
def to_s
"#{term}: #{definition}"
end
end
equire 'SimpleCov'
SimpleCov.start
require './card'
describe Card do
context "#initialize" do
let(:card) { Card.new('test this!', 'happy happy happy')}
it "has a term" do
card.term.should_not be_nil
end
it "has a definition" do
card.definition.should_not be_nil
end
end
end
require './card'
class Deck < Array
attr_accessor :current_card
def initialize(cards = from_file)
cards.each do |card|
self << card
end
@current_card = self.first
end
def from_file(file_name = 'flashcards_data.txt')
temp = []
data = File.open(file_name, "r")
data_string = data.read.to_s
lines = data_string.split("\n")
lines.map! { |line| line.split("\t")}
lines.each do |line|
temp << Card.new(line[0], line[1])
end
temp
end
def set_current_card_to_next
current_index = self.index(@current_card)
next_index = current_index + 1
@current_card = self[next_index]
end
def end_of_deck?
@current_card == self[-1]
end
end
require 'SimpleCov'
SimpleCov.start
require './deck'
describe Deck do
describe "#initialize" do
it "should set the current card to the first card in the deck" do
card_mock1 = mock("Card", :term => "test1", :definition => "a way of stuff1")
card_mock2 = mock("Card", :term => "test2", :definition => "a way of stuff2")
deck_initialize_tester = Deck.new([card_mock1, card_mock2])
deck_initialize_tester.current_card.should eq(card_mock1)
end
end
describe "#from_file" do
it "should pass without a file_name" do
deck = Deck.new
deck.all? {|card| card.is_a?(Card)}.should be_true
end
end
describe "#next_card" do
it "current card should move on to the next card" do
card_mock1 = mock("Card", :term => "test1", :definition => "a way of stuff1")
card_mock2 = mock("Card", :term => "test2", :definition => "a way of stuff2")
deck_next_card_tester = Deck.new([card_mock1, card_mock2])
expect { deck_next_card_tester.set_current_card_to_next }.to change{ deck_next_card_tester.current_card }.from(card_mock1).to(card_mock2)
end
end
describe '#end_of_deck?' do
it "should know when it has reached the end of the deck" do
card_mock1 = mock("Card", :term => "test1", :definition => "a way of stuff1")
card_mock2 = mock("Card", :term => "test2", :definition => "a way of stuff2")
deck_end_of_deck_tester = Deck.new([card_mock1, card_mock2])
deck_end_of_deck_tester.set_current_card_to_next
deck_end_of_deck_tester.end_of_deck?.should == true
end
end
end
require './deck'
class Game
attr_accessor :deck, :output
attr_reader :current_card
attr_writer :input
def initialize
@deck = Deck.new
@input = nil
@output = nil
end
def input=(input)
raise "invalid_string" if input.class != String
@input = input
end
def current_card
@deck.current_card
end
def current_term
current_card.term
end
def current_definition
current_card.definition
end
end
require 'SimpleCov'
SimpleCov.start
require './game'
describe Game do
describe "#initialize" do
game = Game.new
it "should instantiate a deck" do
game.deck.should be_instance_of(Deck)
end
end
it "receives input (string) for matching" do
subject.input = "test"
end
describe '#current_definition' do
it "provide a definition from current card" do
game = Game.new
game.current_definition.should eq (game.current_card.definition)
end
end
end
require './game'
class Memorization < Game
def self.start_up
factory_temp = self.new
factory_temp.set_display_to_current_card
factory_temp
end
def set_display_to_current_card
@output = current_card.to_s
end
def quit_at_end_of_deck
@output = "" if @deck.end_of_deck?
end
def set_current_card_to_next
@deck.set_current_card_to_next
end
def process_user_input
return @output='' if @input == 'quit'
quit_at_end_of_deck
set_current_card_to_next
set_display_to_current_card
end
end
require 'SimpleCov'
SimpleCov.start
require './memorization.rb'
describe 'Memorization' do
it "should set the output to the first card when initialized" do
memorization = Memorization.start_up
memorization.output.should eq("#{memorization.current_term}: #{memorization.current_definition}")
end
it "provides the term and definition" do
memorization = Memorization.start_up
memorization.set_display_to_current_card
memorization.output.should == "#{memorization.current_term}: #{memorization.current_definition}"
end
it "it ends session when it is at the end of the deck" do
memorization = Memorization.start_up
memorization.deck.clear
card_mock1 = mock("Card", :term => "test1", :definition => "a way of stuff1")
memorization.deck << card_mock1
memorization.deck.current_card = card_mock1
memorization.process_user_input
memorization.output.should == ""
end
it "moves on when the user is ready" do
memorization = Memorization.start_up
memorization.input = ""
memorization.deck.should_receive(:set_current_card_to_next)
memorization.process_user_input
end
it "ends when the user wants to quit" do
memorization = Memorization.start_up
memorization.input = "quit"
memorization.process_user_input
memorization.output.should == ""
end
end
require './game'
class Quiz < Game
def self.start_up
factory_temp = self.new
factory_temp.provide_definition
factory_temp.initialize_fail_count
factory_temp
end
def initialize_fail_count
@fail_count = 0
end
def quit_at_end_of_deck
@output = "" if @deck.end_of_deck?
end
def provide_definition
@output = current_definition
end
def correct_match?
@input == current_term
end
def provide_the_correct_answer
@output = "CORRECT ANSWER: " + current_card.to_s
end
def set_current_card_to_next
@deck.set_current_card_to_next
provide_definition
end
def process_user_input
return @output = "" if @input == "quit"
return if @output == ""
if correct_match?
@fail_count = 0
@deck.end_of_deck? ? quit_at_end_of_deck : set_current_card_to_next
elsif @fail_count == 2
provide_the_correct_answer
@fail_count += 1
elsif @fail_count == 3
@fail_count = 0
@deck.end_of_deck? ? quit_at_end_of_deck : set_current_card_to_next
elsif @fail_count < 2
@fail_count += 1
end
end
end
require 'SimpleCov'
SimpleCov.start
require './quiz'
require './game'
describe 'Quiz' do
it "provides the definition" do
quiz = Quiz.start_up
quiz.output.should == quiz.current_definition
end
it "checks guess against actual term of current card" do
card_mock1 = mock("Card", :term => "test1", :definition => "a way of stuff1")
card_mock2 = mock("Card", :term => "test2", :definition => "a way of stuff2")
quiz_match_tester = Quiz.start_up
quiz_match_tester.deck.clear
quiz_match_tester.deck << card_mock1
quiz_match_tester.deck << card_mock2
quiz_match_tester.deck.current_card = card_mock1
quiz_match_tester.input = "test1"
quiz_match_tester.correct_match?.should == true
end
it "ends when the user wants to quit" do
quiz = Quiz.start_up
quiz.input = "quit"
quiz.process_user_input
quiz.output.should == ""
end
context "wrong answer" do
#not doing this
it "should provide the correct answer after the third attempt" do
quiz_fail_counter_tester = Quiz.start_up
quiz_fail_counter_tester.output = "I don't care what is here"
test_state = quiz_fail_counter_tester.output
quiz_fail_counter_tester.input = "failedword"
quiz_fail_counter_tester.process_user_input
quiz_fail_counter_tester.output.should == test_state
quiz_fail_counter_tester.input = "failedword"
quiz_fail_counter_tester.process_user_input
quiz_fail_counter_tester.output.should == test_state
quiz_fail_counter_tester.input = "failedword"
quiz_fail_counter_tester.process_user_input
quiz_fail_counter_tester.output.should_not == test_state
end
#need to understand why its not providing the proper output after the definition
it "moves on to the next card after giving answer after super-fail" do
quiz_move_on = Quiz.start_up
quiz_move_on.deck.should_receive(:set_current_card_to_next)
3.times do
quiz_move_on.input = "failedword"
quiz_move_on.process_user_input
end
quiz_move_on.input = "okay, i got it wrong... next card please..."
quiz_move_on.process_user_input
end
end
context "right answer" do
#not doing this
it "moves to the next card when the guess is correct" do
quiz_move_on = Quiz.start_up
quiz_move_on.input = "a current guess"
quiz_move_on.stub!(:current_term).and_return("a current guess")
quiz_move_on.deck.should_receive(:set_current_card_to_next)
quiz_move_on.process_user_input
end
end
it "it ends when it is at the end of the deck" do
quiz_quit = Quiz.start_up
quiz_quit.deck.should_receive(:end_of_deck?).and_return(true)
quiz_quit.input = "hey. last card guys"
quiz_quit.process_user_input
quiz_quit.output.should == ""
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment