aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test/core_ext
diff options
context:
space:
mode:
authorHongli Lai (Phusion) <hongli@phusion.nl>2008-09-04 23:01:40 +0200
committerHongli Lai (Phusion) <hongli@phusion.nl>2008-09-04 23:01:40 +0200
commitc480c1db1f302ab28a255c5423326e51d27ec5ed (patch)
treeac2afe4deb5ea436d53e421c14650bc627480f45 /activesupport/test/core_ext
parent08704c442d15b16511214731dd94108b737ef407 (diff)
parentd7bd01f543d18e37f9c353d847bda3456bc337c3 (diff)
downloadrails-c480c1db1f302ab28a255c5423326e51d27ec5ed.tar.gz
rails-c480c1db1f302ab28a255c5423326e51d27ec5ed.tar.bz2
rails-c480c1db1f302ab28a255c5423326e51d27ec5ed.zip
Merge branch 'master' of git@github.com:lifo/docrails
Diffstat (limited to 'activesupport/test/core_ext')
-rw-r--r--activesupport/test/core_ext/date_ext_test.rb16
-rw-r--r--activesupport/test/core_ext/duplicable_test.rb2
-rw-r--r--activesupport/test/core_ext/enumerable_test.rb5
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb29
-rw-r--r--activesupport/test/core_ext/module/synchronization_test.rb85
-rw-r--r--activesupport/test/core_ext/object_and_class_ext_test.rb5
-rw-r--r--activesupport/test/core_ext/string_ext_test.rb4
-rw-r--r--activesupport/test/core_ext/time_ext_test.rb34
8 files changed, 166 insertions, 14 deletions
diff --git a/activesupport/test/core_ext/date_ext_test.rb b/activesupport/test/core_ext/date_ext_test.rb
index ddfe1f904f..0f3cf4c75c 100644
--- a/activesupport/test/core_ext/date_ext_test.rb
+++ b/activesupport/test/core_ext/date_ext_test.rb
@@ -198,10 +198,6 @@ class DateExtCalculationsTest < Test::Unit::TestCase
assert_equal Time.local(2005,2,21,23,59,59), Date.new(2005,2,21).end_of_day
end
- def test_date_acts_like_date
- assert Date.new.acts_like_date?
- end
-
def test_xmlschema
with_env_tz 'US/Eastern' do
assert_match(/^1980-02-28T00:00:00-05:?00$/, Date.new(1980, 2, 28).xmlschema)
@@ -245,3 +241,15 @@ class DateExtCalculationsTest < Test::Unit::TestCase
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
+
+class DateExtBehaviorTest < Test::Unit::TestCase
+ def test_date_acts_like_date
+ assert Date.new.acts_like_date?
+ end
+
+ def test_freeze_doesnt_clobber_memoized_instance_methods
+ assert_nothing_raised do
+ Date.today.freeze.inspect
+ end
+ end
+end
diff --git a/activesupport/test/core_ext/duplicable_test.rb b/activesupport/test/core_ext/duplicable_test.rb
index 3ccfedccd7..8b6127f31e 100644
--- a/activesupport/test/core_ext/duplicable_test.rb
+++ b/activesupport/test/core_ext/duplicable_test.rb
@@ -1,7 +1,7 @@
require 'abstract_unit'
class DuplicableTest < Test::Unit::TestCase
- NO = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56')]
+ NO = [nil, false, true, :symbol, 1, 2.3, BigDecimal.new('4.56'), Class.new]
YES = ['1', Object.new, /foo/, [], {}, Time.now]
def test_duplicable
diff --git a/activesupport/test/core_ext/enumerable_test.rb b/activesupport/test/core_ext/enumerable_test.rb
index 2315d8f3db..deb9b7544d 100644
--- a/activesupport/test/core_ext/enumerable_test.rb
+++ b/activesupport/test/core_ext/enumerable_test.rb
@@ -58,6 +58,11 @@ class EnumerableTests < Test::Unit::TestCase
assert_equal Payment.new(0), [].sum(Payment.new(0))
end
+ def test_each_with_object
+ result = %w(foo bar).each_with_object({}) { |str, hsh| hsh[str] = str.upcase }
+ assert_equal({'foo' => 'FOO', 'bar' => 'BAR'}, result)
+ end
+
def test_index_by
payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ]
assert_equal({ 5 => payments[0], 15 => payments[1], 10 => payments[2] },
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index fc8ed45358..7a414e946f 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -62,7 +62,7 @@ class HashExtTest < Test::Unit::TestCase
@symbols = @symbols.with_indifferent_access
@mixed = @mixed.with_indifferent_access
- assert_equal 'a', @strings.send!(:convert_key, :a)
+ assert_equal 'a', @strings.__send__(:convert_key, :a)
assert_equal 1, @strings.fetch('a')
assert_equal 1, @strings.fetch(:a.to_s)
@@ -75,9 +75,9 @@ class HashExtTest < Test::Unit::TestCase
hashes.each do |name, hash|
method_map.sort_by { |m| m.to_s }.each do |meth, expected|
- assert_equal(expected, hash.send!(meth, 'a'),
+ assert_equal(expected, hash.__send__(meth, 'a'),
"Calling #{name}.#{meth} 'a'")
- assert_equal(expected, hash.send!(meth, :a),
+ assert_equal(expected, hash.__send__(meth, :a),
"Calling #{name}.#{meth} :a")
end
end
@@ -733,7 +733,7 @@ class HashToXmlTest < Test::Unit::TestCase
def test_empty_string_works_for_typecast_xml_value
assert_nothing_raised do
- Hash.send!(:typecast_xml_value, "")
+ Hash.__send__(:typecast_xml_value, "")
end
end
@@ -839,6 +839,27 @@ class QueryTest < Test::Unit::TestCase
:person => {:id => [20, 10]}
end
+ def test_expansion_count_is_limited
+ assert_raises RuntimeError do
+ attack_xml = <<-EOT
+ <?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>
+ EOT
+ Hash.from_xml(attack_xml)
+ end
+ end
+
private
def assert_query_equal(expected, actual, message = nil)
assert_equal expected.split('&'), actual.to_query.split('&')
diff --git a/activesupport/test/core_ext/module/synchronization_test.rb b/activesupport/test/core_ext/module/synchronization_test.rb
new file mode 100644
index 0000000000..b1d4bc5e06
--- /dev/null
+++ b/activesupport/test/core_ext/module/synchronization_test.rb
@@ -0,0 +1,85 @@
+require 'abstract_unit'
+
+class SynchronizationTest < Test::Unit::TestCase
+ def setup
+ @target = Class.new
+ @target.cattr_accessor :mutex, :instance_writer => false
+ @target.mutex = Mutex.new
+ @instance = @target.new
+ end
+
+ def test_synchronize_aliases_method_chain_with_synchronize
+ @target.module_eval do
+ attr_accessor :value
+ synchronize :value, :with => :mutex
+ end
+ assert @instance.respond_to?(:value_with_synchronization)
+ assert @instance.respond_to?(:value_without_synchronization)
+ end
+
+ def test_synchronize_does_not_change_behavior
+ @target.module_eval do
+ attr_accessor :value
+ synchronize :value, :with => :mutex
+ end
+ expected = "some state"
+ @instance.value = expected
+ assert_equal expected, @instance.value
+ end
+
+ def test_synchronize_with_no_mutex_raises_an_argument_error
+ assert_raises(ArgumentError) do
+ @target.synchronize :to_s
+ end
+ end
+
+ def test_double_synchronize_raises_an_argument_error
+ @target.synchronize :to_s, :with => :mutex
+ assert_raises(ArgumentError) do
+ @target.synchronize :to_s, :with => :mutex
+ end
+ end
+
+ def dummy_sync
+ dummy = Object.new
+ def dummy.synchronize
+ @sync_count ||= 0
+ @sync_count += 1
+ yield
+ end
+ def dummy.sync_count; @sync_count; end
+ dummy
+ end
+
+ def test_mutex_is_entered_during_method_call
+ @target.mutex = dummy_sync
+ @target.synchronize :to_s, :with => :mutex
+ @instance.to_s
+ @instance.to_s
+ assert_equal 2, @target.mutex.sync_count
+ end
+
+ def test_can_synchronize_method_with_punctuation
+ @target.module_eval do
+ def dangerous?
+ @dangerous
+ end
+ def dangerous!
+ @dangerous = true
+ end
+ end
+ @target.synchronize :dangerous?, :dangerous!, :with => :mutex
+ @instance.dangerous!
+ assert @instance.dangerous?
+ end
+
+ def test_can_synchronize_singleton_methods
+ @target.mutex = dummy_sync
+ class << @target
+ synchronize :to_s, :with => :mutex
+ end
+ assert @target.respond_to?(:to_s_without_synchronization)
+ assert_nothing_raised { @target.to_s; @target.to_s }
+ assert_equal 2, @target.mutex.sync_count
+ end
+end \ No newline at end of file
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 b0a746fdc7..e88dcb52d5 100644
--- a/activesupport/test/core_ext/object_and_class_ext_test.rb
+++ b/activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -108,11 +108,6 @@ class ClassExtTest < Test::Unit::TestCase
end
class ObjectTests < Test::Unit::TestCase
- def test_send_bang_aliases_send_before_19
- assert_respond_to 'a', :send!
- assert_equal 1, 'a'.send!(:size)
- end
-
def test_suppress_re_raises
assert_raises(LoadError) { suppress(ArgumentError) {raise LoadError} }
end
diff --git a/activesupport/test/core_ext/string_ext_test.rb b/activesupport/test/core_ext/string_ext_test.rb
index 19a30f1730..c9f959ef32 100644
--- a/activesupport/test/core_ext/string_ext_test.rb
+++ b/activesupport/test/core_ext/string_ext_test.rb
@@ -32,6 +32,10 @@ class StringInflectionsTest < Test::Unit::TestCase
end
end
+ def test_camelize_lower
+ assert_equal('capital', 'Capital'.camelize(:lower))
+ end
+
def test_underscore
CamelToUnderscore.each do |camel, underscore|
assert_equal(underscore, camel.underscore)
diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb
index 8740497b3d..4749950f25 100644
--- a/activesupport/test/core_ext/time_ext_test.rb
+++ b/activesupport/test/core_ext/time_ext_test.rb
@@ -625,3 +625,37 @@ class TimeExtCalculationsTest < Test::Unit::TestCase
old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ')
end
end
+
+class TimeExtMarshalingTest < Test::Unit::TestCase
+ def test_marshaling_with_utc_instance
+ t = Time.utc(2000)
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+
+ def test_marshaling_with_local_instance
+ t = Time.local(2000)
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+
+ def test_marshaling_with_frozen_utc_instance
+ t = Time.utc(2000).freeze
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+
+ def test_marshaling_with_frozen_local_instance
+ t = Time.local(2000).freeze
+ marshaled = Marshal.dump t
+ unmarshaled = Marshal.load marshaled
+ assert_equal t, unmarshaled
+ assert_equal t.zone, unmarshaled.zone
+ end
+end