diff options
author | Prem Sichanugrist, Brian Morearty, John Reitano <s@sikachu.com, github2@morearty.org, john_reitano@yahoo.com> | 2011-04-10 20:27:08 +0800 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2011-04-11 03:17:08 +0800 |
commit | 635d991683c439da56fa72853880e88e6ac291ed (patch) | |
tree | cb2d51ebe3bedfb71df5661ba5e86246bf064e0a /activesupport/test | |
parent | 62b2755f7a4ca12491a7899348066695a6a26917 (diff) | |
download | rails-635d991683c439da56fa72853880e88e6ac291ed.tar.gz rails-635d991683c439da56fa72853880e88e6ac291ed.tar.bz2 rails-635d991683c439da56fa72853880e88e6ac291ed.zip |
Add support for Object#in? and Object#either? in Active Support [#6321 state:committed]
This will allow you to check if an object is included in another object
or the list of objects or not.
This patch is derived from patch by Brian Morearty and John Reitano on
Lighthouse ticket. I've rewrite it and make sure that we support both
'another object' and 'list of objects' version, as it surely be useful
to support both.
Diffstat (limited to 'activesupport/test')
-rw-r--r-- | activesupport/test/core_ext/object/inclusion_test.rb | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/activesupport/test/core_ext/object/inclusion_test.rb b/activesupport/test/core_ext/object/inclusion_test.rb new file mode 100644 index 0000000000..b4207c20ea --- /dev/null +++ b/activesupport/test/core_ext/object/inclusion_test.rb @@ -0,0 +1,52 @@ +require 'abstract_unit' +require 'active_support/core_ext/object/inclusion' + +class InTest < Test::Unit::TestCase + def test_in_array + assert 1.in?([1,2]) + assert !3.in?([1,2]) + end + + def test_in_hash + h = { "a" => 100, "b" => 200 } + assert "a".in?(h) + assert !"z".in?(h) + end + + def test_in_string + assert "lo".in?("hello") + assert !"ol".in?("hello") + assert ?h.in?("hello") + end + + def test_in_range + assert 25.in?(1..50) + assert !75.in?(1..50) + end + + def test_in_set + s = Set.new([1,2]) + assert 1.in?(s) + assert !3.in?(s) + end + + def test_either + assert 1.either?(1,2,3) + assert !5.either?(1,2,3) + assert [1,2,3].either?([1,2,3], 2, [3,4,5]) + end + + module A + end + class B + include A + end + class C < B + end + + def test_in_module + assert A.in?(B) + assert A.in?(C) + assert !A.in?(A) + end +end |