Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Fix nested described class #1361

Merged
merged 3 commits into from
Mar 4, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
### 3.0.0.rc1 Development
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.0.0.beta2...master)

Breaking Changes for 3.0.0:

* Change `described_class` so that in a nested group like `describe
MyClass`, it returns `MyClass` rather than the outer group's described
class. (Myron Marston)

Enhancements:

* Add `config.default_formatter` attribute, which can be used to set a
Expand Down
12 changes: 3 additions & 9 deletions features/subject/implicit_subject.feature
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Feature: implicitly defined subject
When I run `rspec nested_subject_spec.rb`
Then the examples should all pass

Scenario: subject in a nested group with a different class (outermost wins)
Scenario: subject in a nested group with a different class (innermost wins)
Given a file named "nested_subject_spec.rb" with:
"""ruby
class ArrayWithOneElement < Array
Expand All @@ -46,14 +46,8 @@ Feature: implicitly defined subject
describe Array do
describe ArrayWithOneElement do
context "referenced as subject" do
it "should be empty (because it is the Array declared at the top)" do
expect(subject).to be_empty
end
end

context "created in the example" do
it "should not be empty" do
expect(ArrayWithOneElement.new).not_to be_empty
it "contains one element" do
expect(subject).to include("first element")
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions lib/rspec/core/metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ def described_class
return value unless value.nil?
end
end
end

container_stack.reverse.each do |g|
candidate = g[:description_args].first
return candidate unless String === candidate || Symbol === candidate
end
Expand Down
29 changes: 26 additions & 3 deletions spec/rspec/core/example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def ascending_numbers
context "in a nested group" do
it "inherits the described class/module from the outer group" do
group = ExampleGroup.describe(String) do
describe Array do
describe "nested" do
example "describes is String" do
expect(described_class).to eq(String)
end
Expand All @@ -481,6 +481,29 @@ def ascending_numbers

expect(group.run).to be_truthy, "expected examples in group to pass"
end

context "when a class is passed" do
def described_class_value
value = nil

ExampleGroup.describe(String) do
yield if block_given?
describe Array do
example { value = described_class }
end
end.run

value
end

it "overrides the described class" do
expect(described_class_value).to eq(Array)
end

it "overrides the described class even when described_class is referenced in the outer group" do
expect(described_class_value { described_class }).to eq(Array)
end
end
end

context "for `describe(SomeClass)` within a `describe 'some string' group" do
Expand Down Expand Up @@ -1111,8 +1134,8 @@ def extract_execution_results(group)
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do

describe "A sample nested group", :nested_describe => "yep" do
it "sets the described class to the described class of the outer most group" do |ex|
expect(ex.example_group.described_class).to eq(ExampleGroup)
it "sets the described class to the nearest described class" do |ex|
expect(ex.example_group.described_class).to eq(Object)
end

it "sets the description to 'A sample nested describe'" do |ex|
Expand Down
30 changes: 15 additions & 15 deletions spec/rspec/core/metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,36 +290,36 @@ module Core
end

context "in a nested group" do
it "returns the parent group's described class" do
sm = Metadata.new
sm.process(String)
it "inherits the parent group's described class" do
parent = Metadata.new
parent.process(Hash)

m = Metadata.new(sm)
m.process(Array)
child = Metadata.new(parent)
child.process("sub context")

expect(m[:example_group][key]).to be(String)
expect(child[:example_group][key]).to be(Hash)
end

it "returns own described class if parent doesn't have one" do
sm = Metadata.new
sm.process("foo")
it "sets the described class when passing a class" do
parent = Metadata.new
parent.process(String)

m = Metadata.new(sm)
m.process(Array)
child = Metadata.new(parent)
child.process(Array)

expect(m[:example_group][key]).to be(Array)
expect(child[:example_group][key]).to be(Array)
end

it "can override a parent group's described class" do
it "can override a parent group's described class using metdata" do
parent = Metadata.new
parent.process(String)

child = Metadata.new(parent)
child.process(Fixnum)
child.process("sub context")
child[:example_group][key] = Hash

grandchild = Metadata.new(child)
grandchild.process(Array)
grandchild.process("sub context")

expect(grandchild[:example_group][key]).to be(Hash)
expect(child[:example_group][key]).to be(Hash)
Expand Down
6 changes: 1 addition & 5 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ def run(reporter=nil)
end
end

def in_editor?
ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM')
end

module EnvHelpers
def with_env_vars(vars)
original = ENV.to_hash
Expand Down Expand Up @@ -135,7 +131,7 @@ def without_env_vars(*vars)
end

# runtime options
c.color = !in_editor?
c.color = true
c.include EnvHelpers
c.filter_run_excluding :ruby => lambda {|version|
case version.to_s
Expand Down