PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Tests/Libraries/Rails-3.0.0/activesupport/test/core_ext/object_and_class_ext_test.rb

http://github.com/IronLanguages/main
Ruby | 168 lines | 137 code | 31 blank | 0 comment | 1 complexity | 0e747b6226ceafce0bdca5f21e38bbec MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. require 'abstract_unit'
  2. require 'active_support/time'
  3. require 'active_support/core_ext/object'
  4. require 'active_support/core_ext/class/subclasses'
  5. class ClassA; end
  6. class ClassB < ClassA; end
  7. class ClassC < ClassB; end
  8. class ClassD < ClassA; end
  9. class ClassI; end
  10. class ClassJ < ClassI; end
  11. class ClassK
  12. end
  13. module Nested
  14. class << self
  15. def on_const_missing(&callback)
  16. @on_const_missing = callback
  17. end
  18. private
  19. def const_missing(mod_id)
  20. @on_const_missing[mod_id] if @on_const_missing
  21. super
  22. end
  23. end
  24. class ClassL < ClassK
  25. end
  26. end
  27. module Bar
  28. def bar; end
  29. end
  30. module Baz
  31. def baz; end
  32. end
  33. class Foo
  34. include Bar
  35. end
  36. class ObjectTests < ActiveSupport::TestCase
  37. class DuckTime
  38. def acts_like_time?
  39. true
  40. end
  41. end
  42. def test_duck_typing
  43. object = Object.new
  44. time = Time.now
  45. date = Date.today
  46. dt = DateTime.new
  47. duck = DuckTime.new
  48. assert !object.acts_like?(:time)
  49. assert !object.acts_like?(:date)
  50. assert time.acts_like?(:time)
  51. assert !time.acts_like?(:date)
  52. assert !date.acts_like?(:time)
  53. assert date.acts_like?(:date)
  54. assert dt.acts_like?(:time)
  55. assert dt.acts_like?(:date)
  56. assert duck.acts_like?(:time)
  57. assert !duck.acts_like?(:date)
  58. end
  59. end
  60. class ObjectInstanceVariableTest < Test::Unit::TestCase
  61. def setup
  62. @source, @dest = Object.new, Object.new
  63. @source.instance_variable_set(:@bar, 'bar')
  64. @source.instance_variable_set(:@baz, 'baz')
  65. end
  66. def test_instance_variable_names
  67. assert_equal %w(@bar @baz), @source.instance_variable_names.sort
  68. end
  69. def test_copy_instance_variables_from_without_explicit_excludes
  70. assert_equal [], @dest.instance_variables
  71. @dest.copy_instance_variables_from(@source)
  72. assert_equal %w(@bar @baz), @dest.instance_variables.sort.map(&:to_s)
  73. %w(@bar @baz).each do |name|
  74. assert_equal @source.instance_variable_get(name).object_id,
  75. @dest.instance_variable_get(name).object_id
  76. end
  77. end
  78. def test_copy_instance_variables_from_with_explicit_excludes
  79. @dest.copy_instance_variables_from(@source, ['@baz'])
  80. assert !@dest.instance_variable_defined?('@baz')
  81. assert_equal 'bar', @dest.instance_variable_get('@bar')
  82. end
  83. def test_copy_instance_variables_automatically_excludes_protected_instance_variables
  84. @source.instance_variable_set(:@quux, 'quux')
  85. class << @source
  86. def protected_instance_variables
  87. ['@bar', :@quux]
  88. end
  89. end
  90. @dest.copy_instance_variables_from(@source)
  91. assert !@dest.instance_variable_defined?('@bar')
  92. assert !@dest.instance_variable_defined?('@quux')
  93. assert_equal 'baz', @dest.instance_variable_get('@baz')
  94. end
  95. def test_instance_values
  96. object = Object.new
  97. object.instance_variable_set :@a, 1
  98. object.instance_variable_set :@b, 2
  99. assert_equal({'a' => 1, 'b' => 2}, object.instance_values)
  100. end
  101. def test_instance_exec_passes_arguments_to_block
  102. assert_equal %w(hello goodbye), 'hello'.instance_exec('goodbye') { |v| [self, v] }
  103. end
  104. def test_instance_exec_with_frozen_obj
  105. assert_equal %w(olleh goodbye), 'hello'.freeze.instance_exec('goodbye') { |v| [reverse, v] }
  106. end
  107. def test_instance_exec_nested
  108. assert_equal %w(goodbye olleh bar), 'hello'.instance_exec('goodbye') { |arg|
  109. [arg] + instance_exec('bar') { |v| [reverse, v] } }
  110. end
  111. end
  112. class ObjectTryTest < Test::Unit::TestCase
  113. def setup
  114. @string = "Hello"
  115. end
  116. def test_nonexisting_method
  117. method = :undefined_method
  118. assert !@string.respond_to?(method)
  119. assert_raise(NoMethodError) { @string.try(method) }
  120. end
  121. def test_valid_method
  122. assert_equal 5, @string.try(:size)
  123. end
  124. def test_argument_forwarding
  125. assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
  126. end
  127. def test_block_forwarding
  128. assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
  129. end
  130. def test_nil_to_type
  131. assert_nil nil.try(:to_s)
  132. assert_nil nil.try(:to_i)
  133. end
  134. def test_false_try
  135. assert_equal 'false', false.try(:to_s)
  136. end
  137. end