Refactor & fix specs

This commit is contained in:
Rémy Coutable 2011-09-23 00:22:44 +02:00
parent 916613c027
commit 1cd669bf60
10 changed files with 190 additions and 206 deletions

View File

@ -6,8 +6,6 @@ describe Guard::DslDescriber do
user_config_path = File.expand_path(File.join('~', '.guard.rb'))
File.stub(:exist?).with(user_config_path) { false }
end
subject { described_class }
it 'should evaluate a Guardfile and create the right structure' do
mixed_guardfile_string = <<-GUARD
@ -28,13 +26,13 @@ group "b" do
end
GUARD
subject.evaluate_guardfile(:guardfile_contents => mixed_guardfile_string)
described_class.evaluate_guardfile(:guardfile_contents => mixed_guardfile_string)
subject.guardfile_structure.should == [
described_class.guardfile_structure.should == [
{ :guards => [ { :name => 'test', :options => { :a => :b } } ] },
{ :group => :a, :guards => [ { :name => 'test', :options => {} } ] },
{ :group => :b, :guards => [ { :name => 'another', :options => {} } ] }
]
end
end

View File

@ -2,7 +2,7 @@ require 'spec_helper'
require 'guard/guard'
describe Guard::Dsl do
subject { described_class }
class Guard::Dummy < Guard::Guard; end
before(:each) do
@ -24,24 +24,24 @@ describe Guard::Dsl do
it "should use a string for initializing" do
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
subject.guardfile_contents.should == valid_guardfile_string
lambda { described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
described_class.guardfile_contents.should == valid_guardfile_string
end
it "should use a given file over the default loc" do
fake_guardfile('/abc/Guardfile', "guard :foo")
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
subject.guardfile_contents.should == "guard :foo"
lambda { described_class.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
described_class.guardfile_contents.should == "guard :foo"
end
it "should use a default file if no other options are given" do
fake_guardfile(@local_guardfile_path, "guard :bar")
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile }.should_not raise_error
subject.guardfile_contents.should == "guard :bar"
lambda { described_class.evaluate_guardfile }.should_not raise_error
described_class.guardfile_contents.should == "guard :bar"
end
it "should use a string over any other method" do
@ -49,8 +49,8 @@ describe Guard::Dsl do
fake_guardfile(@local_guardfile_path, "guard :bar")
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
subject.guardfile_contents.should == valid_guardfile_string
lambda { described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
described_class.guardfile_contents.should == valid_guardfile_string
end
it "should use the given Guardfile over default Guardfile" do
@ -58,31 +58,31 @@ describe Guard::Dsl do
fake_guardfile(@local_guardfile_path, "guard :bar")
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
subject.guardfile_contents.should == "guard :foo"
lambda { described_class.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
described_class.guardfile_contents.should == "guard :foo"
end
it 'should append the user config file if present' do
fake_guardfile('/abc/Guardfile', "guard :foo")
fake_guardfile(@user_config_path, "guard :bar")
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
subject.guardfile_contents_with_user_config.should == "guard :foo\nguard :bar"
lambda { described_class.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
described_class.guardfile_contents_with_user_config.should == "guard :foo\nguard :bar"
end
end
it "displays an error message when no Guardfile is found" do
subject.stub(:guardfile_default_path).and_return("no_guardfile_here")
described_class.stub(:guardfile_default_path).and_return("no_guardfile_here")
Guard::UI.should_receive(:error).with("No Guardfile found, please create one with `guard init`.")
lambda { subject.evaluate_guardfile }.should raise_error
lambda { described_class.evaluate_guardfile }.should raise_error
end
it "displays an error message when no guard are defined in Guardfile" do
::Guard::Dsl.stub!(:instance_eval_guardfile)
::Guard.stub!(:guards).and_return([])
Guard::UI.should_receive(:error)
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
end
describe "correctly reads data from its valid data source" do
@ -90,22 +90,22 @@ describe Guard::Dsl do
disable_user_config
it "reads correctly from a string" do
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
subject.guardfile_contents.should == valid_guardfile_string
lambda { described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
described_class.guardfile_contents.should == valid_guardfile_string
end
it "reads correctly from a Guardfile" do
fake_guardfile('/abc/Guardfile', "guard :foo" )
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
subject.guardfile_contents.should == "guard :foo"
lambda { described_class.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
described_class.guardfile_contents.should == "guard :foo"
end
it "reads correctly from a Guardfile" do
fake_guardfile(File.join(Dir.pwd, 'Guardfile'), valid_guardfile_string)
lambda { subject.evaluate_guardfile }.should_not raise_error
subject.guardfile_contents.should == valid_guardfile_string
lambda { described_class.evaluate_guardfile }.should_not raise_error
described_class.guardfile_contents.should == valid_guardfile_string
end
end
@ -117,14 +117,14 @@ describe Guard::Dsl do
File.stub!(:read).with('/def/Guardfile') { raise Errno::EACCES.new("permission error") }
Guard::UI.should_receive(:error).with(/^Error reading file/)
lambda { subject.evaluate_guardfile(:guardfile => '/def/Guardfile') }.should raise_error
lambda { described_class.evaluate_guardfile(:guardfile => '/def/Guardfile') }.should raise_error
end
it "raises error when given Guardfile doesn't exist" do
File.stub!(:exist?).with('/def/Guardfile') { false }
Guard::UI.should_receive(:error).with(/No Guardfile exists at/)
lambda { subject.evaluate_guardfile(:guardfile => '/def/Guardfile') }.should raise_error
lambda { described_class.evaluate_guardfile(:guardfile => '/def/Guardfile') }.should raise_error
end
it "raises error when resorting to use default, finds no default" do
@ -132,24 +132,24 @@ describe Guard::Dsl do
File.stub!(:exist?).with(@home_guardfile_path) { false }
Guard::UI.should_receive(:error).with("No Guardfile found, please create one with `guard init`.")
lambda { subject.evaluate_guardfile }.should raise_error
lambda { described_class.evaluate_guardfile }.should raise_error
end
it "raises error when guardfile_content ends up empty or nil" do
Guard::UI.should_receive(:error).with(/The command file/)
lambda { subject.evaluate_guardfile(:guardfile_contents => "") }.should raise_error
lambda { described_class.evaluate_guardfile(:guardfile_contents => "") }.should raise_error
end
it "doesn't raise error when guardfile_content is nil (skipped)" do
Guard::UI.should_not_receive(:error)
lambda { subject.evaluate_guardfile(:guardfile_contents => nil) }.should_not raise_error
lambda { described_class.evaluate_guardfile(:guardfile_contents => nil) }.should_not raise_error
end
end
it "displays an error message when Guardfile is not valid" do
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:/)
lambda { subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error
lambda { described_class.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error
end
describe ".reevaluate_guardfile" do
@ -157,10 +157,10 @@ describe Guard::Dsl do
it "resets already definded guards before calling evaluate_guardfile" do
Guard::Notifier.turn_off
subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string)
described_class.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string)
::Guard.guards.should_not be_empty
::Guard::Dsl.should_receive(:evaluate_guardfile)
subject.reevaluate_guardfile
described_class.reevaluate_guardfile
::Guard.guards.should be_empty
end
end
@ -173,14 +173,14 @@ describe Guard::Dsl do
context "when there is a local Guardfile" do
it "returns the path to the local Guardfile" do
File.stub(:exist?).with(local_path).and_return(true)
subject.guardfile_default_path.should == local_path
described_class.guardfile_default_path.should == local_path
end
end
context "when there is a Guardfile in the user's home directory" do
it "returns the path to the user Guardfile" do
File.stub(:exist?).with(user_path).and_return(true)
subject.guardfile_default_path.should == user_path
described_class.guardfile_default_path.should == user_path
end
end
@ -188,34 +188,34 @@ describe Guard::Dsl do
it "returns the path to the local Guardfile" do
File.stub(:exist?).with(local_path).and_return(true)
File.stub(:exist?).with(user_path).and_return(true)
subject.guardfile_default_path.should == local_path
described_class.guardfile_default_path.should == local_path
end
end
end
describe ".guardfile_include?" do
it "detects a guard specified by a string with double quotes" do
subject.stub(:guardfile_contents => 'guard "test" {watch("c")}')
described_class.stub(:guardfile_contents => 'guard "test" {watch("c")}')
subject.guardfile_include?('test').should be_true
described_class.guardfile_include?('test').should be_true
end
it "detects a guard specified by a string with single quote" do
subject.stub(:guardfile_contents => 'guard \'test\' {watch("c")}')
described_class.stub(:guardfile_contents => 'guard \'test\' {watch("c")}')
subject.guardfile_include?('test').should be_true
described_class.guardfile_include?('test').should be_true
end
it "detects a guard specified by a symbol" do
subject.stub(:guardfile_contents => 'guard :test {watch("c")}')
described_class.stub(:guardfile_contents => 'guard :test {watch("c")}')
subject.guardfile_include?('test').should be_true
described_class.guardfile_include?('test').should be_true
end
it "detects a guard wrapped in parentheses" do
subject.stub(:guardfile_contents => 'guard(:test) {watch("c")}')
described_class.stub(:guardfile_contents => 'guard(:test) {watch("c")}')
subject.guardfile_include?('test').should be_true
described_class.guardfile_include?('test').should be_true
end
end
@ -226,7 +226,7 @@ describe Guard::Dsl do
::Guard.stub!(:listener).and_return(mock('Listener'))
::Guard.listener.should_receive(:ignore_paths).and_return(ignore_paths = ['faz'])
subject.evaluate_guardfile(:guardfile_contents => "ignore_paths 'foo', 'bar'")
described_class.evaluate_guardfile(:guardfile_contents => "ignore_paths 'foo', 'bar'")
ignore_paths.should == ['faz', 'foo', 'bar']
end
end
@ -238,18 +238,14 @@ describe Guard::Dsl do
::Guard.should_receive(:add_guard).with('pow', [], [], { :group => :default })
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :w })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
::Guard.groups.should eql [{ :name => :default, :options => {} }, { :name => :w, :options => {} }]
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end
it "evaluates only the specified symbol group" do
::Guard.should_receive(:add_guard).with('pow', [], [], { :group => :default })
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :w })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
::Guard.groups.should eql [{ :name => :default, :options => {} }, { :name => :w, :options => {} }]
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end
it "evaluates only the specified groups (with their options)" do
@ -258,18 +254,14 @@ describe Guard::Dsl do
::Guard.should_receive(:add_guard).with('ronn', [], [], { :group => :x })
::Guard.should_receive(:add_guard).with('less', [], [], { :group => :y })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y])
::Guard.groups.should eql [{ :name => :default, :options => {} }, { :name => :x, :options => { :halt_on_fail => true } }, { :name => :y, :options => {} }]
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y])
end
it "evaluates always guard outside any group (even when a group is given)" do
::Guard.should_receive(:add_guard).with('pow', [], [], { :group => :default })
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :w })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
::Guard.groups.should eql [{ :name => :default, :options => {} }, { :name => :w, :options => {} }]
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end
it "evaluates all groups when no group option is specified (with their options)" do
@ -279,10 +271,7 @@ describe Guard::Dsl do
::Guard.should_receive(:add_guard).with('ronn', [], [], { :group => :x })
::Guard.should_receive(:add_guard).with('less', [], [], { :group => :y })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
::Guard.groups.should eql [{ :name => :default, :options => {} }, { :name => :w, :options => {} }, { :name => :x, :options => { :halt_on_fail => true } }, { :name => :y, :options => {} }]
described_class.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
end
end
@ -292,31 +281,31 @@ describe Guard::Dsl do
it "loads a guard specified as a quoted string from the DSL" do
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard 'test'")
described_class.evaluate_guardfile(:guardfile_contents => "guard 'test'")
end
it "loads a guard specified as a double quoted string from the DSL" do
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => 'guard "test"')
described_class.evaluate_guardfile(:guardfile_contents => 'guard "test"')
end
it "loads a guard specified as a symbol from the DSL" do
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard :test")
described_class.evaluate_guardfile(:guardfile_contents => "guard :test")
end
it "loads a guard specified as a symbol and called with parens from the DSL" do
::Guard.should_receive(:add_guard).with('test', [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard(:test)")
described_class.evaluate_guardfile(:guardfile_contents => "guard(:test)")
end
it "receives options when specified, from normal arg" do
::Guard.should_receive(:add_guard).with('test', [], [], { :opt_a => 1, :opt_b => 'fancy', :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
described_class.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
end
end
@ -331,7 +320,7 @@ describe Guard::Dsl do
watchers[1].pattern.should == 'c'
watchers[1].action.should == nil
end
subject.evaluate_guardfile(:guardfile_contents => "
described_class.evaluate_guardfile(:guardfile_contents => "
guard :dummy do
watch('a') { 'b' }
watch('c')
@ -354,7 +343,7 @@ describe Guard::Dsl do
callbacks[1][:events].should == [:start_begin, :run_all_begin]
callbacks[1][:listener].should == MyCustomCallback
end
subject.evaluate_guardfile(:guardfile_contents => '
described_class.evaluate_guardfile(:guardfile_contents => '
guard :dummy do
callback(:start_end) { |guard_class, event, args| "#{guard_class} executed \'#{event}\' hook with #{args}!" }
callback(MyCustomCallback, [:start_begin, :run_all_begin])

View File

@ -2,48 +2,47 @@ require 'spec_helper'
require 'guard/guard'
describe Guard::Hook do
subject { Guard::Hook }
class Guard::Dummy < Guard::Guard; end
let(:guard_class) { ::Guard::Dummy }
let(:listener) { double('listener').as_null_object }
after { subject.reset_callbacks! }
after { described_class.reset_callbacks! }
context "--module methods--" do
before { subject.add_callback(listener, guard_class, :start_begin) }
describe "--module methods--" do
before { described_class.add_callback(listener, guard_class, :start_begin) }
describe ".add_callback" do
it "can add a single callback" do
subject.has_callback?(listener, guard_class, :start_begin).should be_true
described_class.has_callback?(listener, guard_class, :start_begin).should be_true
end
it "can add multiple callbacks" do
subject.add_callback(listener, guard_class, [:event1, :event2])
subject.has_callback?(listener, guard_class, :event1).should be_true
subject.has_callback?(listener, guard_class, :event2).should be_true
described_class.add_callback(listener, guard_class, [:event1, :event2])
described_class.has_callback?(listener, guard_class, :event1).should be_true
described_class.has_callback?(listener, guard_class, :event2).should be_true
end
end
describe ".notify" do
it "sends :call to the given Guard class's callbacks" do
listener.should_receive(:call).with(guard_class, :start_begin, "args")
subject.notify(guard_class, :start_begin, "args")
described_class.notify(guard_class, :start_begin, "args")
end
it "runs only the given callbacks" do
listener2 = double('listener2')
subject.add_callback(listener2, guard_class, :start_end)
described_class.add_callback(listener2, guard_class, :start_end)
listener2.should_not_receive(:call).with(guard_class, :start_end)
subject.notify(guard_class, :start_begin)
described_class.notify(guard_class, :start_begin)
end
it "runs callbacks only for the guard given" do
guard2_class = double('Guard::Dummy2').class
subject.add_callback(listener, guard2_class, :start_begin)
described_class.add_callback(listener, guard2_class, :start_begin)
listener.should_not_receive(:call).with(guard2_class, :start_begin)
subject.notify(guard_class, :start_begin)
described_class.notify(guard_class, :start_begin)
end
end
end

View File

@ -1,7 +1,6 @@
require 'spec_helper'
describe Guard::Listener do
subject { Guard::Listener }
describe ".select_and_init" do
before(:each) { @target_os = RbConfig::CONFIG['target_os'] }
@ -11,30 +10,30 @@ describe Guard::Listener do
RbConfig::CONFIG['target_os'] = 'darwin10.4.0'
Guard::Darwin.stub(:usable?).and_return(true)
Guard::Darwin.should_receive(:new)
subject.select_and_init
described_class.select_and_init
end
it "uses the Windows listener on Windows" do
RbConfig::CONFIG['target_os'] = 'mingw'
Guard::Windows.stub(:usable?).and_return(true)
Guard::Windows.should_receive(:new)
subject.select_and_init
described_class.select_and_init
end
it "uses the Linux listener on Linux" do
RbConfig::CONFIG['target_os'] = 'linux'
Guard::Linux.stub(:usable?).and_return(true)
Guard::Linux.should_receive(:new)
subject.select_and_init
described_class.select_and_init
end
it "forwards its arguments to the constructor" do
subject.stub!(:mac?).and_return(true)
described_class.stub!(:mac?).and_return(true)
Guard::Darwin.stub!(:usable?).and_return(true)
path, opts = 'path', { :foo => 23 }
Guard::Darwin.should_receive(:new).with(path, opts).and_return(true)
subject.select_and_init(path, opts)
described_class.select_and_init(path, opts)
end
end
@ -172,11 +171,11 @@ describe Guard::Listener do
describe "#ignore_paths" do
it "defaults to the default ignore paths" do
subject.new.ignore_paths.should == Guard::Listener::DEFAULT_IGNORE_PATHS
described_class.new.ignore_paths.should == Guard::Listener::DEFAULT_IGNORE_PATHS
end
it "can be added to via :ignore_paths option" do
listener = subject.new 'path', :ignore_paths => ['foo', 'bar']
listener = described_class.new 'path', :ignore_paths => ['foo', 'bar']
listener.ignore_paths.should include('foo', 'bar')
end
end
@ -197,4 +196,5 @@ describe Guard::Listener do
end
end
end
end

View File

@ -2,26 +2,26 @@ require 'spec_helper'
require 'guard/listeners/darwin'
describe Guard::Darwin do
subject { Guard::Darwin }
if windows?
it "isn't usable on windows" do
subject.should_not be_usable
described_class.should_not be_usable
end
end
if linux?
it "isn't usable on linux" do
subject.should_not be_usable
described_class.should_not be_usable
end
end
if mac? && Guard::Darwin.usable?
it "is usable on 10.6" do
subject.should be_usable
described_class.should be_usable
end
it_should_behave_like "a listener that reacts to #on_change"
it_should_behave_like "a listener scoped to a specific directory"
end
end

View File

@ -3,23 +3,22 @@ require 'fileutils'
require 'guard/listeners/linux'
describe Guard::Linux do
subject { Guard::Linux }
if mac?
it "isn't usable on 10.6" do
subject.should_not be_usable
described_class.should_not be_usable
end
end
if windows?
it "isn't usable on windows" do
subject.should_not be_usable
described_class.should_not be_usable
end
end
if linux? && Guard::Linux.usable?
it "is usable on linux" do
subject.should be_usable
described_class.should be_usable
end
describe "#start", :long_running => true do
@ -72,6 +71,6 @@ describe Guard::Linux do
stop
File.open(file, 'w') {|f| f.write('') }
end
end
end

View File

@ -2,8 +2,8 @@ require 'spec_helper'
require 'guard/listeners/polling'
describe Guard::Polling do
subject { Guard::Polling }
it_should_behave_like "a listener that reacts to #on_change"
it_should_behave_like "a listener scoped to a specific directory"
end

View File

@ -2,27 +2,26 @@ require 'spec_helper'
require 'guard/listeners/windows'
describe Guard::Windows do
subject { Guard::Windows }
if linux?
it "isn't usable on linux" do
subject.should_not be_usable
described_class.should_not be_usable
end
end
if mac?
it "isn't usable on Mac" do
subject.should_not be_usable
described_class.should_not be_usable
end
end
if windows?
it "is usable on Windows 2000 and later" do
subject.should be_usable
described_class.should be_usable
end
it_should_behave_like "a listener that reacts to #on_change"
it_should_behave_like "a listener scoped to a specific directory"
end
end

View File

@ -1,12 +1,11 @@
require 'spec_helper'
describe Guard::Notifier do
subject { Guard::Notifier }
describe ".turn_off" do
before do
ENV["GUARD_NOTIFY"] = 'true'
subject.turn_off
described_class.turn_off
end
it "disables the notifications" do
@ -28,10 +27,10 @@ describe Guard::Notifier do
end
it "loads the library and enables the notifications" do
subject.should_receive(:require).with('growl_notify').and_return true
described_class.should_receive(:require).with('growl_notify').and_return true
GrowlNotify.should_receive(:application_name).and_return ''
subject.turn_on
subject.should be_enabled
described_class.turn_on
described_class.should be_enabled
end
after do
@ -41,19 +40,19 @@ describe Guard::Notifier do
context "with the Growl library available" do
it "loads the library and enables the notifications" do
subject.should_receive(:require).with('growl_notify').and_raise LoadError
subject.should_receive(:require).with('growl').and_return true
subject.turn_on
subject.should be_enabled
described_class.should_receive(:require).with('growl_notify').and_raise LoadError
described_class.should_receive(:require).with('growl').and_return true
described_class.turn_on
described_class.should be_enabled
end
end
context "without the Growl library available" do
it "disables the notifications" do
subject.should_receive(:require).with('growl_notify').and_raise LoadError
subject.should_receive(:require).with('growl').and_raise LoadError
subject.turn_on
subject.should_not be_enabled
described_class.should_receive(:require).with('growl_notify').and_raise LoadError
described_class.should_receive(:require).with('growl').and_raise LoadError
described_class.turn_on
described_class.should_not be_enabled
end
end
end
@ -65,17 +64,17 @@ describe Guard::Notifier do
context "with the Libnotify library available" do
it "loads the library and enables the notifications" do
subject.should_receive(:require).with('libnotify').and_return true
subject.turn_on
subject.should be_enabled
described_class.should_receive(:require).with('libnotify').and_return true
described_class.turn_on
described_class.should be_enabled
end
end
context "without the Libnotify library available" do
it "disables the notifications" do
subject.should_receive(:require).with('libnotify').and_raise LoadError
subject.turn_on
subject.should_not be_enabled
described_class.should_receive(:require).with('libnotify').and_raise LoadError
described_class.turn_on
described_class.should_not be_enabled
end
end
end
@ -87,29 +86,29 @@ describe Guard::Notifier do
context "with the rb-notifu library available" do
it "loads the library and enables the notifications" do
subject.should_receive(:require).with('rb-notifu').and_return true
subject.turn_on
subject.should be_enabled
described_class.should_receive(:require).with('rb-notifu').and_return true
described_class.turn_on
described_class.should be_enabled
end
end
context "without the rb-notify library available" do
it "disables the notifications" do
subject.should_receive(:require).with('rb-notifu').and_raise LoadError
subject.turn_on
subject.should_not be_enabled
described_class.should_receive(:require).with('rb-notifu').and_raise LoadError
described_class.turn_on
described_class.should_not be_enabled
end
end
end
end
describe ".notify" do
before { subject.stub(:enabled?).and_return(true) }
before { described_class.stub(:enabled?).and_return(true) }
context "on Mac OS" do
before do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'darwin'
subject.stub(:require_growl)
described_class.stub(:require_growl)
end
context 'with growl gem' do
@ -128,13 +127,13 @@ describe Guard::Notifier do
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:name => "Guard"
)
subject.notify 'great', :title => 'Guard'
described_class.notify 'great', :title => 'Guard'
end
it "don't passes the notification to Growl if library is not available" do
Growl.should_not_receive(:notify)
subject.should_receive(:enabled?).and_return(true, false)
subject.notify 'great', :title => 'Guard'
described_class.should_receive(:enabled?).and_return(true, false)
described_class.notify 'great', :title => 'Guard'
end
it "allows additional notification options" do
@ -144,7 +143,7 @@ describe Guard::Notifier do
:name => "Guard",
:priority => 1
)
subject.notify 'great', :title => 'Guard', :priority => 1
described_class.notify 'great', :title => 'Guard', :priority => 1
end
it "allows to overwrite a default notification option" do
@ -153,7 +152,7 @@ describe Guard::Notifier do
:icon => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:name => "Guard-Cucumber"
)
subject.notify 'great', :title => 'Guard', :name => "Guard-Cucumber"
described_class.notify 'great', :title => 'Guard', :name => "Guard-Cucumber"
end
end
@ -174,13 +173,13 @@ describe Guard::Notifier do
:application_name => "Guard",
:description => 'great'
)
subject.notify 'great', :title => 'Guard'
described_class.notify 'great', :title => 'Guard'
end
it "don't passes the notification to Growl if library is not available" do
GrowlNotify.should_not_receive(:send_notification)
subject.should_receive(:enabled?).and_return(true, false)
subject.notify 'great', :title => 'Guard'
described_class.should_receive(:enabled?).and_return(true, false)
described_class.notify 'great', :title => 'Guard'
end
it "allows additional notification options" do
@ -191,7 +190,7 @@ describe Guard::Notifier do
:description => 'great',
:priority => 1
)
subject.notify 'great', :title => 'Guard', :priority => 1
described_class.notify 'great', :title => 'Guard', :priority => 1
end
it "throws out the application name since Guard should only use one Growl App Name while running" do
@ -201,7 +200,7 @@ describe Guard::Notifier do
:application_name => "Guard",
:description => 'great'
)
subject.notify 'great', :title => 'Guard', :name => "Guard-Cucumber"
described_class.notify 'great', :title => 'Guard', :name => "Guard-Cucumber"
end
end
end
@ -209,7 +208,7 @@ describe Guard::Notifier do
context "on Linux" do
before do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'linux'
subject.stub(:require_libnotify)
described_class.stub(:require_libnotify)
Object.send(:remove_const, :Libnotify) if defined?(Libnotify)
Libnotify = Object.new
end
@ -225,13 +224,13 @@ describe Guard::Notifier do
:icon_path => Pathname.new(File.dirname(__FILE__)).join('../../images/success.png').to_s,
:transient => true
)
subject.notify 'great', :title => 'Guard'
described_class.notify 'great', :title => 'Guard'
end
it "don't passes the notification to Libnotify if library is not available" do
Libnotify.should_not_receive(:show)
subject.should_receive(:enabled?).and_return(true, false)
subject.notify 'great', :title => 'Guard'
described_class.should_receive(:enabled?).and_return(true, false)
described_class.notify 'great', :title => 'Guard'
end
it "allows additional notification options" do
@ -242,7 +241,7 @@ describe Guard::Notifier do
:transient => true,
:urgency => :critical
)
subject.notify 'great', :title => 'Guard', :urgency => :critical
described_class.notify 'great', :title => 'Guard', :urgency => :critical
end
it "allows to overwrite a default notification option" do
@ -252,14 +251,14 @@ describe Guard::Notifier do
:icon_path => '~/.guard/success.png',
:transient => true
)
subject.notify 'great', :title => 'Guard', :icon_path => '~/.guard/success.png'
described_class.notify 'great', :title => 'Guard', :icon_path => '~/.guard/success.png'
end
end
context "on Windows" do
before do
RbConfig::CONFIG.should_receive(:[]).with('target_os').and_return 'mswin'
subject.stub(:require_rbnotifu)
described_class.stub(:require_rbnotifu)
Object.send(:remove_const, :Notifu) if defined?(Notifu)
Notifu = Object.new
end
@ -275,13 +274,13 @@ describe Guard::Notifier do
:type => :info,
:time => 3
)
subject.notify 'great', :title => 'Guard'
described_class.notify 'great', :title => 'Guard'
end
it "don't passes the notification to rb-notifu if library is not available" do
Notifu.should_not_receive(:show)
subject.should_receive(:enabled?).and_return(true, false)
subject.notify 'great', :title => 'Guard'
described_class.should_receive(:enabled?).and_return(true, false)
described_class.notify 'great', :title => 'Guard'
end
it "allows additional notification options" do
@ -292,7 +291,7 @@ describe Guard::Notifier do
:time => 3,
:nosound => true
)
subject.notify 'great', :title => 'Guard', :nosound => true
described_class.notify 'great', :title => 'Guard', :nosound => true
end
it "allows to overwrite a default notification option" do
@ -302,7 +301,7 @@ describe Guard::Notifier do
:type => :info,
:time => 10
)
subject.notify 'great', :title => 'Guard', :time => 10
described_class.notify 'great', :title => 'Guard', :time => 10
end
end
end
@ -320,4 +319,5 @@ describe Guard::Notifier do
it { should_not be_enabled }
end
end
end

View File

@ -5,19 +5,19 @@ describe Guard::Watcher do
describe "#initialize" do
it "requires a pattern parameter" do
expect { Guard::Watcher.new }.to raise_error(ArgumentError)
expect { described_class.new }.to raise_error(ArgumentError)
end
context "with a pattern parameter" do
context "that is a string" do
it "keeps the string pattern unmodified" do
Guard::Watcher.new('spec_helper.rb').pattern.should == 'spec_helper.rb'
described_class.new('spec_helper.rb').pattern.should == 'spec_helper.rb'
end
end
context "that is a regexp" do
it "keeps the regex pattern unmodified" do
Guard::Watcher.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/
described_class.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/
end
end
@ -25,10 +25,10 @@ describe Guard::Watcher do
before(:each) { Guard::UI.should_receive(:info).any_number_of_times }
it "converts the string automatically to a regex" do
Guard::Watcher.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/
Guard::Watcher.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/
Guard::Watcher.new('spec_helper\.rb').pattern.should == /spec_helper\.rb/
Guard::Watcher.new('.*_spec.rb').pattern.should == /.*_spec.rb/
described_class.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/
described_class.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/
described_class.new('spec_helper\.rb').pattern.should == /spec_helper\.rb/
described_class.new('.*_spec.rb').pattern.should == /.*_spec.rb/
end
end
end
@ -36,12 +36,12 @@ describe Guard::Watcher do
describe "#action" do
it "sets the action to nothing by default" do
Guard::Watcher.new(/spec_helper\.rb/).action.should be_nil
described_class.new(/spec_helper\.rb/).action.should be_nil
end
it "sets the action to the supplied block" do
action = lambda { |m| "spec/#{m[1]}_spec.rb" }
Guard::Watcher.new(%r{^lib/(.*).rb}, action).action.should == action
described_class.new(%r{^lib/(.*).rb}, action).action.should == action
end
end
@ -50,18 +50,18 @@ describe Guard::Watcher do
context "with a watcher without action" do
context "that is a regex pattern" do
before(:all) { @guard.watchers = [Guard::Watcher.new(/.*_spec\.rb/)] }
before(:all) { @guard.watchers = [described_class.new(/.*_spec\.rb/)] }
it "returns the paths that matches the regex" do
Guard::Watcher.match_files(@guard, ['guard_rocks_spec.rb', 'guard_rocks.rb']).should == ['guard_rocks_spec.rb']
described_class.match_files(@guard, ['guard_rocks_spec.rb', 'guard_rocks.rb']).should == ['guard_rocks_spec.rb']
end
end
context "that is a string pattern" do
before(:all) { @guard.watchers = [Guard::Watcher.new('guard_rocks_spec.rb')] }
before(:all) { @guard.watchers = [described_class.new('guard_rocks_spec.rb')] }
it "returns the path that matches the string" do
Guard::Watcher.match_files(@guard, ['guard_rocks_spec.rb', 'guard_rocks.rb']).should == ['guard_rocks_spec.rb']
described_class.match_files(@guard, ['guard_rocks_spec.rb', 'guard_rocks.rb']).should == ['guard_rocks_spec.rb']
end
end
end
@ -69,79 +69,79 @@ describe Guard::Watcher do
context "with a watcher action without parameter" do
before(:all) do
@guard.watchers = [
Guard::Watcher.new('spec_helper.rb', lambda { 'spec' }),
Guard::Watcher.new('addition.rb', lambda { 1 + 1 }),
Guard::Watcher.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
Guard::Watcher.new('array.rb', lambda { ['foo', 'bar'] }),
Guard::Watcher.new('blank.rb', lambda { '' }),
Guard::Watcher.new(/^uptime\.rb/, lambda { `uptime > /dev/null` })
described_class.new('spec_helper.rb', lambda { 'spec' }),
described_class.new('addition.rb', lambda { 1 + 1 }),
described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
described_class.new('array.rb', lambda { ['foo', 'bar'] }),
described_class.new('blank.rb', lambda { '' }),
described_class.new(/^uptime\.rb/, lambda { `uptime > /dev/null` })
]
end
it "returns a single file specified within the action" do
Guard::Watcher.match_files(@guard, ['spec_helper.rb']).should == ['spec']
described_class.match_files(@guard, ['spec_helper.rb']).should == ['spec']
end
it "returns multiple files specified within the action" do
Guard::Watcher.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
end
it "returns multiple files by combining the results of different actions" do
Guard::Watcher.match_files(@guard, ['spec_helper.rb', 'array.rb']).should == ['spec', 'foo', 'bar']
described_class.match_files(@guard, ['spec_helper.rb', 'array.rb']).should == ['spec', 'foo', 'bar']
end
it "returns nothing if the action returns something other than a string or an array of strings" do
Guard::Watcher.match_files(@guard, ['addition.rb']).should == []
described_class.match_files(@guard, ['addition.rb']).should == []
end
it "returns nothing if the action response is empty" do
Guard::Watcher.match_files(@guard, ['blank.rb']).should == []
described_class.match_files(@guard, ['blank.rb']).should == []
end
it "returns nothing if the action returns nothing" do
Guard::Watcher.match_files(@guard, ['uptime.rb']).should == []
described_class.match_files(@guard, ['uptime.rb']).should == []
end
end
context "with a watcher action that takes a parameter" do
before(:all) do
@guard.watchers = [
Guard::Watcher.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
Guard::Watcher.new(/addition(.*)\.rb/, lambda { |m| 1 + 1 }),
Guard::Watcher.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
Guard::Watcher.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar'] }),
Guard::Watcher.new(/blank(.*)\.rb/, lambda { |m| '' }),
Guard::Watcher.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
described_class.new(/addition(.*)\.rb/, lambda { |m| 1 + 1 }),
described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar'] }),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
]
end
it "returns a substituted single file specified within the action" do
Guard::Watcher.match_files(@guard, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
described_class.match_files(@guard, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
end
it "returns multiple files specified within the action" do
Guard::Watcher.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
end
it "returns multiple files by combining the results of different actions" do
Guard::Watcher.match_files(@guard, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', 'foo', 'bar']
described_class.match_files(@guard, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', 'foo', 'bar']
end
it "returns nothing if the action returns something other than a string or an array of strings" do
Guard::Watcher.match_files(@guard, ['addition.rb']).should == []
described_class.match_files(@guard, ['addition.rb']).should == []
end
it "returns nothing if the action response is empty" do
Guard::Watcher.match_files(@guard, ['blank.rb']).should == []
described_class.match_files(@guard, ['blank.rb']).should == []
end
it "returns nothing if the action returns nothing" do
Guard::Watcher.match_files(@guard, ['uptime.rb']).should == []
described_class.match_files(@guard, ['uptime.rb']).should == []
end
end
context "with an exception that is raised" do
before(:all) { @guard.watchers = [Guard::Watcher.new('evil.rb', lambda { raise "EVIL" })] }
before(:all) { @guard.watchers = [described_class.new('evil.rb', lambda { raise "EVIL" })] }
it "displays the error and backtrace" do
Guard::UI.should_receive(:error) { |msg|
@ -149,31 +149,31 @@ describe Guard::Watcher do
msg.should include("EVIL")
}
Guard::Watcher.match_files(@guard, ['evil.rb'])
described_class.match_files(@guard, ['evil.rb'])
end
end
end
describe ".match_files?" do
before(:all) do
@guard1 = Guard::Guard.new([Guard::Watcher.new(/.*_spec\.rb/)])
@guard2 = Guard::Guard.new([Guard::Watcher.new('spec_helper.rb', 'spec')])
@guard1 = Guard::Guard.new([described_class.new(/.*_spec\.rb/)])
@guard2 = Guard::Guard.new([described_class.new('spec_helper.rb', 'spec')])
@guards = [@guard1, @guard2]
end
context "with a watcher that matches a file" do
specify { Guard::Watcher.match_files?(@guards, ['lib/my_wonderful_lib.rb', 'guard_rocks_spec.rb']).should be_true }
specify { described_class.match_files?(@guards, ['lib/my_wonderful_lib.rb', 'guard_rocks_spec.rb']).should be_true }
end
context "with no watcher that matches a file" do
specify { Guard::Watcher.match_files?(@guards, ['lib/my_wonderful_lib.rb']).should be_false }
specify { described_class.match_files?(@guards, ['lib/my_wonderful_lib.rb']).should be_false }
end
end
describe "#match_file?" do
context "with a string pattern" do
context "that is a normal string" do
subject { Guard::Watcher.new('guard_rocks_spec.rb') }
subject { described_class.new('guard_rocks_spec.rb') }
context "with a watcher that matches a file" do
specify { subject.match_file?('guard_rocks_spec.rb').should be_true }
@ -185,7 +185,7 @@ describe Guard::Watcher do
end
context "that is a string representing a regexp (deprecated)" do
subject { Guard::Watcher.new('^guard_rocks_spec\.rb$') }
subject { described_class.new('^guard_rocks_spec\.rb$') }
context "with a watcher that matches a file" do
specify { subject.match_file?('guard_rocks_spec.rb').should be_true }
@ -198,7 +198,7 @@ describe Guard::Watcher do
end
context "that is a regexp pattern" do
subject { Guard::Watcher.new(/.*_spec\.rb/) }
subject { described_class.new(/.*_spec\.rb/) }
context "with a watcher that matches a file" do
specify { subject.match_file?('guard_rocks_spec.rb').should be_true }
@ -214,11 +214,11 @@ describe Guard::Watcher do
before(:all) { Guard::Dsl.stub(:guardfile_path) { Dir.pwd + '/Guardfile' } }
context "with files that match the Guardfile" do
specify { Guard::Watcher.match_guardfile?(['Guardfile', 'guard_rocks_spec.rb']).should be_true }
specify { described_class.match_guardfile?(['Guardfile', 'guard_rocks_spec.rb']).should be_true }
end
context "with no files that match the Guardfile" do
specify { Guard::Watcher.match_guardfile?(['guard_rocks.rb', 'guard_rocks_spec.rb']).should be_false }
specify { described_class.match_guardfile?(['guard_rocks.rb', 'guard_rocks_spec.rb']).should be_false }
end
end