opal-activesupport 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.travis.yml +7 -0
- data/Rakefile +9 -2
- data/lib/opal/activesupport/version.rb +1 -1
- data/opal-activesupport.gemspec +1 -1
- data/opal/active_support/core_ext/array.rb +2 -0
- data/opal/active_support/core_ext/array/grouping.rb +99 -0
- data/opal/active_support/core_ext/array/wrap.rb +45 -0
- data/opal/active_support/core_ext/module.rb +2 -0
- data/opal/active_support/core_ext/module/delegation.rb +64 -0
- data/opal/active_support/core_ext/module/introspection.rb +62 -0
- data/test/abstract_unit.rb +46 -0
- data/test/core_ext/array_ext_test.rb +471 -0
- data/test/core_ext/blank_test.rb +24 -0
- data/{spec/core_ext/class/attribute_spec.rb → test/core_ext/class/attribute_test.rb} +16 -20
- data/test/core_ext/kernel_test.rb +124 -0
- data/test/core_ext/module/remove_method_test.rb +29 -0
- data/test/core_ext/module_test.rb +439 -0
- data/{spec/core_ext/numeric_spec.rb → test/core_ext/numeric_ext_test.rb} +110 -116
- data/test/core_ext/object_and_class_ext_test.rb +183 -0
- data/test/core_ext/string_ext_test.rb +628 -0
- data/{spec → test}/empty_bool.rb +0 -0
- data/{spec → test}/inflector_test_cases.rb +0 -0
- data/test/minitest/autorun.rb +29 -0
- metadata +39 -30
- data/spec/core_ext/array/extract_options_spec.rb +0 -49
- data/spec/core_ext/kernel_spec.rb +0 -9
- data/spec/core_ext/module/remove_method_spec.rb +0 -29
- data/spec/core_ext/object/blank_spec.rb +0 -40
- data/spec/core_ext/object/try_spec.rb +0 -102
- data/spec/core_ext/string_spec.rb +0 -71
- data/spec/spec_helper.rb +0 -26
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'abstract_unit'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
|
6
|
+
class BlankTest < ActiveSupport::TestCase
|
7
|
+
BLANK = [ EmptyTrue.new, nil, false, '', ' ', " \n\t \r ", ' ', [], {} ]
|
8
|
+
NOT = [ EmptyFalse.new, Object.new, true, 0, 1, 'a', [nil], { nil => 0 } ]
|
9
|
+
|
10
|
+
def test_blank
|
11
|
+
BLANK.each { |v| assert v.blank?, "#{v.inspect} should be blank" }
|
12
|
+
NOT.each { |v| assert !v.blank?, "#{v.inspect} should not be blank" }
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_present
|
16
|
+
BLANK.each { |v| assert !v.present?, "#{v.inspect} should not be present" }
|
17
|
+
NOT.each { |v| assert v.present?, "#{v.inspect} should be present" }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_presence
|
21
|
+
BLANK.each { |v| assert_equal nil, v.presence, "#{v.inspect}.presence should return nil" }
|
22
|
+
NOT.each { |v| assert_equal v, v.presence, "#{v.inspect}.presence should return self" }
|
23
|
+
end
|
24
|
+
end
|
@@ -1,22 +1,23 @@
|
|
1
|
-
require '
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_support/core_ext/class/attribute'
|
2
3
|
|
3
|
-
|
4
|
-
|
4
|
+
class ClassAttributeTest < ActiveSupport::TestCase
|
5
|
+
def setup
|
5
6
|
@klass = Class.new { class_attribute :setting }
|
6
7
|
@sub = Class.new(@klass)
|
7
8
|
end
|
8
9
|
|
9
|
-
|
10
|
+
test 'defaults to nil' do
|
10
11
|
assert_nil @klass.setting
|
11
12
|
assert_nil @sub.setting
|
12
13
|
end
|
13
14
|
|
14
|
-
|
15
|
+
test 'inheritable' do
|
15
16
|
@klass.setting = 1
|
16
17
|
assert_equal 1, @sub.setting
|
17
18
|
end
|
18
19
|
|
19
|
-
|
20
|
+
test 'overridable' do
|
20
21
|
@sub.setting = 1
|
21
22
|
assert_nil @klass.setting
|
22
23
|
|
@@ -26,20 +27,20 @@ describe Class do
|
|
26
27
|
assert_equal 1, Class.new(@sub).setting
|
27
28
|
end
|
28
29
|
|
29
|
-
|
30
|
+
test 'query method' do
|
30
31
|
assert_equal false, @klass.setting?
|
31
32
|
@klass.setting = 1
|
32
33
|
assert_equal true, @klass.setting?
|
33
34
|
end
|
34
35
|
|
35
|
-
|
36
|
+
test 'instance reader delegates to class' do
|
36
37
|
assert_nil @klass.new.setting
|
37
38
|
|
38
39
|
@klass.setting = 1
|
39
40
|
assert_equal 1, @klass.new.setting
|
40
41
|
end
|
41
42
|
|
42
|
-
|
43
|
+
test 'instance override' do
|
43
44
|
object = @klass.new
|
44
45
|
object.setting = 1
|
45
46
|
assert_nil @klass.setting
|
@@ -47,43 +48,38 @@ describe Class do
|
|
47
48
|
assert_equal 1, object.setting
|
48
49
|
end
|
49
50
|
|
50
|
-
|
51
|
+
test 'instance query' do
|
51
52
|
object = @klass.new
|
52
53
|
assert_equal false, object.setting?
|
53
54
|
object.setting = 1
|
54
55
|
assert_equal true, object.setting?
|
55
56
|
end
|
56
57
|
|
57
|
-
|
58
|
+
test 'disabling instance writer' do
|
58
59
|
object = Class.new { class_attribute :setting, :instance_writer => false }.new
|
59
60
|
assert_raise(NoMethodError) { object.setting = 'boom' }
|
60
61
|
end
|
61
62
|
|
62
|
-
|
63
|
+
test 'disabling instance reader' do
|
63
64
|
object = Class.new { class_attribute :setting, :instance_reader => false }.new
|
64
65
|
assert_raise(NoMethodError) { object.setting }
|
65
66
|
assert_raise(NoMethodError) { object.setting? }
|
66
67
|
end
|
67
68
|
|
68
|
-
|
69
|
+
test 'disabling both instance writer and reader' do
|
69
70
|
object = Class.new { class_attribute :setting, :instance_accessor => false }.new
|
70
71
|
assert_raise(NoMethodError) { object.setting }
|
71
72
|
assert_raise(NoMethodError) { object.setting? }
|
72
73
|
assert_raise(NoMethodError) { object.setting = 'boom' }
|
73
74
|
end
|
74
75
|
|
75
|
-
|
76
|
-
object = Class.new { class_attribute :setting, instance_predicate: false }.new
|
77
|
-
assert_raise(NoMethodError) { object.setting? }
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'works well with singleton classes' do
|
76
|
+
test 'works well with singleton classes' do
|
81
77
|
object = @klass.new
|
82
78
|
object.singleton_class.setting = 'foo'
|
83
79
|
assert_equal 'foo', object.setting
|
84
80
|
end
|
85
81
|
|
86
|
-
|
82
|
+
test 'setter returns set value' do
|
87
83
|
val = @klass.send(:setting=, 1)
|
88
84
|
assert_equal 1, val
|
89
85
|
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_support/core_ext/kernel'
|
3
|
+
|
4
|
+
class KernelTest < ActiveSupport::TestCase
|
5
|
+
# def test_silence_warnings
|
6
|
+
# silence_warnings { assert_nil $VERBOSE }
|
7
|
+
# assert_equal 1234, silence_warnings { 1234 }
|
8
|
+
# end
|
9
|
+
#
|
10
|
+
# def test_silence_warnings_verbose_invariant
|
11
|
+
# old_verbose = $VERBOSE
|
12
|
+
# silence_warnings { raise }
|
13
|
+
# flunk
|
14
|
+
# rescue
|
15
|
+
# assert_equal old_verbose, $VERBOSE
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
#
|
19
|
+
# def test_enable_warnings
|
20
|
+
# enable_warnings { assert_equal true, $VERBOSE }
|
21
|
+
# assert_equal 1234, enable_warnings { 1234 }
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# def test_enable_warnings_verbose_invariant
|
25
|
+
# old_verbose = $VERBOSE
|
26
|
+
# enable_warnings { raise }
|
27
|
+
# flunk
|
28
|
+
# rescue
|
29
|
+
# assert_equal old_verbose, $VERBOSE
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
#
|
33
|
+
# def test_silence_stderr
|
34
|
+
# old_stderr_position = STDERR.tell
|
35
|
+
# silence_stderr { STDERR.puts 'hello world' }
|
36
|
+
# assert_equal old_stderr_position, STDERR.tell
|
37
|
+
# rescue Errno::ESPIPE
|
38
|
+
# # Skip if we can't STDERR.tell
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# def test_quietly
|
42
|
+
# old_stdout_position, old_stderr_position = STDOUT.tell, STDERR.tell
|
43
|
+
# quietly do
|
44
|
+
# puts 'see me, feel me'
|
45
|
+
# STDERR.puts 'touch me, heal me'
|
46
|
+
# end
|
47
|
+
# assert_equal old_stdout_position, STDOUT.tell
|
48
|
+
# assert_equal old_stderr_position, STDERR.tell
|
49
|
+
# rescue Errno::ESPIPE
|
50
|
+
# # Skip if we can't STDERR.tell
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# def test_silence_stderr_with_return_value
|
54
|
+
# assert_equal 1, silence_stderr { 1 }
|
55
|
+
# end
|
56
|
+
|
57
|
+
def test_class_eval
|
58
|
+
o = Object.new
|
59
|
+
class << o; @x = 1; end
|
60
|
+
assert_equal 1, o.class_eval { @x }
|
61
|
+
end
|
62
|
+
|
63
|
+
# def test_capture
|
64
|
+
# assert_equal 'STDERR', capture(:stderr) { $stderr.print 'STDERR' }
|
65
|
+
# assert_equal 'STDOUT', capture(:stdout) { print 'STDOUT' }
|
66
|
+
# assert_equal "STDERR\n", capture(:stderr) { system('echo STDERR 1>&2') }
|
67
|
+
# assert_equal "STDOUT\n", capture(:stdout) { system('echo STDOUT') }
|
68
|
+
# end
|
69
|
+
end
|
70
|
+
|
71
|
+
# class KernelSuppressTest < ActiveSupport::TestCase
|
72
|
+
# def test_reraise
|
73
|
+
# assert_raise(LoadError) do
|
74
|
+
# suppress(ArgumentError) { raise LoadError }
|
75
|
+
# end
|
76
|
+
# end
|
77
|
+
#
|
78
|
+
# def test_suppression
|
79
|
+
# suppress(ArgumentError) { raise ArgumentError }
|
80
|
+
# suppress(LoadError) { raise LoadError }
|
81
|
+
# suppress(LoadError, ArgumentError) { raise LoadError }
|
82
|
+
# suppress(LoadError, ArgumentError) { raise ArgumentError }
|
83
|
+
# end
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
# class MockStdErr
|
87
|
+
# attr_reader :output
|
88
|
+
# def puts(message)
|
89
|
+
# @output ||= []
|
90
|
+
# @output << message
|
91
|
+
# end
|
92
|
+
#
|
93
|
+
# def info(message)
|
94
|
+
# puts(message)
|
95
|
+
# end
|
96
|
+
#
|
97
|
+
# def write(message)
|
98
|
+
# puts(message)
|
99
|
+
# end
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# class KernelDebuggerTest < ActiveSupport::TestCase
|
103
|
+
# def test_debugger_not_available_message_to_stderr
|
104
|
+
# old_stderr = $stderr
|
105
|
+
# $stderr = MockStdErr.new
|
106
|
+
# debugger
|
107
|
+
# assert_match(/Debugger requested/, $stderr.output.first)
|
108
|
+
# ensure
|
109
|
+
# $stderr = old_stderr
|
110
|
+
# end
|
111
|
+
#
|
112
|
+
# def test_debugger_not_available_message_to_rails_logger
|
113
|
+
# rails = Class.new do
|
114
|
+
# def self.logger
|
115
|
+
# @logger ||= MockStdErr.new
|
116
|
+
# end
|
117
|
+
# end
|
118
|
+
# Object.const_set(:Rails, rails)
|
119
|
+
# debugger
|
120
|
+
# assert_match(/Debugger requested/, rails.logger.output.first)
|
121
|
+
# ensure
|
122
|
+
# Object.send(:remove_const, :Rails)
|
123
|
+
# end
|
124
|
+
# end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_support/core_ext/module/remove_method'
|
3
|
+
|
4
|
+
module RemoveMethodTests
|
5
|
+
class A
|
6
|
+
def do_something
|
7
|
+
return 1
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class RemoveMethodTest < ActiveSupport::TestCase
|
14
|
+
|
15
|
+
def test_remove_method_from_an_object
|
16
|
+
RemoveMethodTests::A.class_eval{
|
17
|
+
self.remove_possible_method(:do_something)
|
18
|
+
}
|
19
|
+
assert !RemoveMethodTests::A.new.respond_to?(:do_something)
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_redefine_method_in_an_object
|
23
|
+
RemoveMethodTests::A.class_eval{
|
24
|
+
self.redefine_method(:do_something) { return 100 }
|
25
|
+
}
|
26
|
+
assert_equal 100, RemoveMethodTests::A.new.do_something
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,439 @@
|
|
1
|
+
require 'abstract_unit'
|
2
|
+
require 'active_support/core_ext/module'
|
3
|
+
|
4
|
+
module One
|
5
|
+
Constant1 = "Hello World"
|
6
|
+
Constant2 = "What's up?"
|
7
|
+
end
|
8
|
+
|
9
|
+
class Ab
|
10
|
+
include One
|
11
|
+
Constant1 = "Hello World" # Will have different object id than One::Constant1
|
12
|
+
Constant3 = "Goodbye World"
|
13
|
+
end
|
14
|
+
|
15
|
+
module Xy
|
16
|
+
class Bc
|
17
|
+
include One
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
module Yz
|
22
|
+
module Zy
|
23
|
+
class Cd
|
24
|
+
include One
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Somewhere = Struct.new(:street, :city) do
|
30
|
+
attr_accessor :name
|
31
|
+
end
|
32
|
+
|
33
|
+
class Someone < Struct.new(:name, :place)
|
34
|
+
delegate :street, :city, :to_f, :to => :place
|
35
|
+
delegate :name=, :to => :place, :prefix => true
|
36
|
+
delegate :upcase, :to => "place.city"
|
37
|
+
delegate :table_name, :to => :class
|
38
|
+
delegate :table_name, :to => :class, :prefix => true
|
39
|
+
|
40
|
+
def self.table_name
|
41
|
+
'some_table'
|
42
|
+
end
|
43
|
+
|
44
|
+
FAILED_DELEGATE_LINE = __LINE__ + 1
|
45
|
+
delegate :foo, :to => :place
|
46
|
+
|
47
|
+
FAILED_DELEGATE_LINE_2 = __LINE__ + 1
|
48
|
+
delegate :bar, :to => :place, :allow_nil => true
|
49
|
+
end
|
50
|
+
|
51
|
+
Invoice = Struct.new(:client) do
|
52
|
+
delegate :street, :city, :name, :to => :client, :prefix => true
|
53
|
+
delegate :street, :city, :name, :to => :client, :prefix => :customer
|
54
|
+
end
|
55
|
+
|
56
|
+
Project = Struct.new(:description, :person) do
|
57
|
+
delegate :name, :to => :person, :allow_nil => true
|
58
|
+
delegate :to_f, :to => :description, :allow_nil => true
|
59
|
+
end
|
60
|
+
|
61
|
+
Developer = Struct.new(:client) do
|
62
|
+
delegate :name, :to => :client, :prefix => nil
|
63
|
+
end
|
64
|
+
|
65
|
+
Tester = Struct.new(:client) do
|
66
|
+
delegate :name, :to => :client, :prefix => false
|
67
|
+
end
|
68
|
+
|
69
|
+
class ParameterSet
|
70
|
+
delegate :[], :[]=, :to => :@params
|
71
|
+
|
72
|
+
def initialize
|
73
|
+
@params = {:foo => "bar"}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
class Name
|
78
|
+
delegate :upcase, :to => :@full_name
|
79
|
+
|
80
|
+
def initialize(first, last)
|
81
|
+
@full_name = "#{first} #{last}"
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
class ModuleTest < ActiveSupport::TestCase
|
86
|
+
def setup
|
87
|
+
@david = Someone.new("David", Somewhere.new("Paulina", "Chicago"))
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_delegation_to_methods
|
91
|
+
assert_equal "Paulina", @david.street
|
92
|
+
assert_equal "Chicago", @david.city
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_delegation_to_assignment_method
|
96
|
+
@david.place_name = "Fred"
|
97
|
+
assert_equal "Fred", @david.place.name
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_delegation_to_index_get_method
|
101
|
+
@params = ParameterSet.new
|
102
|
+
assert_equal "bar", @params[:foo]
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_delegation_to_index_set_method
|
106
|
+
@params = ParameterSet.new
|
107
|
+
@params[:foo] = "baz"
|
108
|
+
assert_equal "baz", @params[:foo]
|
109
|
+
end
|
110
|
+
|
111
|
+
# def test_delegation_down_hierarchy
|
112
|
+
# assert_equal "CHICAGO", @david.upcase
|
113
|
+
# end
|
114
|
+
|
115
|
+
def test_delegation_to_instance_variable
|
116
|
+
david = Name.new("David", "Hansson")
|
117
|
+
assert_equal "DAVID HANSSON", david.upcase
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_delegation_to_class_method
|
121
|
+
assert_equal 'some_table', @david.table_name
|
122
|
+
assert_equal 'some_table', @david.class_table_name
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_missing_delegation_target
|
126
|
+
assert_raise(ArgumentError) do
|
127
|
+
Name.send :delegate, :nowhere
|
128
|
+
end
|
129
|
+
assert_raise(ArgumentError) do
|
130
|
+
Name.send :delegate, :noplace, :tos => :hollywood
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_delegation_prefix
|
135
|
+
invoice = Invoice.new(@david)
|
136
|
+
assert_equal invoice.client_name, "David"
|
137
|
+
assert_equal invoice.client_street, "Paulina"
|
138
|
+
assert_equal invoice.client_city, "Chicago"
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_delegation_custom_prefix
|
142
|
+
invoice = Invoice.new(@david)
|
143
|
+
assert_equal invoice.customer_name, "David"
|
144
|
+
assert_equal invoice.customer_street, "Paulina"
|
145
|
+
assert_equal invoice.customer_city, "Chicago"
|
146
|
+
end
|
147
|
+
|
148
|
+
def test_delegation_prefix_with_nil_or_false
|
149
|
+
assert_equal Developer.new(@david).name, "David"
|
150
|
+
assert_equal Tester.new(@david).name, "David"
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_delegation_prefix_with_instance_variable
|
154
|
+
assert_raise ArgumentError do
|
155
|
+
Class.new do
|
156
|
+
def initialize(client)
|
157
|
+
@client = client
|
158
|
+
end
|
159
|
+
delegate :name, :address, :to => :@client, :prefix => true
|
160
|
+
end
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_delegation_with_allow_nil
|
165
|
+
rails = Project.new("Rails", Someone.new("David"))
|
166
|
+
assert_equal rails.name, "David"
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_delegation_with_allow_nil_and_nil_value
|
170
|
+
rails = Project.new("Rails")
|
171
|
+
assert_nil rails.name
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_delegation_with_allow_nil_and_nil_value_and_prefix
|
175
|
+
Project.class_eval do
|
176
|
+
delegate :name, :to => :person, :allow_nil => true, :prefix => true
|
177
|
+
end
|
178
|
+
rails = Project.new("Rails")
|
179
|
+
assert_nil rails.person_name
|
180
|
+
end
|
181
|
+
|
182
|
+
# def test_delegation_without_allow_nil_and_nil_value
|
183
|
+
# david = Someone.new("David")
|
184
|
+
# assert_raise(RuntimeError) { david.street }
|
185
|
+
# end
|
186
|
+
#
|
187
|
+
# def test_delegation_to_method_that_exists_on_nil
|
188
|
+
# nil_person = Someone.new(nil)
|
189
|
+
# assert_equal 0.0, nil_person.to_f
|
190
|
+
# end
|
191
|
+
#
|
192
|
+
# def test_delegation_to_method_that_exists_on_nil_when_allowing_nil
|
193
|
+
# nil_project = Project.new(nil)
|
194
|
+
# assert_equal 0.0, nil_project.to_f
|
195
|
+
# end
|
196
|
+
#
|
197
|
+
# def test_delegation_does_not_raise_error_when_removing_singleton_instance_methods
|
198
|
+
# parent = Class.new do
|
199
|
+
# def self.parent_method; end
|
200
|
+
# end
|
201
|
+
#
|
202
|
+
# assert_nothing_raised do
|
203
|
+
# Class.new(parent) do
|
204
|
+
# class << self
|
205
|
+
# delegate :parent_method, :to => :superclass
|
206
|
+
# end
|
207
|
+
# end
|
208
|
+
# end
|
209
|
+
# end
|
210
|
+
|
211
|
+
# DISABLED: Backtrace in JavaScript is Hard™
|
212
|
+
# def test_delegation_exception_backtrace
|
213
|
+
# someone = Someone.new("foo", "bar")
|
214
|
+
# someone.foo
|
215
|
+
# rescue NoMethodError => e
|
216
|
+
# file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE}"
|
217
|
+
# # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace.
|
218
|
+
# assert e.backtrace.any?{|a| a.include?(file_and_line)},
|
219
|
+
# "[#{e.backtrace.inspect}] did not include [#{file_and_line}]"
|
220
|
+
# end
|
221
|
+
|
222
|
+
# DISABLED: Backtrace in JavaScript is Hard™
|
223
|
+
# def test_delegation_exception_backtrace_with_allow_nil
|
224
|
+
# someone = Someone.new("foo", "bar")
|
225
|
+
# someone.bar
|
226
|
+
# rescue NoMethodError => e
|
227
|
+
# file_and_line = "#{__FILE__}:#{Someone::FAILED_DELEGATE_LINE_2}"
|
228
|
+
# # We can't simply check the first line of the backtrace, because JRuby reports the call to __send__ in the backtrace.
|
229
|
+
# assert e.backtrace.any?{|a| a.include?(file_and_line)},
|
230
|
+
# "[#{e.backtrace.inspect}] did not include [#{file_and_line}]"
|
231
|
+
# end
|
232
|
+
|
233
|
+
def test_parent
|
234
|
+
assert_equal Yz::Zy, Yz::Zy::Cd.parent
|
235
|
+
assert_equal Yz, Yz::Zy.parent
|
236
|
+
assert_equal Object, Yz.parent
|
237
|
+
end
|
238
|
+
|
239
|
+
def test_parents
|
240
|
+
assert_equal [Yz::Zy, Yz, Object], Yz::Zy::Cd.parents
|
241
|
+
assert_equal [Yz, Object], Yz::Zy.parents
|
242
|
+
end
|
243
|
+
|
244
|
+
# def test_local_constants
|
245
|
+
# assert_equal %w(Constant1 Constant3), Ab.local_constants.sort.map(&:to_s)
|
246
|
+
# end
|
247
|
+
#
|
248
|
+
# def test_local_constant_names
|
249
|
+
# ActiveSupport::Deprecation.silence do
|
250
|
+
# assert_equal %w(Constant1 Constant3), Ab.local_constant_names.sort.map(&:to_s)
|
251
|
+
# end
|
252
|
+
# end
|
253
|
+
end
|
254
|
+
|
255
|
+
# module BarMethodAliaser
|
256
|
+
# def self.included(foo_class)
|
257
|
+
# foo_class.class_eval do
|
258
|
+
# include BarMethods
|
259
|
+
# alias_method_chain :bar, :baz
|
260
|
+
# end
|
261
|
+
# end
|
262
|
+
# end
|
263
|
+
#
|
264
|
+
# module BarMethods
|
265
|
+
# def bar_with_baz
|
266
|
+
# bar_without_baz << '_with_baz'
|
267
|
+
# end
|
268
|
+
#
|
269
|
+
# def quux_with_baz!
|
270
|
+
# quux_without_baz! << '_with_baz'
|
271
|
+
# end
|
272
|
+
#
|
273
|
+
# def quux_with_baz?
|
274
|
+
# false
|
275
|
+
# end
|
276
|
+
#
|
277
|
+
# def quux_with_baz=(v)
|
278
|
+
# send(:quux_without_baz=, v) << '_with_baz'
|
279
|
+
# end
|
280
|
+
#
|
281
|
+
# def duck_with_orange
|
282
|
+
# duck_without_orange << '_with_orange'
|
283
|
+
# end
|
284
|
+
# end
|
285
|
+
#
|
286
|
+
# class MethodAliasingTest < ActiveSupport::TestCase
|
287
|
+
# def setup
|
288
|
+
# Object.const_set :FooClassWithBarMethod, Class.new { def bar() 'bar' end }
|
289
|
+
# @instance = FooClassWithBarMethod.new
|
290
|
+
# end
|
291
|
+
#
|
292
|
+
# def teardown
|
293
|
+
# Object.instance_eval { remove_const :FooClassWithBarMethod }
|
294
|
+
# end
|
295
|
+
#
|
296
|
+
# def test_alias_method_chain
|
297
|
+
# assert @instance.respond_to?(:bar)
|
298
|
+
# feature_aliases = [:bar_with_baz, :bar_without_baz]
|
299
|
+
#
|
300
|
+
# feature_aliases.each do |method|
|
301
|
+
# assert !@instance.respond_to?(method)
|
302
|
+
# end
|
303
|
+
#
|
304
|
+
# assert_equal 'bar', @instance.bar
|
305
|
+
#
|
306
|
+
# FooClassWithBarMethod.class_eval { include BarMethodAliaser }
|
307
|
+
#
|
308
|
+
# feature_aliases.each do |method|
|
309
|
+
# assert_respond_to @instance, method
|
310
|
+
# end
|
311
|
+
#
|
312
|
+
# assert_equal 'bar_with_baz', @instance.bar
|
313
|
+
# assert_equal 'bar', @instance.bar_without_baz
|
314
|
+
# end
|
315
|
+
#
|
316
|
+
# def test_alias_method_chain_with_punctuation_method
|
317
|
+
# FooClassWithBarMethod.class_eval do
|
318
|
+
# def quux!; 'quux' end
|
319
|
+
# end
|
320
|
+
#
|
321
|
+
# assert !@instance.respond_to?(:quux_with_baz!)
|
322
|
+
# FooClassWithBarMethod.class_eval do
|
323
|
+
# include BarMethodAliaser
|
324
|
+
# alias_method_chain :quux!, :baz
|
325
|
+
# end
|
326
|
+
# assert_respond_to @instance, :quux_with_baz!
|
327
|
+
#
|
328
|
+
# assert_equal 'quux_with_baz', @instance.quux!
|
329
|
+
# assert_equal 'quux', @instance.quux_without_baz!
|
330
|
+
# end
|
331
|
+
#
|
332
|
+
# def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods
|
333
|
+
# FooClassWithBarMethod.class_eval do
|
334
|
+
# def quux!; 'quux!' end
|
335
|
+
# def quux?; true end
|
336
|
+
# def quux=(v); 'quux=' end
|
337
|
+
# end
|
338
|
+
#
|
339
|
+
# assert !@instance.respond_to?(:quux_with_baz!)
|
340
|
+
# assert !@instance.respond_to?(:quux_with_baz?)
|
341
|
+
# assert !@instance.respond_to?(:quux_with_baz=)
|
342
|
+
#
|
343
|
+
# FooClassWithBarMethod.class_eval { include BarMethodAliaser }
|
344
|
+
# assert_respond_to @instance, :quux_with_baz!
|
345
|
+
# assert_respond_to @instance, :quux_with_baz?
|
346
|
+
# assert_respond_to @instance, :quux_with_baz=
|
347
|
+
#
|
348
|
+
#
|
349
|
+
# FooClassWithBarMethod.alias_method_chain :quux!, :baz
|
350
|
+
# assert_equal 'quux!_with_baz', @instance.quux!
|
351
|
+
# assert_equal 'quux!', @instance.quux_without_baz!
|
352
|
+
#
|
353
|
+
# FooClassWithBarMethod.alias_method_chain :quux?, :baz
|
354
|
+
# assert_equal false, @instance.quux?
|
355
|
+
# assert_equal true, @instance.quux_without_baz?
|
356
|
+
#
|
357
|
+
# FooClassWithBarMethod.alias_method_chain :quux=, :baz
|
358
|
+
# assert_equal 'quux=_with_baz', @instance.send(:quux=, 1234)
|
359
|
+
# assert_equal 'quux=', @instance.send(:quux_without_baz=, 1234)
|
360
|
+
# end
|
361
|
+
#
|
362
|
+
# def test_alias_method_chain_with_feature_punctuation
|
363
|
+
# FooClassWithBarMethod.class_eval do
|
364
|
+
# def quux; 'quux' end
|
365
|
+
# def quux?; 'quux?' end
|
366
|
+
# include BarMethodAliaser
|
367
|
+
# alias_method_chain :quux, :baz!
|
368
|
+
# end
|
369
|
+
#
|
370
|
+
# assert_nothing_raised do
|
371
|
+
# assert_equal 'quux_with_baz', @instance.quux_with_baz!
|
372
|
+
# end
|
373
|
+
#
|
374
|
+
# assert_raise(NameError) do
|
375
|
+
# FooClassWithBarMethod.alias_method_chain :quux?, :baz!
|
376
|
+
# end
|
377
|
+
# end
|
378
|
+
#
|
379
|
+
# def test_alias_method_chain_yields_target_and_punctuation
|
380
|
+
# args = nil
|
381
|
+
#
|
382
|
+
# FooClassWithBarMethod.class_eval do
|
383
|
+
# def quux?; end
|
384
|
+
# include BarMethods
|
385
|
+
#
|
386
|
+
# FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation|
|
387
|
+
# args = [target, punctuation]
|
388
|
+
# end
|
389
|
+
# end
|
390
|
+
#
|
391
|
+
# assert_not_nil args
|
392
|
+
# assert_equal 'quux', args[0]
|
393
|
+
# assert_equal '?', args[1]
|
394
|
+
# end
|
395
|
+
#
|
396
|
+
# def test_alias_method_chain_preserves_private_method_status
|
397
|
+
# FooClassWithBarMethod.class_eval do
|
398
|
+
# def duck; 'duck' end
|
399
|
+
# include BarMethodAliaser
|
400
|
+
# private :duck
|
401
|
+
# alias_method_chain :duck, :orange
|
402
|
+
# end
|
403
|
+
#
|
404
|
+
# assert_raise NoMethodError do
|
405
|
+
# @instance.duck
|
406
|
+
# end
|
407
|
+
#
|
408
|
+
# assert_equal 'duck_with_orange', @instance.instance_eval { duck }
|
409
|
+
# assert FooClassWithBarMethod.private_method_defined?(:duck)
|
410
|
+
# end
|
411
|
+
#
|
412
|
+
# def test_alias_method_chain_preserves_protected_method_status
|
413
|
+
# FooClassWithBarMethod.class_eval do
|
414
|
+
# def duck; 'duck' end
|
415
|
+
# include BarMethodAliaser
|
416
|
+
# protected :duck
|
417
|
+
# alias_method_chain :duck, :orange
|
418
|
+
# end
|
419
|
+
#
|
420
|
+
# assert_raise NoMethodError do
|
421
|
+
# @instance.duck
|
422
|
+
# end
|
423
|
+
#
|
424
|
+
# assert_equal 'duck_with_orange', @instance.instance_eval { duck }
|
425
|
+
# assert FooClassWithBarMethod.protected_method_defined?(:duck)
|
426
|
+
# end
|
427
|
+
#
|
428
|
+
# def test_alias_method_chain_preserves_public_method_status
|
429
|
+
# FooClassWithBarMethod.class_eval do
|
430
|
+
# def duck; 'duck' end
|
431
|
+
# include BarMethodAliaser
|
432
|
+
# public :duck
|
433
|
+
# alias_method_chain :duck, :orange
|
434
|
+
# end
|
435
|
+
#
|
436
|
+
# assert_equal 'duck_with_orange', @instance.duck
|
437
|
+
# assert FooClassWithBarMethod.public_method_defined?(:duck)
|
438
|
+
# end
|
439
|
+
# end
|