aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/test
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport/test')
-rw-r--r--activesupport/test/caching_test.rb93
-rw-r--r--activesupport/test/core_ext/array_ext_test.rb1
-rw-r--r--activesupport/test/core_ext/hash_ext_test.rb1
-rw-r--r--activesupport/test/key_generator_test.rb32
-rw-r--r--activesupport/test/ordered_hash_test.rb1
-rw-r--r--activesupport/test/spec_type_test.rb23
-rw-r--r--activesupport/test/tagged_logging_test.rb25
-rw-r--r--activesupport/test/test_test.rb19
-rw-r--r--activesupport/test/testing/constant_lookup_test.rb59
9 files changed, 210 insertions, 44 deletions
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 71cd9d81b3..febf0eeeff 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -224,25 +224,22 @@ module CacheStoreBehavior
end
def test_read_multi_with_expires
- @cache.write('foo', 'bar', :expires_in => 0.001)
+ time = Time.now
+ @cache.write('foo', 'bar', :expires_in => 10)
@cache.write('fu', 'baz')
@cache.write('fud', 'biz')
- sleep(0.002)
+ Time.stubs(:now).returns(time + 11)
assert_equal({"fu" => "baz"}, @cache.read_multi('foo', 'fu'))
end
def test_read_and_write_compressed_small_data
@cache.write('foo', 'bar', :compress => true)
- raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
assert_equal 'bar', @cache.read('foo')
- assert_equal 'bar', Marshal.load(raw_value)
end
def test_read_and_write_compressed_large_data
@cache.write('foo', 'bar', :compress => true, :compress_threshold => 2)
- raw_value = @cache.send(:read_entry, 'foo', {}).raw_value
assert_equal 'bar', @cache.read('foo')
- assert_equal 'bar', Marshal.load(Zlib::Inflate.inflate(raw_value))
end
def test_read_and_write_compressed_nil
@@ -301,14 +298,6 @@ module CacheStoreBehavior
assert !@cache.exist?('foo')
end
- def test_read_should_return_a_different_object_id_each_time_it_is_called
- @cache.write('foo', 'bar')
- assert_not_equal @cache.read('foo').object_id, @cache.read('foo').object_id
- value = @cache.read('foo')
- value << 'bingo'
- assert_not_equal value, @cache.read('foo')
- end
-
def test_original_store_objects_should_not_be_immutable
bar = 'bar'
@cache.write('foo', bar)
@@ -363,7 +352,7 @@ module CacheStoreBehavior
rescue ArgumentError
end
assert_equal "bar", @cache.read('foo')
- Time.stubs(:now).returns(time + 71)
+ Time.stubs(:now).returns(time + 91)
assert_nil @cache.read('foo')
end
@@ -627,7 +616,7 @@ end
class MemoryStoreTest < ActiveSupport::TestCase
def setup
- @record_size = Marshal.dump("aaaaaaaaaa").bytesize
+ @record_size = ActiveSupport::Cache::Entry.new("aaaaaaaaaa").size
@cache = ActiveSupport::Cache.lookup_store(:memory_store, :expires_in => 60, :size => @record_size * 10)
end
@@ -646,9 +635,9 @@ class MemoryStoreTest < ActiveSupport::TestCase
@cache.prune(@record_size * 3)
assert @cache.exist?(5)
assert @cache.exist?(4)
- assert !@cache.exist?(3)
+ assert !@cache.exist?(3), "no entry"
assert @cache.exist?(2)
- assert !@cache.exist?(1)
+ assert !@cache.exist?(1), "no entry"
end
def test_prune_size_on_write
@@ -670,12 +659,12 @@ class MemoryStoreTest < ActiveSupport::TestCase
assert @cache.exist?(9)
assert @cache.exist?(8)
assert @cache.exist?(7)
- assert !@cache.exist?(6)
- assert !@cache.exist?(5)
+ assert !@cache.exist?(6), "no entry"
+ assert !@cache.exist?(5), "no entry"
assert @cache.exist?(4)
- assert !@cache.exist?(3)
+ assert !@cache.exist?(3), "no entry"
assert @cache.exist?(2)
- assert !@cache.exist?(1)
+ assert !@cache.exist?(1), "no entry"
end
def test_pruning_is_capped_at_a_max_time
@@ -764,6 +753,14 @@ class MemCacheStoreTest < ActiveSupport::TestCase
assert_equal [], cache.read("foo")
end
end
+
+ def test_read_should_return_a_different_object_id_each_time_it_is_called
+ @cache.write('foo', 'bar')
+ assert_not_equal @cache.read('foo').object_id, @cache.read('foo').object_id
+ value = @cache.read('foo')
+ value << 'bingo'
+ assert_not_equal value, @cache.read('foo')
+ end
end
class NullStoreTest < ActiveSupport::TestCase
@@ -844,15 +841,6 @@ class CacheStoreLoggerTest < ActiveSupport::TestCase
end
class CacheEntryTest < ActiveSupport::TestCase
- def test_create_raw_entry
- time = Time.now
- entry = ActiveSupport::Cache::Entry.create("raw", time, :compress => false, :expires_in => 300)
- assert_equal "raw", entry.raw_value
- assert_equal time.to_f, entry.created_at
- assert !entry.compressed?
- assert_equal 300, entry.expires_in
- end
-
def test_expired
entry = ActiveSupport::Cache::Entry.new("value")
assert !entry.expired?, 'entry not expired'
@@ -864,16 +852,43 @@ class CacheEntryTest < ActiveSupport::TestCase
end
def test_compress_values
- entry = ActiveSupport::Cache::Entry.new("value", :compress => true, :compress_threshold => 1)
- assert_equal "value", entry.value
- assert entry.compressed?
- assert_equal "value", Marshal.load(Zlib::Inflate.inflate(entry.raw_value))
+ value = "value" * 100
+ entry = ActiveSupport::Cache::Entry.new(value, :compress => true, :compress_threshold => 1)
+ assert_equal value, entry.value
+ assert(value.bytesize > entry.size, "value is compressed")
end
def test_non_compress_values
- entry = ActiveSupport::Cache::Entry.new("value")
- assert_equal "value", entry.value
- assert_equal "value", Marshal.load(entry.raw_value)
- assert !entry.compressed?
+ value = "value" * 100
+ entry = ActiveSupport::Cache::Entry.new(value)
+ assert_equal value, entry.value
+ assert_equal value.bytesize, entry.size
+ end
+
+ def test_restoring_version_3_entries
+ version_3_entry = ActiveSupport::Cache::Entry.allocate
+ version_3_entry.instance_variable_set(:@value, "hello")
+ version_3_entry.instance_variable_set(:@created_at, Time.now - 60)
+ entry = Marshal.load(Marshal.dump(version_3_entry))
+ assert_equal "hello", entry.value
+ assert_equal false, entry.expired?
+ end
+
+ def test_restoring_compressed_version_3_entries
+ version_3_entry = ActiveSupport::Cache::Entry.allocate
+ version_3_entry.instance_variable_set(:@value, Zlib::Deflate.deflate(Marshal.dump("hello")))
+ version_3_entry.instance_variable_set(:@compressed, true)
+ entry = Marshal.load(Marshal.dump(version_3_entry))
+ assert_equal "hello", entry.value
+ end
+
+ def test_restoring_expired_version_3_entries
+ version_3_entry = ActiveSupport::Cache::Entry.allocate
+ version_3_entry.instance_variable_set(:@value, "hello")
+ version_3_entry.instance_variable_set(:@created_at, Time.now - 60)
+ version_3_entry.instance_variable_set(:@expires_in, 58.9)
+ entry = Marshal.load(Marshal.dump(version_3_entry))
+ assert_equal "hello", entry.value
+ assert_equal true, entry.expired?
end
end
diff --git a/activesupport/test/core_ext/array_ext_test.rb b/activesupport/test/core_ext/array_ext_test.rb
index 9dfa2cbf11..0b33a63460 100644
--- a/activesupport/test/core_ext/array_ext_test.rb
+++ b/activesupport/test/core_ext/array_ext_test.rb
@@ -1,7 +1,6 @@
require 'abstract_unit'
require 'active_support/core_ext/array'
require 'active_support/core_ext/big_decimal'
-require 'active_support/core_ext/object/conversions'
require 'active_support/core_ext' # FIXME: pulling in all to_xml extensions
require 'active_support/hash_with_indifferent_access'
diff --git a/activesupport/test/core_ext/hash_ext_test.rb b/activesupport/test/core_ext/hash_ext_test.rb
index 01934dd2c3..53ea2aad16 100644
--- a/activesupport/test/core_ext/hash_ext_test.rb
+++ b/activesupport/test/core_ext/hash_ext_test.rb
@@ -3,7 +3,6 @@ require 'active_support/core_ext/hash'
require 'bigdecimal'
require 'active_support/core_ext/string/access'
require 'active_support/ordered_hash'
-require 'active_support/core_ext/object/conversions'
require 'active_support/core_ext/object/deep_dup'
require 'active_support/inflections'
diff --git a/activesupport/test/key_generator_test.rb b/activesupport/test/key_generator_test.rb
new file mode 100644
index 0000000000..525082d313
--- /dev/null
+++ b/activesupport/test/key_generator_test.rb
@@ -0,0 +1,32 @@
+require 'abstract_unit'
+
+begin
+ require 'openssl'
+ OpenSSL::PKCS5
+rescue LoadError, NameError
+ $stderr.puts "Skipping KeyGenerator test: broken OpenSSL install"
+else
+
+require 'active_support/time'
+require 'active_support/json'
+
+class KeyGeneratorTest < ActiveSupport::TestCase
+ def setup
+ @secret = SecureRandom.hex(64)
+ @generator = ActiveSupport::KeyGenerator.new(@secret, :iterations=>2)
+ end
+
+ test "Generating a key of the default length" do
+ derived_key = @generator.generate_key("some_salt")
+ assert_kind_of String, derived_key
+ assert_equal OpenSSL::Digest::SHA1.new.block_length, derived_key.length, "Should have generated a key of the default size"
+ end
+
+ test "Generating a key of an alternative length" do
+ derived_key = @generator.generate_key("some_salt", 32)
+ assert_kind_of String, derived_key
+ assert_equal 32, derived_key.length, "Should have generated a key of the right size"
+ end
+end
+
+end
diff --git a/activesupport/test/ordered_hash_test.rb b/activesupport/test/ordered_hash_test.rb
index ac85ba1f21..14ba4e0076 100644
--- a/activesupport/test/ordered_hash_test.rb
+++ b/activesupport/test/ordered_hash_test.rb
@@ -216,7 +216,6 @@ class OrderedHashTest < ActiveSupport::TestCase
alternate = ActiveSupport::OrderedHash[ [
[1, 2],
[3, 4],
- "bad key value pair",
[ 'missing value' ]
]]
diff --git a/activesupport/test/spec_type_test.rb b/activesupport/test/spec_type_test.rb
new file mode 100644
index 0000000000..95a982d8fd
--- /dev/null
+++ b/activesupport/test/spec_type_test.rb
@@ -0,0 +1,23 @@
+require "abstract_unit"
+require "active_record"
+
+class SomeRandomModel < ActiveRecord::Base; end
+
+class SpecTypeTest < ActiveSupport::TestCase
+
+ def assert_support actual
+ assert_equal ActiveSupport::TestCase, actual
+ end
+
+ def assert_spec actual
+ assert_equal MiniTest::Spec, actual
+ end
+
+ def test_spec_type_resolves_for_actitive_record_constants
+ assert_support MiniTest::Spec.spec_type(SomeRandomModel)
+ end
+
+ def test_spec_type_doesnt_resolve_random_strings
+ assert_spec MiniTest::Spec.spec_type("Unmatched String")
+ end
+end
diff --git a/activesupport/test/tagged_logging_test.rb b/activesupport/test/tagged_logging_test.rb
index 43cf1a8e4f..27f629474e 100644
--- a/activesupport/test/tagged_logging_test.rb
+++ b/activesupport/test/tagged_logging_test.rb
@@ -14,6 +14,14 @@ class TaggedLoggingTest < ActiveSupport::TestCase
@logger = ActiveSupport::TaggedLogging.new(MyLogger.new(@output))
end
+ test 'sets logger.formatter if missing and extends it with a tagging API' do
+ logger = Logger.new(StringIO.new)
+ assert_nil logger.formatter
+ ActiveSupport::TaggedLogging.new(logger)
+ assert_not_nil logger.formatter
+ assert logger.formatter.respond_to?(:tagged)
+ end
+
test "tagged once" do
@logger.tagged("BCX") { @logger.info "Funky time" }
assert_equal "[BCX] Funky time\n", @output.string
@@ -29,6 +37,23 @@ class TaggedLoggingTest < ActiveSupport::TestCase
assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
end
+ test "tagged are flattened" do
+ @logger.tagged("BCX", %w(Jason New)) { @logger.info "Funky time" }
+ assert_equal "[BCX] [Jason] [New] Funky time\n", @output.string
+ end
+
+ test "push and pop tags directly" do
+ assert_equal %w(A B C), @logger.push_tags('A', ['B', ' ', ['C']])
+ @logger.info 'a'
+ assert_equal %w(C), @logger.pop_tags
+ @logger.info 'b'
+ assert_equal %w(B), @logger.pop_tags(1)
+ @logger.info 'c'
+ assert_equal [], @logger.clear_tags!
+ @logger.info 'd'
+ assert_equal "[A] [B] [C] a\n[A] [B] b\n[A] c\nd\n", @output.string
+ end
+
test "does not strip message content" do
@logger.info " Hello"
assert_equal " Hello\n", @output.string
diff --git a/activesupport/test/test_test.rb b/activesupport/test/test_test.rb
index 2473cec384..9516556844 100644
--- a/activesupport/test/test_test.rb
+++ b/activesupport/test/test_test.rb
@@ -100,11 +100,11 @@ class AssertPresentTest < ActiveSupport::TestCase
BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", [], {} ]
NOT_BLANK = [ EmptyFalse.new, Object.new, true, 0, 1, 'x', [nil], { nil => 0 } ]
- def test_assert_blank_true
+ def test_assert_present_true
NOT_BLANK.each { |v| assert_present v }
end
- def test_assert_blank_false
+ def test_assert_present_false
BLANK.each { |v|
begin
assert_present v
@@ -171,3 +171,18 @@ class SubclassSetupAndTeardownTest < SetupAndTeardownTest
assert_equal [:foo, :bar, :bar, :foo], @called_back
end
end
+
+
+class TestCaseTaggedLoggingTest < ActiveSupport::TestCase
+ def before_setup
+ require 'stringio'
+ @out = StringIO.new
+ self.tagged_logger = ActiveSupport::TaggedLogging.new(Logger.new(@out))
+ super
+ end
+
+ def test_logs_tagged_with_current_test_case
+ tagged_logger.info 'test'
+ assert_equal "[#{self.class.name}] [#{__name__}] test\n", @out.string
+ end
+end
diff --git a/activesupport/test/testing/constant_lookup_test.rb b/activesupport/test/testing/constant_lookup_test.rb
new file mode 100644
index 0000000000..c56c032cde
--- /dev/null
+++ b/activesupport/test/testing/constant_lookup_test.rb
@@ -0,0 +1,59 @@
+require 'abstract_unit'
+
+class Foo; end
+class Bar < Foo;
+ def index; end
+ def self.index; end
+end
+class Baz < Bar; end
+module FooBar; end
+
+class ConstantLookupTest < ActiveSupport::TestCase
+ include ActiveSupport::Testing::ConstantLookup
+
+ def find_foo(name)
+ self.class.determine_constant_from_test_name(name) do |constant|
+ Class === constant && constant < Foo
+ end
+ end
+
+ def find_module(name)
+ self.class.determine_constant_from_test_name(name) do |constant|
+ Module === constant
+ end
+ end
+
+ def test_find_bar_from_foo
+ assert_equal Bar, find_foo("Bar")
+ assert_equal Bar, find_foo("Bar::index")
+ assert_equal Bar, find_foo("Bar::index::authenticated")
+ assert_equal Bar, find_foo("BarTest")
+ assert_equal Bar, find_foo("BarTest::index")
+ assert_equal Bar, find_foo("BarTest::index::authenticated")
+ end
+
+ def test_find_module
+ assert_equal FooBar, find_module("FooBar")
+ assert_equal FooBar, find_module("FooBar::index")
+ assert_equal FooBar, find_module("FooBar::index::authenticated")
+ assert_equal FooBar, find_module("FooBarTest")
+ assert_equal FooBar, find_module("FooBarTest::index")
+ assert_equal FooBar, find_module("FooBarTest::index::authenticated")
+ end
+
+ def test_returns_nil_when_cant_find_foo
+ assert_nil find_foo("DoesntExist")
+ assert_nil find_foo("DoesntExistTest")
+ assert_nil find_foo("DoesntExist::Nadda")
+ assert_nil find_foo("DoesntExist::Nadda::Nope")
+ assert_nil find_foo("DoesntExist::Nadda::Nope::NotHere")
+ end
+
+ def test_returns_nil_when_cant_find_module
+ assert_nil find_module("DoesntExist")
+ assert_nil find_module("DoesntExistTest")
+ assert_nil find_module("DoesntExist::Nadda")
+ assert_nil find_module("DoesntExist::Nadda::Nope")
+ assert_nil find_module("DoesntExist::Nadda::Nope::NotHere")
+ end
+end