require "spec_helper" module CLI describe App do before do @app = App.new("hello") do name "Hello" version "0.0.1" option "-m", "--message MESSAGE" do |msg| options[:msg] = msg end action "hi" do puts "Hi!" end default do puts options[:msg] || "Hello, World!" end end end it "has a binary name" do @app.binary.should eq("hello") end it "has a name" do @app.name.should eq("Hello") end it "has a version" do @app.version.should eq("0.0.1") end it "runs an action" do $stdout.should_receive(:puts).with("Hi!") @app.run("hi") end it "fails when action is not found" do lambda do @app.run("bad") end.should raise_error(ArgumentError, "action 'bad' not found") end it "runs the default action" do $stdout.should_receive(:puts).with("Hello, World!") @app.run("default") end describe "#run!" do describe "when arguments are empty" do it "runs the default action" do @app.should_receive(:run).with("default") @app.run!([]) end end describe "when 1st argument is a valid action name" do it "runs the action" do @app.should_receive(:run).with("hi") @app.run!(%w( hi )) end end describe "when 1st argument is an invalid action name" do it "prints error message" do $stdout.should_receive(:puts).with("Action 'bad' not found") @app.run!(%w( bad )) end end describe "with a valid option" do it "sets the option and runs the default action" do $stdout.should_receive(:puts).with("Goodmorning!") @app.run!(%w( -m Goodmorning! )) end end end end describe App, "a simple app" do before do @app = App.new("hello") do default do puts args[0] end end end it "always runs the default action" do $stdout.should_receive(:puts).with("Hello, World!") @app.run!(["Hello, World!"]) $stdout.should_receive(:puts).with("1") @app.run!(%w(1 2 3)) end end describe App, "blank app" do describe "run!" do it "should fail" do @app = App.new("blank") do end $stdout.should_receive(:puts).with("Error: you have to define 'default' action") @app.run!([]) end end end end