test and code, code works, test fails.
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
#Here's the spec: | |
module Antlers | |
describe Menu do | |
context "display a Menu from a text file" do | |
before (:each) do | |
@messenger = mock("messenger").as_null_object | |
@menu = Menu.new("Main Menu") | |
end #before | |
it "should have a title with title named Title" do | |
@menu.title.should == "Main Menu" | |
end | |
it "should expect a text file that establishes choices" do | |
@menu.txtfile.should == @menu.homedir + "mainmenu.txt" | |
end | |
it "should throw an error if asked to display a menu that does not exist" do | |
menu2 = Menu.new("Title_Menu") | |
menu2.display_menu(@messenger).should == "#{menu2.txtfile} does not exist!" | |
end | |
it "should load the contents of the text file into the action list" do | |
@menu.action_list.empty?.should == false | |
@menu.display_menu(@messenger) | |
end | |
end #new menu context | |
end #menu | |
end #module | |
#And here's the code: | |
module Antlers | |
class Menu | |
attr_accessor :title, :txtfile, :action_list | |
attr_reader :homedir | |
def initialize(title) | |
@title = title | |
@action_list = {} | |
@homedir = File.join(File.dirname(__FILE__), "/../txt_files/") | |
@txtfile = @homedir + @title.downcase.strip.gsub(" ", "") + ".txt" | |
end | |
def display_menu(messenger) | |
return "#{self.txtfile} does not exist!" if !File.exist?(self.txtfile) | |
messenger.puts " == " + self.title + " == " | |
CSV.foreach(self.txtfile, :col_sep => ',', :row_sep => :auto) do |row| | |
@action_list[row[0]] = row[1..3] | |
end | |
messenger.puts "Choice Action" | |
messenger.puts "====== ======" | |
@action_list.each do | choice, action | | |
messenger.puts " #{choice} #{action[0]} " | |
end | |
end | |
end | |
end #This modification to the Antlers module. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment