Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Akshay Vishnoi
committed
Jul 28, 2014
1 parent
55cedf2
commit a45d1f6
Showing
3 changed files
with
65 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'abstract_unit' | ||
require 'active_support/core_ext/object' | ||
|
||
class ObjectTests < ActiveSupport::TestCase | ||
class DuckTime | ||
def acts_like_time? | ||
true | ||
end | ||
end | ||
|
||
def test_duck_typing | ||
object = Object.new | ||
time = Time.now | ||
date = Date.today | ||
dt = DateTime.new | ||
duck = DuckTime.new | ||
|
||
assert !object.acts_like?(:time) | ||
assert !object.acts_like?(:date) | ||
|
||
assert time.acts_like?(:time) | ||
assert !time.acts_like?(:date) | ||
|
||
assert !date.acts_like?(:time) | ||
assert date.acts_like?(:date) | ||
|
||
assert dt.acts_like?(:time) | ||
assert dt.acts_like?(:date) | ||
|
||
assert duck.acts_like?(:time) | ||
assert !duck.acts_like?(:date) | ||
end | ||
end |
31 changes: 31 additions & 0 deletions
31
activesupport/test/core_ext/object/instance_variables_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
require 'abstract_unit' | ||
require 'active_support/core_ext/object' | ||
|
||
class ObjectInstanceVariableTest < ActiveSupport::TestCase | ||
def setup | ||
@source, @dest = Object.new, Object.new | ||
@source.instance_variable_set(:@bar, 'bar') | ||
@source.instance_variable_set(:@baz, 'baz') | ||
end | ||
|
||
def test_instance_variable_names | ||
assert_equal %w(@bar @baz), @source.instance_variable_names.sort | ||
end | ||
|
||
def test_instance_values | ||
assert_equal({'bar' => 'bar', 'baz' => 'baz'}, @source.instance_values) | ||
end | ||
|
||
def test_instance_exec_passes_arguments_to_block | ||
assert_equal %w(hello goodbye), 'hello'.instance_exec('goodbye') { |v| [self, v] } | ||
end | ||
|
||
def test_instance_exec_with_frozen_obj | ||
assert_equal %w(olleh goodbye), 'hello'.freeze.instance_exec('goodbye') { |v| [reverse, v] } | ||
end | ||
|
||
def test_instance_exec_nested | ||
assert_equal %w(goodbye olleh bar), 'hello'.instance_exec('goodbye') { |arg| | ||
[arg] + instance_exec('bar') { |v| [reverse, v] } } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters