Skip to content
Browse files

Ensure support for overriding described class.

Fixes bug introduced by 0704cbb and
exposed in rspec-rails.

Also refactored some names in Metadata.
  • Loading branch information...
1 parent ca3c0fc commit 50df862212da4c08fce859929041c7c0951b8fdf @dchelimsky dchelimsky committed Oct 2, 2011
Showing with 58 additions and 35 deletions.
  1. +20 −30 lib/rspec/core/metadata.rb
  2. +38 −5 spec/rspec/core/metadata_spec.rb
View
50 lib/rspec/core/metadata.rb
@@ -16,11 +16,11 @@ def [](key)
when :execution_result
store(:execution_result, {})
when :describes
- store(:describes, described_class_from(*self[:description_args]))
- when :description
- store(:description, description_from(*self[:description_args]))
+ store(:describes, described_class_for(self))
when :full_description
- store(:full_description, full_description)
+ store(:full_description, full_description_for(self))
+ when :description
+ store(:description, build_description_from(*self[:description_args]))
else
super
end
@@ -39,41 +39,31 @@ def first_caller_from_outside_rspec
self[:caller].detect {|l| l !~ /\/lib\/rspec\/core/}
end
- def description_from(*args)
- args.inject("") do |result, a|
- a = a.to_s.strip
- if result == ""
- a
- elsif a =~ /^(#|::|\.)/
- "#{result}#{a}"
- else
- "#{result} #{a}"
- end
+ def described_class_for(m)
+ while m.has_key?(:example_group)
+ return m[:example_group][:describes] if m[:example_group].has_key?(:describes)
+ m = m[:example_group]
end
+ candidate = m[:description_args].first
+ String === candidate || Symbol === candidate ? nil : candidate
end
- def full_description
- x = self
- all_args = [self[:description_args]].compact
- while x.has_key?(:example_group)
- x = x[:example_group]
- all_args.unshift x[:description_args]
+ def full_description_for(m)
+ parts = [m[:description]]
+ while m.has_key?(:example_group)
+ m = m[:example_group]
+ parts.unshift m[:full_description]
end
- description_from(*all_args.flatten)
+ build_description_from(*parts.flatten)
end
- def described_class_from(*args)
- x = self
- while x.has_key?(:example_group)
- x = x[:example_group]
- end
- y = x[:description_args].first
- String === y || Symbol === y ? nil :y
+ def build_description_from(*parts)
+ parts.map {|p| p.to_s}.reduce do |desc, p|
+ p =~ /^(#|::|\.)/ ? "#{desc}#{p}" : "#{desc} #{p}"
+ end || ""
end
end
- attr_reader :parent_group_metadata
-
def initialize(parent_group_metadata=nil)
if parent_group_metadata
update(parent_group_metadata)
View
43 spec/rspec/core/metadata_spec.rb
@@ -211,8 +211,8 @@ module Core
end
end
- context "with describes from a superclass metadata" do
- it "returns the superclass' described class" do
+ context "with describes from a parent group metadata" do
+ it "returns the parent group's described class" do
sm = Metadata.new
sm.process(String)
@@ -222,13 +222,37 @@ module Core
m = m.for_example("example", {})
m[:example_group][:describes].should be(String)
end
+
+ it "can override a parent group's described class" do
+ parent = Metadata.new
+ parent.process(String)
+
+ child = Metadata.new(parent)
+ child.process(Hash)
+ child[:example_group][:describes] = Hash
+
+ grandchild = Metadata.new(child)
+ grandchild.process(Array)
+
+ grandchild.for_example("example", {}).tap do |e|
+ e[:example_group][:describes].should be(Hash)
+ end
+
+ child.for_example("example", {}).tap do |e|
+ e[:example_group][:describes].should be(Hash)
+ end
+
+ parent.for_example("example", {}).tap do |e|
+ e[:example_group][:describes].should be(String)
+ end
+ end
end
end
describe ":description" do
it "just has the example description" do
m = Metadata.new
- m.process('group')
+ m.process("group")
m = m.for_example("example", {})
m[:description].should eq("example")
@@ -237,7 +261,7 @@ module Core
context "with a string" do
it "provides the submitted description" do
m = Metadata.new
- m.process('group')
+ m.process("group")
m[:example_group][:description].should eq("group")
end
@@ -246,7 +270,7 @@ module Core
context "with a non-string" do
it "provides the submitted description" do
m = Metadata.new
- m.process('group')
+ m.process("group")
m[:example_group][:description].should eq("group")
end
@@ -260,6 +284,15 @@ module Core
m[:example_group][:description].should eq("Object group")
end
end
+
+ context "with empty args" do
+ it "returns empty string for [:example_group][:description]" do
+ m = Metadata.new
+ m.process()
+
+ m[:example_group][:description].should eq("")
+ end
+ end
end
describe ":full_description" do

0 comments on commit 50df862

Please sign in to comment.
Something went wrong with that request. Please try again.