Permalink
Browse files

Simplify metadata description/name scheme

- eliminated :name and :display_name metadata keys
- now we have the following:
  describe "parent group" do
    describe "child group" do
      it "example" do

  parent group metadata[:example_group][:description] = "parent group"
  parent group metadata[:example_group][:full_description] = "parent group"
  child group metadata[:example_group][:description] = "child group"
  child group metadata[:example_group][:full_description] = "parent group child group"
  example metadata[:description] = "example"
  example metadata[:full_description] = "parent group child group example"
  • Loading branch information...
1 parent 9fedc54 commit 28db011df70968693eaaf8ac41292cb84dd8da63 @dchelimsky dchelimsky committed Mar 7, 2010
View
2 lib/rspec/core/example_group.rb
@@ -85,7 +85,7 @@ def self.metadata
end
def self.display_name
- metadata[:example_group][:name]
+ metadata[:example_group][:description]
end
def self.description
View
6 lib/rspec/core/formatters/documentation_formatter.rb
@@ -16,10 +16,8 @@ def add_example_group(example_group)
described_example_group_chain.each_with_index do |nested_example_group, i|
unless nested_example_group == @previous_nested_example_groups[i]
- at_root_level = (i == 0)
- desc_or_name = at_root_level ? nested_example_group.display_name : nested_example_group.description
- output.puts if at_root_level
- output.puts "#{' ' * i}#{desc_or_name}"
+ output.puts if i == 0
+ output.puts "#{' ' * i}#{nested_example_group.description}"
end
end
View
40 lib/rspec/core/metadata.rb
@@ -23,13 +23,12 @@ def process(*args)
extra_metadata.delete(:example_group) # Remove it when present to prevent it clobbering the one we setup
extra_metadata.delete(:behaviour) # Remove it when present to prevent it clobbering the one we setup
- self[:example_group][:describes] = args.shift unless args.first.is_a?(String)
- self[:example_group][:describes] ||= self.superclass_metadata && self.superclass_metadata[:example_group][:describes]
- self[:example_group][:description] = args.shift || ''
+ self[:example_group][:describes] = described_class_from(args)
+ self[:example_group][:description] = description_from(args)
+ self[:example_group][:full_description] = full_description_from(args)
- self[:example_group][:name] = determine_name
self[:example_group][:block] = extra_metadata.delete(:example_group_block)
- self[:example_group][:caller] = extra_metadata.delete(:caller)
+ self[:example_group][:caller] = extra_metadata.delete(:caller) || caller(1)
self[:example_group][:file_path] = file_path_from(self[:example_group], extra_metadata.delete(:file_path))
self[:example_group][:line_number] = line_number_from(self[:example_group], extra_metadata.delete(:line_number))
self[:example_group][:location] = location_from(self[:example_group])
@@ -43,7 +42,7 @@ def for_example(description, options)
def configure_for_example(description, options)
store(:description, description.to_s)
- store(:full_description, "#{self[:example_group][:name]} #{self[:description]}")
+ store(:full_description, "#{self[:example_group][:full_description]} #{self[:description]}")
store(:execution_result, {})
store(:caller, options.delete(:caller))
if self[:caller]
@@ -52,7 +51,6 @@ def configure_for_example(description, options)
end
self[:location] = location_from(self)
update(options)
- self
end
def apply_condition(filter_on, filter, metadata=nil)
@@ -83,6 +81,26 @@ def all_apply?(filters)
private
+ def description_from(args)
+ @build_description ||= args.map{|a| a.to_s.strip}.join(" ")
+ end
+
+ def full_description_from(args)
+ if superclass_metadata && superclass_metadata[:example_group][:full_description]
+ "#{superclass_metadata[:example_group][:full_description]} #{description_from(args)}"
+ else
+ description_from(args)
+ end
+ end
+
+ def described_class_from(args)
+ if args.first.is_a?(String)
+ self.superclass_metadata && self.superclass_metadata[:example_group][:describes]
+ else
+ args.first
+ end
+ end
+
def file_path_from(metadata, given_file_path=nil)
given_file_path || file_and_line_number(metadata)[0].strip
end
@@ -103,14 +121,6 @@ def candidate_entries_from_caller(metadata)
metadata[:caller].grep(/\_spec\.rb:/i)
end
- def determine_name
- if superclass_metadata && superclass_metadata[:example_group][:name]
- self[:example_group][:name] = "#{superclass_metadata[:example_group][:name]} #{self[:example_group][:description]}".strip
- else
- self[:example_group][:name] = "#{self[:example_group][:describes]} #{self[:example_group][:description]}".strip
- end
- end
-
end
end
end
View
31 spec/rspec/core/example_group_spec.rb
@@ -26,28 +26,6 @@ module Rspec::Core
end
- describe '#display_name' do
-
- it "uses the first parameter as name" do
- ExampleGroup.create("my favorite pony") { }.display_name.should == 'my favorite pony'
- end
-
- it "accepts a constant as the first parameter" do
- ExampleGroup.create(Object) { }.display_name.should == 'Object'
- end
-
- it "concats nested names" do
- group = ExampleGroup.create(Object, 'test') {}
- group.display_name.should == 'Object test'
-
- nested_group_one = group.describe('nested one') { }
- nested_group_one.display_name.should == 'Object test nested one'
-
- nested_group_two = nested_group_one.describe('nested two') { }
- nested_group_two.display_name.should == 'Object test nested one nested two'
- end
-
- end
describe '#describes' do
@@ -71,12 +49,9 @@ module Rspec::Core
describe '#description' do
- it "exposes the second parameter as description" do
- ExampleGroup.create(Object, "my desc") { }.description.should == 'my desc'
- end
-
- it "allows the second parameter to be nil" do
- ExampleGroup.create(Object, nil) { }.description.should == ""
+ it "grabs the description from the metadata" do
+ group = ExampleGroup.create(Object, "my desc") { }
+ group.description.should == group.metadata[:example_group][:description]
end
end
View
58 spec/rspec/core/metadata_spec.rb
@@ -3,44 +3,58 @@
module Rspec
module Core
describe Metadata do
- describe "[:example_group][:name]" do
- it "generates name for top level example group" do
- m = Metadata.new
- m.process("description", :caller => caller(0))
- m[:example_group][:name].should == "description"
- end
- it "concats args to describe()" do
- m = Metadata.new
- m.process(String, "with dots", :caller => caller(0))
- m[:example_group][:name].should == "String with dots"
+ describe "[:example_group][:description]" do
+ context "with a string" do
+ it "provides the submitted description" do
+ m = Metadata.new
+ m.process('group')
+
+ m[:example_group][:description].should == "group"
+ end
end
- it "concats nested names" do
- parent = Metadata.new
- parent.process("parent", :caller => caller(0))
- m = Metadata.new(parent)
- m.process("child", :caller => caller(0))
- m[:example_group][:name].should == "parent child"
+ context "with a non-string" do
+ it "provides the submitted description" do
+ m = Metadata.new
+ m.process('group')
+
+ m[:example_group][:description].should == "group"
+ end
end
- it "strips the name" do
- m = Metadata.new
- m.process(" description \n", :caller => caller(0))
- m[:example_group][:name].should == "description"
+ context "with a non-string and a string" do
+ it "concats the args" do
+ m = Metadata.new
+ m.process(Object, 'group')
+
+ m[:example_group][:description].should == "Object group"
+ end
end
end
describe "[:full_description]" do
it "concats the example group name and description" do
m = Metadata.new
- m[:example_group][:name] = "group"
+ m.process('group')
m = m.for_example("example", {})
m[:full_description].should == "group example"
end
end
+ describe "[:example_group][:full_description]" do
+ it "concats the nested example group descriptions" do
+ parent = Metadata.new
+ parent.process(Object, 'parent')
+
+ child = Metadata.new(parent)
+ child.process('child')
+
+ child[:example_group][:full_description].should == "Object parent child"
+ end
+ end
+
describe "#determine_file_path" do
it "finds the first spec file in the caller array" do
m = Metadata.new
@@ -80,7 +94,7 @@ module Core
describe "#metadata_for_example" do
let(:caller_for_example) { caller(0) }
let(:line_number) { __LINE__ - 1 }
- let(:metadata) { Metadata.new.process("group description", :caller => caller(0)) }
+ let(:metadata) { Metadata.new.process("group description") }
let(:mfe) { metadata.for_example("example description", {:caller => caller_for_example, :arbitrary => :options}) }
it "stores the description" do
View
2 spec/rspec/core/world_spec.rb
@@ -49,7 +49,7 @@ module Rspec::Core
end
it "finds matching groups when filtering on :description with text" do
- @world.apply_inclusion_filters(@example_groups, :example_group => { :description => 'find group-1' }).should == [@bg1]
+ @world.apply_inclusion_filters(@example_groups, :example_group => { :description => 'Bar find group-1' }).should == [@bg1]
end
it "finds matching groups when filtering on :description with a lambda" do

0 comments on commit 28db011

Please sign in to comment.