Skip to content

Instantly share code, notes, and snippets.

@qrprat77
Created August 31, 2011 08:18
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save qrprat77/1183066 to your computer and use it in GitHub Desktop.
test and code, code works, test fails.
#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