From 21e7b8462113fc7c0fd1882dcc2bf7225de67b1a Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 12 Oct 2009 22:15:43 -0500 Subject: Callbacks, DeprecatedCallbacks = NewCallbacks, Callbacks --- activesupport/test/callback_inheritance_test.rb | 115 ++++ activesupport/test/callbacks_test.rb | 632 ++++++++++++++++----- .../test/new_callback_inheritance_test.rb | 115 ---- activesupport/test/new_callbacks_test.rb | 528 ----------------- 4 files changed, 601 insertions(+), 789 deletions(-) create mode 100644 activesupport/test/callback_inheritance_test.rb delete mode 100644 activesupport/test/new_callback_inheritance_test.rb delete mode 100644 activesupport/test/new_callbacks_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/callback_inheritance_test.rb b/activesupport/test/callback_inheritance_test.rb new file mode 100644 index 0000000000..18721eab19 --- /dev/null +++ b/activesupport/test/callback_inheritance_test.rb @@ -0,0 +1,115 @@ +require 'test/unit' +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'active_support' + +class GrandParent + include ActiveSupport::Callbacks + + attr_reader :log, :action_name + def initialize(action_name) + @action_name, @log = action_name, [] + end + + define_callbacks :dispatch + set_callback :dispatch, :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }} + set_callback :dispatch, :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }} + + def before1 + @log << "before1" + end + + def before2 + @log << "before2" + end + + def after1 + @log << "after1" + end + + def after2 + @log << "after2" + end + + def dispatch + run_callbacks(:dispatch, action_name) do + @log << action_name + end + self + end +end + +class Parent < GrandParent + skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }} + skip_callback :dispatch, :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }} +end + +class Child < GrandParent + skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open? + + def state_open? + @state == :open + end + + def initialize(action_name, state) + super(action_name) + @state = state + end +end + + +class BasicCallbacksTest < Test::Unit::TestCase + def setup + @index = GrandParent.new("index").dispatch + @update = GrandParent.new("update").dispatch + @delete = GrandParent.new("delete").dispatch + @unknown = GrandParent.new("unknown").dispatch + end + + def test_basic_per_key1 + assert_equal %w(before1 before2 index), @index.log + end + + def test_basic_per_key2 + assert_equal %w(before1 before2 update after2 after1), @update.log + end + + def test_basic_per_key3 + assert_equal %w(delete after2 after1), @delete.log + end +end + +class InheritedCallbacksTest < Test::Unit::TestCase + def setup + @index = Parent.new("index").dispatch + @update = Parent.new("update").dispatch + @delete = Parent.new("delete").dispatch + @unknown = Parent.new("unknown").dispatch + end + + def test_inherited_excluded + assert_equal %w(before1 index), @index.log + end + + def test_inherited_not_excluded + assert_equal %w(before1 before2 update after1), @update.log + end + + def test_partially_excluded + assert_equal %w(delete after2 after1), @delete.log + end +end + +class InheritedCallbacksTest2 < Test::Unit::TestCase + def setup + @update1 = Child.new("update", :open).dispatch + @update2 = Child.new("update", :closed).dispatch + end + + def test_crazy_mix_on + assert_equal %w(before1 update after2 after1), @update1.log + end + + def test_crazy_mix_off + assert_equal %w(before1 before2 update after2 after1), @update2.log + end +end diff --git a/activesupport/test/callbacks_test.rb b/activesupport/test/callbacks_test.rb index 2f747e2238..df98644436 100644 --- a/activesupport/test/callbacks_test.rb +++ b/activesupport/test/callbacks_test.rb @@ -1,188 +1,528 @@ -require 'abstract_unit' +# require 'abstract_unit' +require 'test/unit' +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'active_support' -class Record - include ActiveSupport::Callbacks +module CallbacksTest + class Record + include ActiveSupport::Callbacks - define_callbacks :before_save, :after_save + define_callbacks :save - class << self - def callback_symbol(callback_method) - method_name = "#{callback_method}_method" - define_method(method_name) do - history << [callback_method, :symbol] + def self.before_save(*filters, &blk) + set_callback(:save, :before, *filters, &blk) + end + + def self.after_save(*filters, &blk) + set_callback(:save, :after, *filters, &blk) + end + + class << self + def callback_symbol(callback_method) + method_name = :"#{callback_method}_method" + define_method(method_name) do + history << [callback_method, :symbol] + end + method_name + end + + def callback_string(callback_method) + "history << [#{callback_method.to_sym.inspect}, :string]" + end + + def callback_proc(callback_method) + Proc.new { |model| model.history << [callback_method, :proc] } + end + + def callback_object(callback_method) + klass = Class.new + klass.send(:define_method, callback_method) do |model| + model.history << [:"#{callback_method}_save", :object] + end + klass.new end - method_name end - def callback_string(callback_method) - "history << [#{callback_method.to_sym.inspect}, :string]" + def history + @history ||= [] + end + end + + class Person < Record + [:before_save, :after_save].each do |callback_method| + callback_method_sym = callback_method.to_sym + send(callback_method, callback_symbol(callback_method_sym)) + send(callback_method, callback_string(callback_method_sym)) + send(callback_method, callback_proc(callback_method_sym)) + send(callback_method, callback_object(callback_method_sym.to_s.gsub(/_save/, ''))) + send(callback_method) { |model| model.history << [callback_method_sym, :block] } end - def callback_proc(callback_method) - Proc.new { |model| model.history << [callback_method, :proc] } + def save + run_callbacks :save end + end + + class PersonSkipper < Person + skip_callback :save, :before, :before_save_method, :if => :yes + skip_callback :save, :after, :before_save_method, :unless => :yes + skip_callback :save, :after, :before_save_method, :if => :no + skip_callback :save, :before, :before_save_method, :unless => :no + def yes; true; end + def no; false; end + end + + class ParentController + include ActiveSupport::Callbacks + + define_callbacks :dispatch + + set_callback :dispatch, :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }} + set_callback :dispatch, :after, :log2 - def callback_object(callback_method) - klass = Class.new - klass.send(:define_method, callback_method) do |model| - model.history << [callback_method, :object] + attr_reader :action_name, :logger + def initialize(action_name) + @action_name, @logger = action_name, [] + end + + def log + @logger << action_name + end + + def log2 + @logger << action_name + end + + def dispatch + run_callbacks :dispatch, action_name do + @logger << "Done" end - klass.new + self end end - def history - @history ||= [] + class Child < ParentController + skip_callback :dispatch, :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} } + skip_callback :dispatch, :after, :log2 end -end -class Person < Record - [:before_save, :after_save].each do |callback_method| - callback_method_sym = callback_method.to_sym - send(callback_method, callback_symbol(callback_method_sym)) - send(callback_method, callback_string(callback_method_sym)) - send(callback_method, callback_proc(callback_method_sym)) - send(callback_method, callback_object(callback_method_sym)) - send(callback_method) { |model| model.history << [callback_method_sym, :block] } + class OneTimeCompile < Record + @@starts_true, @@starts_false = true, false + + def initialize + super + end + + before_save Proc.new {|r| r.history << [:before_save, :starts_true, :if] }, :per_key => {:if => :starts_true} + before_save Proc.new {|r| r.history << [:before_save, :starts_false, :if] }, :per_key => {:if => :starts_false} + before_save Proc.new {|r| r.history << [:before_save, :starts_true, :unless] }, :per_key => {:unless => :starts_true} + before_save Proc.new {|r| r.history << [:before_save, :starts_false, :unless] }, :per_key => {:unless => :starts_false} + + def starts_true + if @@starts_true + @@starts_true = false + return true + end + @@starts_true + end + + def starts_false + unless @@starts_false + @@starts_false = true + return false + end + @@starts_false + end + + def save + run_callbacks :save, :action + end end - def save - run_callbacks(:before_save) - run_callbacks(:after_save) + class OneTimeCompileTest < Test::Unit::TestCase + def test_optimized_first_compile + around = OneTimeCompile.new + around.save + assert_equal [ + [:before_save, :starts_true, :if], + [:before_save, :starts_true, :unless] + ], around.history + end end -end -class ConditionalPerson < Record - # proc - before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true } - before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false } - before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false } - before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true } - # symbol - before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :if => :yes - before_save Proc.new { |r| r.history << "b00m" }, :if => :no - before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :unless => :no - before_save Proc.new { |r| r.history << "b00m" }, :unless => :yes - # string - before_save Proc.new { |r| r.history << [:before_save, :string] }, :if => 'yes' - before_save Proc.new { |r| r.history << "b00m" }, :if => 'no' - before_save Proc.new { |r| r.history << [:before_save, :string] }, :unless => 'no' - before_save Proc.new { |r| r.history << "b00m" }, :unless => 'yes' - # Array with conditions - before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :if => [:yes, :other_yes] - before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, :no] - before_save Proc.new { |r| r.history << [:before_save, :symbol_array] }, :unless => [:no, :other_no] - before_save Proc.new { |r| r.history << "b00m" }, :unless => [:yes, :no] - # Combined if and unless - before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, :if => :yes, :unless => :no - before_save Proc.new { |r| r.history << "b00m" }, :if => :yes, :unless => :yes - # Array with different types of conditions - before_save Proc.new { |r| r.history << [:before_save, :symbol_proc_string_array] }, :if => [:yes, Proc.new { |r| true }, 'yes'] - before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no'] - # Array with different types of conditions comibned if and unless - before_save Proc.new { |r| r.history << [:before_save, :combined_symbol_proc_string_array] }, - :if => [:yes, Proc.new { |r| true }, 'yes'], :unless => [:no, 'no'] - before_save Proc.new { |r| r.history << "b00m" }, :if => [:yes, Proc.new { |r| true }, 'no'], :unless => [:no, 'no'] - - def yes; true; end - def other_yes; true; end - def no; false; end - def other_no; false; end - - def save - run_callbacks(:before_save) - run_callbacks(:after_save) + class ConditionalPerson < Record + # proc + before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true } + before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false } + before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false } + before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true } + # symbol + before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :if => :yes + before_save Proc.new { |r| r.history << "b00m" }, :if => :no + before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :unless => :no + before_save Proc.new { |r| r.history << "b00m" }, :unless => :yes + # string + before_save Proc.new { |r| r.history << [:before_save, :string] }, :if => 'yes' + before_save Proc.new { |r| r.history << "b00m" }, :if => 'no' + before_save Proc.new { |r| r.history << [:before_save, :string] }, :unless => 'no' + before_save Proc.new { |r| r.history << "b00m" }, :unless => 'yes' + # Combined if and unless + before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, :if => :yes, :unless => :no + before_save Proc.new { |r| r.history << "b00m" }, :if => :yes, :unless => :yes + + def yes; true; end + def other_yes; true; end + def no; false; end + def other_no; false; end + + def save + run_callbacks :save + end end -end -class CallbacksTest < Test::Unit::TestCase - def test_save_person - person = Person.new - assert_equal [], person.history - person.save - assert_equal [ - [:before_save, :symbol], - [:before_save, :string], - [:before_save, :proc], - [:before_save, :object], - [:before_save, :block], - [:after_save, :symbol], - [:after_save, :string], - [:after_save, :proc], - [:after_save, :object], - [:after_save, :block] - ], person.history + class CleanPerson < ConditionalPerson + reset_callbacks :save end -end -class ConditionalCallbackTest < Test::Unit::TestCase - def test_save_conditional_person - person = ConditionalPerson.new - person.save - assert_equal [ - [:before_save, :proc], - [:before_save, :proc], - [:before_save, :symbol], - [:before_save, :symbol], - [:before_save, :string], - [:before_save, :string], - [:before_save, :symbol_array], - [:before_save, :symbol_array], - [:before_save, :combined_symbol], - [:before_save, :symbol_proc_string_array], - [:before_save, :combined_symbol_proc_string_array] - ], person.history + class MySuper + include ActiveSupport::Callbacks + define_callbacks :save end -end -class CallbackTest < Test::Unit::TestCase - include ActiveSupport::Callbacks + class AroundPerson < MySuper + attr_reader :history + + set_callback :save, :before, :nope, :if => :no + set_callback :save, :before, :nope, :unless => :yes + set_callback :save, :after, :tweedle + set_callback :save, :before, "tweedle_dee" + set_callback :save, :before, proc {|m| m.history << "yup" } + set_callback :save, :before, :nope, :if => proc { false } + set_callback :save, :before, :nope, :unless => proc { true } + set_callback :save, :before, :yup, :if => proc { true } + set_callback :save, :before, :yup, :unless => proc { false } + set_callback :save, :around, :tweedle_dum + set_callback :save, :around, :w0tyes, :if => :yes + set_callback :save, :around, :w0tno, :if => :no + set_callback :save, :around, :tweedle_deedle + + def no; false; end + def yes; true; end + + def nope + @history << "boom" + end + + def yup + @history << "yup" + end + + def w0tyes + @history << "w0tyes before" + yield + @history << "w0tyes after" + end + + def w0tno + @history << "boom" + yield + end + + def tweedle_dee + @history << "tweedle dee" + end + + def tweedle_dum + @history << "tweedle dum pre" + yield + @history << "tweedle dum post" + end + + def tweedle + @history << "tweedle" + end + + def tweedle_deedle + @history << "tweedle deedle pre" + yield + @history << "tweedle deedle post" + end + + def initialize + @history = [] + end - def test_eql - callback = Callback.new(:before, :save, :identifier => :lifesaver) - assert callback.eql?(Callback.new(:before, :save, :identifier => :lifesaver)) - assert callback.eql?(Callback.new(:before, :save)) - assert callback.eql?(:lifesaver) - assert callback.eql?(:save) - assert !callback.eql?(Callback.new(:before, :destroy)) - assert !callback.eql?(:destroy) + def save + run_callbacks :save do + @history << "running" + end + end end - def test_dup - a = Callback.new(:before, :save) - assert_equal({}, a.options) - b = a.dup - b.options[:unless] = :pigs_fly - assert_equal({:unless => :pigs_fly}, b.options) - assert_equal({}, a.options) + class HyphenatedCallbacks + include ActiveSupport::Callbacks + define_callbacks :save + attr_reader :stuff + + set_callback :save, :before, :omg, :per_key => {:if => :yes} + + def yes() true end + + def omg + @stuff = "OMG" + end + + def save + run_callbacks :save, "hyphen-ated" do + @stuff + end + end + end + + class AroundCallbacksTest < Test::Unit::TestCase + def test_save_around + around = AroundPerson.new + around.save + assert_equal [ + "tweedle dee", + "yup", "yup", + "tweedle dum pre", + "w0tyes before", + "tweedle deedle pre", + "running", + "tweedle deedle post", + "w0tyes after", + "tweedle dum post", + "tweedle" + ], around.history + end + end + + class SkipCallbacksTest < Test::Unit::TestCase + def test_skip_person + person = PersonSkipper.new + assert_equal [], person.history + person.save + assert_equal [ + [:before_save, :string], + [:before_save, :proc], + [:before_save, :object], + [:before_save, :block], + [:after_save, :block], + [:after_save, :object], + [:after_save, :proc], + [:after_save, :string], + [:after_save, :symbol] + ], person.history + end + end + + class CallbacksTest < Test::Unit::TestCase + def test_save_person + person = Person.new + assert_equal [], person.history + person.save + assert_equal [ + [:before_save, :symbol], + [:before_save, :string], + [:before_save, :proc], + [:before_save, :object], + [:before_save, :block], + [:after_save, :block], + [:after_save, :object], + [:after_save, :proc], + [:after_save, :string], + [:after_save, :symbol] + ], person.history + end + end + + class ConditionalCallbackTest < Test::Unit::TestCase + def test_save_conditional_person + person = ConditionalPerson.new + person.save + assert_equal [ + [:before_save, :proc], + [:before_save, :proc], + [:before_save, :symbol], + [:before_save, :symbol], + [:before_save, :string], + [:before_save, :string], + [:before_save, :combined_symbol], + ], person.history + end + end + + class ResetCallbackTest < Test::Unit::TestCase + def test_save_conditional_person + person = CleanPerson.new + person.save + assert_equal [], person.history + end + end + + class CallbackTerminator + include ActiveSupport::Callbacks + + define_callbacks :save, :terminator => "result == :halt" + + set_callback :save, :before, :first + set_callback :save, :before, :second + set_callback :save, :around, :around_it + set_callback :save, :before, :third + set_callback :save, :after, :first + set_callback :save, :around, :around_it + set_callback :save, :after, :second + set_callback :save, :around, :around_it + set_callback :save, :after, :third + + + attr_reader :history, :saved + def initialize + @history = [] + end + + def around_it + @history << "around1" + yield + @history << "around2" + end + + def first + @history << "first" + end + + def second + @history << "second" + :halt + end + + def third + @history << "third" + end + + def save + run_callbacks :save do + @saved = true + end + end + end + + class CallbackObject + def before(caller) + caller.record << "before" + end + + def before_save(caller) + caller.record << "before save" + end + + def around(caller) + caller.record << "around before" + yield + caller.record << "around after" + end + end + + class UsingObjectBefore + include ActiveSupport::Callbacks + + define_callbacks :save + set_callback :save, :before, CallbackObject.new + + attr_accessor :record + def initialize + @record = [] + end + + def save + run_callbacks :save do + @record << "yielded" + end + end end -end -class CallbackChainTest < Test::Unit::TestCase - include ActiveSupport::Callbacks + class UsingObjectAround + include ActiveSupport::Callbacks - def setup - @chain = CallbackChain.build(:make, :bacon, :lettuce, :tomato) + define_callbacks :save + set_callback :save, :around, CallbackObject.new + + attr_accessor :record + def initialize + @record = [] + end + + def save + run_callbacks :save do + @record << "yielded" + end + end end - def test_build - assert_equal 3, @chain.size - assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method) + class CustomScopeObject + include ActiveSupport::Callbacks + + define_callbacks :save, :scope => [:kind, :name] + set_callback :save, :before, CallbackObject.new + + attr_accessor :record + def initialize + @record = [] + end + + def save + run_callbacks :save do + @record << "yielded" + "CallbackResult" + end + end end - def test_find - assert_equal :bacon, @chain.find(:bacon).method + class UsingObjectTest < Test::Unit::TestCase + def test_before_object + u = UsingObjectBefore.new + u.save + assert_equal ["before", "yielded"], u.record + end + + def test_around_object + u = UsingObjectAround.new + u.save + assert_equal ["around before", "yielded", "around after"], u.record + end + + def test_customized_object + u = CustomScopeObject.new + u.save + assert_equal ["before save", "yielded"], u.record + end + + def test_block_result_is_returned + u = CustomScopeObject.new + assert_equal "CallbackResult", u.save + end end - def test_replace_or_append - assert_equal [:bacon, :lettuce, :tomato], (@chain.replace_or_append!(Callback.new(:make, :bacon))).map(&:method) - assert_equal [:bacon, :lettuce, :tomato, :turkey], (@chain.replace_or_append!(Callback.new(:make, :turkey))).map(&:method) - assert_equal [:bacon, :lettuce, :tomato, :turkey, :mayo], (@chain.replace_or_append!(Callback.new(:make, :mayo))).map(&:method) + class CallbackTerminatorTest < Test::Unit::TestCase + def test_termination + terminator = CallbackTerminator.new + terminator.save + assert_equal ["first", "second", "third", "second", "first"], terminator.history + end + + def test_block_never_called_if_terminated + obj = CallbackTerminator.new + obj.save + assert !obj.saved + end end - def test_delete - assert_equal [:bacon, :lettuce, :tomato], @chain.map(&:method) - @chain.delete(:bacon) - assert_equal [:lettuce, :tomato], @chain.map(&:method) + class HyphenatedKeyTest < Test::Unit::TestCase + def test_save + obj = HyphenatedCallbacks.new + obj.save + assert_equal obj.stuff, "OMG" + end end end diff --git a/activesupport/test/new_callback_inheritance_test.rb b/activesupport/test/new_callback_inheritance_test.rb deleted file mode 100644 index fe316ae3da..0000000000 --- a/activesupport/test/new_callback_inheritance_test.rb +++ /dev/null @@ -1,115 +0,0 @@ -require 'test/unit' -$:.unshift "#{File.dirname(__FILE__)}/../lib" -require 'active_support' - -class GrandParent - include ActiveSupport::NewCallbacks - - attr_reader :log, :action_name - def initialize(action_name) - @action_name, @log = action_name, [] - end - - define_callbacks :dispatch - set_callback :dispatch, :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }} - set_callback :dispatch, :after, :after1, :after2, :per_key => {:if => proc {|c| c.action_name == "update" || c.action_name == "delete" }} - - def before1 - @log << "before1" - end - - def before2 - @log << "before2" - end - - def after1 - @log << "after1" - end - - def after2 - @log << "after2" - end - - def dispatch - _run_dispatch_callbacks(action_name) do - @log << action_name - end - self - end -end - -class Parent < GrandParent - skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }} - skip_callback :dispatch, :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }} -end - -class Child < GrandParent - skip_callback :dispatch, :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }}, :if => :state_open? - - def state_open? - @state == :open - end - - def initialize(action_name, state) - super(action_name) - @state = state - end -end - - -class BasicCallbacksTest < Test::Unit::TestCase - def setup - @index = GrandParent.new("index").dispatch - @update = GrandParent.new("update").dispatch - @delete = GrandParent.new("delete").dispatch - @unknown = GrandParent.new("unknown").dispatch - end - - def test_basic_per_key1 - assert_equal %w(before1 before2 index), @index.log - end - - def test_basic_per_key2 - assert_equal %w(before1 before2 update after2 after1), @update.log - end - - def test_basic_per_key3 - assert_equal %w(delete after2 after1), @delete.log - end -end - -class InheritedCallbacksTest < Test::Unit::TestCase - def setup - @index = Parent.new("index").dispatch - @update = Parent.new("update").dispatch - @delete = Parent.new("delete").dispatch - @unknown = Parent.new("unknown").dispatch - end - - def test_inherited_excluded - assert_equal %w(before1 index), @index.log - end - - def test_inherited_not_excluded - assert_equal %w(before1 before2 update after1), @update.log - end - - def test_partially_excluded - assert_equal %w(delete after2 after1), @delete.log - end -end - -class InheritedCallbacksTest2 < Test::Unit::TestCase - def setup - @update1 = Child.new("update", :open).dispatch - @update2 = Child.new("update", :closed).dispatch - end - - def test_crazy_mix_on - assert_equal %w(before1 update after2 after1), @update1.log - end - - def test_crazy_mix_off - assert_equal %w(before1 before2 update after2 after1), @update2.log - end -end diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb deleted file mode 100644 index 04db376fc6..0000000000 --- a/activesupport/test/new_callbacks_test.rb +++ /dev/null @@ -1,528 +0,0 @@ -# require 'abstract_unit' -require 'test/unit' -$:.unshift "#{File.dirname(__FILE__)}/../lib" -require 'active_support' - -module NewCallbacksTest - class Record - include ActiveSupport::NewCallbacks - - define_callbacks :save - - def self.before_save(*filters, &blk) - set_callback(:save, :before, *filters, &blk) - end - - def self.after_save(*filters, &blk) - set_callback(:save, :after, *filters, &blk) - end - - class << self - def callback_symbol(callback_method) - method_name = :"#{callback_method}_method" - define_method(method_name) do - history << [callback_method, :symbol] - end - method_name - end - - def callback_string(callback_method) - "history << [#{callback_method.to_sym.inspect}, :string]" - end - - def callback_proc(callback_method) - Proc.new { |model| model.history << [callback_method, :proc] } - end - - def callback_object(callback_method) - klass = Class.new - klass.send(:define_method, callback_method) do |model| - model.history << [:"#{callback_method}_save", :object] - end - klass.new - end - end - - def history - @history ||= [] - end - end - - class Person < Record - [:before_save, :after_save].each do |callback_method| - callback_method_sym = callback_method.to_sym - send(callback_method, callback_symbol(callback_method_sym)) - send(callback_method, callback_string(callback_method_sym)) - send(callback_method, callback_proc(callback_method_sym)) - send(callback_method, callback_object(callback_method_sym.to_s.gsub(/_save/, ''))) - send(callback_method) { |model| model.history << [callback_method_sym, :block] } - end - - def save - _run_save_callbacks {} - end - end - - class PersonSkipper < Person - skip_callback :save, :before, :before_save_method, :if => :yes - skip_callback :save, :after, :before_save_method, :unless => :yes - skip_callback :save, :after, :before_save_method, :if => :no - skip_callback :save, :before, :before_save_method, :unless => :no - def yes; true; end - def no; false; end - end - - class ParentController - include ActiveSupport::NewCallbacks - - define_callbacks :dispatch - - set_callback :dispatch, :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }} - set_callback :dispatch, :after, :log2 - - attr_reader :action_name, :logger - def initialize(action_name) - @action_name, @logger = action_name, [] - end - - def log - @logger << action_name - end - - def log2 - @logger << action_name - end - - def dispatch - _run_dispatch_callbacks(action_name) { - @logger << "Done" - } - self - end - end - - class Child < ParentController - skip_callback :dispatch, :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} } - skip_callback :dispatch, :after, :log2 - end - - class OneTimeCompile < Record - @@starts_true, @@starts_false = true, false - - def initialize - super - end - - before_save Proc.new {|r| r.history << [:before_save, :starts_true, :if] }, :per_key => {:if => :starts_true} - before_save Proc.new {|r| r.history << [:before_save, :starts_false, :if] }, :per_key => {:if => :starts_false} - before_save Proc.new {|r| r.history << [:before_save, :starts_true, :unless] }, :per_key => {:unless => :starts_true} - before_save Proc.new {|r| r.history << [:before_save, :starts_false, :unless] }, :per_key => {:unless => :starts_false} - - def starts_true - if @@starts_true - @@starts_true = false - return true - end - @@starts_true - end - - def starts_false - unless @@starts_false - @@starts_false = true - return false - end - @@starts_false - end - - def save - _run_save_callbacks(:action) {} - end - end - - class OneTimeCompileTest < Test::Unit::TestCase - def test_optimized_first_compile - around = OneTimeCompile.new - around.save - assert_equal [ - [:before_save, :starts_true, :if], - [:before_save, :starts_true, :unless] - ], around.history - end - end - - class ConditionalPerson < Record - # proc - before_save Proc.new { |r| r.history << [:before_save, :proc] }, :if => Proc.new { |r| true } - before_save Proc.new { |r| r.history << "b00m" }, :if => Proc.new { |r| false } - before_save Proc.new { |r| r.history << [:before_save, :proc] }, :unless => Proc.new { |r| false } - before_save Proc.new { |r| r.history << "b00m" }, :unless => Proc.new { |r| true } - # symbol - before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :if => :yes - before_save Proc.new { |r| r.history << "b00m" }, :if => :no - before_save Proc.new { |r| r.history << [:before_save, :symbol] }, :unless => :no - before_save Proc.new { |r| r.history << "b00m" }, :unless => :yes - # string - before_save Proc.new { |r| r.history << [:before_save, :string] }, :if => 'yes' - before_save Proc.new { |r| r.history << "b00m" }, :if => 'no' - before_save Proc.new { |r| r.history << [:before_save, :string] }, :unless => 'no' - before_save Proc.new { |r| r.history << "b00m" }, :unless => 'yes' - # Combined if and unless - before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, :if => :yes, :unless => :no - before_save Proc.new { |r| r.history << "b00m" }, :if => :yes, :unless => :yes - - def yes; true; end - def other_yes; true; end - def no; false; end - def other_no; false; end - - def save - _run_save_callbacks {} - end - end - - class CleanPerson < ConditionalPerson - reset_callbacks :save - end - - class MySuper - include ActiveSupport::NewCallbacks - define_callbacks :save - end - - class AroundPerson < MySuper - attr_reader :history - - set_callback :save, :before, :nope, :if => :no - set_callback :save, :before, :nope, :unless => :yes - set_callback :save, :after, :tweedle - set_callback :save, :before, "tweedle_dee" - set_callback :save, :before, proc {|m| m.history << "yup" } - set_callback :save, :before, :nope, :if => proc { false } - set_callback :save, :before, :nope, :unless => proc { true } - set_callback :save, :before, :yup, :if => proc { true } - set_callback :save, :before, :yup, :unless => proc { false } - set_callback :save, :around, :tweedle_dum - set_callback :save, :around, :w0tyes, :if => :yes - set_callback :save, :around, :w0tno, :if => :no - set_callback :save, :around, :tweedle_deedle - - def no; false; end - def yes; true; end - - def nope - @history << "boom" - end - - def yup - @history << "yup" - end - - def w0tyes - @history << "w0tyes before" - yield - @history << "w0tyes after" - end - - def w0tno - @history << "boom" - yield - end - - def tweedle_dee - @history << "tweedle dee" - end - - def tweedle_dum - @history << "tweedle dum pre" - yield - @history << "tweedle dum post" - end - - def tweedle - @history << "tweedle" - end - - def tweedle_deedle - @history << "tweedle deedle pre" - yield - @history << "tweedle deedle post" - end - - def initialize - @history = [] - end - - def save - _run_save_callbacks do - @history << "running" - end - end - end - - class HyphenatedCallbacks - include ActiveSupport::NewCallbacks - define_callbacks :save - attr_reader :stuff - - set_callback :save, :before, :omg, :per_key => {:if => :yes} - - def yes() true end - - def omg - @stuff = "OMG" - end - - def save - _run_save_callbacks("hyphen-ated") do - @stuff - end - end - end - - class AroundCallbacksTest < Test::Unit::TestCase - def test_save_around - around = AroundPerson.new - around.save - assert_equal [ - "tweedle dee", - "yup", "yup", - "tweedle dum pre", - "w0tyes before", - "tweedle deedle pre", - "running", - "tweedle deedle post", - "w0tyes after", - "tweedle dum post", - "tweedle" - ], around.history - end - end - - class SkipCallbacksTest < Test::Unit::TestCase - def test_skip_person - person = PersonSkipper.new - assert_equal [], person.history - person.save - assert_equal [ - [:before_save, :string], - [:before_save, :proc], - [:before_save, :object], - [:before_save, :block], - [:after_save, :block], - [:after_save, :object], - [:after_save, :proc], - [:after_save, :string], - [:after_save, :symbol] - ], person.history - end - end - - class CallbacksTest < Test::Unit::TestCase - def test_save_person - person = Person.new - assert_equal [], person.history - person.save - assert_equal [ - [:before_save, :symbol], - [:before_save, :string], - [:before_save, :proc], - [:before_save, :object], - [:before_save, :block], - [:after_save, :block], - [:after_save, :object], - [:after_save, :proc], - [:after_save, :string], - [:after_save, :symbol] - ], person.history - end - end - - class ConditionalCallbackTest < Test::Unit::TestCase - def test_save_conditional_person - person = ConditionalPerson.new - person.save - assert_equal [ - [:before_save, :proc], - [:before_save, :proc], - [:before_save, :symbol], - [:before_save, :symbol], - [:before_save, :string], - [:before_save, :string], - [:before_save, :combined_symbol], - ], person.history - end - end - - class ResetCallbackTest < Test::Unit::TestCase - def test_save_conditional_person - person = CleanPerson.new - person.save - assert_equal [], person.history - end - end - - class CallbackTerminator - include ActiveSupport::NewCallbacks - - define_callbacks :save, :terminator => "result == :halt" - - set_callback :save, :before, :first - set_callback :save, :before, :second - set_callback :save, :around, :around_it - set_callback :save, :before, :third - set_callback :save, :after, :first - set_callback :save, :around, :around_it - set_callback :save, :after, :second - set_callback :save, :around, :around_it - set_callback :save, :after, :third - - - attr_reader :history, :saved - def initialize - @history = [] - end - - def around_it - @history << "around1" - yield - @history << "around2" - end - - def first - @history << "first" - end - - def second - @history << "second" - :halt - end - - def third - @history << "third" - end - - def save - _run_save_callbacks do - @saved = true - end - end - end - - class CallbackObject - def before(caller) - caller.record << "before" - end - - def before_save(caller) - caller.record << "before save" - end - - def around(caller) - caller.record << "around before" - yield - caller.record << "around after" - end - end - - class UsingObjectBefore - include ActiveSupport::NewCallbacks - - define_callbacks :save - set_callback :save, :before, CallbackObject.new - - attr_accessor :record - def initialize - @record = [] - end - - def save - _run_save_callbacks do - @record << "yielded" - end - end - end - - class UsingObjectAround - include ActiveSupport::NewCallbacks - - define_callbacks :save - set_callback :save, :around, CallbackObject.new - - attr_accessor :record - def initialize - @record = [] - end - - def save - _run_save_callbacks do - @record << "yielded" - end - end - end - - class CustomScopeObject - include ActiveSupport::NewCallbacks - - define_callbacks :save, :scope => [:kind, :name] - set_callback :save, :before, CallbackObject.new - - attr_accessor :record - def initialize - @record = [] - end - - def save - _run_save_callbacks do - @record << "yielded" - "CallbackResult" - end - end - end - - class UsingObjectTest < Test::Unit::TestCase - def test_before_object - u = UsingObjectBefore.new - u.save - assert_equal ["before", "yielded"], u.record - end - - def test_around_object - u = UsingObjectAround.new - u.save - assert_equal ["around before", "yielded", "around after"], u.record - end - - def test_customized_object - u = CustomScopeObject.new - u.save - assert_equal ["before save", "yielded"], u.record - end - - def test_block_result_is_returned - u = CustomScopeObject.new - assert_equal "CallbackResult", u.save - end - end - - class CallbackTerminatorTest < Test::Unit::TestCase - def test_termination - terminator = CallbackTerminator.new - terminator.save - assert_equal ["first", "second", "third", "second", "first"], terminator.history - end - - def test_block_never_called_if_terminated - obj = CallbackTerminator.new - obj.save - assert !obj.saved - end - end - - class HyphenatedKeyTest < Test::Unit::TestCase - def test_save - obj = HyphenatedCallbacks.new - obj.save - assert_equal obj.stuff, "OMG" - end - end -end -- cgit v1.2.3 From 29b280666b6a8216a46b8349fa76c85f5b5dcc55 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 12 Oct 2009 23:03:02 -0500 Subject: Get AS TestCase off deprecated callbacks --- activesupport/test/test_test.rb | 56 ++--------------------------------------- 1 file changed, 2 insertions(+), 54 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 40d3d612e7..7a45dab60b 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -4,7 +4,7 @@ require 'active_support/core_ext/kernel/reporting' class AssertDifferenceTest < ActiveSupport::TestCase def setup @object = Class.new do - attr_accessor :num + attr_accessor :num def increment self.num += 1 end @@ -12,7 +12,7 @@ class AssertDifferenceTest < ActiveSupport::TestCase def decrement self.num -= 1 end - end.new + end.new @object.num = 0 end @@ -95,55 +95,3 @@ end class AlsoDoingNothingTest < ActiveSupport::TestCase end - -# Setup and teardown callbacks. -class SetupAndTeardownTest < ActiveSupport::TestCase - setup :reset_callback_record, :foo - teardown :foo, :sentinel, :foo - - def test_inherited_setup_callbacks - assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method) - assert_equal [:foo], @called_back - assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method) - end - - def setup - end - - def teardown - end - - protected - def reset_callback_record - @called_back = [] - end - - def foo - @called_back << :foo - end - - def sentinel - assert_equal [:foo, :foo], @called_back - end -end - - -class SubclassSetupAndTeardownTest < SetupAndTeardownTest - setup :bar - teardown :bar - - def test_inherited_setup_callbacks - assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method) - assert_equal [:foo, :bar], @called_back - assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method) - end - - protected - def bar - @called_back << :bar - end - - def sentinel - assert_equal [:foo, :bar, :bar, :foo], @called_back - end -end -- cgit v1.2.3 From fc46c9b2207c62d4b029c2c891c61fc660c0b627 Mon Sep 17 00:00:00 2001 From: Jacob Lauemoeller Date: Fri, 2 Oct 2009 16:34:28 +0200 Subject: Added CDATA support to the XmlMini LibXML engine, adjusted whitespace handling to closer match that of the REXML engine, and added a LibXML engine test Signed-off-by: Michael Koziarski --- activesupport/test/xml_mini/libxml_engine_test.rb | 194 ++++++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 activesupport/test/xml_mini/libxml_engine_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/xml_mini/libxml_engine_test.rb b/activesupport/test/xml_mini/libxml_engine_test.rb new file mode 100644 index 0000000000..900c8052d6 --- /dev/null +++ b/activesupport/test/xml_mini/libxml_engine_test.rb @@ -0,0 +1,194 @@ +require 'abstract_unit' +require 'active_support/xml_mini' +require 'active_support/core_ext/hash/conversions' + +begin + require 'libxml' +rescue LoadError + # Skip libxml tests +else + +class LibxmlEngineTest < Test::Unit::TestCase + include ActiveSupport + + def setup + @default_backend = XmlMini.backend + XmlMini.backend = 'LibXML' + + LibXML::XML::Error.set_handler(&lambda { |error| }) #silence libxml, exceptions will do + end + + def teardown + XmlMini.backend = @default_backend + end + + def test_exception_thrown_on_expansion_attack + assert_raise LibXML::XML::Error do + attack_xml = %{ + + + + + + + + ]> + + &a; + + } + Hash.from_xml(attack_xml) + end + end + + def test_setting_libxml_as_backend + XmlMini.backend = 'LibXML' + assert_equal XmlMini_LibXML, XmlMini.backend + end + + def test_blank_returns_empty_hash + assert_equal({}, XmlMini.parse(nil)) + assert_equal({}, XmlMini.parse('')) + end + + def test_array_type_makes_an_array + assert_equal_rexml(<<-eoxml) + + + a post + another post + + + eoxml + end + + def test_one_node_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_one_node_with_attributes_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_products_node_with_book_node_as_hash + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + def test_products_node_with_two_book_nodes_as_hash + assert_equal_rexml(<<-eoxml) + + + + + eoxml + end + + def test_single_node_with_content_as_hash + assert_equal_rexml(<<-eoxml) + + hello world + + eoxml + end + + def test_children_with_children + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text + assert_equal_rexml(<<-eoxml) + + + hello everyone + + + eoxml + end + + def test_children_with_non_adjacent_text + assert_equal_rexml(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + end + + def test_parse_from_io + io = StringIO.new(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + XmlMini.parse(io) + end + + def test_children_with_simple_cdata + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_multiple_cdata + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text_and_cdata + assert_equal_rexml(<<-eoxml) + + + hello + morning + + + eoxml + end + + def test_children_with_blank_text + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + private + def assert_equal_rexml(xml) + hash = XmlMini.with_backend('REXML') { XmlMini.parse(xml) } + assert_equal(hash, XmlMini.parse(xml)) + end +end + +end -- cgit v1.2.3 From 00eb09e016837e361fb9dcf6a46d1715ec59beca Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 14 Oct 2009 16:12:19 -0700 Subject: Revert "Get AS TestCase off deprecated callbacks" This reverts commit 29b280666b6a8216a46b8349fa76c85f5b5dcc55. --- activesupport/test/test_test.rb | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb index 7a45dab60b..5cbffb81fc 100644 --- a/activesupport/test/test_test.rb +++ b/activesupport/test/test_test.rb @@ -95,3 +95,55 @@ end class AlsoDoingNothingTest < ActiveSupport::TestCase end + +# Setup and teardown callbacks. +class SetupAndTeardownTest < ActiveSupport::TestCase + setup :reset_callback_record, :foo + teardown :foo, :sentinel, :foo + + def test_inherited_setup_callbacks + assert_equal [:reset_callback_record, :foo], self.class.setup_callback_chain.map(&:method) + assert_equal [:foo], @called_back + assert_equal [:foo, :sentinel, :foo], self.class.teardown_callback_chain.map(&:method) + end + + def setup + end + + def teardown + end + + protected + def reset_callback_record + @called_back = [] + end + + def foo + @called_back << :foo + end + + def sentinel + assert_equal [:foo, :foo], @called_back + end +end + + +class SubclassSetupAndTeardownTest < SetupAndTeardownTest + setup :bar + teardown :bar + + def test_inherited_setup_callbacks + assert_equal [:reset_callback_record, :foo, :bar], self.class.setup_callback_chain.map(&:method) + assert_equal [:foo, :bar], @called_back + assert_equal [:foo, :sentinel, :foo, :bar], self.class.teardown_callback_chain.map(&:method) + end + + protected + def bar + @called_back << :bar + end + + def sentinel + assert_equal [:foo, :bar, :bar, :foo], @called_back + end +end -- cgit v1.2.3 From 8cbf825425dc8ad3770881ea4e100b9023c69ce2 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 14 Oct 2009 19:50:06 -0500 Subject: Rename Orchestra to Notifications [#3321 state:resolved] --- activesupport/test/notifications_test.rb | 161 +++++++++++++++++++++++++++++++ activesupport/test/orchestra_test.rb | 161 ------------------------------- 2 files changed, 161 insertions(+), 161 deletions(-) create mode 100644 activesupport/test/notifications_test.rb delete mode 100644 activesupport/test/orchestra_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb new file mode 100644 index 0000000000..8f00eff106 --- /dev/null +++ b/activesupport/test/notifications_test.rb @@ -0,0 +1,161 @@ +require 'abstract_unit' + +class NotificationsEventTest < Test::Unit::TestCase + def setup + @parent = ActiveSupport::Notifications::Event.new(:parent) + end + + def test_initialization_with_name_and_parent_and_payload + event = ActiveSupport::Notifications::Event.new(:awesome, @parent, :payload => "notifications") + assert_equal(:awesome, event.name) + assert_equal(@parent, event.parent) + assert_equal({ :payload => "notifications" }, event.payload) + end + + def test_thread_id_is_set_on_initialization + event = ActiveSupport::Notifications::Event.new(:awesome) + assert_equal Thread.current.object_id, event.thread_id + end + + def test_current_time_is_set_on_initialization + previous_time = Time.now.utc + event = ActiveSupport::Notifications::Event.new(:awesome) + assert_kind_of Time, event.time + assert event.time.to_f >= previous_time.to_f + end + + def test_duration_is_set_when_event_finishes + event = ActiveSupport::Notifications::Event.new(:awesome) + sleep(0.1) + event.finish! + assert_in_delta 100, event.duration, 30 + end +end + +class NotificationsMainTest < Test::Unit::TestCase + def setup + @listener = [] + ActiveSupport::Notifications.register @listener + end + + def teardown + ActiveSupport::Notifications.unregister @listener + end + + def test_notifications_allows_any_action_to_be_instrumented + event = ActiveSupport::Notifications.instrument(:awesome, "notifications") do + sleep(0.1) + end + + assert_equal :awesome, event.name + assert_equal "notifications", event.payload + assert_in_delta 100, event.duration, 30 + end + + def test_block_result_is_stored + event = ActiveSupport::Notifications.instrument(:awesome, "notifications") do + 1 + 1 + end + + assert_equal 2, event.result + end + + def test_events_are_published_to_a_listener + event = ActiveSupport::Notifications.instrument(:awesome, "notifications") do + 1 + 1 + end + + assert_equal 1, @listener.size + assert_equal :awesome, @listener.last.name + assert_equal "notifications", @listener.last.payload + end + + def test_nested_events_can_be_instrumented + ActiveSupport::Notifications.instrument(:awesome, "notifications") do + ActiveSupport::Notifications.instrument(:wot, "child") do + sleep(0.1) + end + + assert_equal 1, @listener.size + assert_equal :wot, @listener.first.name + assert_equal "child", @listener.first.payload + + assert_nil @listener.first.parent.duration + assert_in_delta 100, @listener.first.duration, 30 + end + + assert_equal 2, @listener.size + assert_equal :awesome, @listener.last.name + assert_equal "notifications", @listener.last.payload + assert_in_delta 100, @listener.first.parent.duration, 30 + end + + def test_event_is_pushed_even_if_block_fails + ActiveSupport::Notifications.instrument(:awesome, "notifications") do + raise "OMG" + end rescue RuntimeError + + assert_equal 1, @listener.size + assert_equal :awesome, @listener.last.name + assert_equal "notifications", @listener.last.payload + end +end + +class NotificationsListenerTest < Test::Unit::TestCase + class MyListener < ActiveSupport::Notifications::Listener + attr_reader :consumed + + def consume(event) + @consumed ||= [] + @consumed << event + end + end + + def setup + @listener = MyListener.new + ActiveSupport::Notifications.register @listener + end + + def teardown + ActiveSupport::Notifications.unregister @listener + end + + def test_thread_is_exposed_by_listener + assert_kind_of Thread, @listener.thread + end + + def test_event_is_consumed_when_an_action_is_instrumented + ActiveSupport::Notifications.instrument(:sum) do + 1 + 1 + end + sleep 0.1 + assert_equal 1, @listener.consumed.size + assert_equal :sum, @listener.consumed.first.name + assert_equal 2, @listener.consumed.first.result + end + + def test_with_sevaral_consumers_and_several_events + @another = MyListener.new + ActiveSupport::Notifications.register @another + + 1.upto(100) do |i| + ActiveSupport::Notifications.instrument(:value) do + i + end + end + + sleep 0.1 + + assert_equal 100, @listener.consumed.size + assert_equal :value, @listener.consumed.first.name + assert_equal 1, @listener.consumed.first.result + assert_equal 100, @listener.consumed.last.result + + assert_equal 100, @another.consumed.size + assert_equal :value, @another.consumed.first.name + assert_equal 1, @another.consumed.first.result + assert_equal 100, @another.consumed.last.result + ensure + ActiveSupport::Notifications.unregister @another + end +end diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb deleted file mode 100644 index 683cc36f6a..0000000000 --- a/activesupport/test/orchestra_test.rb +++ /dev/null @@ -1,161 +0,0 @@ -require 'abstract_unit' - -class OrchestraEventTest < Test::Unit::TestCase - def setup - @parent = ActiveSupport::Orchestra::Event.new(:parent) - end - - def test_initialization_with_name_and_parent_and_payload - event = ActiveSupport::Orchestra::Event.new(:awesome, @parent, :payload => "orchestra") - assert_equal(:awesome, event.name) - assert_equal(@parent, event.parent) - assert_equal({ :payload => "orchestra" }, event.payload) - end - - def test_thread_id_is_set_on_initialization - event = ActiveSupport::Orchestra::Event.new(:awesome) - assert_equal Thread.current.object_id, event.thread_id - end - - def test_current_time_is_set_on_initialization - previous_time = Time.now.utc - event = ActiveSupport::Orchestra::Event.new(:awesome) - assert_kind_of Time, event.time - assert event.time.to_f >= previous_time.to_f - end - - def test_duration_is_set_when_event_finishes - event = ActiveSupport::Orchestra::Event.new(:awesome) - sleep(0.1) - event.finish! - assert_in_delta 100, event.duration, 30 - end -end - -class OrchestraMainTest < Test::Unit::TestCase - def setup - @listener = [] - ActiveSupport::Orchestra.register @listener - end - - def teardown - ActiveSupport::Orchestra.unregister @listener - end - - def test_orchestra_allows_any_action_to_be_instrumented - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - sleep(0.1) - end - - assert_equal :awesome, event.name - assert_equal "orchestra", event.payload - assert_in_delta 100, event.duration, 30 - end - - def test_block_result_is_stored - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - 1 + 1 - end - - assert_equal 2, event.result - end - - def test_events_are_published_to_a_listener - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - 1 + 1 - end - - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload - end - - def test_nested_events_can_be_instrumented - ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - ActiveSupport::Orchestra.instrument(:wot, "child") do - sleep(0.1) - end - - assert_equal 1, @listener.size - assert_equal :wot, @listener.first.name - assert_equal "child", @listener.first.payload - - assert_nil @listener.first.parent.duration - assert_in_delta 100, @listener.first.duration, 30 - end - - assert_equal 2, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload - assert_in_delta 100, @listener.first.parent.duration, 30 - end - - def test_event_is_pushed_even_if_block_fails - ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - raise "OMG" - end rescue RuntimeError - - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload - end -end - -class OrchestraListenerTest < Test::Unit::TestCase - class MyListener < ActiveSupport::Orchestra::Listener - attr_reader :consumed - - def consume(event) - @consumed ||= [] - @consumed << event - end - end - - def setup - @listener = MyListener.new - ActiveSupport::Orchestra.register @listener - end - - def teardown - ActiveSupport::Orchestra.unregister @listener - end - - def test_thread_is_exposed_by_listener - assert_kind_of Thread, @listener.thread - end - - def test_event_is_consumed_when_an_action_is_instrumented - ActiveSupport::Orchestra.instrument(:sum) do - 1 + 1 - end - sleep 0.1 - assert_equal 1, @listener.consumed.size - assert_equal :sum, @listener.consumed.first.name - assert_equal 2, @listener.consumed.first.result - end - - def test_with_sevaral_consumers_and_several_events - @another = MyListener.new - ActiveSupport::Orchestra.register @another - - 1.upto(100) do |i| - ActiveSupport::Orchestra.instrument(:value) do - i - end - end - - sleep 0.1 - - assert_equal 100, @listener.consumed.size - assert_equal :value, @listener.consumed.first.name - assert_equal 1, @listener.consumed.first.result - assert_equal 100, @listener.consumed.last.result - - assert_equal 100, @another.consumed.size - assert_equal :value, @another.consumed.first.name - assert_equal 1, @another.consumed.first.result - assert_equal 100, @another.consumed.last.result - ensure - ActiveSupport::Orchestra.unregister @another - end -end -- cgit v1.2.3 From 8b340ab2f62bac2af9d5917e296bb4101530282a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 18:06:15 -0300 Subject: Revert "Rename Orchestra to Notifications [#3321 state:resolved]" This reverts commit 8cbf825425dc8ad3770881ea4e100b9023c69ce2. --- activesupport/test/notifications_test.rb | 161 ------------------------------- activesupport/test/orchestra_test.rb | 161 +++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 161 deletions(-) delete mode 100644 activesupport/test/notifications_test.rb create mode 100644 activesupport/test/orchestra_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb deleted file mode 100644 index 8f00eff106..0000000000 --- a/activesupport/test/notifications_test.rb +++ /dev/null @@ -1,161 +0,0 @@ -require 'abstract_unit' - -class NotificationsEventTest < Test::Unit::TestCase - def setup - @parent = ActiveSupport::Notifications::Event.new(:parent) - end - - def test_initialization_with_name_and_parent_and_payload - event = ActiveSupport::Notifications::Event.new(:awesome, @parent, :payload => "notifications") - assert_equal(:awesome, event.name) - assert_equal(@parent, event.parent) - assert_equal({ :payload => "notifications" }, event.payload) - end - - def test_thread_id_is_set_on_initialization - event = ActiveSupport::Notifications::Event.new(:awesome) - assert_equal Thread.current.object_id, event.thread_id - end - - def test_current_time_is_set_on_initialization - previous_time = Time.now.utc - event = ActiveSupport::Notifications::Event.new(:awesome) - assert_kind_of Time, event.time - assert event.time.to_f >= previous_time.to_f - end - - def test_duration_is_set_when_event_finishes - event = ActiveSupport::Notifications::Event.new(:awesome) - sleep(0.1) - event.finish! - assert_in_delta 100, event.duration, 30 - end -end - -class NotificationsMainTest < Test::Unit::TestCase - def setup - @listener = [] - ActiveSupport::Notifications.register @listener - end - - def teardown - ActiveSupport::Notifications.unregister @listener - end - - def test_notifications_allows_any_action_to_be_instrumented - event = ActiveSupport::Notifications.instrument(:awesome, "notifications") do - sleep(0.1) - end - - assert_equal :awesome, event.name - assert_equal "notifications", event.payload - assert_in_delta 100, event.duration, 30 - end - - def test_block_result_is_stored - event = ActiveSupport::Notifications.instrument(:awesome, "notifications") do - 1 + 1 - end - - assert_equal 2, event.result - end - - def test_events_are_published_to_a_listener - event = ActiveSupport::Notifications.instrument(:awesome, "notifications") do - 1 + 1 - end - - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "notifications", @listener.last.payload - end - - def test_nested_events_can_be_instrumented - ActiveSupport::Notifications.instrument(:awesome, "notifications") do - ActiveSupport::Notifications.instrument(:wot, "child") do - sleep(0.1) - end - - assert_equal 1, @listener.size - assert_equal :wot, @listener.first.name - assert_equal "child", @listener.first.payload - - assert_nil @listener.first.parent.duration - assert_in_delta 100, @listener.first.duration, 30 - end - - assert_equal 2, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "notifications", @listener.last.payload - assert_in_delta 100, @listener.first.parent.duration, 30 - end - - def test_event_is_pushed_even_if_block_fails - ActiveSupport::Notifications.instrument(:awesome, "notifications") do - raise "OMG" - end rescue RuntimeError - - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "notifications", @listener.last.payload - end -end - -class NotificationsListenerTest < Test::Unit::TestCase - class MyListener < ActiveSupport::Notifications::Listener - attr_reader :consumed - - def consume(event) - @consumed ||= [] - @consumed << event - end - end - - def setup - @listener = MyListener.new - ActiveSupport::Notifications.register @listener - end - - def teardown - ActiveSupport::Notifications.unregister @listener - end - - def test_thread_is_exposed_by_listener - assert_kind_of Thread, @listener.thread - end - - def test_event_is_consumed_when_an_action_is_instrumented - ActiveSupport::Notifications.instrument(:sum) do - 1 + 1 - end - sleep 0.1 - assert_equal 1, @listener.consumed.size - assert_equal :sum, @listener.consumed.first.name - assert_equal 2, @listener.consumed.first.result - end - - def test_with_sevaral_consumers_and_several_events - @another = MyListener.new - ActiveSupport::Notifications.register @another - - 1.upto(100) do |i| - ActiveSupport::Notifications.instrument(:value) do - i - end - end - - sleep 0.1 - - assert_equal 100, @listener.consumed.size - assert_equal :value, @listener.consumed.first.name - assert_equal 1, @listener.consumed.first.result - assert_equal 100, @listener.consumed.last.result - - assert_equal 100, @another.consumed.size - assert_equal :value, @another.consumed.first.name - assert_equal 1, @another.consumed.first.result - assert_equal 100, @another.consumed.last.result - ensure - ActiveSupport::Notifications.unregister @another - end -end diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb new file mode 100644 index 0000000000..683cc36f6a --- /dev/null +++ b/activesupport/test/orchestra_test.rb @@ -0,0 +1,161 @@ +require 'abstract_unit' + +class OrchestraEventTest < Test::Unit::TestCase + def setup + @parent = ActiveSupport::Orchestra::Event.new(:parent) + end + + def test_initialization_with_name_and_parent_and_payload + event = ActiveSupport::Orchestra::Event.new(:awesome, @parent, :payload => "orchestra") + assert_equal(:awesome, event.name) + assert_equal(@parent, event.parent) + assert_equal({ :payload => "orchestra" }, event.payload) + end + + def test_thread_id_is_set_on_initialization + event = ActiveSupport::Orchestra::Event.new(:awesome) + assert_equal Thread.current.object_id, event.thread_id + end + + def test_current_time_is_set_on_initialization + previous_time = Time.now.utc + event = ActiveSupport::Orchestra::Event.new(:awesome) + assert_kind_of Time, event.time + assert event.time.to_f >= previous_time.to_f + end + + def test_duration_is_set_when_event_finishes + event = ActiveSupport::Orchestra::Event.new(:awesome) + sleep(0.1) + event.finish! + assert_in_delta 100, event.duration, 30 + end +end + +class OrchestraMainTest < Test::Unit::TestCase + def setup + @listener = [] + ActiveSupport::Orchestra.register @listener + end + + def teardown + ActiveSupport::Orchestra.unregister @listener + end + + def test_orchestra_allows_any_action_to_be_instrumented + event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + sleep(0.1) + end + + assert_equal :awesome, event.name + assert_equal "orchestra", event.payload + assert_in_delta 100, event.duration, 30 + end + + def test_block_result_is_stored + event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + 1 + 1 + end + + assert_equal 2, event.result + end + + def test_events_are_published_to_a_listener + event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + 1 + 1 + end + + assert_equal 1, @listener.size + assert_equal :awesome, @listener.last.name + assert_equal "orchestra", @listener.last.payload + end + + def test_nested_events_can_be_instrumented + ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + ActiveSupport::Orchestra.instrument(:wot, "child") do + sleep(0.1) + end + + assert_equal 1, @listener.size + assert_equal :wot, @listener.first.name + assert_equal "child", @listener.first.payload + + assert_nil @listener.first.parent.duration + assert_in_delta 100, @listener.first.duration, 30 + end + + assert_equal 2, @listener.size + assert_equal :awesome, @listener.last.name + assert_equal "orchestra", @listener.last.payload + assert_in_delta 100, @listener.first.parent.duration, 30 + end + + def test_event_is_pushed_even_if_block_fails + ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + raise "OMG" + end rescue RuntimeError + + assert_equal 1, @listener.size + assert_equal :awesome, @listener.last.name + assert_equal "orchestra", @listener.last.payload + end +end + +class OrchestraListenerTest < Test::Unit::TestCase + class MyListener < ActiveSupport::Orchestra::Listener + attr_reader :consumed + + def consume(event) + @consumed ||= [] + @consumed << event + end + end + + def setup + @listener = MyListener.new + ActiveSupport::Orchestra.register @listener + end + + def teardown + ActiveSupport::Orchestra.unregister @listener + end + + def test_thread_is_exposed_by_listener + assert_kind_of Thread, @listener.thread + end + + def test_event_is_consumed_when_an_action_is_instrumented + ActiveSupport::Orchestra.instrument(:sum) do + 1 + 1 + end + sleep 0.1 + assert_equal 1, @listener.consumed.size + assert_equal :sum, @listener.consumed.first.name + assert_equal 2, @listener.consumed.first.result + end + + def test_with_sevaral_consumers_and_several_events + @another = MyListener.new + ActiveSupport::Orchestra.register @another + + 1.upto(100) do |i| + ActiveSupport::Orchestra.instrument(:value) do + i + end + end + + sleep 0.1 + + assert_equal 100, @listener.consumed.size + assert_equal :value, @listener.consumed.first.name + assert_equal 1, @listener.consumed.first.result + assert_equal 100, @listener.consumed.last.result + + assert_equal 100, @another.consumed.size + assert_equal :value, @another.consumed.first.name + assert_equal 1, @another.consumed.first.result + assert_equal 100, @another.consumed.last.result + ensure + ActiveSupport::Orchestra.unregister @another + end +end -- cgit v1.2.3 From a60bdd7d2921ca10b0a5ae3f750b402e12981004 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 30 Sep 2009 08:59:15 -0300 Subject: Added queue abstraction to Orchestra. --- activesupport/test/orchestra_test.rb | 111 +++++++++++++++-------------------- 1 file changed, 47 insertions(+), 64 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 683cc36f6a..e343d6322b 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -1,5 +1,12 @@ require 'abstract_unit' +# Allow LittleFanout to be cleaned. +class ActiveSupport::Orchestra::LittleFanout + def clear + @listeners.clear + end +end + class OrchestraEventTest < Test::Unit::TestCase def setup @parent = ActiveSupport::Orchestra::Event.new(:parent) @@ -34,12 +41,12 @@ end class OrchestraMainTest < Test::Unit::TestCase def setup - @listener = [] - ActiveSupport::Orchestra.register @listener + @events = [] + ActiveSupport::Orchestra.subscribe { |event| @events << event } end def teardown - ActiveSupport::Orchestra.unregister @listener + ActiveSupport::Orchestra.queue.clear end def test_orchestra_allows_any_action_to_be_instrumented @@ -65,9 +72,9 @@ class OrchestraMainTest < Test::Unit::TestCase 1 + 1 end - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal "orchestra", @events.last.payload end def test_nested_events_can_be_instrumented @@ -76,18 +83,18 @@ class OrchestraMainTest < Test::Unit::TestCase sleep(0.1) end - assert_equal 1, @listener.size - assert_equal :wot, @listener.first.name - assert_equal "child", @listener.first.payload + assert_equal 1, @events.size + assert_equal :wot, @events.first.name + assert_equal "child", @events.first.payload - assert_nil @listener.first.parent.duration - assert_in_delta 100, @listener.first.duration, 30 + assert_nil @events.first.parent.duration + assert_in_delta 100, @events.first.duration, 30 end - assert_equal 2, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload - assert_in_delta 100, @listener.first.parent.duration, 30 + assert_equal 2, @events.size + assert_equal :awesome, @events.last.name + assert_equal "orchestra", @events.last.payload + assert_in_delta 100, @events.first.parent.duration, 30 end def test_event_is_pushed_even_if_block_fails @@ -95,67 +102,43 @@ class OrchestraMainTest < Test::Unit::TestCase raise "OMG" end rescue RuntimeError - assert_equal 1, @listener.size - assert_equal :awesome, @listener.last.name - assert_equal "orchestra", @listener.last.payload + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal "orchestra", @events.last.payload end -end - -class OrchestraListenerTest < Test::Unit::TestCase - class MyListener < ActiveSupport::Orchestra::Listener - attr_reader :consumed - def consume(event) - @consumed ||= [] - @consumed << event - end - end + def test_subscriber_with_pattern + @another = [] + ActiveSupport::Orchestra.subscribe(/cache/) { |event| @another << event } - def setup - @listener = MyListener.new - ActiveSupport::Orchestra.register @listener - end + ActiveSupport::Orchestra.instrument(:something){ 0 } + ActiveSupport::Orchestra.instrument(:cache){ 10 } - def teardown - ActiveSupport::Orchestra.unregister @listener - end - - def test_thread_is_exposed_by_listener - assert_kind_of Thread, @listener.thread - end - - def test_event_is_consumed_when_an_action_is_instrumented - ActiveSupport::Orchestra.instrument(:sum) do - 1 + 1 - end sleep 0.1 - assert_equal 1, @listener.consumed.size - assert_equal :sum, @listener.consumed.first.name - assert_equal 2, @listener.consumed.first.result + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 10, @another.first.result end - def test_with_sevaral_consumers_and_several_events - @another = MyListener.new - ActiveSupport::Orchestra.register @another + def test_with_several_consumers_and_several_events + @another = [] + ActiveSupport::Orchestra.subscribe { |event| @another << event } 1.upto(100) do |i| - ActiveSupport::Orchestra.instrument(:value) do - i - end + ActiveSupport::Orchestra.instrument(:value){ i } end sleep 0.1 - assert_equal 100, @listener.consumed.size - assert_equal :value, @listener.consumed.first.name - assert_equal 1, @listener.consumed.first.result - assert_equal 100, @listener.consumed.last.result - - assert_equal 100, @another.consumed.size - assert_equal :value, @another.consumed.first.name - assert_equal 1, @another.consumed.first.result - assert_equal 100, @another.consumed.last.result - ensure - ActiveSupport::Orchestra.unregister @another + assert_equal 100, @events.size + assert_equal :value, @events.first.name + assert_equal 1, @events.first.result + assert_equal 100, @events.last.result + + assert_equal 100, @another.size + assert_equal :value, @another.first.name + assert_equal 1, @another.first.result + assert_equal 100, @another.last.result end end -- cgit v1.2.3 From 7b7796e23d12b526fa35976c514da91169dd2566 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 1 Oct 2009 21:14:38 -0300 Subject: Events are created inside threads. --- activesupport/test/orchestra_test.rb | 73 ++++++++++++------------------------ 1 file changed, 24 insertions(+), 49 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index e343d6322b..608531416c 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -8,103 +8,78 @@ class ActiveSupport::Orchestra::LittleFanout end class OrchestraEventTest < Test::Unit::TestCase - def setup - @parent = ActiveSupport::Orchestra::Event.new(:parent) + def test_events_are_initialized_with_name_and_payload + event = ActiveSupport::Orchestra::Event.new(:foo, :payload => :bar) + assert_equal :foo, event.name + assert_equal Hash[:payload => :bar], event.payload end - def test_initialization_with_name_and_parent_and_payload - event = ActiveSupport::Orchestra::Event.new(:awesome, @parent, :payload => "orchestra") - assert_equal(:awesome, event.name) - assert_equal(@parent, event.parent) - assert_equal({ :payload => "orchestra" }, event.payload) - end + def test_events_consumes_information_given_as_payload + event = ActiveSupport::Orchestra::Event.new(:foo, + :time => (time = Time.now), :result => 1, :duration => 10) - def test_thread_id_is_set_on_initialization - event = ActiveSupport::Orchestra::Event.new(:awesome) - assert_equal Thread.current.object_id, event.thread_id - end - - def test_current_time_is_set_on_initialization - previous_time = Time.now.utc - event = ActiveSupport::Orchestra::Event.new(:awesome) - assert_kind_of Time, event.time - assert event.time.to_f >= previous_time.to_f - end - - def test_duration_is_set_when_event_finishes - event = ActiveSupport::Orchestra::Event.new(:awesome) - sleep(0.1) - event.finish! - assert_in_delta 100, event.duration, 30 + assert_equal Hash.new, event.payload + assert_equal time, event.time + assert_equal 1, event.result + assert_equal 10, event.duration end end class OrchestraMainTest < Test::Unit::TestCase def setup @events = [] + Thread.abort_on_exception = true ActiveSupport::Orchestra.subscribe { |event| @events << event } end def teardown + Thread.abort_on_exception = false ActiveSupport::Orchestra.queue.clear end - def test_orchestra_allows_any_action_to_be_instrumented - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - sleep(0.1) - end - - assert_equal :awesome, event.name - assert_equal "orchestra", event.payload - assert_in_delta 100, event.duration, 30 - end - - def test_block_result_is_stored - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + def test_orchestra_returns_action_result + result = ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do 1 + 1 end - assert_equal 2, event.result + assert_equal 2, result end def test_events_are_published_to_a_listener - event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do 1 + 1 end assert_equal 1, @events.size assert_equal :awesome, @events.last.name - assert_equal "orchestra", @events.last.payload + assert_equal Hash[:payload => "orchestra"], @events.last.payload end def test_nested_events_can_be_instrumented - ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do - ActiveSupport::Orchestra.instrument(:wot, "child") do + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do + ActiveSupport::Orchestra.instrument(:wot, :payload => "child") do sleep(0.1) end assert_equal 1, @events.size assert_equal :wot, @events.first.name - assert_equal "child", @events.first.payload - - assert_nil @events.first.parent.duration + assert_equal Hash[:payload => "child"], @events.first.payload assert_in_delta 100, @events.first.duration, 30 end assert_equal 2, @events.size assert_equal :awesome, @events.last.name - assert_equal "orchestra", @events.last.payload - assert_in_delta 100, @events.first.parent.duration, 30 + assert_equal Hash[:payload => "orchestra"], @events.last.payload end def test_event_is_pushed_even_if_block_fails - ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do raise "OMG" end rescue RuntimeError assert_equal 1, @events.size assert_equal :awesome, @events.last.name - assert_equal "orchestra", @events.last.payload + assert_equal Hash[:payload => "orchestra"], @events.last.payload end def test_subscriber_with_pattern -- cgit v1.2.3 From 5d0f8abc003cc6edfdb471ada05754580725b353 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 6 Oct 2009 09:42:42 -0300 Subject: Orchestra listeners have their own queue. --- activesupport/test/orchestra_test.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 608531416c..810d99ebeb 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -50,6 +50,8 @@ class OrchestraMainTest < Test::Unit::TestCase 1 + 1 end + sleep(0.1) + assert_equal 1, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "orchestra"], @events.last.payload @@ -58,18 +60,22 @@ class OrchestraMainTest < Test::Unit::TestCase def test_nested_events_can_be_instrumented ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do ActiveSupport::Orchestra.instrument(:wot, :payload => "child") do - sleep(0.1) + 1 + 1 end + sleep(0.1) + assert_equal 1, @events.size assert_equal :wot, @events.first.name assert_equal Hash[:payload => "child"], @events.first.payload - assert_in_delta 100, @events.first.duration, 30 end + sleep(0.1) + assert_equal 2, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "orchestra"], @events.last.payload + assert_in_delta 100, @events.last.duration, 70 end def test_event_is_pushed_even_if_block_fails @@ -77,6 +83,8 @@ class OrchestraMainTest < Test::Unit::TestCase raise "OMG" end rescue RuntimeError + sleep(0.1) + assert_equal 1, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "orchestra"], @events.last.payload @@ -89,7 +97,7 @@ class OrchestraMainTest < Test::Unit::TestCase ActiveSupport::Orchestra.instrument(:something){ 0 } ActiveSupport::Orchestra.instrument(:cache){ 10 } - sleep 0.1 + sleep(0.1) assert_equal 1, @another.size assert_equal :cache, @another.first.name -- cgit v1.2.3 From af0d1fa8920793a95fae456d1f5debdc50287eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 7 Oct 2009 11:17:50 -0300 Subject: Update Orchestra instrumentations and move part of logging to Orchestra. --- activesupport/test/orchestra_test.rb | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 810d99ebeb..7a6e9208b4 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -90,18 +90,39 @@ class OrchestraMainTest < Test::Unit::TestCase assert_equal Hash[:payload => "orchestra"], @events.last.payload end + def test_event_is_pushed_even_without_block + ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "orchestra"], @events.last.payload + end + def test_subscriber_with_pattern @another = [] - ActiveSupport::Orchestra.subscribe(/cache/) { |event| @another << event } + ActiveSupport::Orchestra.subscribe("cache"){ |event| @another << event } + ActiveSupport::Orchestra.instrument(:cache){ 1 } + + sleep(0.1) + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 1, @another.first.result + end + + def test_subscriber_with_pattern_as_regexp + @another = [] + ActiveSupport::Orchestra.subscribe(/cache/){ |event| @another << event } ActiveSupport::Orchestra.instrument(:something){ 0 } - ActiveSupport::Orchestra.instrument(:cache){ 10 } + ActiveSupport::Orchestra.instrument(:cache){ 1 } sleep(0.1) assert_equal 1, @another.size assert_equal :cache, @another.first.name - assert_equal 10, @another.first.result + assert_equal 1, @another.first.result end def test_with_several_consumers_and_several_events -- cgit v1.2.3 From a15e02d44ac2afb27a7e8e652c98a796d271b645 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Fri, 9 Oct 2009 09:52:25 -0300 Subject: Unify benchmark APIs. --- activesupport/test/benchmarkable_test.rb | 87 ++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 activesupport/test/benchmarkable_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/benchmarkable_test.rb b/activesupport/test/benchmarkable_test.rb new file mode 100644 index 0000000000..e807bcb732 --- /dev/null +++ b/activesupport/test/benchmarkable_test.rb @@ -0,0 +1,87 @@ +require 'abstract_unit' +require 'action_view/helpers/benchmark_helper' + +class BenchmarkableTest < ActiveSupport::TestCase + include ActiveSupport::Benchmarkable + + def teardown + logger.send(:clear_buffer) + end + + def test_without_block + assert_raise(LocalJumpError) { benchmark } + assert buffer.empty? + end + + def test_defaults + i_was_run = false + benchmark { i_was_run = true } + assert i_was_run + assert_last_logged + end + + def test_with_message + i_was_run = false + benchmark('test_run') { i_was_run = true } + assert i_was_run + assert_last_logged 'test_run' + end + + def test_with_message_and_deprecated_level + i_was_run = false + + assert_deprecated do + benchmark('debug_run', :debug) { i_was_run = true } + end + + assert i_was_run + assert_last_logged 'debug_run' + end + + def test_within_level + logger.level = ActiveSupport::BufferedLogger::DEBUG + benchmark('included_debug_run', :level => :debug) { } + assert_last_logged 'included_debug_run' + end + + def test_outside_level + logger.level = ActiveSupport::BufferedLogger::ERROR + benchmark('skipped_debug_run', :level => :debug) { } + assert_no_match(/skipped_debug_run/, buffer.last) + ensure + logger.level = ActiveSupport::BufferedLogger::DEBUG + end + + def test_without_silencing + benchmark('debug_run', :silence => false) do + logger.info "not silenced!" + end + + assert_equal 2, buffer.size + end + + def test_with_silencing + benchmark('debug_run', :silence => true) do + logger.info "silenced!" + end + + assert_equal 1, buffer.size + end + + private + def logger + @logger ||= begin + logger = ActiveSupport::BufferedLogger.new(StringIO.new) + logger.auto_flushing = false + logger + end + end + + def buffer + logger.send(:buffer) + end + + def assert_last_logged(message = 'Benchmarking') + assert_match(/^#{message} \(.*\)$/, buffer.last) + end +end -- cgit v1.2.3 From 5988b87c30eb0ce50c235187f5dfcfcfb98da01b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 14:49:29 -0300 Subject: Added parent_of? method to help tracing events. --- activesupport/test/orchestra_test.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb index 7a6e9208b4..1b2f98c7dd 100644 --- a/activesupport/test/orchestra_test.rb +++ b/activesupport/test/orchestra_test.rb @@ -9,20 +9,36 @@ end class OrchestraEventTest < Test::Unit::TestCase def test_events_are_initialized_with_name_and_payload - event = ActiveSupport::Orchestra::Event.new(:foo, :payload => :bar) + event = event(:foo, :payload => :bar) assert_equal :foo, event.name assert_equal Hash[:payload => :bar], event.payload end def test_events_consumes_information_given_as_payload - event = ActiveSupport::Orchestra::Event.new(:foo, - :time => (time = Time.now), :result => 1, :duration => 10) + event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) assert_equal Hash.new, event.payload assert_equal time, event.time assert_equal 1, event.result assert_equal 10, event.duration end + + def test_event_is_parent_based_on_time_frame + parent = event(:foo, :time => Time.utc(2009), :duration => 10000) + child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) + not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) + + assert parent.parent_of?(child) + assert !child.parent_of?(parent) + assert !parent.parent_of?(not_child) + assert !not_child.parent_of?(parent) + end + + protected + + def event(*args) + ActiveSupport::Orchestra::Event.new(*args) + end end class OrchestraMainTest < Test::Unit::TestCase -- cgit v1.2.3 From 2d7abe245e7a2b1717e48ef550e4083318fd7ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 15 Oct 2009 18:51:51 -0300 Subject: Renamed Orchestra to Notifications once again [#3321 state:resolved] --- activesupport/test/notifications_test.rb | 164 +++++++++++++++++++++++++++++++ activesupport/test/orchestra_test.rb | 164 ------------------------------- 2 files changed, 164 insertions(+), 164 deletions(-) create mode 100644 activesupport/test/notifications_test.rb delete mode 100644 activesupport/test/orchestra_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb new file mode 100644 index 0000000000..561ee2b0ba --- /dev/null +++ b/activesupport/test/notifications_test.rb @@ -0,0 +1,164 @@ +require 'abstract_unit' + +# Allow LittleFanout to be cleaned. +class ActiveSupport::Notifications::LittleFanout + def clear + @listeners.clear + end +end + +class NotificationsEventTest < Test::Unit::TestCase + def test_events_are_initialized_with_name_and_payload + event = event(:foo, :payload => :bar) + assert_equal :foo, event.name + assert_equal Hash[:payload => :bar], event.payload + end + + def test_events_consumes_information_given_as_payload + event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) + + assert_equal Hash.new, event.payload + assert_equal time, event.time + assert_equal 1, event.result + assert_equal 10, event.duration + end + + def test_event_is_parent_based_on_time_frame + parent = event(:foo, :time => Time.utc(2009), :duration => 10000) + child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) + not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) + + assert parent.parent_of?(child) + assert !child.parent_of?(parent) + assert !parent.parent_of?(not_child) + assert !not_child.parent_of?(parent) + end + + protected + + def event(*args) + ActiveSupport::Notifications::Event.new(*args) + end +end + +class NotificationsMainTest < Test::Unit::TestCase + def setup + @events = [] + Thread.abort_on_exception = true + ActiveSupport::Notifications.subscribe { |event| @events << event } + end + + def teardown + Thread.abort_on_exception = false + ActiveSupport::Notifications.queue.clear + end + + def test_notifications_returns_action_result + result = ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + 1 + 1 + end + + assert_equal 2, result + end + + def test_events_are_published_to_a_listener + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + 1 + 1 + end + + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + + def test_nested_events_can_be_instrumented + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + ActiveSupport::Notifications.instrument(:wot, :payload => "child") do + 1 + 1 + end + + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :wot, @events.first.name + assert_equal Hash[:payload => "child"], @events.first.payload + end + + sleep(0.1) + + assert_equal 2, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + assert_in_delta 100, @events.last.duration, 70 + end + + def test_event_is_pushed_even_if_block_fails + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do + raise "OMG" + end rescue RuntimeError + + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + + def test_event_is_pushed_even_without_block + ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") + sleep(0.1) + + assert_equal 1, @events.size + assert_equal :awesome, @events.last.name + assert_equal Hash[:payload => "notifications"], @events.last.payload + end + + def test_subscriber_with_pattern + @another = [] + ActiveSupport::Notifications.subscribe("cache"){ |event| @another << event } + ActiveSupport::Notifications.instrument(:cache){ 1 } + + sleep(0.1) + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 1, @another.first.result + end + + def test_subscriber_with_pattern_as_regexp + @another = [] + ActiveSupport::Notifications.subscribe(/cache/){ |event| @another << event } + + ActiveSupport::Notifications.instrument(:something){ 0 } + ActiveSupport::Notifications.instrument(:cache){ 1 } + + sleep(0.1) + + assert_equal 1, @another.size + assert_equal :cache, @another.first.name + assert_equal 1, @another.first.result + end + + def test_with_several_consumers_and_several_events + @another = [] + ActiveSupport::Notifications.subscribe { |event| @another << event } + + 1.upto(100) do |i| + ActiveSupport::Notifications.instrument(:value){ i } + end + + sleep 0.1 + + assert_equal 100, @events.size + assert_equal :value, @events.first.name + assert_equal 1, @events.first.result + assert_equal 100, @events.last.result + + assert_equal 100, @another.size + assert_equal :value, @another.first.name + assert_equal 1, @another.first.result + assert_equal 100, @another.last.result + end +end diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb deleted file mode 100644 index 1b2f98c7dd..0000000000 --- a/activesupport/test/orchestra_test.rb +++ /dev/null @@ -1,164 +0,0 @@ -require 'abstract_unit' - -# Allow LittleFanout to be cleaned. -class ActiveSupport::Orchestra::LittleFanout - def clear - @listeners.clear - end -end - -class OrchestraEventTest < Test::Unit::TestCase - def test_events_are_initialized_with_name_and_payload - event = event(:foo, :payload => :bar) - assert_equal :foo, event.name - assert_equal Hash[:payload => :bar], event.payload - end - - def test_events_consumes_information_given_as_payload - event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) - - assert_equal Hash.new, event.payload - assert_equal time, event.time - assert_equal 1, event.result - assert_equal 10, event.duration - end - - def test_event_is_parent_based_on_time_frame - parent = event(:foo, :time => Time.utc(2009), :duration => 10000) - child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) - not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) - - assert parent.parent_of?(child) - assert !child.parent_of?(parent) - assert !parent.parent_of?(not_child) - assert !not_child.parent_of?(parent) - end - - protected - - def event(*args) - ActiveSupport::Orchestra::Event.new(*args) - end -end - -class OrchestraMainTest < Test::Unit::TestCase - def setup - @events = [] - Thread.abort_on_exception = true - ActiveSupport::Orchestra.subscribe { |event| @events << event } - end - - def teardown - Thread.abort_on_exception = false - ActiveSupport::Orchestra.queue.clear - end - - def test_orchestra_returns_action_result - result = ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - 1 + 1 - end - - assert_equal 2, result - end - - def test_events_are_published_to_a_listener - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - 1 + 1 - end - - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - end - - def test_nested_events_can_be_instrumented - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - ActiveSupport::Orchestra.instrument(:wot, :payload => "child") do - 1 + 1 - end - - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :wot, @events.first.name - assert_equal Hash[:payload => "child"], @events.first.payload - end - - sleep(0.1) - - assert_equal 2, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - assert_in_delta 100, @events.last.duration, 70 - end - - def test_event_is_pushed_even_if_block_fails - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") do - raise "OMG" - end rescue RuntimeError - - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - end - - def test_event_is_pushed_even_without_block - ActiveSupport::Orchestra.instrument(:awesome, :payload => "orchestra") - sleep(0.1) - - assert_equal 1, @events.size - assert_equal :awesome, @events.last.name - assert_equal Hash[:payload => "orchestra"], @events.last.payload - end - - def test_subscriber_with_pattern - @another = [] - ActiveSupport::Orchestra.subscribe("cache"){ |event| @another << event } - ActiveSupport::Orchestra.instrument(:cache){ 1 } - - sleep(0.1) - - assert_equal 1, @another.size - assert_equal :cache, @another.first.name - assert_equal 1, @another.first.result - end - - def test_subscriber_with_pattern_as_regexp - @another = [] - ActiveSupport::Orchestra.subscribe(/cache/){ |event| @another << event } - - ActiveSupport::Orchestra.instrument(:something){ 0 } - ActiveSupport::Orchestra.instrument(:cache){ 1 } - - sleep(0.1) - - assert_equal 1, @another.size - assert_equal :cache, @another.first.name - assert_equal 1, @another.first.result - end - - def test_with_several_consumers_and_several_events - @another = [] - ActiveSupport::Orchestra.subscribe { |event| @another << event } - - 1.upto(100) do |i| - ActiveSupport::Orchestra.instrument(:value){ i } - end - - sleep 0.1 - - assert_equal 100, @events.size - assert_equal :value, @events.first.name - assert_equal 1, @events.first.result - assert_equal 100, @events.last.result - - assert_equal 100, @another.size - assert_equal :value, @another.first.name - assert_equal 1, @another.first.result - assert_equal 100, @another.last.result - end -end -- cgit v1.2.3 From ef75d05829a4bbaeab4edb157194c2bd7f0ef60a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 15 Oct 2009 17:39:19 -0700 Subject: Get rid of stray require again --- activesupport/test/benchmarkable_test.rb | 1 - 1 file changed, 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/benchmarkable_test.rb b/activesupport/test/benchmarkable_test.rb index e807bcb732..766956f50f 100644 --- a/activesupport/test/benchmarkable_test.rb +++ b/activesupport/test/benchmarkable_test.rb @@ -1,5 +1,4 @@ require 'abstract_unit' -require 'action_view/helpers/benchmark_helper' class BenchmarkableTest < ActiveSupport::TestCase include ActiveSupport::Benchmarkable -- cgit v1.2.3 From a1df2590744ed126981dfd5b5709ff6fd5dc6476 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 19 Oct 2009 23:32:06 -0500 Subject: Replace decaying routing internals w/ rack-mount --- activesupport/test/core_ext/regexp_ext_test.rb | 19 ------------------- 1 file changed, 19 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/regexp_ext_test.rb b/activesupport/test/core_ext/regexp_ext_test.rb index cc3f07d5c5..68b089d5b4 100644 --- a/activesupport/test/core_ext/regexp_ext_test.rb +++ b/activesupport/test/core_ext/regexp_ext_test.rb @@ -2,28 +2,9 @@ require 'abstract_unit' require 'active_support/core_ext/regexp' class RegexpExtAccessTests < Test::Unit::TestCase - def test_number_of_captures - assert_equal 0, //.number_of_captures - assert_equal 1, /.(.)./.number_of_captures - assert_equal 2, /.(.).(?:.).(.)/.number_of_captures - assert_equal 3, /.((.).(?:.).(.))/.number_of_captures - end - def test_multiline assert_equal true, //m.multiline? assert_equal false, //.multiline? assert_equal false, /(?m:)/.multiline? end - - def test_optionalize - assert_equal "a?", Regexp.optionalize("a") - assert_equal "(?:foo)?", Regexp.optionalize("foo") - assert_equal "", Regexp.optionalize("") - end - - def test_unoptionalize - assert_equal "a", Regexp.unoptionalize("a?") - assert_equal "foo", Regexp.unoptionalize("(?:foo)?") - assert_equal "", Regexp.unoptionalize("") - end end -- cgit v1.2.3 From 4f6d6f7031a88b647814fc0154e6b69b636dc912 Mon Sep 17 00:00:00 2001 From: Yehuda Katz + Carl Lerche Date: Tue, 20 Oct 2009 16:33:54 -0700 Subject: Have all the tests running off a single Gemfile --- activesupport/test/abstract_unit.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index af9656615c..f390c66838 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -1,4 +1,12 @@ ORIG_ARGV = ARGV.dup +root = File.expand_path('../../..', __FILE__) +begin + require "#{root}/vendor/gems/environment" +rescue LoadError + $:.unshift("#{root}/activesupport/lib") + $:.unshift("#{root}/activerecord/lib") +end + require 'test/unit' @@ -11,7 +19,6 @@ rescue LoadError end ENV['NO_RELOAD'] = '1' -$:.unshift "#{File.dirname(__FILE__)}/../lib" require 'active_support' require 'active_support/test_case' -- cgit v1.2.3 From b3a198041befa933a26a597451f84482df268d0f Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 27 Oct 2009 00:07:21 -0700 Subject: Some optimizations on AS::Notifications. This does not change the public-facing API. --- activesupport/test/notifications_test.rb | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 561ee2b0ba..7d2bdf5ccf 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -8,25 +8,28 @@ class ActiveSupport::Notifications::LittleFanout end class NotificationsEventTest < Test::Unit::TestCase - def test_events_are_initialized_with_name_and_payload - event = event(:foo, :payload => :bar) + def test_events_are_initialized_with_details + event = event(:foo, Time.now, Time.now + 1, 1, random_id, :payload => :bar) assert_equal :foo, event.name assert_equal Hash[:payload => :bar], event.payload end def test_events_consumes_information_given_as_payload - event = event(:foo, :time => (time = Time.now), :result => 1, :duration => 10) + time = Time.now + event = event(:foo, time, time + 0.01, 1, random_id, {}) assert_equal Hash.new, event.payload assert_equal time, event.time assert_equal 1, event.result - assert_equal 10, event.duration + assert_equal 10.0, event.duration end def test_event_is_parent_based_on_time_frame - parent = event(:foo, :time => Time.utc(2009), :duration => 10000) - child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 1000) - not_child = event(:foo, :time => Time.utc(2009, 01, 01, 0, 0, 1), :duration => 10000) + time = Time.utc(2009, 01, 01, 0, 0, 1) + + parent = event(:foo, Time.utc(2009), Time.utc(2009) + 100, nil, random_id, {}) + child = event(:foo, time, time + 10, nil, random_id, {}) + not_child = event(:foo, time, time + 100, nil, random_id, {}) assert parent.parent_of?(child) assert !child.parent_of?(parent) @@ -34,11 +37,15 @@ class NotificationsEventTest < Test::Unit::TestCase assert !not_child.parent_of?(parent) end - protected +protected - def event(*args) - ActiveSupport::Notifications::Event.new(*args) - end + def random_id + @random_id ||= ActiveSupport::SecureRandom.hex(10) + end + + def event(*args) + ActiveSupport::Notifications::Event.new(*args) + end end class NotificationsMainTest < Test::Unit::TestCase -- cgit v1.2.3 From cbcb947b00a7c6992cfe42c6b369e87b4fa4ee23 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Tue, 27 Oct 2009 21:01:31 -0700 Subject: AS::Notifications.subscribe blocks are now yielded the arguments to pass to AS::Notifications::Event.new --- activesupport/test/notifications_test.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 7d2bdf5ccf..b763b740af 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -52,7 +52,9 @@ class NotificationsMainTest < Test::Unit::TestCase def setup @events = [] Thread.abort_on_exception = true - ActiveSupport::Notifications.subscribe { |event| @events << event } + ActiveSupport::Notifications.subscribe do |*args| + @events << ActiveSupport::Notifications::Event.new(*args) + end end def teardown @@ -124,7 +126,11 @@ class NotificationsMainTest < Test::Unit::TestCase def test_subscriber_with_pattern @another = [] - ActiveSupport::Notifications.subscribe("cache"){ |event| @another << event } + + ActiveSupport::Notifications.subscribe("cache") do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end + ActiveSupport::Notifications.instrument(:cache){ 1 } sleep(0.1) @@ -136,7 +142,9 @@ class NotificationsMainTest < Test::Unit::TestCase def test_subscriber_with_pattern_as_regexp @another = [] - ActiveSupport::Notifications.subscribe(/cache/){ |event| @another << event } + ActiveSupport::Notifications.subscribe(/cache/) do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end ActiveSupport::Notifications.instrument(:something){ 0 } ActiveSupport::Notifications.instrument(:cache){ 1 } @@ -150,7 +158,9 @@ class NotificationsMainTest < Test::Unit::TestCase def test_with_several_consumers_and_several_events @another = [] - ActiveSupport::Notifications.subscribe { |event| @another << event } + ActiveSupport::Notifications.subscribe do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end 1.upto(100) do |i| ActiveSupport::Notifications.instrument(:value){ i } -- cgit v1.2.3 From c9487ed6aff76693f33ff89e466ba944297681d3 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 28 Oct 2009 01:58:33 -0700 Subject: Change Event#thread_id to #transaction_id. Defaults to one "transaction" per thread but you can explicitly declare the start of a new one. This makes it possible for each request to have it own id. --- activesupport/test/notifications_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index b763b740af..9175c8f26e 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -124,6 +124,26 @@ class NotificationsMainTest < Test::Unit::TestCase assert_equal Hash[:payload => "notifications"], @events.last.payload end + def test_subscribed_in_a_transaction + @another = [] + + ActiveSupport::Notifications.subscribe("cache") do |*args| + @another << ActiveSupport::Notifications::Event.new(*args) + end + + ActiveSupport::Notifications.instrument(:cache){ 1 } + ActiveSupport::Notifications.transaction do + ActiveSupport::Notifications.instrument(:cache){ 1 } + end + ActiveSupport::Notifications.instrument(:cache){ 1 } + + sleep 0.1 + + before, during, after = @another.map {|e| e.transaction_id } + assert_equal before, after + assert_not_equal before, during + end + def test_subscriber_with_pattern @another = [] -- cgit v1.2.3 From a107103e85a2cc294faedddbb44707fd2bc2e206 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 28 Oct 2009 09:50:18 -0200 Subject: Allow :instance_reader to be given to superclass_delegating_accessor as well. --- activesupport/test/core_ext/class/delegating_attributes_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/class/delegating_attributes_test.rb b/activesupport/test/core_ext/class/delegating_attributes_test.rb index b51d68551d..beb55ba17e 100644 --- a/activesupport/test/core_ext/class/delegating_attributes_test.rb +++ b/activesupport/test/core_ext/class/delegating_attributes_test.rb @@ -52,6 +52,13 @@ class DelegatingAttributesTest < Test::Unit::TestCase assert !single_class.public_instance_methods.map(&:to_s).include?("both=") end + def test_simple_accessor_declaration_with_instance_reader_false + single_class.superclass_delegating_accessor :no_instance_reader, :instance_reader => false + assert single_class.respond_to?(:no_instance_reader) + assert single_class.respond_to?(:no_instance_reader=) + assert !single_class.public_instance_methods.map(&:to_s).include?("no_instance_reader") + end + def test_working_with_simple_attributes single_class.superclass_delegating_accessor :both -- cgit v1.2.3 From 13004c37e658dac960ba3a75cdf90206fc58118e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Nov 2009 13:44:24 +0100 Subject: Give useful test:isolated failures --- activesupport/test/ts_isolated.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/ts_isolated.rb b/activesupport/test/ts_isolated.rb index 9378a13766..cbab61a523 100644 --- a/activesupport/test/ts_isolated.rb +++ b/activesupport/test/ts_isolated.rb @@ -8,8 +8,8 @@ class TestIsolated < Test::Unit::TestCase Dir["#{File.dirname(__FILE__)}/**/*_test.rb"].each do |file| define_method("test #{file}") do command = "#{ruby} -Ilib:test #{file}" - silence_stderr { `#{command}` } - assert_equal 0, $?.to_i, command + result = silence_stderr { `#{command}` } + assert_block("#{command}\n#{result}") { $?.to_i.zero? } end end end -- cgit v1.2.3 From 36121d29a53c71cbdf4f0280718d572f16360f8c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Nov 2009 10:34:16 +0100 Subject: Ruby 1.9 doesn't recognize EM SPACE as whitespace, breaking String#strip --- activesupport/test/multibyte_chars_test.rb | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 680936ded5..0d3c3c7716 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -102,7 +102,13 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase # NEWLINE, SPACE, EM SPACE @whitespace = "\n#{[32, 8195].pack('U*')}" - @whitespace.force_encoding(Encoding::UTF_8) if @whitespace.respond_to?(:force_encoding) + + # Ruby 1.9 doesn't recognize EM SPACE as whitespace! + if @whitespace.respond_to?(:force_encoding) + @whitespace.slice!(2) + @whitespace.force_encoding(Encoding::UTF_8) + end + @byte_order_mark = [65279].pack('U') end -- cgit v1.2.3 From a2de13e1e0de5bba3cbd6050fe4884595704e1e2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Nov 2009 10:38:02 +0100 Subject: Ruby 1.9.2: URI.escape is obsolete --- activesupport/test/core_ext/uri_ext_test.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/uri_ext_test.rb b/activesupport/test/core_ext/uri_ext_test.rb index 2d4f38d095..e4a242abc4 100644 --- a/activesupport/test/core_ext/uri_ext_test.rb +++ b/activesupport/test/core_ext/uri_ext_test.rb @@ -7,7 +7,11 @@ class URIExtTest < Test::Unit::TestCase str = "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # Ni-ho-nn-go in UTF-8, means Japanese. str.force_encoding(Encoding::UTF_8) if str.respond_to?(:force_encoding) - assert_equal str, URI.unescape(URI.escape(str)) - assert_equal str, URI.decode(URI.escape(str)) + if URI.const_defined?(:Parser) + parser = URI::Parser.new + assert_equal str, parser.unescape(parser.escape(str)) + else + assert_equal str, URI.unescape(URI.escape(str)) + end end end -- cgit v1.2.3 From b540eca5889d7a28fac39c9ec0df715aa89487ce Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Nov 2009 11:06:47 +0100 Subject: Consolidate Object#to_param and #to_query core extensions --- activesupport/test/core_ext/boolean_ext_test.rb | 12 ------ activesupport/test/core_ext/hash_ext_test.rb | 41 --------------------- activesupport/test/core_ext/nil_ext_test.rb | 8 ---- .../test/core_ext/object/to_param_test.rb | 19 ++++++++++ .../test/core_ext/object/to_query_test.rb | 43 ++++++++++++++++++++++ activesupport/test/core_ext/object_ext_test.rb | 7 ---- 6 files changed, 62 insertions(+), 68 deletions(-) delete mode 100644 activesupport/test/core_ext/boolean_ext_test.rb delete mode 100644 activesupport/test/core_ext/nil_ext_test.rb create mode 100644 activesupport/test/core_ext/object/to_param_test.rb create mode 100644 activesupport/test/core_ext/object/to_query_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/boolean_ext_test.rb b/activesupport/test/core_ext/boolean_ext_test.rb deleted file mode 100644 index 9439716efb..0000000000 --- a/activesupport/test/core_ext/boolean_ext_test.rb +++ /dev/null @@ -1,12 +0,0 @@ -require 'abstract_unit' -require 'active_support/core_ext/boolean/conversions' - -class BooleanExtAccessTests < Test::Unit::TestCase - def test_to_param_on_true - assert_equal true, true.to_param - end - - def test_to_param_on_false - assert_equal false, false.to_param - end -end diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb index eb4c37aaf0..4642bb1330 100644 --- a/activesupport/test/core_ext/hash_ext_test.rb +++ b/activesupport/test/core_ext/hash_ext_test.rb @@ -899,42 +899,6 @@ class HashToXmlTest < Test::Unit::TestCase # :builder, etc, shouldn't be added to options assert_equal({:skip_instruct => true}, options) end -end - -class QueryTest < Test::Unit::TestCase - def test_simple_conversion - assert_query_equal 'a=10', :a => 10 - end - - def test_cgi_escaping - assert_query_equal 'a%3Ab=c+d', 'a:b' => 'c d' - end - - def test_nil_parameter_value - empty = Object.new - def empty.to_param; nil end - assert_query_equal 'a=', 'a' => empty - end - - def test_nested_conversion - assert_query_equal 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas', - :person => {:name => 'Nicholas', :login => 'seckar'} - end - - def test_multiple_nested - assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10', - :person => {:id => 10}, :account => {:person => {:id => 20}} - end - - def test_array_values - assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20', - :person => {:id => [10, 20]} - end - - def test_array_values_are_not_sorted - assert_query_equal 'person%5Bid%5D%5B%5D=20&person%5Bid%5D%5B%5D=10', - :person => {:id => [20, 10]} - end def test_expansion_count_is_limited expected = { @@ -962,9 +926,4 @@ class QueryTest < Test::Unit::TestCase Hash.from_xml(attack_xml) end end - - private - def assert_query_equal(expected, actual, message = nil) - assert_equal expected.split('&'), actual.to_query.split('&') - end end diff --git a/activesupport/test/core_ext/nil_ext_test.rb b/activesupport/test/core_ext/nil_ext_test.rb deleted file mode 100644 index 1062676d65..0000000000 --- a/activesupport/test/core_ext/nil_ext_test.rb +++ /dev/null @@ -1,8 +0,0 @@ -require 'abstract_unit' -require 'active_support/core_ext/nil/conversions' - -class NilExtAccessTests < Test::Unit::TestCase - def test_to_param - assert_nil nil.to_param - end -end diff --git a/activesupport/test/core_ext/object/to_param_test.rb b/activesupport/test/core_ext/object/to_param_test.rb new file mode 100644 index 0000000000..c3efefddb5 --- /dev/null +++ b/activesupport/test/core_ext/object/to_param_test.rb @@ -0,0 +1,19 @@ +require 'abstract_unit' +require 'active_support/core_ext/object/to_param' + +class ToParamTest < Test::Unit::TestCase + def test_object + foo = Object.new + def foo.to_s; 'foo' end + assert_equal 'foo', foo.to_param + end + + def test_nil + assert_nil nil.to_param + end + + def test_boolean + assert_equal true, true.to_param + assert_equal false, false.to_param + end +end diff --git a/activesupport/test/core_ext/object/to_query_test.rb b/activesupport/test/core_ext/object/to_query_test.rb new file mode 100644 index 0000000000..0fb15be654 --- /dev/null +++ b/activesupport/test/core_ext/object/to_query_test.rb @@ -0,0 +1,43 @@ +require 'abstract_unit' +require 'active_support/core_ext/object/to_query' + +class ToQueryTest < Test::Unit::TestCase + def test_simple_conversion + assert_query_equal 'a=10', :a => 10 + end + + def test_cgi_escaping + assert_query_equal 'a%3Ab=c+d', 'a:b' => 'c d' + end + + def test_nil_parameter_value + empty = Object.new + def empty.to_param; nil end + assert_query_equal 'a=', 'a' => empty + end + + def test_nested_conversion + assert_query_equal 'person%5Blogin%5D=seckar&person%5Bname%5D=Nicholas', + :person => {:name => 'Nicholas', :login => 'seckar'} + end + + def test_multiple_nested + assert_query_equal 'account%5Bperson%5D%5Bid%5D=20&person%5Bid%5D=10', + :person => {:id => 10}, :account => {:person => {:id => 20}} + end + + def test_array_values + assert_query_equal 'person%5Bid%5D%5B%5D=10&person%5Bid%5D%5B%5D=20', + :person => {:id => [10, 20]} + end + + def test_array_values_are_not_sorted + assert_query_equal 'person%5Bid%5D%5B%5D=20&person%5Bid%5D%5B%5D=10', + :person => {:id => [20, 10]} + end + + private + def assert_query_equal(expected, actual, message = nil) + assert_equal expected.split('&'), actual.to_query.split('&') + end +end diff --git a/activesupport/test/core_ext/object_ext_test.rb b/activesupport/test/core_ext/object_ext_test.rb index 484eecaab6..04c19ed2aa 100644 --- a/activesupport/test/core_ext/object_ext_test.rb +++ b/activesupport/test/core_ext/object_ext_test.rb @@ -1,16 +1,9 @@ require 'abstract_unit' require 'active_support/core_ext/object/metaclass' -require 'active_support/core_ext/object/conversions' class ObjectExtTest < Test::Unit::TestCase def test_tap_yields_and_returns_self foo = Object.new assert_equal foo, foo.tap { |x| assert_equal foo, x; :bar } end - - def test_to_param - foo = Object.new - foo.class_eval("def to_s; 'foo'; end") - assert_equal 'foo', foo.to_param - end end -- cgit v1.2.3 From 36a9644b86c53e322a891669cbdd2cf62cd648ac Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 1 Nov 2009 13:43:53 +0100 Subject: Clarify date/time dependencies --- activesupport/test/caching_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 892aa97ad7..6a51ce9402 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -154,13 +154,13 @@ class FileStoreTest < ActiveSupport::TestCase File.stubs(:mtime).returns(time) @cache.write('foo', 'bar') - cache_read = lambda { @cache.read('foo', :expires_in => 1.minute) } + cache_read = lambda { @cache.read('foo', :expires_in => 60) } assert_equal 'bar', cache_read.call - Time.stubs(:now).returns(time + 30.seconds) + Time.stubs(:now).returns(time + 30) assert_equal 'bar', cache_read.call - Time.stubs(:now).returns(time + 2.minutes) + Time.stubs(:now).returns(time + 120) assert_nil cache_read.call end end -- cgit v1.2.3 From 23780850237876cf81038534d8f59fa307af0b31 Mon Sep 17 00:00:00 2001 From: Matias Flores Date: Sat, 26 Sep 2009 21:32:21 -0300 Subject: Fix chars.reverse for multibyte decomposed strings [#597 state:committed] Signed-off-by: Jeremy Kemper --- activesupport/test/multibyte_chars_test.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 0d3c3c7716..a1ea8c8f4c 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -384,6 +384,17 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase assert_equal 'わちにこ', @chars.reverse end + def test_reverse_should_work_with_normalized_strings + str = 'bös' + reversed_str = 'söb' + assert_equal chars(reversed_str).normalize(:kc), chars(str).normalize(:kc).reverse + assert_equal chars(reversed_str).normalize(:c), chars(str).normalize(:c).reverse + assert_equal chars(reversed_str).normalize(:d), chars(str).normalize(:d).reverse + assert_equal chars(reversed_str).normalize(:kd), chars(str).normalize(:kd).reverse + assert_equal chars(reversed_str).decompose, chars(str).decompose.reverse + assert_equal chars(reversed_str).compose, chars(str).compose.reverse + end + def test_slice_should_take_character_offsets assert_equal nil, ''.mb_chars.slice(0) assert_equal 'こ', @chars.slice(0) -- cgit v1.2.3 From 935bd0fef8e26f4ec65fe411a1d29942493f8d46 Mon Sep 17 00:00:00 2001 From: Manfred Stienstra Date: Sun, 1 Nov 2009 17:18:27 +0100 Subject: Add ActiveSupport::Multibyte::Chars#limit. The limit method limits the number of bytes in a string. Useful when the storage space of the string is limited, for instance in a database column definition. Sharpen up the implementation of translate offset. [#3192 state:committed] --- activesupport/test/multibyte_chars_test.rb | 59 ++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index a1ea8c8f4c..fc7b5e5081 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -169,6 +169,7 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase assert chars('').strip.kind_of?(ActiveSupport::Multibyte.proxy_class) assert chars('').reverse.kind_of?(ActiveSupport::Multibyte.proxy_class) assert chars(' ').slice(0).kind_of?(ActiveSupport::Multibyte.proxy_class) + assert chars('').limit(0).kind_of?(ActiveSupport::Multibyte.proxy_class) assert chars('').upcase.kind_of?(ActiveSupport::Multibyte.proxy_class) assert chars('').downcase.kind_of?(ActiveSupport::Multibyte.proxy_class) assert chars('').capitalize.kind_of?(ActiveSupport::Multibyte.proxy_class) @@ -196,7 +197,9 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase def test_should_return_character_offset_for_regexp_matches assert_nil(@chars =~ /wrong/u) assert_equal 0, (@chars =~ /こ/u) + assert_equal 0, (@chars =~ /こに/u) assert_equal 1, (@chars =~ /に/u) + assert_equal 2, (@chars =~ /ち/u) assert_equal 3, (@chars =~ /わ/u) end @@ -493,6 +496,44 @@ class MultibyteCharsExtrasTest < Test::Unit::TestCase end end + def test_limit_should_not_break_on_blank_strings + chars = ''.mb_chars + + assert_equal '', chars.limit(0) + assert_equal '', chars.limit(1) + end + + def test_limit_should_work_on_a_multibyte_string + chars = UNICODE_STRING.mb_chars + + assert_equal UNICODE_STRING, chars.limit(UNICODE_STRING.length) + assert_equal '', chars.limit(0) + assert_equal '', chars.limit(1) + assert_equal 'こ', chars.limit(3) + assert_equal 'こに', chars.limit(6) + assert_equal 'こに', chars.limit(8) + assert_equal 'こにち', chars.limit(9) + assert_equal 'こにちわ', chars.limit(50) + end + + def test_limit_should_work_on_an_ascii_string + ascii = ASCII_STRING.mb_chars + + assert_equal ASCII_STRING, ascii.limit(ASCII_STRING.length) + assert_equal '', ascii.limit(0) + assert_equal 'o', ascii.limit(1) + assert_equal 'oh', ascii.limit(2) + assert_equal 'ohay', ascii.limit(4) + assert_equal 'ohayo', ascii.limit(50) + end + + def test_limit_should_keep_under_the_specified_byte_limit + chars = UNICODE_STRING.mb_chars + (1..UNICODE_STRING.length).each do |limit| + assert chars.limit(limit).to_s.length <= limit + end + end + def test_composition_exclusion_is_set_up_properly # Normalization of DEVANAGARI LETTER QA breaks when composition exclusion isn't used correctly qa = [0x915, 0x93c].pack('U*') @@ -603,3 +644,21 @@ class MultibyteCharsExtrasTest < Test::Unit::TestCase end.pack('U*') end end + +class MultibyteInternalsTest < ActiveSupport::TestCase + include MultibyteTestHelpers + + test "Chars translates a character offset to a byte offset" do + chars = "Puisque c'était son erreur, il m'a aidé".mb_chars + [ + [0, 0], + [3, 3], + [12, 11], + [14, 13], + [41, 39] + ].each do |byte_offset, character_offset| + assert_equal character_offset, chars.send(:translate_offset, byte_offset), + "Expected byte offset #{byte_offset} to translate to #{character_offset}" + end + end +end \ No newline at end of file -- cgit v1.2.3 From da3a228a93e1858a46f9a0cefcb9c72285479a56 Mon Sep 17 00:00:00 2001 From: Manfred Stienstra Date: Mon, 2 Nov 2009 21:34:07 +0100 Subject: Make ActiveSupport::Chars#limit run on Ruby 1.9. --- activesupport/test/multibyte_chars_test.rb | 56 ++++++++++++++---------------- 1 file changed, 27 insertions(+), 29 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index fc7b5e5081..4ff74abf61 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -100,15 +100,14 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase def setup @chars = UNICODE_STRING.dup.mb_chars - # NEWLINE, SPACE, EM SPACE - @whitespace = "\n#{[32, 8195].pack('U*')}" - - # Ruby 1.9 doesn't recognize EM SPACE as whitespace! - if @whitespace.respond_to?(:force_encoding) - @whitespace.slice!(2) - @whitespace.force_encoding(Encoding::UTF_8) + if RUBY_VERSION < '1.9' + # Multibyte support all kinds of whitespace (ie. NEWLINE, SPACE, EM SPACE) + @whitespace = "\n\t#{[32, 8195].pack('U*')}" + else + # Ruby 1.9 only supports basic whitespace + @whitespace = "\n\t ".force_encoding(Encoding::UTF_8) end - + @byte_order_mark = [65279].pack('U') end @@ -497,28 +496,27 @@ class MultibyteCharsExtrasTest < Test::Unit::TestCase end def test_limit_should_not_break_on_blank_strings - chars = ''.mb_chars - - assert_equal '', chars.limit(0) - assert_equal '', chars.limit(1) + example = chars('') + assert_equal example, example.limit(0) + assert_equal example, example.limit(1) end def test_limit_should_work_on_a_multibyte_string - chars = UNICODE_STRING.mb_chars + example = chars(UNICODE_STRING) + bytesize = UNICODE_STRING.respond_to?(:bytesize) ? UNICODE_STRING.bytesize : UNICODE_STRING.size - assert_equal UNICODE_STRING, chars.limit(UNICODE_STRING.length) - assert_equal '', chars.limit(0) - assert_equal '', chars.limit(1) - assert_equal 'こ', chars.limit(3) - assert_equal 'こに', chars.limit(6) - assert_equal 'こに', chars.limit(8) - assert_equal 'こにち', chars.limit(9) - assert_equal 'こにちわ', chars.limit(50) + assert_equal UNICODE_STRING, example.limit(bytesize) + assert_equal '', example.limit(0) + assert_equal '', example.limit(1) + assert_equal 'こ', example.limit(3) + assert_equal 'こに', example.limit(6) + assert_equal 'こに', example.limit(8) + assert_equal 'こにち', example.limit(9) + assert_equal 'こにちわ', example.limit(50) end def test_limit_should_work_on_an_ascii_string - ascii = ASCII_STRING.mb_chars - + ascii = chars(ASCII_STRING) assert_equal ASCII_STRING, ascii.limit(ASCII_STRING.length) assert_equal '', ascii.limit(0) assert_equal 'o', ascii.limit(1) @@ -528,12 +526,12 @@ class MultibyteCharsExtrasTest < Test::Unit::TestCase end def test_limit_should_keep_under_the_specified_byte_limit - chars = UNICODE_STRING.mb_chars + example = chars(UNICODE_STRING) (1..UNICODE_STRING.length).each do |limit| - assert chars.limit(limit).to_s.length <= limit + assert example.limit(limit).to_s.length <= limit end end - + def test_composition_exclusion_is_set_up_properly # Normalization of DEVANAGARI LETTER QA breaks when composition exclusion isn't used correctly qa = [0x915, 0x93c].pack('U*') @@ -647,9 +645,9 @@ end class MultibyteInternalsTest < ActiveSupport::TestCase include MultibyteTestHelpers - + test "Chars translates a character offset to a byte offset" do - chars = "Puisque c'était son erreur, il m'a aidé".mb_chars + example = chars("Puisque c'était son erreur, il m'a aidé") [ [0, 0], [3, 3], @@ -657,7 +655,7 @@ class MultibyteInternalsTest < ActiveSupport::TestCase [14, 13], [41, 39] ].each do |byte_offset, character_offset| - assert_equal character_offset, chars.send(:translate_offset, byte_offset), + assert_equal character_offset, example.send(:translate_offset, byte_offset), "Expected byte offset #{byte_offset} to translate to #{character_offset}" end end -- cgit v1.2.3 From 6c59e5a558922b8f4084533071c3d93e151858ac Mon Sep 17 00:00:00 2001 From: Chris Hapgood Date: Wed, 4 Nov 2009 10:13:59 -0500 Subject: Fix OrderedHash#replace Signed-off-by: Michael Koziarski --- activesupport/test/ordered_hash_test.rb | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb index 15bd57181f..1521279437 100644 --- a/activesupport/test/ordered_hash_test.rb +++ b/activesupport/test/ordered_hash_test.rb @@ -191,4 +191,11 @@ class OrderedHashTest < Test::Unit::TestCase assert_equal "odd number of arguments for Hash", $!.message end end + + def test_replace_updates_keys + @other_ordered_hash = ActiveSupport::OrderedHash[:black, '000000', :white, '000000'] + original = @ordered_hash.replace(@other_ordered_hash) + assert_same original, @ordered_hash + assert_equal @other_ordered_hash.keys, @ordered_hash.keys + end end -- cgit v1.2.3 From 20cdaddfd27dfeef5c853e85fafa4e13b6da05f3 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Nov 2009 21:55:43 -0800 Subject: Ruby 1.9.2: work around changes to flatten and nil.to_str --- activesupport/test/multibyte_chars_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 4ff74abf61..9245263270 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -228,8 +228,8 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase assert !@chars.include?('a') end - def test_include_raises_type_error_when_nil_is_passed - assert_raise(TypeError) do + def test_include_raises_when_nil_is_passed + assert_raise(RUBY_VERSION >= '1.9.2' ? NoMethodError : TypeError) do @chars.include?(nil) end end @@ -659,4 +659,4 @@ class MultibyteInternalsTest < ActiveSupport::TestCase "Expected byte offset #{byte_offset} to translate to #{character_offset}" end end -end \ No newline at end of file +end -- cgit v1.2.3 From deddd55086ad3e9ae43bb0fa44911a77c8ee4495 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 8 Nov 2009 22:01:50 -0800 Subject: Work around assert_raise limitation --- activesupport/test/multibyte_chars_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb index 9245263270..0e489c10e1 100644 --- a/activesupport/test/multibyte_chars_test.rb +++ b/activesupport/test/multibyte_chars_test.rb @@ -229,9 +229,9 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase end def test_include_raises_when_nil_is_passed - assert_raise(RUBY_VERSION >= '1.9.2' ? NoMethodError : TypeError) do - @chars.include?(nil) - end + @chars.include?(nil) + flunk "Expected chars.include?(nil) to raise TypeError or NoMethodError" + rescue Exception => e end def test_index_should_return_character_offset -- cgit v1.2.3 From d4513ac69958063de3cad9aa655fe9d63e82ec76 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 20:26:46 +0100 Subject: Object#instance_variable_defined? is not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/object_and_class_ext_test.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index f0121b862d..e6fbdb637b 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -182,13 +182,6 @@ class ObjectInstanceVariableTest < Test::Unit::TestCase assert_equal %w(@bar @baz), @source.instance_variable_names.sort end - def test_instance_variable_defined - assert @source.instance_variable_defined?('@bar') - assert @source.instance_variable_defined?(:@bar) - assert !@source.instance_variable_defined?(:@foo) - assert !@source.instance_variable_defined?('@foo') - end - def test_copy_instance_variables_from_without_explicit_excludes assert_equal [], @dest.instance_variables @dest.copy_instance_variables_from(@source) -- cgit v1.2.3 From 1979e9c8553f4d7905822fdcc99e52d179e78c3c Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:11:26 +0100 Subject: Symbol#to_proc is not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/enumerable_test.rb | 1 - activesupport/test/core_ext/symbol_test.rb | 9 --------- activesupport/test/dependencies_test.rb | 1 - 3 files changed, 11 deletions(-) delete mode 100644 activesupport/test/core_ext/symbol_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index 4170de3dce..a8e98891d3 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -1,6 +1,5 @@ require 'abstract_unit' require 'active_support/core_ext/array' -require 'active_support/core_ext/symbol' require 'active_support/core_ext/enumerable' Payment = Struct.new(:price) diff --git a/activesupport/test/core_ext/symbol_test.rb b/activesupport/test/core_ext/symbol_test.rb deleted file mode 100644 index 1eaccb9965..0000000000 --- a/activesupport/test/core_ext/symbol_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'abstract_unit' - -class SymbolTests < Test::Unit::TestCase - def test_to_proc - assert_equal %w(one two three), [:one, :two, :three].map(&:to_s) - assert_equal(%w(one two three), - {1 => "one", 2 => "two", 3 => "three"}.sort_by(&:first).map(&:last)) - end -end diff --git a/activesupport/test/dependencies_test.rb b/activesupport/test/dependencies_test.rb index 97d70cf8c4..0fcf1eaf00 100644 --- a/activesupport/test/dependencies_test.rb +++ b/activesupport/test/dependencies_test.rb @@ -3,7 +3,6 @@ require 'pp' require 'active_support/dependencies' require 'active_support/core_ext/module/loading' require 'active_support/core_ext/kernel/reporting' -require 'active_support/core_ext/symbol/to_proc' module ModuleWithMissing mattr_accessor :missing_count -- cgit v1.2.3 From f8e713f488bba264ba73251b56ad56385b8ed824 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:23:19 +0100 Subject: Object#tap is not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/object_ext_test.rb | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 activesupport/test/core_ext/object_ext_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/object_ext_test.rb b/activesupport/test/core_ext/object_ext_test.rb deleted file mode 100644 index 04c19ed2aa..0000000000 --- a/activesupport/test/core_ext/object_ext_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'abstract_unit' -require 'active_support/core_ext/object/metaclass' - -class ObjectExtTest < Test::Unit::TestCase - def test_tap_yields_and_returns_self - foo = Object.new - assert_equal foo, foo.tap { |x| assert_equal foo, x; :bar } - end -end -- cgit v1.2.3 From 004db18cb0f690486336f0fa141ad383c79f9558 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:27:58 +0100 Subject: String#bytesize is not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/string_ext_test.rb | 7 ------- 1 file changed, 7 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 584a41b631..0f988e78a4 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -350,13 +350,6 @@ class TestGetTextString < Test::Unit::TestCase end end -class StringBytesizeTest < Test::Unit::TestCase - def test_bytesize - assert_respond_to 'foo', :bytesize - assert_equal 3, 'foo'.bytesize - end -end - class OutputSafetyTest < ActiveSupport::TestCase def setup @string = "hello" -- cgit v1.2.3 From c0bb4c6ed2f5a5676569746e7bfd70405346ef8f Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:35:43 +0100 Subject: String#start_with? and String#end_with? are not needed for Ruby >= 1.8.7, leaves their plural aliases though --- activesupport/test/core_ext/string_ext_test.rb | 8 -------- 1 file changed, 8 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 0f988e78a4..dd9e9e0ebb 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -185,17 +185,9 @@ class StringInflectionsTest < Test::Unit::TestCase assert s.starts_with?('hel') assert !s.starts_with?('el') - assert s.start_with?('h') - assert s.start_with?('hel') - assert !s.start_with?('el') - assert s.ends_with?('o') assert s.ends_with?('lo') assert !s.ends_with?('el') - - assert s.end_with?('o') - assert s.end_with?('lo') - assert !s.end_with?('el') end def test_string_squish -- cgit v1.2.3 From b6598c683bb8f8ef484f54755cced77a5d6200bb Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:39:32 +0100 Subject: String#each_char is not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/string_ext_test.rb | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index dd9e9e0ebb..ffcd086716 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -206,17 +206,6 @@ class StringInflectionsTest < Test::Unit::TestCase # And changes the original string: assert_equal original, expected end - - if RUBY_VERSION < '1.9' - def test_each_char_with_utf8_string_when_kcode_is_utf8 - with_kcode('UTF8') do - '€2.99'.each_char do |char| - assert_not_equal 1, char.length - break - end - end - end - end end class StringBehaviourTest < Test::Unit::TestCase -- cgit v1.2.3 From 329e7f44417c00a8d6fee337c288a463e9d64346 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 21:46:16 +0100 Subject: Integer#even? and Integer#odd? are not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/integer_ext_test.rb | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/integer_ext_test.rb b/activesupport/test/core_ext/integer_ext_test.rb index 956ae5189d..e1591089f5 100644 --- a/activesupport/test/core_ext/integer_ext_test.rb +++ b/activesupport/test/core_ext/integer_ext_test.rb @@ -2,22 +2,6 @@ require 'abstract_unit' require 'active_support/core_ext/integer' class IntegerExtTest < Test::Unit::TestCase - def test_even - assert [ -2, 0, 2, 4 ].all? { |i| i.even? } - assert ![ -1, 1, 3 ].all? { |i| i.even? } - - assert 22953686867719691230002707821868552601124472329079.odd? - assert !22953686867719691230002707821868552601124472329079.even? - assert 22953686867719691230002707821868552601124472329080.even? - assert !22953686867719691230002707821868552601124472329080.odd? - end - - def test_odd - assert ![ -2, 0, 2, 4 ].all? { |i| i.odd? } - assert [ -1, 1, 3 ].all? { |i| i.odd? } - assert 1000000000000000000000000000000000000000000000000000000001.odd? - end - def test_multiple_of [ -7, 0, 7, 14 ].each { |i| assert i.multiple_of?(7) } [ -7, 7, 14 ].each { |i| assert ! i.multiple_of?(6) } -- cgit v1.2.3 From db2c0d79e3f9ba4fa921b7a122e9e7849729de64 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Mon, 9 Nov 2009 22:05:31 +0100 Subject: Enumerable#none? is not needed for Ruby >= 1.8.7 --- activesupport/test/core_ext/enumerable_test.rb | 11 ----------- 1 file changed, 11 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb index a8e98891d3..66f5f9fbde 100644 --- a/activesupport/test/core_ext/enumerable_test.rb +++ b/activesupport/test/core_ext/enumerable_test.rb @@ -89,15 +89,4 @@ class EnumerableTests < Test::Unit::TestCase assert ![ 1, 2 ].many? {|x| x > 1 } assert [ 1, 2, 2 ].many? {|x| x > 1 } end - - def test_none - assert [].none? - assert [nil, false].none? - assert ![1].none? - - assert [].none? {|x| x > 1 } - assert ![ 2 ].none? {|x| x > 1 } - assert ![ 1, 2 ].none? {|x| x > 1 } - assert [ 1, 1 ].none? {|x| x > 1 } - end end -- cgit v1.2.3 From eeac054d59dd9373313596dc3ef9db30c4b227d5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 9 Nov 2009 16:57:10 -0800 Subject: Fix missing dependency --- activesupport/test/core_ext/string_ext_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index ffcd086716..1e8a1c4f08 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -5,6 +5,7 @@ require 'inflector_test_cases' require 'active_support/core_ext/string' require 'active_support/core_ext/time' +require 'active_support/core_ext/kernel/reporting' class StringInflectionsTest < Test::Unit::TestCase include InflectorTestCases -- cgit v1.2.3 From feaa762e40e7ca7fd9b43172dea92b14ed6b3584 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 9 Nov 2009 16:59:38 -0800 Subject: Ditto --- activesupport/test/multibyte_utils_test.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'activesupport/test') diff --git a/activesupport/test/multibyte_utils_test.rb b/activesupport/test/multibyte_utils_test.rb index 0a2f20d282..1dff944922 100644 --- a/activesupport/test/multibyte_utils_test.rb +++ b/activesupport/test/multibyte_utils_test.rb @@ -2,6 +2,7 @@ require 'abstract_unit' require 'multibyte_test_helpers' +require 'active_support/core_ext/kernel/reporting' class MultibyteUtilsTest < ActiveSupport::TestCase include MultibyteTestHelpers -- cgit v1.2.3 From 94058c689a1ad74ccc6df2732c360fcdea267cae Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 9 Nov 2009 22:44:28 -0600 Subject: Remove automatic rubygems loading from AS test runner --- activesupport/test/abstract_unit.rb | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index f390c66838..8967b54537 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -7,16 +7,8 @@ rescue LoadError $:.unshift("#{root}/activerecord/lib") end - require 'test/unit' - -begin - require 'mocha' -rescue LoadError - $stderr.puts 'Loading rubygems' - require 'rubygems' - require 'mocha' -end +require 'mocha' ENV['NO_RELOAD'] = '1' require 'active_support' -- cgit v1.2.3 From 22b581e04cb13a1a80768c0487490eec3dd85879 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 9 Nov 2009 22:56:49 -0600 Subject: Always add AS lib/ to path when running its test suite --- activesupport/test/abstract_unit.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index 8967b54537..d640c9bc63 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -1,12 +1,12 @@ ORIG_ARGV = ARGV.dup -root = File.expand_path('../../..', __FILE__) + begin - require "#{root}/vendor/gems/environment" + require File.expand_path('../../../vendor/gems/environment', __FILE__) rescue LoadError - $:.unshift("#{root}/activesupport/lib") - $:.unshift("#{root}/activerecord/lib") end +$:.unshift(File.dirname(__FILE__) + '/../lib') + require 'test/unit' require 'mocha' -- cgit v1.2.3 From 11e798ae0f2f46498811282756c9d21df3d4b523 Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 9 Nov 2009 23:28:36 -0600 Subject: Avoid adding component lib/ to load path multiple times --- activesupport/test/abstract_unit.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb index d640c9bc63..dda139372e 100644 --- a/activesupport/test/abstract_unit.rb +++ b/activesupport/test/abstract_unit.rb @@ -5,7 +5,8 @@ begin rescue LoadError end -$:.unshift(File.dirname(__FILE__) + '/../lib') +lib = File.expand_path("#{File.dirname(__FILE__)}/../lib") +$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib) require 'test/unit' require 'mocha' -- cgit v1.2.3 From c0ebc2149380a59371553765053f55671c737533 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 12 Nov 2009 16:19:33 -0800 Subject: Test that Array.wrap works with proxy objects and structs --- activesupport/test/core_ext/array_ext_test.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb index 8198b9bd2c..f5f91ddd80 100644 --- a/activesupport/test/core_ext/array_ext_test.rb +++ b/activesupport/test/core_ext/array_ext_test.rb @@ -339,6 +339,11 @@ class ArrayWrapperTests < Test::Unit::TestCase end end + class Proxy + def initialize(target) @target = target end + def method_missing(*a) @target.send(*a) end + end + def test_array ary = %w(foo bar) assert_same ary, Array.wrap(ary) @@ -364,4 +369,19 @@ class ArrayWrapperTests < Test::Unit::TestCase def test_object_with_to_ary assert_equal ["foo", "bar"], Array.wrap(FakeCollection.new) end + + def test_proxy_object + p = Proxy.new(Object.new) + assert_equal [p], Array.wrap(p) + end + + def test_proxy_to_object_with_to_ary + p = Proxy.new(FakeCollection.new) + assert_equal [p], Array.wrap(p) + end + + def test_struct + o = Struct.new(:foo).new(123) + assert_equal [o], Array.wrap(o) + end end -- cgit v1.2.3 From 9acc824d96db039486fc493c6f904035fe386967 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 13 Nov 2009 12:15:49 -0800 Subject: Ruby 1.9.2: disallow explicit coercion via method_missing. Only give friendly nil errors for Array and Active Record methods. --- activesupport/test/core_ext/name_error_test.rb | 26 +++++++++++--------------- activesupport/test/whiny_nil_test.rb | 14 +++++++++++++- 2 files changed, 24 insertions(+), 16 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/name_error_test.rb b/activesupport/test/core_ext/name_error_test.rb index 10913e2ade..6352484d04 100644 --- a/activesupport/test/core_ext/name_error_test.rb +++ b/activesupport/test/core_ext/name_error_test.rb @@ -3,23 +3,19 @@ require 'active_support/core_ext/name_error' class NameErrorTest < Test::Unit::TestCase def test_name_error_should_set_missing_name - begin - SomeNameThatNobodyWillUse____Really ? 1 : 0 - flunk "?!?!" - rescue NameError => exc - assert_equal "NameErrorTest::SomeNameThatNobodyWillUse____Really", exc.missing_name - assert exc.missing_name?(:SomeNameThatNobodyWillUse____Really) - assert exc.missing_name?("NameErrorTest::SomeNameThatNobodyWillUse____Really") - end + SomeNameThatNobodyWillUse____Really ? 1 : 0 + flunk "?!?!" + rescue NameError => exc + assert_equal "NameErrorTest::SomeNameThatNobodyWillUse____Really", exc.missing_name + assert exc.missing_name?(:SomeNameThatNobodyWillUse____Really) + assert exc.missing_name?("NameErrorTest::SomeNameThatNobodyWillUse____Really") end def test_missing_method_should_ignore_missing_name - begin - some_method_that_does_not_exist - flunk "?!?!" - rescue NameError => exc - assert_equal nil, exc.missing_name - assert ! exc.missing_name?(:Foo) - end + some_method_that_does_not_exist + flunk "?!?!" + rescue NameError => exc + assert !exc.missing_name?(:Foo) + assert_nil exc.missing_name end end diff --git a/activesupport/test/whiny_nil_test.rb b/activesupport/test/whiny_nil_test.rb index 4cb22c41b2..009d97940f 100644 --- a/activesupport/test/whiny_nil_test.rb +++ b/activesupport/test/whiny_nil_test.rb @@ -13,7 +13,7 @@ class WhinyNilTest < Test::Unit::TestCase def test_unchanged nil.method_thats_not_in_whiners rescue NoMethodError => nme - assert_match(/nil.method_thats_not_in_whiners/, nme.message) + assert(nme.message =~ /nil:NilClass/) end def test_active_record @@ -35,4 +35,16 @@ class WhinyNilTest < Test::Unit::TestCase rescue RuntimeError => nme assert(!(nme.message =~ /nil:NilClass/)) end + + def test_no_to_ary_coercion + nil.to_ary + rescue NoMethodError => nme + assert(nme.message =~ /nil:NilClass/) + end + + def test_no_to_str_coercion + nil.to_str + rescue NoMethodError => nme + assert(nme.message =~ /nil:NilClass/) + end end -- cgit v1.2.3 From 2841a14f4b26e093e88cdb6d84c82d120f53ca46 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 13 Nov 2009 12:42:40 -0800 Subject: Ruby 1.9.2: fix broken to_ary expectation --- activesupport/test/caching_test.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb index 6a51ce9402..00e05f76fe 100644 --- a/activesupport/test/caching_test.rb +++ b/activesupport/test/caching_test.rb @@ -30,7 +30,9 @@ class CacheStoreSettingTest < ActiveSupport::TestCase def test_mem_cache_fragment_cache_store_with_given_mem_cache_like_object MemCache.expects(:new).never - store = ActiveSupport::Cache.lookup_store :mem_cache_store, stub("memcache", :get => true) + memcache = Object.new + def memcache.get() true end + store = ActiveSupport::Cache.lookup_store :mem_cache_store, memcache assert_kind_of(ActiveSupport::Cache::MemCacheStore, store) end -- cgit v1.2.3 From 7ab78b9ccd1f760960a77b5be9aac0c27212abe2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 13 Nov 2009 18:57:10 -0800 Subject: CI: slow down brittle notifications tests --- activesupport/test/notifications_test.rb | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 9175c8f26e..334496374b 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -75,7 +75,7 @@ class NotificationsMainTest < Test::Unit::TestCase 1 + 1 end - sleep(0.1) + sleep 1 assert_equal 1, @events.size assert_equal :awesome, @events.last.name @@ -88,14 +88,14 @@ class NotificationsMainTest < Test::Unit::TestCase 1 + 1 end - sleep(0.1) + sleep 1 assert_equal 1, @events.size assert_equal :wot, @events.first.name assert_equal Hash[:payload => "child"], @events.first.payload end - sleep(0.1) + sleep 1 assert_equal 2, @events.size assert_equal :awesome, @events.last.name @@ -108,7 +108,7 @@ class NotificationsMainTest < Test::Unit::TestCase raise "OMG" end rescue RuntimeError - sleep(0.1) + sleep 1 assert_equal 1, @events.size assert_equal :awesome, @events.last.name @@ -117,7 +117,7 @@ class NotificationsMainTest < Test::Unit::TestCase def test_event_is_pushed_even_without_block ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") - sleep(0.1) + sleep 1 assert_equal 1, @events.size assert_equal :awesome, @events.last.name @@ -137,8 +137,9 @@ class NotificationsMainTest < Test::Unit::TestCase end ActiveSupport::Notifications.instrument(:cache){ 1 } - sleep 0.1 + sleep 1 + assert_equal 3, @another.size before, during, after = @another.map {|e| e.transaction_id } assert_equal before, after assert_not_equal before, during @@ -153,7 +154,7 @@ class NotificationsMainTest < Test::Unit::TestCase ActiveSupport::Notifications.instrument(:cache){ 1 } - sleep(0.1) + sleep 1 assert_equal 1, @another.size assert_equal :cache, @another.first.name @@ -169,7 +170,7 @@ class NotificationsMainTest < Test::Unit::TestCase ActiveSupport::Notifications.instrument(:something){ 0 } ActiveSupport::Notifications.instrument(:cache){ 1 } - sleep(0.1) + sleep 1 assert_equal 1, @another.size assert_equal :cache, @another.first.name @@ -186,7 +187,7 @@ class NotificationsMainTest < Test::Unit::TestCase ActiveSupport::Notifications.instrument(:value){ i } end - sleep 0.1 + sleep 1 assert_equal 100, @events.size assert_equal :value, @events.first.name -- cgit v1.2.3 From 66fda6b8949cde07966ab098125e1da7969e9468 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 13 Nov 2009 19:28:59 -0800 Subject: Fix duration check for longer sleep --- activesupport/test/notifications_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 334496374b..466af49931 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -100,7 +100,7 @@ class NotificationsMainTest < Test::Unit::TestCase assert_equal 2, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "notifications"], @events.last.payload - assert_in_delta 100, @events.last.duration, 70 + assert_in_delta 1000, @events.last.duration, 70 end def test_event_is_pushed_even_if_block_fails -- cgit v1.2.3 From 58c0d31487d158286576b0745a55a0941ed076ad Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 13 Nov 2009 20:58:38 -0800 Subject: Notifications: queue.drained? for testability in place of brittle sleeps --- activesupport/test/notifications_test.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb index 466af49931..01106e83e9 100644 --- a/activesupport/test/notifications_test.rb +++ b/activesupport/test/notifications_test.rb @@ -75,7 +75,7 @@ class NotificationsMainTest < Test::Unit::TestCase 1 + 1 end - sleep 1 + drain assert_equal 1, @events.size assert_equal :awesome, @events.last.name @@ -88,19 +88,18 @@ class NotificationsMainTest < Test::Unit::TestCase 1 + 1 end - sleep 1 + drain assert_equal 1, @events.size assert_equal :wot, @events.first.name assert_equal Hash[:payload => "child"], @events.first.payload end - sleep 1 + drain assert_equal 2, @events.size assert_equal :awesome, @events.last.name assert_equal Hash[:payload => "notifications"], @events.last.payload - assert_in_delta 1000, @events.last.duration, 70 end def test_event_is_pushed_even_if_block_fails @@ -108,7 +107,7 @@ class NotificationsMainTest < Test::Unit::TestCase raise "OMG" end rescue RuntimeError - sleep 1 + drain assert_equal 1, @events.size assert_equal :awesome, @events.last.name @@ -117,7 +116,7 @@ class NotificationsMainTest < Test::Unit::TestCase def test_event_is_pushed_even_without_block ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") - sleep 1 + drain assert_equal 1, @events.size assert_equal :awesome, @events.last.name @@ -137,7 +136,7 @@ class NotificationsMainTest < Test::Unit::TestCase end ActiveSupport::Notifications.instrument(:cache){ 1 } - sleep 1 + drain assert_equal 3, @another.size before, during, after = @another.map {|e| e.transaction_id } @@ -154,7 +153,7 @@ class NotificationsMainTest < Test::Unit::TestCase ActiveSupport::Notifications.instrument(:cache){ 1 } - sleep 1 + drain assert_equal 1, @another.size assert_equal :cache, @another.first.name @@ -170,7 +169,7 @@ class NotificationsMainTest < Test::Unit::TestCase ActiveSupport::Notifications.instrument(:something){ 0 } ActiveSupport::Notifications.instrument(:cache){ 1 } - sleep 1 + drain assert_equal 1, @another.size assert_equal :cache, @another.first.name @@ -187,7 +186,7 @@ class NotificationsMainTest < Test::Unit::TestCase ActiveSupport::Notifications.instrument(:value){ i } end - sleep 1 + drain assert_equal 100, @events.size assert_equal :value, @events.first.name @@ -199,4 +198,9 @@ class NotificationsMainTest < Test::Unit::TestCase assert_equal 1, @another.first.result assert_equal 100, @another.last.result end + + private + def drain + sleep(0.1) until ActiveSupport::Notifications.queue.drained? + end end -- cgit v1.2.3 From 7eb4f2ecc53831d850682d88634ccf41676e40a2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 14 Nov 2009 01:13:54 -0800 Subject: Consolidate date & time landscape: require 'active_support/time' --- activesupport/test/core_ext/date_ext_test.rb | 2 +- activesupport/test/core_ext/date_time_ext_test.rb | 2 +- activesupport/test/core_ext/string_ext_test.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb index 18422d68bc..23c9bc7fb1 100644 --- a/activesupport/test/core_ext/date_ext_test.rb +++ b/activesupport/test/core_ext/date_ext_test.rb @@ -1,5 +1,5 @@ require 'abstract_unit' -require 'active_support/core_ext/date' +require 'active_support/time' class DateExtCalculationsTest < Test::Unit::TestCase def test_to_s diff --git a/activesupport/test/core_ext/date_time_ext_test.rb b/activesupport/test/core_ext/date_time_ext_test.rb index a7b179b2be..4341ead488 100644 --- a/activesupport/test/core_ext/date_time_ext_test.rb +++ b/activesupport/test/core_ext/date_time_ext_test.rb @@ -1,5 +1,5 @@ require 'abstract_unit' -require 'active_support/core_ext/date_time' +require 'active_support/time' class DateTimeExtCalculationsTest < Test::Unit::TestCase def test_to_s diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb index 1e8a1c4f08..56ed296dac 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -4,7 +4,7 @@ require 'abstract_unit' require 'inflector_test_cases' require 'active_support/core_ext/string' -require 'active_support/core_ext/time' +require 'active_support/time' require 'active_support/core_ext/kernel/reporting' class StringInflectionsTest < Test::Unit::TestCase -- cgit v1.2.3 From 61843595eaa58fc3c7f30043f2e9d84e2ef2b401 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 14 Nov 2009 02:14:43 -0800 Subject: No need for test stub --- activesupport/test/i18n_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/i18n_test.rb b/activesupport/test/i18n_test.rb index 9868f1e87d..dfcd4f822d 100644 --- a/activesupport/test/i18n_test.rb +++ b/activesupport/test/i18n_test.rb @@ -9,8 +9,8 @@ class I18nTest < Test::Unit::TestCase end def test_time_zone_localization_with_default_format - Time.zone.stubs(:now).returns Time.local(2000) - assert_equal Time.zone.now.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(Time.zone.now) + now = Time.local(2000) + assert_equal now.strftime("%a, %d %b %Y %H:%M:%S %z"), I18n.localize(now) end def test_date_localization_should_use_default_format -- cgit v1.2.3 From bc1538e9951847dd4d1d7e37816d91b791862a0c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 14 Nov 2009 11:37:06 -0800 Subject: Repair time dependencies --- activesupport/test/core_ext/range_ext_test.rb | 2 +- activesupport/test/json/decoding_test.rb | 1 + activesupport/test/message_encryptor_test.rb | 1 + activesupport/test/message_verifier_test.rb | 2 ++ 4 files changed, 5 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/range_ext_test.rb b/activesupport/test/core_ext/range_ext_test.rb index 2565c56b8a..5701eeef28 100644 --- a/activesupport/test/core_ext/range_ext_test.rb +++ b/activesupport/test/core_ext/range_ext_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' +require 'active_support/time' require 'active_support/core_ext/range' -require 'active_support/core_ext/date/conversions' class RangeTest < Test::Unit::TestCase def test_to_s_from_dates diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 7b5a4d0416..8fcb16abfb 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -1,6 +1,7 @@ # encoding: UTF-8 require 'abstract_unit' require 'active_support/json' +require 'active_support/time' require 'active_support/core_ext/kernel/reporting' class TestJSONDecoding < ActiveSupport::TestCase diff --git a/activesupport/test/message_encryptor_test.rb b/activesupport/test/message_encryptor_test.rb index ed3461571a..5c2b44f188 100644 --- a/activesupport/test/message_encryptor_test.rb +++ b/activesupport/test/message_encryptor_test.rb @@ -1,4 +1,5 @@ require 'abstract_unit' +require 'active_support/time' class MessageEncryptorTest < Test::Unit::TestCase def setup diff --git a/activesupport/test/message_verifier_test.rb b/activesupport/test/message_verifier_test.rb index ef300e4e26..714a3b3a39 100644 --- a/activesupport/test/message_verifier_test.rb +++ b/activesupport/test/message_verifier_test.rb @@ -7,6 +7,8 @@ rescue LoadError, NameError $stderr.puts "Skipping MessageVerifier test: broken OpenSSL install" else +require 'active_support/time' + class MessageVerifierTest < Test::Unit::TestCase def setup @verifier = ActiveSupport::MessageVerifier.new("Hey, I'm a secret!") -- cgit v1.2.3