/
test_respond_to.rb
109 lines (94 loc) · 2.51 KB
/
test_respond_to.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
require 'test/unit'
# JRUBY-4548
class TestRespondToConcurrency < Test::Unit::TestCase
module Hashable
def to_h
hash = {}
self.class.hashable_methods.each do |method_entry|
val = self.__send__(method_entry[:method_name])
#Respond to returns true sometimes and false other times
hash[method_entry[:key]] = (val.respond_to?(:to_h) ? val.to_h : val ) if val
#is_a?(Hashable) always seems to work
#hash[method_entry[:key]] = (val.is_a?(Hashable) ? val.to_h : val ) if val
end
hash
end
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
attr_reader :hashable_methods
def method_added(method_name)
@hashable_methods ||= []
@hashable_methods << {:method_name => method_name, :key => method_name.to_s }
end
end
end
class Foo
include Hashable
def bar
@bar ||= Bar.new
end
def dofoo
return 1
end
end
class Bar
include Hashable
def dobar
return 1
end
end
EXPECTED = {"bar"=>{"dobar"=>1}, "dofoo"=>1}
def test_respond_to_heavy_concurrency
assert_nothing_raised do
threads = (0..100).map do
Thread.new do
1000.times do
actual = Foo.new.to_h
assert_equal EXPECTED, actual, "respond_to? concurrency produced an invalid result"
end
end
end
threads.each { |t| t.join }
end
end
end
# JRUBY-4954
class TestRespondToCallSite < Test::Unit::TestCase
class CallSite
@obj = Object.new
def self.respond_to?(name, include_priv = false)
@obj.respond_to?(name, include_priv)
end
end
def test_respond_to_call_site_caches_visibility_check
assert !CallSite.respond_to?(:method_missing)
assert CallSite.respond_to?(:method_missing, true)
end
end
# regression from r91031746 broke m_m invocation when attempting respond_to?
class TestRespondToViaMethodMissing < Test::Unit::TestCase
class ABasicObject #:nodoc:
instance_methods.each do |m|
undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^object_id$)/
end
attr_accessor :respond_to_called
def method_missing(name, *args)
if name == :respond_to? && args[0] == :to_str
@respond_to_called = true
true
elsif name == :==
true
else
super
end
end
end
def test_respond_to_check_can_trigger_method_missing
obj = ABasicObject.new
assert_nothing_raised do
assert "string" == obj
end
end
end