PageRenderTime 75ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/activesupport/test/core_ext/module_test.rb

https://bitbucket.org/druly/rails_cherry_pick
Ruby | 401 lines | 356 code | 44 blank | 1 comment | 0 complexity | ccc66c27508b6a13d8b63b916dac56f5 MD5 | raw file
  1. require 'abstract_unit'
  2. require 'active_support/core_ext/module'
  3. module One
  4. Constant1 = "Hello World"
  5. Constant2 = "What's up?"
  6. end
  7. class Ab
  8. include One
  9. Constant1 = "Hello World" # Will have different object id than One::Constant1
  10. Constant3 = "Goodbye World"
  11. end
  12. module Xy
  13. class Bc
  14. include One
  15. end
  16. end
  17. module Yz
  18. module Zy
  19. class Cd
  20. include One
  21. end
  22. end
  23. end
  24. Somewhere = Struct.new(:street, :city) do
  25. attr_accessor :name
  26. end
  27. class Someone < Struct.new(:name, :place)
  28. delegate :street, :city, :to_f, :to => :place
  29. delegate :name=, :to => :place, :prefix => true
  30. delegate :upcase, :to => "place.city"
  31. FAILED_DELEGATE_LINE = __LINE__ + 1
  32. delegate :foo, :to => :place
  33. FAILED_DELEGATE_LINE_2 = __LINE__ + 1
  34. delegate :bar, :to => :place, :allow_nil => true
  35. end
  36. Invoice = Struct.new(:client) do
  37. delegate :street, :city, :name, :to => :client, :prefix => true
  38. delegate :street, :city, :name, :to => :client, :prefix => :customer
  39. end
  40. Project = Struct.new(:description, :person) do
  41. delegate :name, :to => :person, :allow_nil => true
  42. delegate :to_f, :to => :description, :allow_nil => true
  43. end
  44. Developer = Struct.new(:client) do
  45. delegate :name, :to => :client, :prefix => nil
  46. end
  47. Tester = Struct.new(:client) do
  48. delegate :name, :to => :client, :prefix => false
  49. end
  50. class Name
  51. delegate :upcase, :to => :@full_name
  52. def initialize(first, last)
  53. @full_name = "#{first} #{last}"
  54. end
  55. end
  56. class ModuleTest < Test::Unit::TestCase
  57. def setup
  58. @david = Someone.new("David", Somewhere.new("Paulina", "Chicago"))
  59. end
  60. def test_delegation_to_methods
  61. assert_equal "Paulina", @david.street
  62. assert_equal "Chicago", @david.city
  63. end
  64. def test_delegation_to_assignment_method
  65. @david.place_name = "Fred"
  66. assert_equal "Fred", @david.place.name
  67. end
  68. def test_delegation_down_hierarchy
  69. assert_equal "CHICAGO", @david.upcase
  70. end
  71. def test_delegation_to_instance_variable
  72. david = Name.new("David", "Hansson")
  73. assert_equal "DAVID HANSSON", david.upcase
  74. end
  75. def test_missing_delegation_target
  76. assert_raise(ArgumentError) do
  77. Name.send :delegate, :nowhere
  78. end
  79. assert_raise(ArgumentError) do
  80. Name.send :delegate, :noplace, :tos => :hollywood
  81. end
  82. end
  83. def test_delegation_prefix
  84. invoice = Invoice.new(@david)
  85. assert_equal invoice.client_name, "David"
  86. assert_equal invoice.client_street, "Paulina"
  87. assert_equal invoice.client_city, "Chicago"
  88. end
  89. def test_delegation_custom_prefix
  90. invoice = Invoice.new(@david)
  91. assert_equal invoice.customer_name, "David"
  92. assert_equal invoice.customer_street, "Paulina"
  93. assert_equal invoice.customer_city, "Chicago"
  94. end
  95. def test_delegation_prefix_with_nil_or_false
  96. assert_equal Developer.new(@david).name, "David"
  97. assert_equal Tester.new(@david).name, "David"
  98. end
  99. def test_delegation_prefix_with_instance_variable
  100. assert_raise ArgumentError do
  101. Class.new do
  102. def initialize(client)
  103. @client = client
  104. end
  105. delegate :name, :address, :to => :@client, :prefix => true
  106. end
  107. end
  108. end
  109. def test_delegation_with_allow_nil
  110. rails = Project.new("Rails", Someone.new("David"))
  111. assert_equal rails.name, "David"
  112. end
  113. def test_delegation_with_allow_nil_and_nil_value
  114. rails = Project.new("Rails")
  115. assert_nil rails.name
  116. end
  117. def test_delegation_with_allow_nil_and_nil_value_and_prefix
  118. Project.class_eval do
  119. delegate :name, :to => :person, :allow_nil => true, :prefix => true
  120. end
  121. rails = Project.new("Rails")
  122. assert_nil rails.person_name
  123. end
  124. def test_delegation_without_allow_nil_and_nil_value
  125. david = Someone.new("David")
  126. assert_raise(RuntimeError) { david.street }
  127. end
  128. def test_delegation_to_method_that_exists_on_nil
  129. nil_person = Someone.new(nil)
  130. assert_equal 0.0, nil_person.to_f
  131. end
  132. def test_delegation_to_method_that_exists_on_nil_when_allowing_nil
  133. nil_project = Project.new(nil)
  134. assert_equal 0.0, nil_project.to_f
  135. end
  136. def test_delegation_does_not_raise_error_when_removing_singleton_instance_methods
  137. parent = Class.new do
  138. def self.parent_method; end
  139. end
  140. assert_nothing_raised do
  141. Class.new(parent) do
  142. class << self
  143. delegate :parent_method, :to => :superclass
  144. end
  145. end
  146. end
  147. end
  148. def test_delegation_exception_backtrace
  149. someone = Someone.new("foo", "bar")
  150. someone.foo
  151. rescue NoMethodError => e
  152. file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE}"
  153. # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace.
  154. assert e.backtrace.any?{|a| a.include?(file_and_line)},
  155. "[#{e.backtrace.inspect}] did not include [#{file_and_line}]"
  156. end
  157. def test_delegation_exception_backtrace_with_allow_nil
  158. someone = Someone.new("foo", "bar")
  159. someone.bar
  160. rescue NoMethodError => e
  161. file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE_2}"
  162. # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace.
  163. assert e.backtrace.any?{|a| a.include?(file_and_line)},
  164. "[#{e.backtrace.inspect}] did not include [#{file_and_line}]"
  165. end
  166. def test_parent
  167. assert_equal Yz::Zy, Yz::Zy::Cd.parent
  168. assert_equal Yz, Yz::Zy.parent
  169. assert_equal Object, Yz.parent
  170. end
  171. def test_parents
  172. assert_equal [Yz::Zy, Yz, Object], Yz::Zy::Cd.parents
  173. assert_equal [Yz, Object], Yz::Zy.parents
  174. end
  175. def test_local_constants
  176. assert_equal %w(Constant1 Constant3), Ab.local_constants.sort.map(&:to_s)
  177. end
  178. end
  179. module BarMethodAliaser
  180. def self.included(foo_class)
  181. foo_class.class_eval do
  182. include BarMethods
  183. alias_method_chain :bar, :baz
  184. end
  185. end
  186. end
  187. module BarMethods
  188. def bar_with_baz
  189. bar_without_baz << '_with_baz'
  190. end
  191. def quux_with_baz!
  192. quux_without_baz! << '_with_baz'
  193. end
  194. def quux_with_baz?
  195. false
  196. end
  197. def quux_with_baz=(v)
  198. send(:quux_without_baz=, v) << '_with_baz'
  199. end
  200. def duck_with_orange
  201. duck_without_orange << '_with_orange'
  202. end
  203. end
  204. class MethodAliasingTest < Test::Unit::TestCase
  205. def setup
  206. Object.const_set :FooClassWithBarMethod, Class.new { def bar() 'bar' end }
  207. @instance = FooClassWithBarMethod.new
  208. end
  209. def teardown
  210. Object.instance_eval { remove_const :FooClassWithBarMethod }
  211. end
  212. def test_alias_method_chain
  213. assert @instance.respond_to?(:bar)
  214. feature_aliases = [:bar_with_baz, :bar_without_baz]
  215. feature_aliases.each do |method|
  216. assert !@instance.respond_to?(method)
  217. end
  218. assert_equal 'bar', @instance.bar
  219. FooClassWithBarMethod.class_eval { include BarMethodAliaser }
  220. feature_aliases.each do |method|
  221. assert_respond_to @instance, method
  222. end
  223. assert_equal 'bar_with_baz', @instance.bar
  224. assert_equal 'bar', @instance.bar_without_baz
  225. end
  226. def test_alias_method_chain_with_punctuation_method
  227. FooClassWithBarMethod.class_eval do
  228. def quux!; 'quux' end
  229. end
  230. assert !@instance.respond_to?(:quux_with_baz!)
  231. FooClassWithBarMethod.class_eval do
  232. include BarMethodAliaser
  233. alias_method_chain :quux!, :baz
  234. end
  235. assert_respond_to @instance, :quux_with_baz!
  236. assert_equal 'quux_with_baz', @instance.quux!
  237. assert_equal 'quux', @instance.quux_without_baz!
  238. end
  239. def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods
  240. FooClassWithBarMethod.class_eval do
  241. def quux!; 'quux!' end
  242. def quux?; true end
  243. def quux=(v); 'quux=' end
  244. end
  245. assert !@instance.respond_to?(:quux_with_baz!)
  246. assert !@instance.respond_to?(:quux_with_baz?)
  247. assert !@instance.respond_to?(:quux_with_baz=)
  248. FooClassWithBarMethod.class_eval { include BarMethodAliaser }
  249. assert_respond_to @instance, :quux_with_baz!
  250. assert_respond_to @instance, :quux_with_baz?
  251. assert_respond_to @instance, :quux_with_baz=
  252. FooClassWithBarMethod.alias_method_chain :quux!, :baz
  253. assert_equal 'quux!_with_baz', @instance.quux!
  254. assert_equal 'quux!', @instance.quux_without_baz!
  255. FooClassWithBarMethod.alias_method_chain :quux?, :baz
  256. assert_equal false, @instance.quux?
  257. assert_equal true, @instance.quux_without_baz?
  258. FooClassWithBarMethod.alias_method_chain :quux=, :baz
  259. assert_equal 'quux=_with_baz', @instance.send(:quux=, 1234)
  260. assert_equal 'quux=', @instance.send(:quux_without_baz=, 1234)
  261. end
  262. def test_alias_method_chain_with_feature_punctuation
  263. FooClassWithBarMethod.class_eval do
  264. def quux; 'quux' end
  265. def quux?; 'quux?' end
  266. include BarMethodAliaser
  267. alias_method_chain :quux, :baz!
  268. end
  269. assert_nothing_raised do
  270. assert_equal 'quux_with_baz', @instance.quux_with_baz!
  271. end
  272. assert_raise(NameError) do
  273. FooClassWithBarMethod.alias_method_chain :quux?, :baz!
  274. end
  275. end
  276. def test_alias_method_chain_yields_target_and_punctuation
  277. args = nil
  278. FooClassWithBarMethod.class_eval do
  279. def quux?; end
  280. include BarMethods
  281. FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation|
  282. args = [target, punctuation]
  283. end
  284. end
  285. assert_not_nil args
  286. assert_equal 'quux', args[0]
  287. assert_equal '?', args[1]
  288. end
  289. def test_alias_method_chain_preserves_private_method_status
  290. FooClassWithBarMethod.class_eval do
  291. def duck; 'duck' end
  292. include BarMethodAliaser
  293. private :duck
  294. alias_method_chain :duck, :orange
  295. end
  296. assert_raise NoMethodError do
  297. @instance.duck
  298. end
  299. assert_equal 'duck_with_orange', @instance.instance_eval { duck }
  300. assert FooClassWithBarMethod.private_method_defined?(:duck)
  301. end
  302. def test_alias_method_chain_preserves_protected_method_status
  303. FooClassWithBarMethod.class_eval do
  304. def duck; 'duck' end
  305. include BarMethodAliaser
  306. protected :duck
  307. alias_method_chain :duck, :orange
  308. end
  309. assert_raise NoMethodError do
  310. @instance.duck
  311. end
  312. assert_equal 'duck_with_orange', @instance.instance_eval { duck }
  313. assert FooClassWithBarMethod.protected_method_defined?(:duck)
  314. end
  315. def test_alias_method_chain_preserves_public_method_status
  316. FooClassWithBarMethod.class_eval do
  317. def duck; 'duck' end
  318. include BarMethodAliaser
  319. public :duck
  320. alias_method_chain :duck, :orange
  321. end
  322. assert_equal 'duck_with_orange', @instance.duck
  323. assert FooClassWithBarMethod.public_method_defined?(:duck)
  324. end
  325. end