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/core_ext/boolean_ext_test.rb5
-rw-r--r--activesupport/test/core_ext/nil_ext_test.rb5
-rw-r--r--activesupport/test/core_ext/object_ext_test.rb2
-rw-r--r--activesupport/test/core_ext/regexp_ext_test.rb5
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb8
-rw-r--r--activesupport/test/flush_cache_on_private_memoization_test.rb5
-rw-r--r--activesupport/test/isolation_test.rb248
-rw-r--r--activesupport/test/json/decoding_test.rb10
-rw-r--r--activesupport/test/memoizable_test.rb10
-rw-r--r--activesupport/test/multibyte_chars_test.rb2
-rw-r--r--activesupport/test/multibyte_utils_test.rb137
-rw-r--r--activesupport/test/new_callbacks_test.rb68
-rw-r--r--activesupport/test/orchestra_test.rb161
-rw-r--r--activesupport/test/xml_mini/nokogiri_engine_test.rb17
15 files changed, 531 insertions, 166 deletions
diff --git a/activesupport/test/abstract_unit.rb b/activesupport/test/abstract_unit.rb
index 61914231ef..ee6084dfcd 100644
--- a/activesupport/test/abstract_unit.rb
+++ b/activesupport/test/abstract_unit.rb
@@ -1,15 +1,25 @@
ORIG_ARGV = ARGV.dup
-require 'rubygems'
require 'test/unit'
+begin
+ require 'mocha'
+rescue LoadError
+ $stderr.puts 'Loading rubygems'
+ require 'rubygems'
+ require 'mocha'
+end
+
ENV['NO_RELOAD'] = '1'
$:.unshift "#{File.dirname(__FILE__)}/../lib"
require 'active_support'
require 'active_support/test_case'
+# Include shims until we get off 1.8.6
+require 'active_support/ruby/shim'
+
def uses_memcached(test_name)
- require 'memcache'
+ require 'active_support/vendor/memcache'
begin
MemCache.new('localhost').stats
yield
diff --git a/activesupport/test/core_ext/boolean_ext_test.rb b/activesupport/test/core_ext/boolean_ext_test.rb
index 751f703745..9439716efb 100644
--- a/activesupport/test/core_ext/boolean_ext_test.rb
+++ b/activesupport/test/core_ext/boolean_ext_test.rb
@@ -1,3 +1,6 @@
+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
@@ -6,4 +9,4 @@ class BooleanExtAccessTests < Test::Unit::TestCase
def test_to_param_on_false
assert_equal false, false.to_param
end
-end \ No newline at end of file
+end
diff --git a/activesupport/test/core_ext/nil_ext_test.rb b/activesupport/test/core_ext/nil_ext_test.rb
index 945d3af239..1062676d65 100644
--- a/activesupport/test/core_ext/nil_ext_test.rb
+++ b/activesupport/test/core_ext/nil_ext_test.rb
@@ -1,5 +1,8 @@
+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 \ No newline at end of file
+end
diff --git a/activesupport/test/core_ext/object_ext_test.rb b/activesupport/test/core_ext/object_ext_test.rb
index 72e3bffa4c..484eecaab6 100644
--- a/activesupport/test/core_ext/object_ext_test.rb
+++ b/activesupport/test/core_ext/object_ext_test.rb
@@ -1,4 +1,6 @@
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
diff --git a/activesupport/test/core_ext/regexp_ext_test.rb b/activesupport/test/core_ext/regexp_ext_test.rb
index e2d9140bca..cc3f07d5c5 100644
--- a/activesupport/test/core_ext/regexp_ext_test.rb
+++ b/activesupport/test/core_ext/regexp_ext_test.rb
@@ -1,3 +1,6 @@
+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
@@ -23,4 +26,4 @@ class RegexpExtAccessTests < Test::Unit::TestCase
assert_equal "foo", Regexp.unoptionalize("(?:foo)?")
assert_equal "", Regexp.unoptionalize("")
end
-end \ No newline at end of file
+end
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 1005a7e7ad..db9073e298 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -316,8 +316,12 @@ class TestGetTextString < Test::Unit::TestCase
end
def test_no_placeholder
- assert_equal("aaa", "aaa" % {:num => 1})
- assert_equal("bbb", "bbb" % [1])
+ # Causes a "too many arguments for format string" warning
+ # on 1.8.7 and 1.9 but we still want to make sure the behavior works
+ silence_warnings do
+ assert_equal("aaa", "aaa" % {:num => 1})
+ assert_equal("bbb", "bbb" % [1])
+ end
end
def test_sprintf_ruby19_style
diff --git a/activesupport/test/flush_cache_on_private_memoization_test.rb b/activesupport/test/flush_cache_on_private_memoization_test.rb
index ddbd05b0e0..1cd313ec81 100644
--- a/activesupport/test/flush_cache_on_private_memoization_test.rb
+++ b/activesupport/test/flush_cache_on_private_memoization_test.rb
@@ -1,5 +1,4 @@
-require 'rubygems'
-require 'activesupport'
+require 'active_support'
require 'test/unit'
class FlashCacheOnPrivateMemoizationTest < Test::Unit::TestCase
@@ -41,4 +40,4 @@ class FlashCacheOnPrivateMemoizationTest < Test::Unit::TestCase
end
end
-end \ No newline at end of file
+end
diff --git a/activesupport/test/isolation_test.rb b/activesupport/test/isolation_test.rb
index 7aecdb8009..20e11df1dd 100644
--- a/activesupport/test/isolation_test.rb
+++ b/activesupport/test/isolation_test.rb
@@ -1,158 +1,162 @@
require 'abstract_unit'
+require 'rbconfig'
-# Does awesome
-if defined?(MiniTest)
- $stderr.puts "Umm, MiniTest not supported yet, mmkay?"
-elsif ENV['CHILD']
- class ChildIsolationTest < ActiveSupport::TestCase
- include ActiveSupport::Testing::Isolation
+if defined?(MiniTest) || defined?(Test::Unit::TestResultFailureSupport)
+ $stderr.puts "Isolation tests can test test-unit 1 only"
- def self.setup
- File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "a") do |f|
- f.puts "hello"
+else
+ # Does awesome
+ if ENV['CHILD']
+ class ChildIsolationTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::Isolation
+
+ def self.setup
+ File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "a") do |f|
+ f.puts "hello"
+ end
end
- end
- def setup
- @instance = "HELLO"
- end
+ def setup
+ @instance = "HELLO"
+ end
- def teardown
- raise if @boom
- end
+ def teardown
+ raise if @boom
+ end
- test "runs the test" do
- assert true
- end
+ test "runs the test" do
+ assert true
+ end
- test "captures errors" do
- raise
- end
+ test "captures errors" do
+ raise
+ end
- test "captures failures" do
- assert false
- end
+ test "captures failures" do
+ assert false
+ end
- test "first runs in isolation" do
- assert_nil $x
- $x = 1
- end
+ test "first runs in isolation" do
+ assert_nil $x
+ $x = 1
+ end
- test "second runs in isolation" do
- assert_nil $x
- $x = 2
- end
+ test "second runs in isolation" do
+ assert_nil $x
+ $x = 2
+ end
- test "runs with slow tests" do
- sleep 0.3
- assert true
- sleep 0.2
- end
+ test "runs with slow tests" do
+ sleep 0.3
+ assert true
+ sleep 0.2
+ end
- test "runs setup" do
- assert "HELLO", @instance
- end
+ test "runs setup" do
+ assert "HELLO", @instance
+ end
- test "runs teardown" do
- @boom = true
- end
+ test "runs teardown" do
+ @boom = true
+ end
- test "resets requires one" do
- assert !defined?(OmgOmg)
- assert_equal 0, $LOADED_FEATURES.grep(/fixtures\/omgomg/).size
- require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
- end
+ test "resets requires one" do
+ assert !defined?(OmgOmg)
+ assert_equal 0, $LOADED_FEATURES.grep(/fixtures\/omgomg/).size
+ require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
+ end
- test "resets requires two" do
- assert !defined?(OmgOmg)
- assert_equal 0, $LOADED_FEATURES.grep(/fixtures\/omgomg/).size
- require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
+ test "resets requires two" do
+ assert !defined?(OmgOmg)
+ assert_equal 0, $LOADED_FEATURES.grep(/fixtures\/omgomg/).size
+ require File.expand_path(File.join(File.dirname(__FILE__), "fixtures", "omgomg"))
+ end
end
- end
-else
- class ParentIsolationTest < ActiveSupport::TestCase
-
- File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "w") {}
-
- ENV["CHILD"] = "1"
- OUTPUT = `#{Gem.ruby} -I#{File.dirname(__FILE__)} "#{File.expand_path(__FILE__)}" -v`
- ENV.delete("CHILD")
-
- def setup
- # Extract the results
- @results = {}
- OUTPUT[/Started\n\s*(.*)\s*\nFinished/mi, 1].split(/\s*\n\s*/).each do |result|
- result =~ %r'^(\w+)\(\w+\):\s*(\.|E|F)$'
- @results[$1] = { 'E' => :error, '.' => :success, 'F' => :failure }[$2]
+ else
+ class ParentIsolationTest < ActiveSupport::TestCase
+
+ File.open(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"), "w") {}
+
+ ENV["CHILD"] = "1"
+ OUTPUT = `#{RbConfig::CONFIG["bindir"]}/#{RbConfig::CONFIG["ruby_install_name"]} -I#{File.dirname(__FILE__)} "#{File.expand_path(__FILE__)}" -v`
+ ENV.delete("CHILD")
+
+ def setup
+ # Extract the results
+ @results = {}
+ OUTPUT[/Started\n\s*(.*)\s*\nFinished/mi, 1].to_s.split(/\s*\n\s*/).each do |result|
+ result =~ %r'^(\w+)\(\w+\):\s*(\.|E|F)$'
+ @results[$1] = { 'E' => :error, '.' => :success, 'F' => :failure }[$2]
+ end
+
+ # Extract the backtraces
+ @backtraces = {}
+ OUTPUT.scan(/^\s*\d+\).*?\n\n/m).each do |backtrace|
+ # \n 1) Error:\ntest_captures_errors(ChildIsolationTest):
+ backtrace =~ %r'\s*\d+\)\s*(Error|Failure):\n(\w+)'i
+ @backtraces[$2] = { :type => $1, :output => backtrace }
+ end
end
- # Extract the backtraces
- @backtraces = {}
- OUTPUT.scan(/^\s*\d+\).*?\n\n/m).each do |backtrace|
- # \n 1) Error:\ntest_captures_errors(ChildIsolationTest):
- backtrace =~ %r'\s*\d+\)\s*(Error|Failure):\n(\w+)'i
- @backtraces[$2] = { :type => $1, :output => backtrace }
+ def assert_failing(name)
+ assert_equal :failure, @results[name.to_s], "Test #{name} did not fail"
end
- end
- def assert_failing(name)
- assert_equal :failure, @results[name.to_s], "Test #{name} did not fail"
- end
+ def assert_passing(name)
+ assert_equal :success, @results[name.to_s], "Test #{name} did not pass"
+ end
- def assert_passing(name)
- assert_equal :success, @results[name.to_s], "Test #{name} did not pass"
- end
+ def assert_erroring(name)
+ assert_equal :error, @results[name.to_s], "Test #{name} did not error"
+ end
- def assert_erroring(name)
- assert_equal :error, @results[name.to_s], "Test #{name} did not error"
- end
+ test "has all tests" do
+ assert_equal 10, @results.length
+ end
- test "has all tests" do
- assert_equal 10, @results.length
- end
+ test "passing tests are still reported" do
+ assert_passing :test_runs_the_test
+ assert_passing :test_runs_with_slow_tests
+ end
- test "passing tests are still reported" do
- assert_passing :test_runs_the_test
- assert_passing :test_runs_with_slow_tests
- end
+ test "resets global variables" do
+ assert_passing :test_first_runs_in_isolation
+ assert_passing :test_second_runs_in_isolation
+ end
- test "resets global variables" do
- assert_passing :test_first_runs_in_isolation
- assert_passing :test_second_runs_in_isolation
- end
+ test "resets requires" do
+ assert_passing :test_resets_requires_one
+ assert_passing :test_resets_requires_two
+ end
- test "resets requires" do
- assert_passing :test_resets_requires_one
- assert_passing :test_resets_requires_two
- end
+ test "erroring tests are still reported" do
+ assert_erroring :test_captures_errors
+ end
- test "erroring tests are still reported" do
- assert_erroring :test_captures_errors
- end
+ test "runs setup and teardown methods" do
+ assert_passing :test_runs_setup
+ assert_erroring :test_runs_teardown
+ end
- test "runs setup and teardown methods" do
- assert_passing :test_runs_setup
- assert_erroring :test_runs_teardown
- end
+ test "correct tests fail" do
+ assert_failing :test_captures_failures
+ end
- test "correct tests fail" do
- assert_failing :test_captures_failures
- end
+ test "backtrace is printed for errors" do
+ assert_equal 'Error', @backtraces["test_captures_errors"][:type]
+ assert_match %r{isolation_test.rb:\d+:in `test_captures_errors'}, @backtraces["test_captures_errors"][:output]
+ end
- test "backtrace is printed for errors" do
- assert_equal 'Error', @backtraces["test_captures_errors"][:type]
- assert_match %r{isolation_test.rb:\d+:in `test_captures_errors'}, @backtraces["test_captures_errors"][:output]
- end
+ test "backtrace is printed for failures" do
+ assert_equal 'Failure', @backtraces["test_captures_failures"][:type]
+ assert_match %r{isolation_test.rb:\d+:in `test_captures_failures'}, @backtraces["test_captures_failures"][:output]
+ end
- test "backtrace is printed for failures" do
- assert_equal 'Failure', @backtraces["test_captures_failures"][:type]
- assert_match %r{isolation_test.rb:\d+:in `test_captures_failures'}, @backtraces["test_captures_failures"][:output]
- end
+ test "self.setup is run only once" do
+ text = File.read(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"))
+ assert_equal "hello\n", text
+ end
- test "self.setup is run only once" do
- text = File.read(File.join(File.dirname(__FILE__), "fixtures", "isolation_test"))
- assert_equal "hello\n", text
end
-
end
end
diff --git a/activesupport/test/json/decoding_test.rb b/activesupport/test/json/decoding_test.rb
index 05e420ae36..7b5a4d0416 100644
--- a/activesupport/test/json/decoding_test.rb
+++ b/activesupport/test/json/decoding_test.rb
@@ -47,13 +47,7 @@ class TestJSONDecoding < ActiveSupport::TestCase
ActiveSupport::JSON.backend
backends = %w(Yaml)
- begin
- gem 'json', '>= 1.1'
- require 'json'
- backends << "JSONGem"
- rescue Gem::LoadError
- # Skip JSON gem tests
- end
+ backends << "JSONGem" if defined?(::JSON)
backends.each do |backend|
TESTS.each do |json, expected|
@@ -81,7 +75,7 @@ class TestJSONDecoding < ActiveSupport::TestCase
end
def test_failed_json_decoding
- assert_raise(ActiveSupport::JSON::ParseError) { ActiveSupport::JSON.decode(%({: 1})) }
+ assert_raise(ActiveSupport::JSON.parse_error) { ActiveSupport::JSON.decode(%({: 1})) }
end
end
diff --git a/activesupport/test/memoizable_test.rb b/activesupport/test/memoizable_test.rb
index 214e243aa5..3bad683093 100644
--- a/activesupport/test/memoizable_test.rb
+++ b/activesupport/test/memoizable_test.rb
@@ -124,16 +124,6 @@ class MemoizableTest < ActiveSupport::TestCase
assert_equal 1, @person.age_calls
end
- def test_memorized_results_are_immutable
- # This is purely a performance enhancement that we can revisit once the rest of
- # the code is in place. Ideally, we'd be able to do memoization in a freeze-friendly
- # way without amc hacks
- pending do
- assert_equal "Josh", @person.name
- assert_raise(ActiveSupport::FrozenObjectError) { @person.name.gsub!("Josh", "Gosh") }
- end
- end
-
def test_reloadable
counter = @calculator.counter
assert_equal 1, @calculator.counter
diff --git a/activesupport/test/multibyte_chars_test.rb b/activesupport/test/multibyte_chars_test.rb
index ed37a1a0da..680936ded5 100644
--- a/activesupport/test/multibyte_chars_test.rb
+++ b/activesupport/test/multibyte_chars_test.rb
@@ -390,7 +390,7 @@ class MultibyteCharsUTF8BehaviourTest < Test::Unit::TestCase
assert_equal 'ちわ', @chars.slice(2..10)
assert_equal '', @chars.slice(4..10)
assert_equal 'に', @chars.slice(/に/u)
- assert_equal 'にち', @chars.slice(/に\w/u)
+ assert_equal 'にち', @chars.slice(/に./u)
assert_equal nil, @chars.slice(/unknown/u)
assert_equal 'にち', @chars.slice(/(にち)/u, 1)
assert_equal nil, @chars.slice(/(にち)/u, 2)
diff --git a/activesupport/test/multibyte_utils_test.rb b/activesupport/test/multibyte_utils_test.rb
new file mode 100644
index 0000000000..0a2f20d282
--- /dev/null
+++ b/activesupport/test/multibyte_utils_test.rb
@@ -0,0 +1,137 @@
+# encoding: utf-8
+
+require 'abstract_unit'
+require 'multibyte_test_helpers'
+
+class MultibyteUtilsTest < ActiveSupport::TestCase
+ include MultibyteTestHelpers
+
+ test "valid_character returns an expression for the current encoding" do
+ with_encoding('None') do
+ assert_nil ActiveSupport::Multibyte.valid_character
+ end
+ with_encoding('UTF8') do
+ assert_equal ActiveSupport::Multibyte::VALID_CHARACTER['UTF-8'], ActiveSupport::Multibyte.valid_character
+ end
+ with_encoding('SJIS') do
+ assert_equal ActiveSupport::Multibyte::VALID_CHARACTER['Shift_JIS'], ActiveSupport::Multibyte.valid_character
+ end
+ end
+
+ test "verify verifies ASCII strings are properly encoded" do
+ with_encoding('None') do
+ examples.each do |example|
+ assert ActiveSupport::Multibyte.verify(example)
+ end
+ end
+ end
+
+ test "verify verifies UTF-8 strings are properly encoded" do
+ with_encoding('UTF8') do
+ assert ActiveSupport::Multibyte.verify(example('valid UTF-8'))
+ assert !ActiveSupport::Multibyte.verify(example('invalid UTF-8'))
+ end
+ end
+
+ test "verify verifies Shift-JIS strings are properly encoded" do
+ with_encoding('SJIS') do
+ assert ActiveSupport::Multibyte.verify(example('valid Shift-JIS'))
+ assert !ActiveSupport::Multibyte.verify(example('invalid Shift-JIS'))
+ end
+ end
+
+ test "verify! raises an exception when it finds an invalid character" do
+ with_encoding('UTF8') do
+ assert_raises(ActiveSupport::Multibyte::EncodingError) do
+ ActiveSupport::Multibyte.verify!(example('invalid UTF-8'))
+ end
+ end
+ end
+
+ test "verify! doesn't raise an exception when the encoding is valid" do
+ with_encoding('UTF8') do
+ assert_nothing_raised do
+ ActiveSupport::Multibyte.verify!(example('valid UTF-8'))
+ end
+ end
+ end
+
+ if RUBY_VERSION < '1.9'
+ test "clean leaves ASCII strings intact" do
+ with_encoding('None') do
+ [
+ 'word', "\270\236\010\210\245"
+ ].each do |string|
+ assert_equal string, ActiveSupport::Multibyte.clean(string)
+ end
+ end
+ end
+
+ test "clean cleans invalid characters from UTF-8 encoded strings" do
+ with_encoding('UTF8') do
+ cleaned_utf8 = [8].pack('C*')
+ assert_equal example('valid UTF-8'), ActiveSupport::Multibyte.clean(example('valid UTF-8'))
+ assert_equal cleaned_utf8, ActiveSupport::Multibyte.clean(example('invalid UTF-8'))
+ end
+ end
+
+ test "clean cleans invalid characters from Shift-JIS encoded strings" do
+ with_encoding('SJIS') do
+ cleaned_sjis = [184, 0, 136, 165].pack('C*')
+ assert_equal example('valid Shift-JIS'), ActiveSupport::Multibyte.clean(example('valid Shift-JIS'))
+ assert_equal cleaned_sjis, ActiveSupport::Multibyte.clean(example('invalid Shift-JIS'))
+ end
+ end
+ else
+ test "clean is a no-op" do
+ with_encoding('UTF8') do
+ assert_equal example('invalid Shift-JIS'), ActiveSupport::Multibyte.clean(example('invalid Shift-JIS'))
+ end
+ end
+ end
+
+ private
+
+ STRINGS = {
+ 'valid ASCII' => [65, 83, 67, 73, 73].pack('C*'),
+ 'invalid ASCII' => [128].pack('C*'),
+ 'valid UTF-8' => [227, 129, 147, 227, 129, 171, 227, 129, 161, 227, 130, 143].pack('C*'),
+ 'invalid UTF-8' => [184, 158, 8, 136, 165].pack('C*'),
+ 'valid Shift-JIS' => [131, 122, 129, 91, 131, 128].pack('C*'),
+ 'invalid Shift-JIS' => [184, 158, 8, 0, 255, 136, 165].pack('C*')
+ }
+
+ if Kernel.const_defined?(:Encoding)
+ def example(key)
+ STRINGS[key].force_encoding(Encoding.default_external)
+ end
+
+ def examples
+ STRINGS.values.map { |s| s.force_encoding(Encoding.default_external) }
+ end
+ else
+ def example(key)
+ STRINGS[key]
+ end
+
+ def examples
+ STRINGS.values
+ end
+ end
+
+ if 'string'.respond_to?(:encoding)
+ KCODE_TO_ENCODING = Hash.new(Encoding::BINARY).
+ update('UTF8' => Encoding::UTF_8, 'SJIS' => Encoding::Shift_JIS)
+
+ def with_encoding(enc)
+ before = Encoding.default_external
+ silence_warnings { Encoding.default_external = KCODE_TO_ENCODING[enc] }
+
+ yield
+
+ silence_warnings { Encoding.default_external = before }
+ end
+ else
+ alias with_encoding with_kcode
+ end
+end
diff --git a/activesupport/test/new_callbacks_test.rb b/activesupport/test/new_callbacks_test.rb
index 7e092b5f63..04db376fc6 100644
--- a/activesupport/test/new_callbacks_test.rb
+++ b/activesupport/test/new_callbacks_test.rb
@@ -180,6 +180,10 @@ module NewCallbacksTest
end
end
+ class CleanPerson < ConditionalPerson
+ reset_callbacks :save
+ end
+
class MySuper
include ActiveSupport::NewCallbacks
define_callbacks :save
@@ -349,10 +353,18 @@ module NewCallbacksTest
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, "result == :halt"
+ define_callbacks :save, :terminator => "result == :halt"
set_callback :save, :before, :first
set_callback :save, :before, :second
@@ -400,7 +412,11 @@ module NewCallbacksTest
def before(caller)
caller.record << "before"
end
-
+
+ def before_save(caller)
+ caller.record << "before save"
+ end
+
def around(caller)
caller.record << "around before"
yield
@@ -410,15 +426,15 @@ module NewCallbacksTest
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"
@@ -443,19 +459,49 @@ module NewCallbacksTest
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
+ 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
@@ -469,7 +515,7 @@ module NewCallbacksTest
obj = CallbackTerminator.new
obj.save
assert !obj.saved
- end
+ end
end
class HyphenatedKeyTest < Test::Unit::TestCase
@@ -477,6 +523,6 @@ module NewCallbacksTest
obj = HyphenatedCallbacks.new
obj.save
assert_equal obj.stuff, "OMG"
- end
- end
+ end
+ end
end
diff --git a/activesupport/test/orchestra_test.rb b/activesupport/test/orchestra_test.rb
new file mode 100644
index 0000000000..683cc36f6a
--- /dev/null
+++ b/activesupport/test/orchestra_test.rb
@@ -0,0 +1,161 @@
+require 'abstract_unit'
+
+class OrchestraEventTest < Test::Unit::TestCase
+ def setup
+ @parent = ActiveSupport::Orchestra::Event.new(:parent)
+ end
+
+ def test_initialization_with_name_and_parent_and_payload
+ event = ActiveSupport::Orchestra::Event.new(:awesome, @parent, :payload => "orchestra")
+ assert_equal(:awesome, event.name)
+ assert_equal(@parent, event.parent)
+ assert_equal({ :payload => "orchestra" }, event.payload)
+ end
+
+ def test_thread_id_is_set_on_initialization
+ event = ActiveSupport::Orchestra::Event.new(:awesome)
+ assert_equal Thread.current.object_id, event.thread_id
+ end
+
+ def test_current_time_is_set_on_initialization
+ previous_time = Time.now.utc
+ event = ActiveSupport::Orchestra::Event.new(:awesome)
+ assert_kind_of Time, event.time
+ assert event.time.to_f >= previous_time.to_f
+ end
+
+ def test_duration_is_set_when_event_finishes
+ event = ActiveSupport::Orchestra::Event.new(:awesome)
+ sleep(0.1)
+ event.finish!
+ assert_in_delta 100, event.duration, 30
+ end
+end
+
+class OrchestraMainTest < Test::Unit::TestCase
+ def setup
+ @listener = []
+ ActiveSupport::Orchestra.register @listener
+ end
+
+ def teardown
+ ActiveSupport::Orchestra.unregister @listener
+ end
+
+ def test_orchestra_allows_any_action_to_be_instrumented
+ event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
+ sleep(0.1)
+ end
+
+ assert_equal :awesome, event.name
+ assert_equal "orchestra", event.payload
+ assert_in_delta 100, event.duration, 30
+ end
+
+ def test_block_result_is_stored
+ event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
+ 1 + 1
+ end
+
+ assert_equal 2, event.result
+ end
+
+ def test_events_are_published_to_a_listener
+ event = ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
+ 1 + 1
+ end
+
+ assert_equal 1, @listener.size
+ assert_equal :awesome, @listener.last.name
+ assert_equal "orchestra", @listener.last.payload
+ end
+
+ def test_nested_events_can_be_instrumented
+ ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
+ ActiveSupport::Orchestra.instrument(:wot, "child") do
+ sleep(0.1)
+ end
+
+ assert_equal 1, @listener.size
+ assert_equal :wot, @listener.first.name
+ assert_equal "child", @listener.first.payload
+
+ assert_nil @listener.first.parent.duration
+ assert_in_delta 100, @listener.first.duration, 30
+ end
+
+ assert_equal 2, @listener.size
+ assert_equal :awesome, @listener.last.name
+ assert_equal "orchestra", @listener.last.payload
+ assert_in_delta 100, @listener.first.parent.duration, 30
+ end
+
+ def test_event_is_pushed_even_if_block_fails
+ ActiveSupport::Orchestra.instrument(:awesome, "orchestra") do
+ raise "OMG"
+ end rescue RuntimeError
+
+ assert_equal 1, @listener.size
+ assert_equal :awesome, @listener.last.name
+ assert_equal "orchestra", @listener.last.payload
+ end
+end
+
+class OrchestraListenerTest < Test::Unit::TestCase
+ class MyListener < ActiveSupport::Orchestra::Listener
+ attr_reader :consumed
+
+ def consume(event)
+ @consumed ||= []
+ @consumed << event
+ end
+ end
+
+ def setup
+ @listener = MyListener.new
+ ActiveSupport::Orchestra.register @listener
+ end
+
+ def teardown
+ ActiveSupport::Orchestra.unregister @listener
+ end
+
+ def test_thread_is_exposed_by_listener
+ assert_kind_of Thread, @listener.thread
+ end
+
+ def test_event_is_consumed_when_an_action_is_instrumented
+ ActiveSupport::Orchestra.instrument(:sum) do
+ 1 + 1
+ end
+ sleep 0.1
+ assert_equal 1, @listener.consumed.size
+ assert_equal :sum, @listener.consumed.first.name
+ assert_equal 2, @listener.consumed.first.result
+ end
+
+ def test_with_sevaral_consumers_and_several_events
+ @another = MyListener.new
+ ActiveSupport::Orchestra.register @another
+
+ 1.upto(100) do |i|
+ ActiveSupport::Orchestra.instrument(:value) do
+ i
+ end
+ end
+
+ sleep 0.1
+
+ assert_equal 100, @listener.consumed.size
+ assert_equal :value, @listener.consumed.first.name
+ assert_equal 1, @listener.consumed.first.result
+ assert_equal 100, @listener.consumed.last.result
+
+ assert_equal 100, @another.consumed.size
+ assert_equal :value, @another.consumed.first.name
+ assert_equal 1, @another.consumed.first.result
+ assert_equal 100, @another.consumed.last.result
+ ensure
+ ActiveSupport::Orchestra.unregister @another
+ end
+end
diff --git a/activesupport/test/xml_mini/nokogiri_engine_test.rb b/activesupport/test/xml_mini/nokogiri_engine_test.rb
index 7c3a591e63..e16f36acee 100644
--- a/activesupport/test/xml_mini/nokogiri_engine_test.rb
+++ b/activesupport/test/xml_mini/nokogiri_engine_test.rb
@@ -3,13 +3,11 @@ require 'active_support/xml_mini'
require 'active_support/core_ext/hash/conversions'
begin
- gem 'nokogiri', '>= 1.1.1'
-rescue Gem::LoadError
+ require 'nokogiri'
+rescue LoadError
# Skip nokogiri tests
else
-require 'nokogiri'
-
class NokogiriEngineTest < Test::Unit::TestCase
include ActiveSupport
@@ -161,6 +159,17 @@ class NokogiriEngineTest < Test::Unit::TestCase
XmlMini.parse(io)
end
+ def test_children_with_cdata
+ assert_equal_rexml(<<-eoxml)
+ <root>
+ <products>
+ hello <![CDATA[everyone]]>
+ morning
+ </products>
+ </root>
+ eoxml
+ end
+
private
def assert_equal_rexml(xml)
hash = XmlMini.with_backend('REXML') { XmlMini.parse(xml) }