diff options
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/abstract_unit.rb | 1 | ||||
-rw-r--r-- | activesupport/test/core_ext/duration_test.rb | 25 | ||||
-rw-r--r-- | activesupport/test/notifications_test.rb | 40 |
3 files changed, 52 insertions, 14 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index dda139372e..d91e0415c4 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -13,7 +13,6 @@ require 'mocha' ENV['NO_RELOAD'] = '1' require 'active_support' -require 'active_support/test_case' # Include shims until we get off 1.8.6 require 'active_support/ruby/shim' diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index 42b4f10172..6530de1ef4 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -2,6 +2,31 @@ require 'abstract_unit' require 'active_support/time' class DurationTest < ActiveSupport::TestCase + def test_is_a + d = 1.day + assert d.is_a?(ActiveSupport::Duration) + assert d.is_a?(Numeric) + assert d.is_a?(Fixnum) + assert !d.is_a?(Hash) + + k = Class.new + class << k; undef_method :== end + assert !d.is_a?(k) + end + + def test_threequals + assert ActiveSupport::Duration === 1.day + assert !(ActiveSupport::Duration === 1.day.to_i) + assert !(ActiveSupport::Duration === 'foo') + assert !(ActiveSupport::Duration === ActiveSupport::BasicObject.new) + end + + def test_equals + assert 1.day == 1.day + assert 1.day == 1.day.to_i + assert !(1.day == 'foo') + end + def test_inspect assert_equal '0 seconds', 0.seconds.inspect assert_equal '1 month', 1.month.inspect diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 3ba77ae135..c3eb1a4eb5 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -5,7 +5,8 @@ module Notifications def setup Thread.abort_on_exception = true - @notifier = ActiveSupport::Notifications::Notifier.new + ActiveSupport::Notifications.notifier = nil + @notifier = ActiveSupport::Notifications.notifier @events = [] @notifier.subscribe { |*args| @events << event(*args) } end @@ -82,13 +83,29 @@ module Notifications end class InstrumentationTest < TestCase + delegate :instrument, :instrument!, :to => ActiveSupport::Notifications + def test_instrument_returns_block_result - assert_equal 2, @notifier.instrument(:awesome) { 1 + 1 } + assert_equal 2, instrument(:awesome) { 1 + 1 } + drain + end + + def test_instrument_yields_the_paylod_for_further_modification + assert_equal 2, instrument(:awesome) { |p| p[:result] = 1 + 1 } + drain + + assert_equal 1, @events.size + assert_equal :awesome, @events.first.name + assert_equal Hash[:result => 2], @events.first.payload + end + + def test_instrumenter_exposes_its_id + assert_equal 20, ActiveSupport::Notifications.instrumenter.id.size end def test_nested_events_can_be_instrumented - @notifier.instrument(:awesome, :payload => "notifications") do - @notifier.instrument(:wot, :payload => "child") do + instrument(:awesome, :payload => "notifications") do + instrument(:wot, :payload => "child") do 1 + 1 end @@ -106,24 +123,21 @@ module Notifications assert_equal Hash[:payload => "notifications"], @events.last.payload end - def test_instrument_publishes_when_exception_is_raised + def test_instrument_does_not_publish_when_exception_is_raised begin - @notifier.instrument(:awesome, :payload => "notifications") do + instrument(:awesome, :payload => "notifications") do raise "OMG" end - flunk - rescue + rescue RuntimeError => e + assert_equal "OMG", e.message end drain - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "notifications"], @events.last.payload + assert_equal 0, @events.size end def test_event_is_pushed_even_without_block - @notifier.instrument(:awesome, :payload => "notifications") + instrument(:awesome, :payload => "notifications") drain assert_equal 1, @events.size |