Permalink
Browse files

support filter line number following an example or its parent example…

… group
  • Loading branch information...
1 parent 8000a34 commit dae7920eb7be3b1c8082bd8f5913bc8de4b4cd02 @jimbreen jimbreen committed Mar 30, 2010
Showing with 82 additions and 4 deletions.
  1. +3 −2 lib/rspec/core/metadata.rb
  2. +16 −0 lib/rspec/core/world.rb
  3. +24 −2 spec/rspec/core/metadata_spec.rb
  4. +39 −0 spec/rspec/core/world_spec.rb
@@ -88,15 +88,16 @@ def apply_condition(filter_on, filter, metadata=nil)
filter.call(metadata[filter_on]) rescue false
when Fixnum
if filter_on == :line_number
- [metadata[:line_number],metadata[:example_group][:line_number]].include?(filter)
+ example_or_group_line = preceding_example_or_group_line(filter)
+ [metadata[:line_number],metadata[:example_group][:line_number]].include?(example_or_group_line)
else
metadata[filter_on] == filter
end
else
metadata[filter_on] == filter
end
end
-
+
def all_apply?(filters)
filters.all? do |filter_on, filter|
apply_condition(filter_on, filter)
View
@@ -81,6 +81,22 @@ def find(collection, type_of_filter=:positive, conditions={})
end
end
+ def preceding_example_or_group_line(filter_line)
+ example_and_group_line_numbers.inject(nil) do |highest_prior_example_or_group_line, line|
+ line <= filter_line ? line : highest_prior_example_or_group_line
+ end
+ end
+
+ def example_and_group_line_numbers
+ @line_numbers ||= example_groups.inject([]) do |lines, g|
+ lines << g.metadata[:example_group][:line_number]
+ g.examples.each do |e|
+ lines << e.metadata[:line_number]
+ end
+ lines
+ end
+ end
+
end
end
end
@@ -165,14 +165,36 @@ module Core
let(:group_line_number) { __LINE__ -1 }
let(:example_metadata) { group_metadata.for_example('example', :caller => ["foo_spec.rb:#{__LINE__}"]) }
let(:example_line_number) { __LINE__ -1 }
+ let(:next_example_metadata) {group_metadata.for_example('next_example',
+ :caller => ["foo_spec.rb:#{example_line_number + 2}"])}
- it "matches when the line_number matches the group" do
+ it "matches the group when the line_number is the example group line number" do
+ group_metadata.should_receive(:preceding_example_or_group_line).and_return(group_line_number)
+ # this call doesn't really make sense since apply_condition is only called
+ # for example metadata not group metadata
group_metadata.apply_condition(:line_number, group_line_number).should be_true
end
- it "matches when the line_number matches the example" do
+ it "matches the example when the line_number is the parent example group line number" do
+ example_metadata.should_receive(:preceding_example_or_group_line).and_return(group_line_number)
+ example_metadata.apply_condition(:line_number, group_line_number).should be_true
+ end
+
+ it "matches the example when the line_number is the example line number" do
+ example_metadata.should_receive(:preceding_example_or_group_line).and_return(example_line_number)
example_metadata.apply_condition(:line_number, example_line_number).should be_true
end
+
+ it "matches when the line number is between this example and the next" do
+ example_metadata.should_receive(:preceding_example_or_group_line).and_return(example_line_number)
+ example_metadata.apply_condition(:line_number, example_line_number + 1).should be_true
+ end
+
+ it "does not match when the line number matches the next example" do
+ example_metadata.should_receive(:preceding_example_or_group_line).and_return(example_line_number+2)
+ example_metadata.apply_condition(:line_number, example_line_number + 2).should be_false
+ end
+
end
end
@@ -138,6 +138,45 @@ module Rspec::Core
end
+ describe "preceding_example_or_group_line" do
+ before(:each) do
+ @group1_line = 10
+ @group2_line = 20
+ @group2_example1_line = 30
+ @group2_example2_line = 40
+
+ @group1 = Rspec::Core::ExampleGroup.describe(Bar, "group-1") { }
+ @group2 = Rspec::Core::ExampleGroup.describe(Bar, "group-2") do
+ it('example 1') {}
+ it("example 2") {}
+ end
+ @group1.metadata[:example_group][:line_number] = @group1_line
+ @group2.metadata[:example_group][:line_number] = @group2_line
+ @group2.examples[0].metadata[:line_number] = @group2_example1_line
+ @group2.examples[1].metadata[:line_number] = @group2_example2_line
+ end
+
+ it "should return nil if no example or group precedes the line" do
+ @world.preceding_example_or_group_line(@group1_line-1).should == nil
+ end
+
+ it "should return the argument line number if a group starts on that line" do
+ @world.preceding_example_or_group_line(@group1_line).should == @group1_line
+ end
+
+ it "should return the argument line number if an example starts on that line" do
+ @world.preceding_example_or_group_line(@group2_example1_line).should == @group2_example1_line
+ end
+
+ it "should return line number of a group that immediately precedes the argument line" do
+ @world.preceding_example_or_group_line(@group2_line+1).should == @group2_line
+ end
+
+ it "should return line number of an example that immediately precedes the argument line" do
+ @world.preceding_example_or_group_line(@group2_example1_line+1).should == @group2_example1_line
+ end
+
+ end
end
end

0 comments on commit dae7920

Please sign in to comment.