aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/abstract_unit.rb14
-rw-r--r--activesupport/test/benchmarkable_test.rb86
-rw-r--r--activesupport/test/caching_test.rb10
-rw-r--r--activesupport/test/callback_inheritance_test.rb (renamed from activesupport/test/new_callback_inheritance_test.rb)42
-rw-r--r--activesupport/test/callbacks_test.rb632
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb20
-rw-r--r--activesupport/test/core_ext/boolean_ext_test.rb12
-rw-r--r--activesupport/test/core_ext/class/delegating_attributes_test.rb7
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb2
-rw-r--r--activesupport/test/core_ext/date_time_ext_test.rb2
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb12
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb41
-rw-r--r--activesupport/test/core_ext/integer_ext_test.rb16
-rw-r--r--activesupport/test/core_ext/name_error_test.rb26
-rw-r--r--activesupport/test/core_ext/nil_ext_test.rb8
-rw-r--r--activesupport/test/core_ext/object/to_param_test.rb19
-rw-r--r--activesupport/test/core_ext/object/to_query_test.rb43
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb7
-rw-r--r--activesupport/test/core_ext/object_ext_test.rb16
-rw-r--r--activesupport/test/core_ext/range_ext_test.rb2
-rw-r--r--activesupport/test/core_ext/regexp_ext_test.rb19
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb29
-rw-r--r--activesupport/test/core_ext/symbol_test.rb9
-rw-r--r--activesupport/test/core_ext/uri_ext_test.rb8
-rw-r--r--activesupport/test/dependencies_test.rb1
-rw-r--r--activesupport/test/i18n_test.rb4
-rw-r--r--activesupport/test/json/decoding_test.rb1
-rw-r--r--activesupport/test/message_encryptor_test.rb1
-rw-r--r--activesupport/test/message_verifier_test.rb2
-rw-r--r--activesupport/test/multibyte_chars_test.rb88
-rw-r--r--activesupport/test/multibyte_utils_test.rb1
-rw-r--r--activesupport/test/new_callbacks_test.rb528
-rw-r--r--activesupport/test/notifications_test.rb206
-rw-r--r--activesupport/test/orchestra_test.rb161
-rw-r--r--activesupport/test/ordered_hash_test.rb7
-rw-r--r--activesupport/test/test_test.rb4
-rw-r--r--activesupport/test/ts_isolated.rb4
-rw-r--r--activesupport/test/whiny_nil_test.rb14
-rw-r--r--activesupport/test/xml_mini/libxml_engine_test.rb194
39 files changed, 1229 insertions, 1069 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index af9656615c..dda139372e 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -1,17 +1,17 @@
ORIG_ARGV = ARGV.dup
-require 'test/unit'
-
begin
- require 'mocha'
+ require File.expand_path('../../../vendor/gems/environment', __FILE__)
rescue LoadError
- $stderr.puts 'Loading rubygems'
- require 'rubygems'
- require 'mocha'
end
+lib = File.expand_path("#{File.dirname(__FILE__)}/../lib")
+$:.unshift(lib) unless $:.include?('lib') || $:.include?(lib)
+
+require 'test/unit'
+require 'mocha'
+
ENV['NO_RELOAD'] = '1'
-$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'
require 'active_support/test_case'
diff --git a/activesupport/test/benchmarkable_test.rb b/activesupport/test/benchmarkable_test.rb
new file mode 100644
index 0000000000..766956f50f
--- /dev/null
+++ b/activesupport/test/benchmarkable_test.rb
@@ -0,0 +1,86 @@
+require 'abstract_unit'
+
+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
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 892aa97ad7..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
@@ -154,13 +156,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
diff --git a/activesupport/test/new_callback_inheritance_test.rb b/activesupport/test/callback_inheritance_test.rb
index fe316ae3da..18721eab19 100644
--- a/activesupport/test/new_callback_inheritance_test.rb
+++ b/activesupport/test/callback_inheritance_test.rb
@@ -3,35 +3,35 @@ $:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'
class GrandParent
- include ActiveSupport::NewCallbacks
-
+ 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" }}
-
+ 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
+
+ def after1
@log << "after1"
end
-
+
def after2
@log << "after2"
end
-
+
def dispatch
- _run_dispatch_callbacks(action_name) do
+ run_callbacks(:dispatch, action_name) do
@log << action_name
end
self
@@ -45,11 +45,11 @@ 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
@@ -64,15 +64,15 @@ class BasicCallbacksTest < Test::Unit::TestCase
@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
@@ -85,15 +85,15 @@ class InheritedCallbacksTest < Test::Unit::TestCase
@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
@@ -104,11 +104,11 @@ class InheritedCallbacksTest2 < Test::Unit::TestCase
@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
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/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
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/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
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/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 4170de3dce..66f5f9fbde 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)
@@ -90,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
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/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) }
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/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_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)
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 484eecaab6..0000000000
--- a/activesupport/test/core_ext/object_ext_test.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-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
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/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
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 584a41b631..56ed296dac 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -4,7 +4,8 @@ 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
include InflectorTestCases
@@ -185,17 +186,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
@@ -214,17 +207,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
@@ -350,13 +332,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"
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/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
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
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
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!")
diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb
index 680936ded5..0e489c10e1 100644
--- a/activesupport/test/multibyte_chars_test.rb
+++ b/activesupport/test/multibyte_chars_test.rb
@@ -100,9 +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*')}"
- @whitespace.force_encoding(Encoding::UTF_8) if @whitespace.respond_to?(:force_encoding)
+ 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
@@ -163,6 +168,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)
@@ -190,7 +196,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
@@ -220,10 +228,10 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
assert !@chars.include?('a')
end
- def test_include_raises_type_error_when_nil_is_passed
- assert_raise(TypeError) do
- @chars.include?(nil)
- end
+ def test_include_raises_when_nil_is_passed
+ @chars.include?(nil)
+ flunk "Expected chars.include?(nil) to raise TypeError or NoMethodError"
+ rescue Exception => e
end
def test_index_should_return_character_offset
@@ -378,6 +386,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)
@@ -476,6 +495,43 @@ class MultibyteCharsExtrasTest < Test::Unit::TestCase
end
end
+ def test_limit_should_not_break_on_blank_strings
+ example = chars('')
+ assert_equal example, example.limit(0)
+ assert_equal example, example.limit(1)
+ end
+
+ def test_limit_should_work_on_a_multibyte_string
+ example = chars(UNICODE_STRING)
+ bytesize = UNICODE_STRING.respond_to?(:bytesize) ? UNICODE_STRING.bytesize : UNICODE_STRING.size
+
+ 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 = chars(ASCII_STRING)
+ 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
+ example = chars(UNICODE_STRING)
+ (1..UNICODE_STRING.length).each do |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*')
@@ -586,3 +642,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
+ example = chars("Puisque c'était son erreur, il m'a aidé")
+ [
+ [0, 0],
+ [3, 3],
+ [12, 11],
+ [14, 13],
+ [41, 39]
+ ].each do |byte_offset, character_offset|
+ assert_equal character_offset, example.send(:translate_offset, byte_offset),
+ "Expected byte offset #{byte_offset} to translate to #{character_offset}"
+ end
+ end
+end
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
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
diff --git a/activesupport/test/notifications_test.rb b/activesupport/test/notifications_test.rb
new file mode 100644
index 0000000000..01106e83e9
--- /dev/null
+++ b/activesupport/test/notifications_test.rb
@@ -0,0 +1,206 @@
+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_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
+ 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.0, event.duration
+ end
+
+ def test_event_is_parent_based_on_time_frame
+ 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)
+ assert !parent.parent_of?(not_child)
+ assert !not_child.parent_of?(parent)
+ end
+
+protected
+
+ 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
+ def setup
+ @events = []
+ Thread.abort_on_exception = true
+ ActiveSupport::Notifications.subscribe do |*args|
+ @events << ActiveSupport::Notifications::Event.new(*args)
+ end
+ 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
+
+ drain
+
+ 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
+
+ drain
+
+ assert_equal 1, @events.size
+ assert_equal :wot, @events.first.name
+ assert_equal Hash[:payload => "child"], @events.first.payload
+ end
+
+ drain
+
+ assert_equal 2, @events.size
+ assert_equal :awesome, @events.last.name
+ assert_equal Hash[:payload => "notifications"], @events.last.payload
+ end
+
+ def test_event_is_pushed_even_if_block_fails
+ ActiveSupport::Notifications.instrument(:awesome, :payload => "notifications") do
+ raise "OMG"
+ end rescue RuntimeError
+
+ drain
+
+ 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")
+ drain
+
+ assert_equal 1, @events.size
+ assert_equal :awesome, @events.last.name
+ 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 }
+
+ drain
+
+ assert_equal 3, @another.size
+ 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 = []
+
+ ActiveSupport::Notifications.subscribe("cache") do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+
+ drain
+
+ 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/) do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ ActiveSupport::Notifications.instrument(:something){ 0 }
+ ActiveSupport::Notifications.instrument(:cache){ 1 }
+
+ drain
+
+ 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 do |*args|
+ @another << ActiveSupport::Notifications::Event.new(*args)
+ end
+
+ 1.upto(100) do |i|
+ ActiveSupport::Notifications.instrument(:value){ i }
+ end
+
+ drain
+
+ 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
+
+ private
+ def drain
+ sleep(0.1) until ActiveSupport::Notifications.queue.drained?
+ 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
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
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 40d3d612e7..5cbffb81fc 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
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
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
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 = %{<?xml version="1.0" encoding="UTF-8"?>
+ <!DOCTYPE member [
+ <!ENTITY a "&b;&b;&b;&b;&b;&b;&b;&b;&b;&b;">
+ <!ENTITY b "&c;&c;&c;&c;&c;&c;&c;&c;&c;&c;">
+ <!ENTITY c "&d;&d;&d;&d;&d;&d;&d;&d;&d;&d;">
+ <!ENTITY d "&e;&e;&e;&e;&e;&e;&e;&e;&e;&e;">
+ <!ENTITY e "&f;&f;&f;&f;&f;&f;&f;&f;&f;&f;">
+ <!ENTITY f "&g;&g;&g;&g;&g;&g;&g;&g;&g;&g;">
+ <!ENTITY g "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx">
+ ]>
+ <member>
+ &a;
+ </member>
+ }
+ 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)
+ <blog>
+ <posts type="array">
+ <post>a post</post>
+ <post>another post</post>
+ </posts>
+ </blog>
+ eoxml
+ end
+
+ def test_one_node_document_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products/>
+ eoxml
+ end
+
+ def test_one_node_with_attributes_document_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products foo="bar"/>
+ eoxml
+ end
+
+ def test_products_node_with_book_node_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products>
+ <book name="awesome" id="12345" />
+ </products>
+ eoxml
+ end
+
+ def test_products_node_with_two_book_nodes_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products>
+ <book name="awesome" id="12345" />
+ <book name="america" id="67890" />
+ </products>
+ eoxml
+ end
+
+ def test_single_node_with_content_as_hash
+ assert_equal_rexml(<<-eoxml)
+ <products>
+ hello world
+ </products>
+ eoxml
+ end
+
+ def test_children_with_children
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ <book name="america" id="67890" />
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_text
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ hello everyone
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_non_adjacent_text
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ good
+ <products>
+ hello everyone
+ </products>
+ morning
+ </root>
+ eoxml
+ end
+
+ def test_parse_from_io
+ io = StringIO.new(<<-eoxml)
+ <root>
+ good
+ <products>
+ hello everyone
+ </products>
+ morning
+ </root>
+ eoxml
+ XmlMini.parse(io)
+ end
+
+ def test_children_with_simple_cdata
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ <![CDATA[cdatablock]]>
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_multiple_cdata
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ <![CDATA[cdatablock1]]><![CDATA[cdatablock2]]>
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_text_and_cdata
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ hello <![CDATA[cdatablock]]>
+ morning
+ </products>
+ </root>
+ eoxml
+ end
+
+ def test_children_with_blank_text
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products> </products>
+ </root>
+ 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