PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/object_and_class_ext_test.rb

https://bitbucket.org/nicksieger/rails
Ruby | 275 lines | 225 code | 49 blank | 1 comment | 1 complexity | 6d23ddb686b476b0a0c2faaaeda3aed9 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/core_ext/object'
  3. require 'active_support/core_ext/class/removal'
  4. require 'active_support/core/time'
  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 ClassExtTest < Test::Unit::TestCase
  37. def test_methods
  38. assert defined?(ClassB)
  39. assert defined?(ClassC)
  40. assert defined?(ClassD)
  41. ClassA.remove_subclasses
  42. assert !defined?(ClassB)
  43. assert !defined?(ClassC)
  44. assert !defined?(ClassD)
  45. end
  46. def test_subclasses_of
  47. cj = ClassJ
  48. assert_equal [ClassJ], Object.subclasses_of(ClassI)
  49. ClassI.remove_subclasses
  50. assert_equal [], Object.subclasses_of(ClassI)
  51. ensure
  52. Object.const_set :ClassJ, cj
  53. end
  54. def test_subclasses_of_should_find_nested_classes
  55. assert Object.subclasses_of(ClassK).include?(Nested::ClassL)
  56. end
  57. def test_subclasses_of_should_not_return_removed_classes
  58. # First create the removed class
  59. old_class = Nested.class_eval { remove_const :ClassL }
  60. new_class = Class.new(ClassK)
  61. Nested.const_set :ClassL, new_class
  62. assert_equal "Nested::ClassL", new_class.name # Sanity check
  63. subclasses = Object.subclasses_of(ClassK)
  64. assert subclasses.include?(new_class)
  65. assert ! subclasses.include?(old_class)
  66. ensure
  67. Nested.const_set :ClassL, old_class unless defined?(Nested::ClassL)
  68. end
  69. def test_subclasses_of_should_not_trigger_const_missing
  70. const_missing = false
  71. Nested.on_const_missing { const_missing = true }
  72. subclasses = Object.subclasses_of ClassK
  73. assert !const_missing
  74. assert_equal [ Nested::ClassL ], subclasses
  75. removed = Nested.class_eval { remove_const :ClassL } # keep it in memory
  76. subclasses = Object.subclasses_of ClassK
  77. assert !const_missing
  78. assert subclasses.empty?
  79. ensure
  80. Nested.const_set :ClassL, removed unless defined?(Nested::ClassL)
  81. end
  82. def test_subclasses_of_with_multiple_roots
  83. classes = Object.subclasses_of(ClassI, ClassK)
  84. assert_equal %w(ClassJ Nested::ClassL), classes.collect(&:to_s).sort
  85. end
  86. def test_subclasses_of_doesnt_find_anonymous_classes
  87. assert_equal [], Object.subclasses_of(Foo)
  88. bar = Class.new(Foo)
  89. assert_nothing_raised do
  90. assert_equal [bar], Object.subclasses_of(Foo)
  91. end
  92. end
  93. end
  94. class ObjectTests < Test::Unit::TestCase
  95. def test_extended_by
  96. foo = Foo.new
  97. assert foo.extended_by.include?(Bar)
  98. foo.extend(Baz)
  99. assert(([Bar, Baz] - foo.extended_by).empty?, "Expected Bar, Baz in #{foo.extended_by.inspect}")
  100. end
  101. def test_extend_with_included_modules_from
  102. foo, object = Foo.new, Object.new
  103. assert !object.respond_to?(:bar)
  104. assert !object.respond_to?(:baz)
  105. object.extend_with_included_modules_from(foo)
  106. assert object.respond_to?(:bar)
  107. assert !object.respond_to?(:baz)
  108. foo.extend(Baz)
  109. object.extend_with_included_modules_from(foo)
  110. assert object.respond_to?(:bar)
  111. assert object.respond_to?(:baz)
  112. end
  113. class DuckTime
  114. def acts_like_time?
  115. true
  116. end
  117. end
  118. def test_duck_typing
  119. object = Object.new
  120. time = Time.now
  121. date = Date.today
  122. dt = DateTime.new
  123. duck = DuckTime.new
  124. assert !object.acts_like?(:time)
  125. assert !object.acts_like?(:date)
  126. assert time.acts_like?(:time)
  127. assert !time.acts_like?(:date)
  128. assert !date.acts_like?(:time)
  129. assert date.acts_like?(:date)
  130. assert dt.acts_like?(:time)
  131. assert dt.acts_like?(:date)
  132. assert duck.acts_like?(:time)
  133. assert !duck.acts_like?(:date)
  134. end
  135. def test_metaclass
  136. string = "Hello"
  137. string.metaclass.instance_eval do
  138. define_method(:foo) { "bar" }
  139. end
  140. assert_equal "bar", string.foo
  141. end
  142. end
  143. class ObjectInstanceVariableTest < Test::Unit::TestCase
  144. def setup
  145. @source, @dest = Object.new, Object.new
  146. @source.instance_variable_set(:@bar, 'bar')
  147. @source.instance_variable_set(:@baz, 'baz')
  148. end
  149. def test_instance_variable_names
  150. assert_equal %w(@bar @baz), @source.instance_variable_names.sort
  151. end
  152. def test_instance_variable_defined
  153. assert @source.instance_variable_defined?('@bar')
  154. assert @source.instance_variable_defined?(:@bar)
  155. assert !@source.instance_variable_defined?(:@foo)
  156. assert !@source.instance_variable_defined?('@foo')
  157. end
  158. def test_copy_instance_variables_from_without_explicit_excludes
  159. assert_equal [], @dest.instance_variables
  160. @dest.copy_instance_variables_from(@source)
  161. assert_equal %w(@bar @baz), @dest.instance_variables.sort.map(&:to_s)
  162. %w(@bar @baz).each do |name|
  163. assert_equal @source.instance_variable_get(name).object_id,
  164. @dest.instance_variable_get(name).object_id
  165. end
  166. end
  167. def test_copy_instance_variables_from_with_explicit_excludes
  168. @dest.copy_instance_variables_from(@source, ['@baz'])
  169. assert !@dest.instance_variable_defined?('@baz')
  170. assert_equal 'bar', @dest.instance_variable_get('@bar')
  171. end
  172. def test_copy_instance_variables_automatically_excludes_protected_instance_variables
  173. @source.instance_variable_set(:@quux, 'quux')
  174. class << @source
  175. def protected_instance_variables
  176. ['@bar', :@quux]
  177. end
  178. end
  179. @dest.copy_instance_variables_from(@source)
  180. assert !@dest.instance_variable_defined?('@bar')
  181. assert !@dest.instance_variable_defined?('@quux')
  182. assert_equal 'baz', @dest.instance_variable_get('@baz')
  183. end
  184. def test_instance_values
  185. object = Object.new
  186. object.instance_variable_set :@a, 1
  187. object.instance_variable_set :@b, 2
  188. assert_equal({'a' => 1, 'b' => 2}, object.instance_values)
  189. end
  190. def test_instance_exec_passes_arguments_to_block
  191. assert_equal %w(hello goodbye), 'hello'.instance_exec('goodbye') { |v| [self, v] }
  192. end
  193. def test_instance_exec_with_frozen_obj
  194. assert_equal %w(olleh goodbye), 'hello'.freeze.instance_exec('goodbye') { |v| [reverse, v] }
  195. end
  196. def test_instance_exec_nested
  197. assert_equal %w(goodbye olleh bar), 'hello'.instance_exec('goodbye') { |arg|
  198. [arg] + instance_exec('bar') { |v| [reverse, v] } }
  199. end
  200. end
  201. class ObjectTryTest < Test::Unit::TestCase
  202. def setup
  203. @string = "Hello"
  204. end
  205. def test_nonexisting_method
  206. method = :undefined_method
  207. assert !@string.respond_to?(method)
  208. assert_raise(NoMethodError) { @string.try(method) }
  209. end
  210. def test_valid_method
  211. assert_equal 5, @string.try(:size)
  212. end
  213. def test_argument_forwarding
  214. assert_equal 'Hey', @string.try(:sub, 'llo', 'y')
  215. end
  216. def test_block_forwarding
  217. assert_equal 'Hey', @string.try(:sub, 'llo') { |match| 'y' }
  218. end
  219. def test_nil_to_type
  220. assert_nil nil.try(:to_s)
  221. assert_nil nil.try(:to_i)
  222. end
  223. def test_false_try
  224. assert_equal 'false', false.try(:to_s)
  225. end
  226. end