From ced328d38abfc09719b53e8b0bcf88cd703dbecb Mon Sep 17 00:00:00 2001 From: Ryan Fitzgerald Date: Sat, 13 Oct 2012 21:00:36 +0000 Subject: [PATCH] Refactor Pry.toplevel_binding to allow reset during tests --- lib/pry/pry_class.rb | 25 ++++++++++++++++--------- test/helper.rb | 15 ++++++++++++++- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 47c9a2ad..38231886 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -390,16 +390,23 @@ class Pry end end end -end -# Grab a copy of the TOPLEVEL_BINDING without any local variables. -# This binding has a default definee of Object, and new methods are -# private (just as in TOPLEVEL_BINDING). -def self.__pry__ - binding + def self.toplevel_binding + unless @toplevel_binding + # Grab a copy of the TOPLEVEL_BINDING without any local variables. + # This binding has a default definee of Object, and new methods are + # private (just as in TOPLEVEL_BINDING). + TOPLEVEL_BINDING.eval <<-RUBY + def self.__pry__ + binding + end + Pry.toplevel_binding = __pry__ + class << self; undef __pry__; end + RUBY + end + @toplevel_binding.eval('private') + @toplevel_binding + end end -Pry.toplevel_binding = __pry__ -Pry.toplevel_binding.eval("private") -class << self; undef __pry__; end Pry.init diff --git a/test/helper.rb b/test/helper.rb index 33032f90..d53be564 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -49,6 +49,17 @@ module Bacon end end +# Reset toplevel binding at the beginning of each test case. +module Bacon + class Context + alias _real_it it + def it(description, &block) + Pry.toplevel_binding = nil + _real_it(description, &block) + end + end +end + # A global space for storing temporary state during tests. Pad = OpenStruct.new def Pad.clear @@ -269,7 +280,9 @@ class PryTester @pry = Pry.new(options) if context - @pry.binding_stack << Pry.binding_for(context) + target = Pry.binding_for(context) + @pry.binding_stack << target + @pry.inject_sticky_locals(target) end @pry.input_array << nil # TODO: shouldn't need this