From f6cb5923912f7598e544e04a1e6a45a0c8a37778 Mon Sep 17 00:00:00 2001 From: David Chelimsky Date: Mon, 28 Mar 2011 04:05:54 -0500 Subject: [PATCH] nullify ivars after each example --- lib/rspec/core/example.rb | 3 +++ spec/rspec/core/example_spec.rb | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/lib/rspec/core/example.rb b/lib/rspec/core/example.rb index 64b683c3df..e5eddd62ff 100644 --- a/lib/rspec/core/example.rb +++ b/lib/rspec/core/example.rb @@ -58,6 +58,9 @@ def run(example_group_instance, reporter) rescue Exception => e set_exception(e) ensure + @example_group_instance.instance_variables.each do |ivar| + @example_group_instance.instance_variable_set(ivar, nil) + end @example_group_instance = nil begin diff --git a/spec/rspec/core/example_spec.rb b/spec/rspec/core/example_spec.rb index e9fd398bda..bc21860e13 100644 --- a/spec/rspec/core/example_spec.rb +++ b/spec/rspec/core/example_spec.rb @@ -176,6 +176,35 @@ def assert(val) "around (after)" ]) end + + context "clearing ivars" do + it "sets ivars to nil to prep them for GC" do + group = RSpec::Core::ExampleGroup.describe do + before(:all) { @before_all = :before_all } + before(:each) { @before_each = :before_each } + after(:each) { @after_each = :after_each } + after(:all) { @after_all = :after_all } + end + example = group.example("does something") do + @in_example = :in_example + end + example_group_instance = group.new + example.run(example_group_instance, double('reporter').as_null_object) + + %w[@before_all @before_each @after_each @after_all].each do |ivar| + example_group_instance.instance_variable_get(ivar).should be_nil + end + end + + it "does not impact the before_all_ivars which are copied to each example" do + group = RSpec::Core::ExampleGroup.describe do + before(:all) { @before_all = "abc" } + example("first") { @before_all.should_not be_nil } + example("second") { @before_all.should_not be_nil } + end + group.run.should be_true + end + end end describe "#pending" do