Permalink
Browse files

any return is ok. Pass the path and some other parameters if you woul…

…d like and they don't become strings!
  • Loading branch information...
1 parent 47f3d87 commit a5dd0c78477fe0e891328371d33cf56308fca396 @earlonrails committed Oct 6, 2011
Showing with 18 additions and 16 deletions.
  1. +2 −2 lib/guard/watcher.rb
  2. +16 −14 spec/guard/watcher_spec.rb
View
@@ -46,13 +46,13 @@ def self.match_files(guard, files)
if matches = watcher.match_file?(file)
if watcher.action
result = watcher.call_action(matches)
- paths << Array(result) if result
+ paths << result
else
paths << matches[0]
end
end
end
- return paths
+ paths
@netzpirat

netzpirat Oct 7, 2011

Indention is wrong here.

end
end
View
@@ -79,37 +79,39 @@
end
it "returns a single file specified within the action" do
- described_class.match_files(@guard, ['spec_helper.rb']).should == ['spec']
+ described_class.match_files(@guard, ['spec_helper.rb']).class.should == Array
+ described_class.match_files(@guard, ['spec_helper.rb']).empty?.should == false
end
it "returns multiple files specified within the action" do
- described_class.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
- described_class.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
- described_class.match_files(@guard, ['addition.rb']).should == []
+ described_class.match_files(@guard, ['addition.rb']).class.should == Array
+ described_class.match_files(@guard, ['addition.rb'])[0].should == 2
end
it "returns nothing if the action response is empty" do
- described_class.match_files(@guard, ['blank.rb']).should == []
+ described_class.match_files(@guard, ['blank.rb']).should == ['']
end
it "returns nothing if the action returns nothing" do
- described_class.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 = [
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(/addition(.*)\.rb/, lambda { |m| (1 + 1).to_s + m[0] }),
+ described_class.new('hash.rb', lambda {|m| Hash[:foo, 'bar', :file_name, m[0]] }),
+ described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar', m[0]] }),
described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
]
@@ -120,23 +122,23 @@
end
it "returns multiple files specified within the action" do
- described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
+ described_class.match_files(@guard, ['hash.rb']).should == [{:foo => 'bar', :file_name => 'hash.rb'}]
end
it "returns multiple files by combining the results of different actions" do
- described_class.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', "array.rb"]]
end
it "returns nothing if the action returns something other than a string or an array of strings" do
@netzpirat

netzpirat Oct 7, 2011

You should keep the description in sync with the code. This applies to more places than this, but here is a good example where the description says it does not return something but the spec tests a return value.

- described_class.match_files(@guard, ['addition.rb']).should == []
+ described_class.match_files(@guard, ['addition.rb']).should == ["2addition.rb"]
end
it "returns nothing if the action response is empty" do
- described_class.match_files(@guard, ['blank.rb']).should == []
+ described_class.match_files(@guard, ['blank.rb']).should == ['']
@netzpirat

netzpirat Oct 7, 2011

Is this desired behavior? I think this should be filtered in the Watcher.

end
it "returns nothing if the action returns nothing" do
- described_class.match_files(@guard, ['uptime.rb']).should == []
+ described_class.match_files(@guard, ['uptime.rb']).should == ['']
end
end

1 comment on commit a5dd0c7

As @rymai already commented, this removes some regression specs of the wide spread watcher usage: Matching files as Strings. Since this is the most important usage of Guard, this shouldn't be changed. I suggest that you leave the original specs that validates file matching as String and surround these in a context block and add your watcher specs that returns Objects into another context block.

context 'for a Watcher that matches file strings` do
  # Original specs
end

context 'for a Watcher that matches information objects' do
  # Your specs goes here
end
Please sign in to comment.