Skip to content
This repository
Browse code

Merge pull request #1490 from rspec/metadata-regression

Fix a regression in our metadata backwards compatibility
  • Loading branch information...
commit b691a41233e967a0fbc515ad8097062ceb70991b 2 parents a1518a3 + 9ffcc38
Myron Marston authored April 17, 2014
15  lib/rspec/core/metadata.rb
@@ -149,6 +149,11 @@ def ensure_valid_user_keys
149 149
       class ExampleHash < HashPopulator
150 150
         def self.create(group_metadata, user_metadata, description, block)
151 151
           example_metadata = group_metadata.dup
  152
+          group_metadata = Hash.new(&ExampleGroupHash.backwards_compatibility_default_proc do |hash|
  153
+            hash[:parent_example_group]
  154
+          end)
  155
+          group_metadata.update(example_metadata)
  156
+
152 157
           example_metadata[:example_group] = group_metadata
153 158
           example_metadata.delete(:parent_example_group)
154 159
 
@@ -184,14 +189,18 @@ def self.create(parent_group_metadata, user_metadata, *args, &block)
184 189
         end
185 190
 
186 191
         def self.hash_with_backwards_compatibility_default_proc
187  
-          Hash.new do |hash, key|
  192
+          Hash.new(&backwards_compatibility_default_proc { |hash| hash })
  193
+        end
  194
+
  195
+        def self.backwards_compatibility_default_proc(&example_group_selector)
  196
+          Proc.new do |hash, key|
188 197
             case key
189 198
             when :example_group
190 199
               RSpec.deprecate("The `:example_group` key in an example group's metadata hash",
191 200
                               :replacement => "the example group's hash directly for the " +
192 201
                               "computed keys and `:parent_example_group` to access the parent " +
193 202
                               "example group metadata")
194  
-              LegacyExampleGroupHash.new(hash)
  203
+              LegacyExampleGroupHash.new(example_group_selector.call(hash))
195 204
             when :example_group_block
196 205
               RSpec.deprecate("`metadata[:example_group_block]`",
197 206
                               :replacement => "`metadata[:block]`")
@@ -370,7 +379,7 @@ class LegacyExampleGroupHash
370 379
 
371 380
       def initialize(metadata)
372 381
         @metadata = metadata
373  
-        self[:example_group] = metadata[:parent_example_group]
  382
+        self[:example_group] = metadata[:parent_example_group][:example_group]
374 383
       end
375 384
 
376 385
       def to_h
21  spec/rspec/core/metadata_spec.rb
@@ -117,6 +117,8 @@ def metadata_for(*args)
117 117
           end
118 118
 
119 119
           a[:description] = "new description"
  120
+
  121
+          pending "Cannot maintain this and provide full `:example_group` backwards compatibility (see GH #1490):("
120 122
           expect(b[:description]).to eq("new description")
121 123
         end
122 124
 
@@ -460,13 +462,30 @@ def value_for(*args)
460 462
               describe("nested") { child = metadata }
461 463
             end
462 464
 
463  
-            expect(child[:example_group][:example_group]).to include(
  465
+            expect(child[:example_group][:example_group].to_h).to include(
464 466
               :foo => 3,
465 467
               :description => "Object group",
466 468
               :line_number => parent_line
467 469
             )
468 470
           end
469 471
 
  472
+          it "works properly with deep nesting" do
  473
+            inner_metadata = nil
  474
+
  475
+            RSpec.describe "Level 1" do
  476
+              describe "Level 2" do
  477
+                describe "Level 3" do
  478
+                  inner_metadata = example("Level 4").metadata
  479
+                end
  480
+              end
  481
+            end
  482
+
  483
+            expect(inner_metadata[:description]).to eq("Level 4")
  484
+            expect(inner_metadata[:example_group][:description]).to eq("Level 3")
  485
+            expect(inner_metadata[:example_group][:example_group][:description]).to eq("Level 2")
  486
+            expect(inner_metadata[:example_group][:example_group][:example_group][:description]).to eq("Level 1")
  487
+          end
  488
+
470 489
           it 'can mutate attributes when accessing them via [:example_group]' do
471 490
             meta = nil
472 491
 

0 notes on commit b691a41

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