Skip to content

Commit

Permalink
Merge remote branch 'jimbreen/issue-5' into issue-5
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Apr 15, 2010
2 parents 37b53f3 + 297c242 commit e338d51
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
53 changes: 52 additions & 1 deletion features/command_line/line_number_appended_to_path.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,23 @@ Feature: line number appended to file path
And I should see "example in nested group"

@wip
Scenario: nested groups - outer group inside block before example
When I run "rspec example_spec.rb:2 --format doc"
Then I should see "3 examples, 0 failures"
And I should see "second example in outer group"
And I should see "first example in outer group"
And I should see "example in nested group"

Scenario: nested groups - inner group on declaration line
When I run "rspec example_spec.rb:11 --format doc"
Then I should see "3 examples, 0 failures"
Then I should see "1 example, 0 failures"
And I should see "example in nested group"
And I should not see "second example in outer group"
And I should not see "first example in outer group"

Scenario: nested groups - inner group inside block before example
When I run "rspec example_spec.rb:12 --format doc"
Then I should see "1 example, 0 failures"
And I should see "example in nested group"
And I should not see "second example in outer group"
And I should not see "first example in outer group"
Expand All @@ -51,9 +65,46 @@ Feature: line number appended to file path
But I should not see "second example in outer group"
And I should not see "example in nested group"

Scenario: two examples - first example inside block
When I run "rspec example_spec.rb:4 --format doc"
Then I should see "1 example, 0 failures"
And I should see "first example in outer group"
But I should not see "second example in outer group"
And I should not see "example in nested group"

Scenario: two examples - first example on end
When I run "rspec example_spec.rb:5 --format doc"
Then I should see "1 example, 0 failures"
And I should see "first example in outer group"
But I should not see "second example in outer group"
And I should not see "example in nested group"

Scenario: two examples - first example after end but before next example
When I run "rspec example_spec.rb:6 --format doc"
Then I should see "1 example, 0 failures"
And I should see "first example in outer group"
But I should not see "second example in outer group"
And I should not see "example in nested group"

Scenario: two examples - second example on declaration line
When I run "rspec example_spec.rb:7 --format doc"
Then I should see "1 example, 0 failures"
And I should see "second example in outer group"
But I should not see "first example in outer group"
And I should not see "example in nested group"

Scenario: two examples - second example inside block
When I run "rspec example_spec.rb:7 --format doc"
Then I should see "1 example, 0 failures"
And I should see "second example in outer group"
But I should not see "first example in outer group"
And I should not see "example in nested group"

Scenario: two examples - second example on end
When I run "rspec example_spec.rb:7 --format doc"
Then I should see "1 example, 0 failures"
And I should see "second example in outer group"
But I should not see "first example in outer group"
And I should not see "example in nested group"


3 changes: 2 additions & 1 deletion lib/rspec/core/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ 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 = Rspec::Core.world.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
Expand Down
16 changes: 16 additions & 0 deletions lib/rspec/core/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
26 changes: 24 additions & 2 deletions spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,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
Rspec::Core.world.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
Rspec::Core.world.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
Rspec::Core.world.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
Rspec::Core.world.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
Rspec::Core.world.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
Expand Down
39 changes: 39 additions & 0 deletions spec/rspec/core/world_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 e338d51

Please sign in to comment.