From b2bb1aaf66673a4d5bcb63ed0f5c15023c99d3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Tue, 21 Jan 2014 23:49:02 -0200 Subject: Implement a simple stub feature to use in the Time travel helpers --- .../lib/active_support/testing/time_helpers.rb | 48 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 4 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 94230e56ba..0e48456715 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -1,7 +1,48 @@ module ActiveSupport module Testing + class SimpleStubs + Stub = Struct.new(:object, :method_name, :original_method) + + def initialize + @stubs = {} + end + + def stub_object(object, method_name, return_value) + key = [object.object_id, method_name] + + if (stub = @stubs[key]) + unstub_object(stub) + end + + @stubs[key] = Stub.new(object, method_name, object.method(method_name)) + + object.define_singleton_method(method_name) { return_value } + end + + def unstub_all! + @stubs.each do |_, stub| + unstub_object(stub) + end + @stubs = {} + end + + def unstub_object(stub) + stub.object.define_singleton_method(stub.method_name, stub.original_method) + end + end + # Containing helpers that helps you test passage of time. module TimeHelpers + def before_setup + super + @simple_stubs = SimpleStubs.new + end + + def after_teardown #:nodoc: + @simple_stubs.unstub_all! + super + end + # Change current time to the time in the future or in the past by a given time difference by # stubbing +Time.now+ and +Date.today+. Note that the stubs are automatically removed # at the end of each test. @@ -41,13 +82,12 @@ module ActiveSupport # end # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 def travel_to(date_or_time, &block) - Time.stubs now: date_or_time.to_time - Date.stubs today: date_or_time.to_date + @simple_stubs.stub_object(Time, :now, date_or_time.to_time) + @simple_stubs.stub_object(Date, :today, date_or_time.to_date) if block_given? block.call - Time.unstub :now - Date.unstub :today + @simple_stubs.unstub_all! end end end -- cgit v1.2.3 From 049a10d4051a48136199fcdfd77bf35df9e9ad11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 29 Jan 2014 21:07:46 -0200 Subject: Alias the original method first to avoid warnings --- activesupport/lib/active_support/testing/time_helpers.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 0e48456715..cd56ae1883 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -14,8 +14,11 @@ module ActiveSupport unstub_object(stub) end - @stubs[key] = Stub.new(object, method_name, object.method(method_name)) + new_name = "__simple_stub__#{method_name}" + @stubs[key] = Stub.new(object, method_name, new_name) + + object.singleton_class.send :alias_method, new_name, method_name object.define_singleton_method(method_name) { return_value } end @@ -27,7 +30,9 @@ module ActiveSupport end def unstub_object(stub) - stub.object.define_singleton_method(stub.method_name, stub.original_method) + stub.object.singleton_class.send :undef_method, stub.method_name + stub.object.singleton_class.send :alias_method, stub.method_name, stub.original_method + stub.object.singleton_class.send :undef_method, stub.original_method end end -- cgit v1.2.3 From 6dce4367c2bba894bb94e27cdfe4c56fdcc2c3df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 29 Jan 2014 21:10:10 -0200 Subject: Use instance method instead of before hook --- .../lib/active_support/testing/time_helpers.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index cd56ae1883..77b8ba261e 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -38,13 +38,8 @@ module ActiveSupport # Containing helpers that helps you test passage of time. module TimeHelpers - def before_setup - super - @simple_stubs = SimpleStubs.new - end - def after_teardown #:nodoc: - @simple_stubs.unstub_all! + simple_stubs.unstub_all! super end @@ -87,14 +82,18 @@ module ActiveSupport # end # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 def travel_to(date_or_time, &block) - @simple_stubs.stub_object(Time, :now, date_or_time.to_time) - @simple_stubs.stub_object(Date, :today, date_or_time.to_date) + simple_stubs.stub_object(Time, :now, date_or_time.to_time) + simple_stubs.stub_object(Date, :today, date_or_time.to_date) if block_given? block.call - @simple_stubs.unstub_all! + simple_stubs.unstub_all! end end + + def simple_stubs + @simple_stubs ||= SimpleStubs.new + end end end end -- cgit v1.2.3 From 7cf9a1c6a6cba5565817aa11edc227b6369685e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 29 Jan 2014 21:16:52 -0200 Subject: Change the class and method visibility --- .../lib/active_support/testing/time_helpers.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 77b8ba261e..af19c5a9f5 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -1,6 +1,6 @@ module ActiveSupport module Testing - class SimpleStubs + class SimpleStubs # :nodoc: Stub = Struct.new(:object, :method_name, :original_method) def initialize @@ -29,11 +29,13 @@ module ActiveSupport @stubs = {} end - def unstub_object(stub) - stub.object.singleton_class.send :undef_method, stub.method_name - stub.object.singleton_class.send :alias_method, stub.method_name, stub.original_method - stub.object.singleton_class.send :undef_method, stub.original_method - end + private + + def unstub_object(stub) + stub.object.singleton_class.send :undef_method, stub.method_name + stub.object.singleton_class.send :alias_method, stub.method_name, stub.original_method + stub.object.singleton_class.send :undef_method, stub.original_method + end end # Containing helpers that helps you test passage of time. @@ -91,9 +93,11 @@ module ActiveSupport end end - def simple_stubs - @simple_stubs ||= SimpleStubs.new - end + private + + def simple_stubs + @simple_stubs ||= SimpleStubs.new + end end end end -- cgit v1.2.3 From 17cb1266c7b91c934dcef2afe38ea4dab677ef91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 29 Jan 2014 21:17:05 -0200 Subject: Store the singleton_class in a local variable --- activesupport/lib/active_support/testing/time_helpers.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index af19c5a9f5..84cbdf9608 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -32,9 +32,10 @@ module ActiveSupport private def unstub_object(stub) - stub.object.singleton_class.send :undef_method, stub.method_name - stub.object.singleton_class.send :alias_method, stub.method_name, stub.original_method - stub.object.singleton_class.send :undef_method, stub.original_method + singleton_class = stub.object.singleton_class + singleton_class.send :undef_method, stub.method_name + singleton_class.send :alias_method, stub.method_name, stub.original_method + singleton_class.send :undef_method, stub.original_method end end -- cgit v1.2.3 From 06e06cd454f3699d970c7dc8404170ac236c7743 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 29 Jan 2014 21:22:15 -0200 Subject: Use each_value --- activesupport/lib/active_support/testing/time_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport') diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 84cbdf9608..0ee6332d6f 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -23,7 +23,7 @@ module ActiveSupport end def unstub_all! - @stubs.each do |_, stub| + @stubs.each_value do |stub| unstub_object(stub) end @stubs = {} -- cgit v1.2.3