From db619c4a4115d5eeca9473f8f77f34c877e4e1dd Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 5 Jan 2009 01:10:55 -0800 Subject: Sync 'rails/rails/master' --- activesupport/test/core_ext/time_with_zone_test.rb | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index dc36336239..7c8fb7dd94 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -256,6 +256,15 @@ class TimeWithZoneTest < Test::Unit::TestCase twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) assert_equal 86_400.0, twz2 - twz1 end + + def test_minus_with_datetime + assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1) + end + + def test_minus_with_wrapped_datetime + assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - Time.utc(2000, 1, 1) + assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( DateTime.civil(2000, 1, 2), ActiveSupport::TimeZone['UTC'] ) - DateTime.civil(2000, 1, 1) + end def test_plus_and_minus_enforce_spring_dst_rules silence_warnings do # silence warnings raised by tzinfo gem -- cgit v1.2.3 From b1530545d259c144770bd8fd7881cb16160c0afc Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Wed, 7 Jan 2009 17:51:11 +0000 Subject: Fix JSON decoder date-converter regexp [#1662 state:resolved] [Jonathan del Strother] --- activesupport/test/json/decoding_test.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb index 19ae3a01a8..558b03b90d 100644 --- a/activesupport/test/json/decoding_test.rb +++ b/activesupport/test/json/decoding_test.rb @@ -15,7 +15,8 @@ class TestJSONDecoding < Test::Unit::TestCase # no time zone %({a: "2007-01-01 01:12:34"}) => {'a' => "2007-01-01 01:12:34"}, # needs to be *exact* - %({a: " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "}, + %({a: " 2007-01-01 01:12:34 Z "}) => {'a' => " 2007-01-01 01:12:34 Z "}, + %({a: "2007-01-01 : it's your birthday"}) => {'a' => "2007-01-01 : it's your birthday"}, %([]) => [], %({}) => {}, %(1) => 1, -- cgit v1.2.3 From c2d23affad0ed4542e3906c334a7b27b07fc695c Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 7 Jan 2009 13:19:48 -0800 Subject: Object#tap for Ruby < 1.8.7 --- activesupport/test/core_ext/object_ext_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create 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 new file mode 100644 index 0000000000..a413d331c4 --- /dev/null +++ b/activesupport/test/core_ext/object_ext_test.rb @@ -0,0 +1,8 @@ +require 'abstract_unit' + +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 44cdf0c5fe080d470429ab0640af25e927244907 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 2 Feb 2009 09:54:23 -0800 Subject: Some merge cleanup --- activesupport/test/new_callbacks_test.rb | 121 +++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 activesupport/test/new_callbacks_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb new file mode 100644 index 0000000000..e0edf262fc --- /dev/null +++ b/activesupport/test/new_callbacks_test.rb @@ -0,0 +1,121 @@ +# require 'abstract_unit' +require 'test/unit' +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'active_support' + +class Record + include ActiveSupport::Callbacks + define_callbacks :save +end + +class AroundPerson < Record + attr_reader :history + + save_callback :before, :nope, :if => :no + save_callback :before, :nope, :unless => :yes + save_callback :after, :tweedle + save_callback :before, "tweedle_dee" + save_callback :before, proc {|m| m.history << "yup" } + save_callback :before, :nope, :if => proc { false } + save_callback :before, :nope, :unless => proc { true } + save_callback :before, :yup, :if => proc { true } + save_callback :before, :yup, :unless => proc { false } + save_callback :around, :tweedle_dum + save_callback :around, :w0tyes, :if => :yes + save_callback :around, :w0tno, :if => :no + save_callback :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 Foo + include ActiveSupport::Callbacks + define_callbacks :save +end + +class Bar < Foo + save_callback(:before) {|s| puts "Before" } +end + +class Baz < Bar + save_callback(:after) {|s| puts "After"} +end + +class Bat < Baz + def inside + _run_save_callbacks do + puts "Inside" + end + end +end + +Bat.new.inside + +# class AroundCallbacksTest < Test::Unit::TestCase +# def test_save_around +# around = AroundPerson.new +# around.save +# assert_equal [ +# "tweedle dee", +# "yup", "yup", "yup", +# "tweedle dum pre", +# "w0tyes before", +# "tweedle deedle pre", +# "running", +# "tweedle deedle post", +# "w0tyes after", +# "tweedle dum post", +# "tweedle" +# ], around.history +# end +# end \ No newline at end of file -- cgit v1.2.3 From d4a817c066ec66ab6f44958a3e10619f952b77e8 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 2 Feb 2009 14:01:29 -0800 Subject: Make tests pass --- activesupport/test/new_callbacks_test.rb | 121 ------------------------------- 1 file changed, 121 deletions(-) delete mode 100644 activesupport/test/new_callbacks_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb deleted file mode 100644 index e0edf262fc..0000000000 --- a/activesupport/test/new_callbacks_test.rb +++ /dev/null @@ -1,121 +0,0 @@ -# require 'abstract_unit' -require 'test/unit' -$:.unshift "#{File.dirname(__FILE__)}/../lib" -require 'active_support' - -class Record - include ActiveSupport::Callbacks - define_callbacks :save -end - -class AroundPerson < Record - attr_reader :history - - save_callback :before, :nope, :if => :no - save_callback :before, :nope, :unless => :yes - save_callback :after, :tweedle - save_callback :before, "tweedle_dee" - save_callback :before, proc {|m| m.history << "yup" } - save_callback :before, :nope, :if => proc { false } - save_callback :before, :nope, :unless => proc { true } - save_callback :before, :yup, :if => proc { true } - save_callback :before, :yup, :unless => proc { false } - save_callback :around, :tweedle_dum - save_callback :around, :w0tyes, :if => :yes - save_callback :around, :w0tno, :if => :no - save_callback :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 Foo - include ActiveSupport::Callbacks - define_callbacks :save -end - -class Bar < Foo - save_callback(:before) {|s| puts "Before" } -end - -class Baz < Bar - save_callback(:after) {|s| puts "After"} -end - -class Bat < Baz - def inside - _run_save_callbacks do - puts "Inside" - end - end -end - -Bat.new.inside - -# class AroundCallbacksTest < Test::Unit::TestCase -# def test_save_around -# around = AroundPerson.new -# around.save -# assert_equal [ -# "tweedle dee", -# "yup", "yup", "yup", -# "tweedle dum pre", -# "w0tyes before", -# "tweedle deedle pre", -# "running", -# "tweedle deedle post", -# "w0tyes after", -# "tweedle dum post", -# "tweedle" -# ], around.history -# end -# end \ No newline at end of file -- cgit v1.2.3 From c16c7a8de4e543a92de10a138bdd7caa5ac902d7 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Fri, 27 Feb 2009 19:25:45 -0800 Subject: Add support for callbacks --- .../test/new_callback_inheritance_test.rb | 115 +++++++ activesupport/test/new_callbacks_test.rb | 382 +++++++++++++++++++++ 2 files changed, 497 insertions(+) create mode 100644 activesupport/test/new_callback_inheritance_test.rb create mode 100644 activesupport/test/new_callbacks_test.rb (limited to 'activesupport/test') diff --git a/activesupport/test/new_callback_inheritance_test.rb b/activesupport/test/new_callback_inheritance_test.rb new file mode 100644 index 0000000000..95020389b0 --- /dev/null +++ b/activesupport/test/new_callback_inheritance_test.rb @@ -0,0 +1,115 @@ +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 + dispatch_callback :before, :before1, :before2, :per_key => {:if => proc {|c| c.action_name == "index" || c.action_name == "update" }} + dispatch_callback :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_dispatch_callback :before, :before2, :per_key => {:unless => proc {|c| c.action_name == "update" }} + skip_dispatch_callback :after, :after2, :per_key => {:unless => proc {|c| c.action_name == "delete" }} +end + +class Child < GrandParent + skip_dispatch_callback :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 \ No newline at end of file diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb new file mode 100644 index 0000000000..6948ad23dc --- /dev/null +++ b/activesupport/test/new_callbacks_test.rb @@ -0,0 +1,382 @@ +# require 'abstract_unit' +require 'test/unit' +$:.unshift "#{File.dirname(__FILE__)}/../lib" +require 'active_support' + +class Record + include ActiveSupport::NewCallbacks + + define_callbacks :save + + def self.before_save(*filters, &blk) + save_callback(:before, *filters, &blk) + end + + def self.after_save(*filters, &blk) + save_callback(:after, *filters, &blk) + end + + class << self + def callback_symbol(callback_method) + returning(:"#{callback_method}_method") do |method_name| + define_method(method_name) do + history << [callback_method, :symbol] + end + end + 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, :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)) + send(callback_method) { |model| model.history << [callback_method_sym, :block] } + end + + def save + _run_save_callbacks {} + end +end + +class PersonSkipper < Person + skip_save_callback :before, :before_save_method, :if => :yes + skip_save_callback :after, :before_save_method, :unless => :yes + skip_save_callback :after, :before_save_method, :if => :no + skip_save_callback :before, :before_save_method, :unless => :no + def yes; true; end + def no; false; end +end + +class ParentController + include ActiveSupport::NewCallbacks + + define_callbacks :dispatch + + dispatch_callback :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }} + dispatch_callback :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_dispatch_callback :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} } + skip_dispatch_callback :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 MySuper + include ActiveSupport::NewCallbacks + define_callbacks :save +end + +class AroundPerson < MySuper + attr_reader :history + + save_callback :before, :nope, :if => :no + save_callback :before, :nope, :unless => :yes + save_callback :after, :tweedle + save_callback :before, "tweedle_dee" + save_callback :before, proc {|m| m.history << "yup" } + save_callback :before, :nope, :if => proc { false } + save_callback :before, :nope, :unless => proc { true } + save_callback :before, :yup, :if => proc { true } + save_callback :before, :yup, :unless => proc { false } + save_callback :around, :tweedle_dum + save_callback :around, :w0tyes, :if => :yes + save_callback :around, :w0tno, :if => :no + save_callback :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 AroundCallbacksTest < Test::Unit::TestCase + def test_save_around + around = AroundPerson.new + around.save + assert_equal [ + "tweedle dee", + "yup", "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 CallbackTerminator + include ActiveSupport::NewCallbacks + + define_callbacks :save, "result == :halt" + + save_callback :before, :first + save_callback :before, :second + save_callback :around, :around_it + save_callback :before, :third + save_callback :after, :first + save_callback :around, :around_it + save_callback :after, :second + save_callback :around, :around_it + save_callback :after, :third + + + attr_reader :history + 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 + 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 +end -- cgit v1.2.3 From 5a8b481f717470b952ac7eb890f260ea98428153 Mon Sep 17 00:00:00 2001 From: Michael Curtis Date: Tue, 10 Mar 2009 12:14:54 -0500 Subject: Time.local instances: Adding 24.hours across the DST boundary adds 24 hours instead of one day [#2066 state:resolved] --- activesupport/test/core_ext/duration_test.rb | 12 +++++++++++ activesupport/test/core_ext/time_ext_test.rb | 32 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 16 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index ab5a86668e..8954295d10 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -109,6 +109,18 @@ class DurationTest < ActiveSupport::TestCase ensure Time.zone_default = nil end + + def test_adding_hours_across_dst_boundary + with_env_tz 'CET' do + assert_equal Time.local(2009,3,29,0,0,0) + 24.hours, Time.local(2009,3,30,1,0,0) + end + end + + def test_adding_day_across_dst_boundary + with_env_tz 'CET' do + assert_equal Time.local(2009,3,29,0,0,0) + 1.day, Time.local(2009,3,30,0,0,0) + end + end protected def with_env_tz(new_tz = 'US/Eastern') diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index c0855520e8..1775f81d3d 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -183,26 +183,26 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_backward_start with_env_tz 'US/Eastern' do # dt: US: 2005 April 3rd 4:18am - assert_equal Time.local(2005,4,2,4,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-1.day=>st' - assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-1.day=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(24.hours), 'dt-1.day=>st' + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(24.hours), 'st-1.day=>st' end with_env_tz 'NZ' do # dt: New Zealand: 2006 October 1st 4:18am - assert_equal Time.local(2006,9,30,4,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-1.day=>st' - assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-1.day=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(24.hours), 'dt-1.day=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(24.hours), 'st-1.day=>st' end end def test_daylight_savings_time_crossings_backward_end with_env_tz 'US/Eastern' do # st: US: 2005 October 30th 4:03am - assert_equal Time.local(2005,10,29,4,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-1.day=>dt' - assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-1.day=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(24.hours), 'st-1.day=>dt' + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(24.hours), 'dt-1.day=>dt' end with_env_tz 'NZ' do # st: New Zealand: 2006 March 19th 4:03am - assert_equal Time.local(2006,3,18,4,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-1.day=>dt' - assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-1.day=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(24.hours), 'st-1.day=>dt' + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(24.hours), 'dt-1.day=>dt' end end @@ -243,13 +243,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_forward_start with_env_tz 'US/Eastern' do # st: US: 2005 April 2nd 7:27pm - assert_equal Time.local(2005,4,3,19,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+1.day=>dt' - assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+1.day=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(24.hours), 'st+1.day=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+1.day=>dt' end with_env_tz 'NZ' do # st: New Zealand: 2006 September 30th 7:27pm - assert_equal Time.local(2006,10,1,19,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+1.day=>dt' - assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+1.day=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(24.hours), 'st+1.day=>dt' + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(24.hours), 'dt+1.day=>dt' end end @@ -295,13 +295,13 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_forward_end with_env_tz 'US/Eastern' do # dt: US: 2005 October 30th 12:45am - assert_equal Time.local(2005,10,31,0,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+1.day=>st' - assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+1.day=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(24.hours), 'dt+1.day=>st' + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(24.hours), 'st+1.day=>st' end with_env_tz 'NZ' do # dt: New Zealand: 2006 March 19th 1:45am - assert_equal Time.local(2006,3,20,1,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+1.day=>st' - assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+1.day=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(24.hours), 'dt+1.day=>st' + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(24.hours), 'st+1.day=>st' end end -- cgit v1.2.3 From ea8077c6427d208188f9cd11f88ebdc8f60dec28 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Sun, 29 Mar 2009 16:37:13 -0500 Subject: Enhance Time #since and #ago DST tests. --- activesupport/test/core_ext/time_ext_test.rb | 73 ++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 16 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 1775f81d3d..70cf4a9450 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -183,26 +183,46 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_backward_start with_env_tz 'US/Eastern' do # dt: US: 2005 April 3rd 4:18am - assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(24.hours), 'dt-1.day=>st' - assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(24.hours), 'st-1.day=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(24.hours), 'dt-24.hours=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(86400), 'dt-86400=>st' + assert_equal Time.local(2005,4,2,3,18,0), Time.local(2005,4,3,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st' + + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(24.hours), 'st-24.hours=>st' + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400), 'st-86400=>st' + assert_equal Time.local(2005,4,1,4,18,0), Time.local(2005,4,2,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st' end with_env_tz 'NZ' do # dt: New Zealand: 2006 October 1st 4:18am - assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(24.hours), 'dt-1.day=>st' - assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(24.hours), 'st-1.day=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(24.hours), 'dt-24.hours=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-86400=>st' + + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-86400=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(24.hours), 'st-24.hours=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st' end end def test_daylight_savings_time_crossings_backward_end with_env_tz 'US/Eastern' do # st: US: 2005 October 30th 4:03am - assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(24.hours), 'st-1.day=>dt' - assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(24.hours), 'dt-1.day=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(24.hours), 'st-24.hours=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(86400), 'st-86400=>dt' + assert_equal Time.local(2005,10,29,5,3), Time.local(2005,10,30,4,3,0).ago(86400.seconds), 'st-86400.seconds=>dt' + + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(24.hours), 'dt-24.hours=>dt' + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400), 'dt-86400=>dt' + assert_equal Time.local(2005,10,28,4,3), Time.local(2005,10,29,4,3,0).ago(86400.seconds), 'dt-86400.seconds=>dt' end with_env_tz 'NZ' do # st: New Zealand: 2006 March 19th 4:03am - assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(24.hours), 'st-1.day=>dt' - assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(24.hours), 'dt-1.day=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(24.hours), 'st-24.hours=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(86400), 'st-86400=>dt' + assert_equal Time.local(2006,3,18,5,3), Time.local(2006,3,19,4,3,0).ago(86400.seconds), 'st-86400.seconds=>dt' + + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(24.hours), 'dt-24.hours=>dt' + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400), 'dt-86400=>dt' + assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(86400.seconds), 'dt-86400.seconds=>dt' end end @@ -231,6 +251,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2006,3,17,4,3), Time.local(2006,3,18,4,3,0).ago(1.day), 'dt-1.day=>dt' end end + def test_since assert_equal Time.local(2005,2,22,10,10,11), Time.local(2005,2,22,10,10,10).since(1) assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600) @@ -243,13 +264,23 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_forward_start with_env_tz 'US/Eastern' do # st: US: 2005 April 2nd 7:27pm - assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(24.hours), 'st+1.day=>dt' - assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+1.day=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(24.hours), 'st+24.hours=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+86400=>dt' + assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt' + + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+24.hoursy=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+86400=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt' end with_env_tz 'NZ' do # st: New Zealand: 2006 September 30th 7:27pm - assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(24.hours), 'st+1.day=>dt' - assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(24.hours), 'dt+1.day=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(24.hours), 'st+24.hours=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(86400), 'st+86400=>dt' + assert_equal Time.local(2006,10,1,20,27,0), Time.local(2006,9,30,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt' + + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(24.hours), 'dt+24.hours=>dt' + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400), 'dt+86400=>dt' + assert_equal Time.local(2006,10,2,19,27,0), Time.local(2006,10,1,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt' end end @@ -295,13 +326,23 @@ class TimeExtCalculationsTest < Test::Unit::TestCase def test_daylight_savings_time_crossings_forward_end with_env_tz 'US/Eastern' do # dt: US: 2005 October 30th 12:45am - assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(24.hours), 'dt+1.day=>st' - assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(24.hours), 'st+1.day=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(24.hours), 'dt+24.hours=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(86400), 'dt+86400=>st' + assert_equal Time.local(2005,10,30,23,45,0), Time.local(2005,10,30,0,45,0).since(86400.seconds), 'dt+86400.seconds=>st' + + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(24.hours), 'st+24.hours=>st' + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400), 'st+86400=>st' + assert_equal Time.local(2005,11, 1,0,45,0), Time.local(2005,10,31,0,45,0).since(86400.seconds), 'st+86400.seconds=>st' end with_env_tz 'NZ' do # dt: New Zealand: 2006 March 19th 1:45am - assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(24.hours), 'dt+1.day=>st' - assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(24.hours), 'st+1.day=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(24.hours), 'dt+24.hours=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(86400), 'dt+86400=>st' + assert_equal Time.local(2006,3,20,0,45,0), Time.local(2006,3,19,1,45,0).since(86400.seconds), 'dt+86400.seconds=>st' + + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(24.hours), 'st+24.hours=>st' + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400), 'st+86400=>st' + assert_equal Time.local(2006,3,21,1,45,0), Time.local(2006,3,20,1,45,0).since(86400.seconds), 'st+86400.seconds=>st' end end -- cgit v1.2.3 From c00f2d25de640b8de1c8583474085f3836ee1768 Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Sun, 5 Apr 2009 10:08:54 -0500 Subject: TimeWithZone.name returns 'Time', to further thwart type checking --- activesupport/test/core_ext/time_with_zone_test.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index accfe51ed6..7be19e7900 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -317,6 +317,10 @@ class TimeWithZoneTest < Test::Unit::TestCase assert @twz.kind_of?(Time) assert @twz.is_a?(ActiveSupport::TimeWithZone) end + + def test_class_name + assert_equal 'Time', ActiveSupport::TimeWithZone.name + end def test_method_missing_with_time_return_value assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) -- cgit v1.2.3 From 660fc93942697b74a8093d4d5338d1ced25cbe7f Mon Sep 17 00:00:00 2001 From: Geoff Buesing Date: Sun, 5 Apr 2009 10:19:03 -0500 Subject: Test cleanup --- activesupport/test/core_ext/time_ext_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index 70cf4a9450..8ee4904036 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -194,11 +194,11 @@ class TimeExtCalculationsTest < Test::Unit::TestCase with_env_tz 'NZ' do # dt: New Zealand: 2006 October 1st 4:18am assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(24.hours), 'dt-24.hours=>st' - assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st' assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400), 'dt-86400=>st' + assert_equal Time.local(2006,9,30,3,18,0), Time.local(2006,10,1,4,18,0).ago(86400.seconds), 'dt-86400.seconds=>st' - assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-86400=>st' assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(24.hours), 'st-24.hours=>st' + assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400), 'st-86400=>st' assert_equal Time.local(2006,9,29,4,18,0), Time.local(2006,9,30,4,18,0).ago(86400.seconds), 'st-86400.seconds=>st' end end @@ -268,7 +268,7 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400), 'st+86400=>dt' assert_equal Time.local(2005,4,3,20,27,0), Time.local(2005,4,2,19,27,0).since(86400.seconds), 'st+86400.seconds=>dt' - assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+24.hoursy=>dt' + assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(24.hours), 'dt+24.hours=>dt' assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400), 'dt+86400=>dt' assert_equal Time.local(2005,4,4,19,27,0), Time.local(2005,4,3,19,27,0).since(86400.seconds), 'dt+86400.seconds=>dt' end -- cgit v1.2.3 From 2036d3ba75da1a0f3061bf5a33c89e2b2eaff420 Mon Sep 17 00:00:00 2001 From: Carl Lerche & Yehuda Katz Date: Thu, 9 Apr 2009 12:33:39 -0700 Subject: Revert rspec-runner change. TODO: Add back support for the rspec T::U runner. --- activesupport/test/new_callbacks_test.rb | 600 ++++++++++++++++--------------- 1 file changed, 301 insertions(+), 299 deletions(-) (limited to 'activesupport/test') diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb index 6948ad23dc..5cde078b65 100644 --- a/activesupport/test/new_callbacks_test.rb +++ b/activesupport/test/new_callbacks_test.rb @@ -3,380 +3,382 @@ require 'test/unit' $:.unshift "#{File.dirname(__FILE__)}/../lib" require 'active_support' -class Record - include ActiveSupport::NewCallbacks +module NewCallbacksTest + class Record + include ActiveSupport::NewCallbacks - define_callbacks :save + define_callbacks :save - def self.before_save(*filters, &blk) - save_callback(:before, *filters, &blk) - end + def self.before_save(*filters, &blk) + save_callback(:before, *filters, &blk) + end - def self.after_save(*filters, &blk) - save_callback(:after, *filters, &blk) - end + def self.after_save(*filters, &blk) + save_callback(:after, *filters, &blk) + end - class << self - def callback_symbol(callback_method) - returning(:"#{callback_method}_method") do |method_name| - define_method(method_name) do - history << [callback_method, :symbol] + class << self + def callback_symbol(callback_method) + returning(:"#{callback_method}_method") do |method_name| + define_method(method_name) do + history << [callback_method, :symbol] + end end end - end - def callback_string(callback_method) - "history << [#{callback_method.to_sym.inspect}, :string]" - 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_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, :object] + def callback_object(callback_method) + klass = Class.new + klass.send(:define_method, callback_method) do |model| + model.history << [callback_method, :object] + end + klass.new end - klass.new end - end - def history - @history ||= [] + def history + @history ||= [] + end 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] } - 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] } + end - def save - _run_save_callbacks {} + def save + _run_save_callbacks {} + end end -end -class PersonSkipper < Person - skip_save_callback :before, :before_save_method, :if => :yes - skip_save_callback :after, :before_save_method, :unless => :yes - skip_save_callback :after, :before_save_method, :if => :no - skip_save_callback :before, :before_save_method, :unless => :no - def yes; true; end - def no; false; end -end + class PersonSkipper < Person + skip_save_callback :before, :before_save_method, :if => :yes + skip_save_callback :after, :before_save_method, :unless => :yes + skip_save_callback :after, :before_save_method, :if => :no + skip_save_callback :before, :before_save_method, :unless => :no + def yes; true; end + def no; false; end + end -class ParentController - include ActiveSupport::NewCallbacks + class ParentController + include ActiveSupport::NewCallbacks - define_callbacks :dispatch + define_callbacks :dispatch - dispatch_callback :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }} - dispatch_callback :after, :log2 + dispatch_callback :before, :log, :per_key => {:unless => proc {|c| c.action_name == :index || c.action_name == :show }} + dispatch_callback :after, :log2 - attr_reader :action_name, :logger - def initialize(action_name) - @action_name, @logger = action_name, [] - end + attr_reader :action_name, :logger + def initialize(action_name) + @action_name, @logger = action_name, [] + end - def log - @logger << action_name - end + def log + @logger << action_name + end - def log2 - @logger << action_name - end + def log2 + @logger << action_name + end - def dispatch - _run_dispatch_callbacks(action_name) { - @logger << "Done" - } - self + def dispatch + _run_dispatch_callbacks(action_name) { + @logger << "Done" + } + self + end end -end -class Child < ParentController - skip_dispatch_callback :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} } - skip_dispatch_callback :after, :log2 -end + class Child < ParentController + skip_dispatch_callback :before, :log, :per_key => {:if => proc {|c| c.action_name == :update} } + skip_dispatch_callback :after, :log2 + end -class OneTimeCompile < Record - @@starts_true, @@starts_false = true, false + class OneTimeCompile < Record + @@starts_true, @@starts_false = true, false - def initialize - super - end + 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} + 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 + def starts_true + if @@starts_true + @@starts_true = false + return true + end + @@starts_true end - @@starts_true - end - def starts_false - unless @@starts_false - @@starts_false = true - return false + def starts_false + unless @@starts_false + @@starts_false = true + return false + end + @@starts_false end - @@starts_false - end - def save - _run_save_callbacks(:action) {} + def save + _run_save_callbacks(:action) {} + end 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 + 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' - # 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 + 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 yes; true; end + def other_yes; true; end + def no; false; end + def other_no; false; end - def save - _run_save_callbacks {} + def save + _run_save_callbacks {} + end end -end -class MySuper - include ActiveSupport::NewCallbacks - define_callbacks :save -end + class MySuper + include ActiveSupport::NewCallbacks + define_callbacks :save + end -class AroundPerson < MySuper - attr_reader :history + class AroundPerson < MySuper + attr_reader :history - save_callback :before, :nope, :if => :no - save_callback :before, :nope, :unless => :yes - save_callback :after, :tweedle - save_callback :before, "tweedle_dee" - save_callback :before, proc {|m| m.history << "yup" } - save_callback :before, :nope, :if => proc { false } - save_callback :before, :nope, :unless => proc { true } - save_callback :before, :yup, :if => proc { true } - save_callback :before, :yup, :unless => proc { false } - save_callback :around, :tweedle_dum - save_callback :around, :w0tyes, :if => :yes - save_callback :around, :w0tno, :if => :no - save_callback :around, :tweedle_deedle + save_callback :before, :nope, :if => :no + save_callback :before, :nope, :unless => :yes + save_callback :after, :tweedle + save_callback :before, "tweedle_dee" + save_callback :before, proc {|m| m.history << "yup" } + save_callback :before, :nope, :if => proc { false } + save_callback :before, :nope, :unless => proc { true } + save_callback :before, :yup, :if => proc { true } + save_callback :before, :yup, :unless => proc { false } + save_callback :around, :tweedle_dum + save_callback :around, :w0tyes, :if => :yes + save_callback :around, :w0tno, :if => :no + save_callback :around, :tweedle_deedle - def no; false; end - def yes; true; end + def no; false; end + def yes; true; end - def nope - @history << "boom" - end + def nope + @history << "boom" + end - def yup - @history << "yup" - end + def yup + @history << "yup" + end - def w0tyes - @history << "w0tyes before" - yield - @history << "w0tyes after" - end + def w0tyes + @history << "w0tyes before" + yield + @history << "w0tyes after" + end - def w0tno - @history << "boom" - yield - end + def w0tno + @history << "boom" + yield + end - def tweedle_dee - @history << "tweedle dee" - end + def tweedle_dee + @history << "tweedle dee" + end - def tweedle_dum - @history << "tweedle dum pre" - yield - @history << "tweedle dum post" - end + def tweedle_dum + @history << "tweedle dum pre" + yield + @history << "tweedle dum post" + end - def tweedle - @history << "tweedle" - end + def tweedle + @history << "tweedle" + end - def tweedle_deedle - @history << "tweedle deedle pre" - yield - @history << "tweedle deedle post" - end + def tweedle_deedle + @history << "tweedle deedle pre" + yield + @history << "tweedle deedle post" + end - def initialize - @history = [] - end + def initialize + @history = [] + end - def save - _run_save_callbacks do - @history << "running" + def save + _run_save_callbacks do + @history << "running" + end end end -end -class AroundCallbacksTest < Test::Unit::TestCase - def test_save_around - around = AroundPerson.new - around.save - assert_equal [ - "tweedle dee", - "yup", "yup", "yup", - "tweedle dum pre", - "w0tyes before", - "tweedle deedle pre", - "running", - "tweedle deedle post", - "w0tyes after", - "tweedle dum post", - "tweedle" - ], around.history + 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 -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 + 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 -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 + 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 -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 + 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 -end -class CallbackTerminator - include ActiveSupport::NewCallbacks + class CallbackTerminator + include ActiveSupport::NewCallbacks - define_callbacks :save, "result == :halt" + define_callbacks :save, "result == :halt" - save_callback :before, :first - save_callback :before, :second - save_callback :around, :around_it - save_callback :before, :third - save_callback :after, :first - save_callback :around, :around_it - save_callback :after, :second - save_callback :around, :around_it - save_callback :after, :third + save_callback :before, :first + save_callback :before, :second + save_callback :around, :around_it + save_callback :before, :third + save_callback :after, :first + save_callback :around, :around_it + save_callback :after, :second + save_callback :around, :around_it + save_callback :after, :third - attr_reader :history - def initialize - @history = [] - end + attr_reader :history + def initialize + @history = [] + end - def around_it - @history << "around1" - yield - @history << "around2" - end + def around_it + @history << "around1" + yield + @history << "around2" + end - def first - @history << "first" - end + def first + @history << "first" + end - def second - @history << "second" - :halt - end + def second + @history << "second" + :halt + end - def third - @history << "third" - end + def third + @history << "third" + end - def save - _run_save_callbacks + def save + _run_save_callbacks + end end -end -class CallbackTerminatorTest < Test::Unit::TestCase - def test_termination - terminator = CallbackTerminator.new - terminator.save - assert_equal ["first", "second", "third", "second", "first"], terminator.history + class CallbackTerminatorTest < Test::Unit::TestCase + def test_termination + terminator = CallbackTerminator.new + terminator.save + assert_equal ["first", "second", "third", "second", "first"], terminator.history + end end -end +end \ No newline at end of file -- cgit v1.2.3 From 647b83d50cba655422bcb83815f618b5a92bfc7d Mon Sep 17 00:00:00 2001 From: Carl Lerche & Yehuda Katz Date: Mon, 13 Apr 2009 16:56:04 -0700 Subject: Resurrecting 1.9 compatibility. --- activesupport/test/memoizable_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activesupport/test') diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb index b03178900f..214e243aa5 100644 --- a/activesupport/test/memoizable_test.rb +++ b/activesupport/test/memoizable_test.rb @@ -1,6 +1,6 @@ require 'abstract_unit' -class MemoizableTest < Test::Unit::TestCase +class MemoizableTest < ActiveSupport::TestCase class Person extend ActiveSupport::Memoizable -- cgit v1.2.3 From 60896ca6f4c89260cb9770487f80dec829674b89 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 17 Apr 2009 13:44:59 -0500 Subject: Clearer String#first and #last edge cases. Fix that foo.first(0) == instead of foo. --- activesupport/test/core_ext/string_ext_test.rb | 2 ++ 1 file changed, 2 insertions(+) (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 6c9b7e7236..7d51e81feb 100644 --- a/activesupport/test/core_ext/string_ext_test.rb +++ b/activesupport/test/core_ext/string_ext_test.rb @@ -132,10 +132,12 @@ class StringInflectionsTest < Test::Unit::TestCase assert_equal "h", s.first assert_equal "he", s.first(2) + assert_equal "", s.first(0) assert_equal "o", s.last assert_equal "llo", s.last(3) assert_equal "hello", s.last(10) + assert_equal "", s.last(0) assert_equal 'x', 'x'.first assert_equal 'x', 'x'.first(4) -- cgit v1.2.3