quality_extensions 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Readme +54 -0
- data/lib/qualitysmith_extensions/all.rb +4 -0
- data/lib/qualitysmith_extensions/array/all.rb +2 -0
- data/lib/qualitysmith_extensions/array/average.rb +44 -0
- data/lib/qualitysmith_extensions/array/classify.rb +97 -0
- data/lib/qualitysmith_extensions/array/expand_ranges.rb +52 -0
- data/lib/qualitysmith_extensions/array/group_by.rb +134 -0
- data/lib/qualitysmith_extensions/array/sequence.rb +66 -0
- data/lib/qualitysmith_extensions/array/shell_escape.rb +36 -0
- data/lib/qualitysmith_extensions/array/to_a_recursive.rb +41 -0
- data/lib/qualitysmith_extensions/array/to_query_string.rb +96 -0
- data/lib/qualitysmith_extensions/collection_extensions_for_cgi.rb +2 -0
- data/lib/qualitysmith_extensions/colored/toggleability.rb +62 -0
- data/lib/qualitysmith_extensions/console/command.facets.1.8.51.rb +749 -0
- data/lib/qualitysmith_extensions/console/command.facets.1.8.54.rb +748 -0
- data/lib/qualitysmith_extensions/console/command.rb +944 -0
- data/lib/qualitysmith_extensions/date/all.rb +2 -0
- data/lib/qualitysmith_extensions/date/deprecated.rb +40 -0
- data/lib/qualitysmith_extensions/date/iso8601.rb +31 -0
- data/lib/qualitysmith_extensions/date/month_ranges.rb +122 -0
- data/lib/qualitysmith_extensions/dir/each_child.rb +58 -0
- data/lib/qualitysmith_extensions/enumerable/enum.rb +69 -0
- data/lib/qualitysmith_extensions/enumerable/select_until.rb +4 -0
- data/lib/qualitysmith_extensions/enumerable/select_while.rb +109 -0
- data/lib/qualitysmith_extensions/exception/inspect_with_backtrace.rb +65 -0
- data/lib/qualitysmith_extensions/file/exact_match_regexp.rb +34 -0
- data/lib/qualitysmith_extensions/file_test/binary_file.rb +110 -0
- data/lib/qualitysmith_extensions/find/select.rb +68 -0
- data/lib/qualitysmith_extensions/global_variable_set.rb +153 -0
- data/lib/qualitysmith_extensions/hash/all.rb +2 -0
- data/lib/qualitysmith_extensions/hash/to_date.rb +34 -0
- data/lib/qualitysmith_extensions/hash/to_query_string.rb +121 -0
- data/lib/qualitysmith_extensions/kernel/all.rb +2 -0
- data/lib/qualitysmith_extensions/kernel/autoreload.rb +128 -0
- data/lib/qualitysmith_extensions/kernel/backtrace.rb +71 -0
- data/lib/qualitysmith_extensions/kernel/capture_output.rb +115 -0
- data/lib/qualitysmith_extensions/kernel/die.rb +49 -0
- data/lib/qualitysmith_extensions/kernel/example_printer.rb +81 -0
- data/lib/qualitysmith_extensions/kernel/filter_output.rb +108 -0
- data/lib/qualitysmith_extensions/kernel/remove_const.rb +178 -0
- data/lib/qualitysmith_extensions/kernel/remove_module.rb +127 -0
- data/lib/qualitysmith_extensions/kernel/require_all.rb +186 -0
- data/lib/qualitysmith_extensions/kernel/require_local_all.rb +4 -0
- data/lib/qualitysmith_extensions/kernel/require_once.rb +18 -0
- data/lib/qualitysmith_extensions/kernel/simulate_input.rb +52 -0
- data/lib/qualitysmith_extensions/kernel/trap_chain.rb +61 -0
- data/lib/qualitysmith_extensions/kernel/windows_platform.rb +46 -0
- data/lib/qualitysmith_extensions/module/alias_method.rb +6 -0
- data/lib/qualitysmith_extensions/module/alias_method_chain.rb +165 -0
- data/lib/qualitysmith_extensions/module/ancestry_of_instance_method.rb +43 -0
- data/lib/qualitysmith_extensions/module/attribute_accessors.rb +49 -0
- data/lib/qualitysmith_extensions/module/basename.rb +76 -0
- data/lib/qualitysmith_extensions/module/bool_attr_accessor.rb +497 -0
- data/lib/qualitysmith_extensions/module/class_methods.rb +87 -0
- data/lib/qualitysmith_extensions/module/create.rb +315 -0
- data/lib/qualitysmith_extensions/module/create_setter.rb +9 -0
- data/lib/qualitysmith_extensions/module/dirname.rb +4 -0
- data/lib/qualitysmith_extensions/module/guard_method.rb +312 -0
- data/lib/qualitysmith_extensions/module/includable_once.rb +10 -0
- data/lib/qualitysmith_extensions/module/join.rb +66 -0
- data/lib/qualitysmith_extensions/module/malias_method_chain.rb +92 -0
- data/lib/qualitysmith_extensions/module/module_methods.rb +4 -0
- data/lib/qualitysmith_extensions/module/namespace.rb +112 -0
- data/lib/qualitysmith_extensions/module/parents.rb +61 -0
- data/lib/qualitysmith_extensions/module/remove_const.rb +117 -0
- data/lib/qualitysmith_extensions/module/split.rb +55 -0
- data/lib/qualitysmith_extensions/month.rb +66 -0
- data/lib/qualitysmith_extensions/mutex/if_available.rb +75 -0
- data/lib/qualitysmith_extensions/object/ancestry_of_method.rb +257 -0
- data/lib/qualitysmith_extensions/object/default.rb +69 -0
- data/lib/qualitysmith_extensions/object/if_else.rb +157 -0
- data/lib/qualitysmith_extensions/object/ignore_access.rb +84 -0
- data/lib/qualitysmith_extensions/object/mcall.rb +92 -0
- data/lib/qualitysmith_extensions/object/methods.rb +63 -0
- data/lib/qualitysmith_extensions/object/send_if.rb +151 -0
- data/lib/qualitysmith_extensions/object/send_if_not_nil.rb +35 -0
- data/lib/qualitysmith_extensions/object/singleton_send.rb +129 -0
- data/lib/qualitysmith_extensions/regexp/join.rb +111 -0
- data/lib/qualitysmith_extensions/string/all.rb +2 -0
- data/lib/qualitysmith_extensions/string/constantize.rb +4 -0
- data/lib/qualitysmith_extensions/string/digits_only.rb +27 -0
- data/lib/qualitysmith_extensions/string/each_char_with_index.rb +41 -0
- data/lib/qualitysmith_extensions/string/md5.rb +29 -0
- data/lib/qualitysmith_extensions/string/shell_escape.rb +43 -0
- data/lib/qualitysmith_extensions/string/to_underscored_label.rb +37 -0
- data/lib/qualitysmith_extensions/string/with_knowledge_of_color.rb +64 -0
- data/lib/qualitysmith_extensions/symbol/constantize.rb +69 -0
- data/lib/qualitysmith_extensions/symbol/match.rb +157 -0
- data/lib/qualitysmith_extensions/template.rb +33 -0
- data/lib/qualitysmith_extensions/test/all.rb +2 -0
- data/lib/qualitysmith_extensions/test/assert_anything.rb +93 -0
- data/lib/qualitysmith_extensions/test/assert_changed.rb +66 -0
- data/lib/qualitysmith_extensions/test/assert_exception.rb +66 -0
- data/lib/qualitysmith_extensions/test/assert_includes.rb +36 -0
- data/lib/qualitysmith_extensions/test/assert_user_error.rb +37 -0
- data/lib/qualitysmith_extensions/test/difference_highlighting.rb +323 -0
- data/lib/qualitysmith_extensions/time/all.rb +2 -0
- data/lib/qualitysmith_extensions/time/deprecated.rb +31 -0
- data/test/all.rb +16 -0
- metadata +148 -0
@@ -0,0 +1,55 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Yes!
|
6
|
+
# Developer notes::
|
7
|
+
#++
|
8
|
+
|
9
|
+
require 'rubygems'
|
10
|
+
require 'facets/core/symbol/to_proc'
|
11
|
+
|
12
|
+
class Module
|
13
|
+
# Very similar to Facets' +Module#nesting+, but, whereas +nesting+ will break <tt>A::B</tt> into an array of _constants_ represting nesting
|
14
|
+
# (<tt>[A, A::B]</tt>), this method will split it into an array of _symbols_: <tt>[:A, :B]</tt>.
|
15
|
+
#
|
16
|
+
# Note that the second element in this array, <tt>:B</tt>, is _not_ fully qualified, so you could not do a <tt>const_get</tt>
|
17
|
+
# on that symbol.
|
18
|
+
def split
|
19
|
+
name.split(/::/).map(&:to_sym)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Like Module#split, only this operates on a string/symbol. Useful for when you don't want to or can't actually instantiate
|
23
|
+
# the module represented by the symbol.
|
24
|
+
def self.split_name(name)
|
25
|
+
name.to_s.split(/::/).map(&:to_sym)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
# _____ _
|
31
|
+
# |_ _|__ ___| |_
|
32
|
+
# | |/ _ \/ __| __|
|
33
|
+
# | | __/\__ \ |_
|
34
|
+
# |_|\___||___/\__|
|
35
|
+
#
|
36
|
+
=begin test
|
37
|
+
require 'test/unit'
|
38
|
+
|
39
|
+
module A
|
40
|
+
module B
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
class TheTest < Test::Unit::TestCase
|
45
|
+
def test_A
|
46
|
+
assert_equal [:A], A.split
|
47
|
+
end
|
48
|
+
def test_A_B
|
49
|
+
assert_equal [:A, :B], A::B.split
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
=end
|
54
|
+
|
55
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Not sure. Who really cares about Months anyway?
|
6
|
+
# Developer notes::
|
7
|
+
# * Compare with http://rubyforge.org/projects/dateutils/. Merge/abandon?
|
8
|
+
#++
|
9
|
+
|
10
|
+
require "rubygems"
|
11
|
+
require "active_support"
|
12
|
+
require File.dirname(__FILE__) + "/date/all"
|
13
|
+
|
14
|
+
class Month
|
15
|
+
include Comparable
|
16
|
+
attr_reader :year, :month
|
17
|
+
|
18
|
+
def initialize(year, month)
|
19
|
+
@year = year
|
20
|
+
@month = month
|
21
|
+
end
|
22
|
+
|
23
|
+
def succ
|
24
|
+
(to_date >> 1).to_month
|
25
|
+
end
|
26
|
+
|
27
|
+
def to_date
|
28
|
+
Date.new(year, month)
|
29
|
+
end
|
30
|
+
|
31
|
+
def <=>(other)
|
32
|
+
#puts "#{self.inspect} <=> #{other.inspect}"
|
33
|
+
return self.to_date <=> other.to_date
|
34
|
+
end
|
35
|
+
|
36
|
+
def inspect
|
37
|
+
"#{@year}-#{@month}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
# _____ _
|
43
|
+
# |_ _|__ ___| |_
|
44
|
+
# | |/ _ \/ __| __|
|
45
|
+
# | | __/\__ \ |_
|
46
|
+
# |_|\___||___/\__|
|
47
|
+
#
|
48
|
+
=begin test
|
49
|
+
class TheTest < Test::Unit::TestCase
|
50
|
+
def test_months_range
|
51
|
+
range = Month.new(2006, 6)..Month.new(2006, 9)
|
52
|
+
assert_equal Range.new(Month.new(2006, 6), Month.new(2006, 9)), range
|
53
|
+
|
54
|
+
range = Date.new(2006, 6, 1).to_month..Date.new(2006, 9, 3).to_month
|
55
|
+
assert_equal Range.new(Month.new(2006, 6), Month.new(2006, 9)), range
|
56
|
+
|
57
|
+
assert_equal [
|
58
|
+
Month.new(2006, 6),
|
59
|
+
Month.new(2006, 7),
|
60
|
+
Month.new(2006, 8),
|
61
|
+
Month.new(2006, 9)
|
62
|
+
],
|
63
|
+
range.to_a
|
64
|
+
end
|
65
|
+
end
|
66
|
+
=end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Nolan Cafferky
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Yes.
|
6
|
+
#++
|
7
|
+
|
8
|
+
require 'thread'
|
9
|
+
class Mutex
|
10
|
+
# Acts like synchronize, except that if the lock cannot be acquired immediately,
|
11
|
+
# the program continues without executing the given block.
|
12
|
+
#
|
13
|
+
# ==Example:
|
14
|
+
#
|
15
|
+
# mutex = Mutex.new
|
16
|
+
# # ...
|
17
|
+
# mutex.if_available do
|
18
|
+
# # Some process that we only want one thread to be running at a time,
|
19
|
+
# # and we don't mind skipping if some other thread is already doing it.
|
20
|
+
# loop do
|
21
|
+
# notify_mechanics if danger_to_teh_manifold!
|
22
|
+
# sleep 60
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
def if_available
|
26
|
+
if try_lock
|
27
|
+
begin
|
28
|
+
yield
|
29
|
+
ensure
|
30
|
+
unlock
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
|
38
|
+
# _____ _
|
39
|
+
# |_ _|__ ___| |_
|
40
|
+
# | |/ _ \/ __| __|
|
41
|
+
# | | __/\__ \ |_
|
42
|
+
# |_|\___||___/\__|
|
43
|
+
#
|
44
|
+
=begin test
|
45
|
+
class TheTest < Test::Unit::TestCase
|
46
|
+
def setup
|
47
|
+
@semaphore = Mutex.new
|
48
|
+
end
|
49
|
+
|
50
|
+
def teardown
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_executes_if_lock_is_available
|
54
|
+
i_ran = nil
|
55
|
+
|
56
|
+
@semaphore.if_available do
|
57
|
+
i_ran = true
|
58
|
+
end
|
59
|
+
|
60
|
+
assert i_ran
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_continues_if_lock_is_unavailable
|
64
|
+
i_ran = nil
|
65
|
+
|
66
|
+
@semaphore.lock
|
67
|
+
@semaphore.if_available do
|
68
|
+
i_ran = true
|
69
|
+
end
|
70
|
+
@semaphore.unlock
|
71
|
+
|
72
|
+
assert_nil i_ran
|
73
|
+
end
|
74
|
+
end
|
75
|
+
=end
|
@@ -0,0 +1,257 @@
|
|
1
|
+
#--
|
2
|
+
# Author:: Tyler Rick
|
3
|
+
# Copyright:: Copyright (c) 2007 QualitySmith, Inc.
|
4
|
+
# License:: Ruby License
|
5
|
+
# Submit to Facets?:: Yes.
|
6
|
+
# Developer notes::
|
7
|
+
# * May not have taken every single case into consideration. Needs a bit more testing.
|
8
|
+
# * public/private/protected?
|
9
|
+
# * Rename to origin_of_method (or source_of_method)?? Since strictly speaking it may return a method that is not from any *ancestors* but is from the Class class.
|
10
|
+
#++
|
11
|
+
|
12
|
+
require 'rubygems'
|
13
|
+
require 'qualitysmith_extensions/module/class_methods'
|
14
|
+
require 'qualitysmith_extensions/module/ancestry_of_instance_method'
|
15
|
+
|
16
|
+
|
17
|
+
class Object
|
18
|
+
# Returns the module/class which defined the given method. If more than one module/class defined the method, returns the _closest_
|
19
|
+
# ancestor to have defined it (would be +self+ if it is defined in +self+).
|
20
|
+
#
|
21
|
+
# This is (as far as I know -- patches welcome) <b>the method that would _would_ be called if you actually called the method</b>.
|
22
|
+
# So if you override
|
23
|
+
#
|
24
|
+
# It does this by first checking
|
25
|
+
# searching the methods defined in each ancestor in turn (in the order that <tt>self.ancestors</tt> returns them) and
|
26
|
+
# returning the first module/class that satisfies the search.
|
27
|
+
#
|
28
|
+
# This looks at the results of <tt>methods</tt>, which means that if you call this on a module/class, it will _not_ return
|
29
|
+
# any instance methods, only _class_ methods.
|
30
|
+
#
|
31
|
+
# class Base
|
32
|
+
# def self.it; end
|
33
|
+
# end
|
34
|
+
# class SubWithIt < Base
|
35
|
+
# def self.it; end
|
36
|
+
# end
|
37
|
+
# class SubWithoutIt < Base
|
38
|
+
# end
|
39
|
+
# SubWithIt.ancestry_of_instance_method(:it) # => SubWithIt # (Stops with self)
|
40
|
+
# SubWithoutIt.ancestry_of_instance_method(:it) # => Base # (Goes one step up the ancestry tree)
|
41
|
+
#
|
42
|
+
# If you call this on an object that is _not_ a module or a class (in other words, if you call it on an _instance_ of some
|
43
|
+
# class), then it will assume you actually want to know about an _instance_ method defined in self.class or one of the
|
44
|
+
# ancestors of self.class. (Since non-modules don't even technically have the concept of _ancestors_.) Therefore, this:
|
45
|
+
# class Klass
|
46
|
+
# def it; end
|
47
|
+
# end
|
48
|
+
# o = Klass.new
|
49
|
+
# o.ancestry_of_method(:it) # => Klass
|
50
|
+
# is really just a shorthand way of doing:
|
51
|
+
# o.class.ancestry_of_instance_method(:it) # => Klass
|
52
|
+
#
|
53
|
+
# If the method is a singleton method of +self+, it will return +self+:
|
54
|
+
# class << (foo = SubWithIt.new)
|
55
|
+
# def it; end
|
56
|
+
# end
|
57
|
+
# foo.ancestry_of_method(:it) # => #<SubWithIt:0xb7e5614c>
|
58
|
+
#
|
59
|
+
# Returns nil if it cannot be found in self or in any ancestor.
|
60
|
+
def ancestry_of_method(method_name)
|
61
|
+
method_name = method_name.to_s
|
62
|
+
(self if self.methods(false).include?(method_name)) \
|
63
|
+
||
|
64
|
+
if self.is_a?(Module)
|
65
|
+
self.ancestors.find do |ancestor|
|
66
|
+
ancestor.methods(false).include? method_name
|
67
|
+
end or
|
68
|
+
# The above search does not take into account *instance* methods provided by Class, Module, or Kernel.
|
69
|
+
# Remember that ancestors and instances/class/superclass are different concepts, and that although classes/modules
|
70
|
+
# do not have Class or Module as an "ancestor", they are still *instances* of Module or Class (which is a subclass of module).
|
71
|
+
# self.ancestors does NOT include Class or Module, and yet we're still able to "inherit" instance methods from Class or Module.
|
72
|
+
# So we have to do this extra search in case the method came from one of the instance methods of Class or Module or Kernel
|
73
|
+
# (are there any other cases I'm missing?).
|
74
|
+
begin
|
75
|
+
# self.class.ancestors is usually [Class, Module, Object, PP::ObjectMixin, Kernel]
|
76
|
+
self.class.ancestors.find do |ancestor|
|
77
|
+
ancestor.instance_methods(false).include? method_name
|
78
|
+
# || ancestor.private_instance_method_defined?( method_name.to_sym )
|
79
|
+
end
|
80
|
+
end
|
81
|
+
else
|
82
|
+
self.class.ancestry_of_instance_method(method_name)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
# _____ _
|
88
|
+
# |_ _|__ ___| |_
|
89
|
+
# | |/ _ \/ __| __|
|
90
|
+
# | | __/\__ \ |_
|
91
|
+
# |_|\___||___/\__|
|
92
|
+
#
|
93
|
+
=begin test
|
94
|
+
require 'test/unit'
|
95
|
+
require 'rubygems'
|
96
|
+
require 'qualitysmith_extensions/test/assert_anything'
|
97
|
+
|
98
|
+
class Base
|
99
|
+
def it; end
|
100
|
+
end
|
101
|
+
class SubWithIt < Base
|
102
|
+
def it; end
|
103
|
+
end
|
104
|
+
class SubWithoutIt < Base
|
105
|
+
end
|
106
|
+
|
107
|
+
class << ($with_singleton_method = SubWithIt.new)
|
108
|
+
def it; end
|
109
|
+
end
|
110
|
+
|
111
|
+
class ClassyBase
|
112
|
+
def self.classy_it; end
|
113
|
+
end
|
114
|
+
class ClassySubWithIt < ClassyBase
|
115
|
+
def self.classy_it; end
|
116
|
+
end
|
117
|
+
class ClassySubWithoutIt < ClassyBase
|
118
|
+
end
|
119
|
+
|
120
|
+
class << ($class_with_singleton_method = ClassySubWithIt)
|
121
|
+
def classy_it; end
|
122
|
+
end
|
123
|
+
|
124
|
+
class ATest < Test::Unit::TestCase
|
125
|
+
def test_non_existent_method
|
126
|
+
assert_equal nil, Base.ancestry_of_method(:not_a_method)
|
127
|
+
assert_equal nil, Base.ancestry_of_instance_method(:not_a_method)
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
class InstanceMethodsTest < Test::Unit::TestCase
|
132
|
+
def test_1
|
133
|
+
assert_equal [SubWithoutIt, Base, Object, PP::ObjectMixin, Kernel], SubWithoutIt.ancestors
|
134
|
+
assert_equal Base, Base.ancestry_of_instance_method(:it)
|
135
|
+
assert_equal Base, SubWithoutIt.ancestry_of_instance_method(:it)
|
136
|
+
assert_equal SubWithIt, SubWithIt.ancestry_of_instance_method(:it)
|
137
|
+
assert_equal SubWithIt, SubWithIt.ancestry_of_instance_method(:it)
|
138
|
+
end
|
139
|
+
def test_ancestry_of_method__falls_back_to_using__ancestry_of_instance_method__if_is_an_instance
|
140
|
+
assert_include? Base.instance_methods, :it.to_s
|
141
|
+
assert_equal Base, Base.new.ancestry_of_method(:it)
|
142
|
+
assert_equal Base, SubWithoutIt.new.ancestry_of_method(:it)
|
143
|
+
assert_equal SubWithIt, SubWithIt.new.ancestry_of_method(:it)
|
144
|
+
|
145
|
+
# In other words, it's a *shortcut*. See?:
|
146
|
+
o = SubWithIt.new
|
147
|
+
assert_equal o.class.ancestry_of_instance_method(:it), o.ancestry_of_method(:it)
|
148
|
+
end
|
149
|
+
def test_singleton_method
|
150
|
+
assert_equal $with_singleton_method, $with_singleton_method.ancestry_of_method(:it)
|
151
|
+
end
|
152
|
+
def test_that_you_cant_use_ancestry_of_instance_method_for_instances
|
153
|
+
# undefined method `ancestors' for #<SubWithIt:0xb7e0fbc0>
|
154
|
+
assert_raise(NoMethodError) { SubWithIt.new.ancestry_of_instance_method(:it) }
|
155
|
+
assert_raise(NoMethodError) { $with_singleton_method.ancestry_of_instance_method(:it) }
|
156
|
+
end
|
157
|
+
end
|
158
|
+
class ClassMethodsTest < Test::Unit::TestCase
|
159
|
+
def test_ancestry_of_method_doesnt_return_instance_methods
|
160
|
+
assert_include? Base.instance_methods, :it.to_s
|
161
|
+
assert_not_include? Base.methods, :it.to_s
|
162
|
+
|
163
|
+
assert_equal [SubWithoutIt, Base, Object, PP::ObjectMixin, Kernel], SubWithoutIt.ancestors
|
164
|
+
|
165
|
+
assert_equal nil, Base.ancestry_of_method(:it)
|
166
|
+
assert_equal nil, SubWithoutIt.ancestry_of_method(:it)
|
167
|
+
assert_equal nil, SubWithIt.ancestry_of_method(:it)
|
168
|
+
end
|
169
|
+
def test_ancestry_of_method_does_return_class_methods
|
170
|
+
assert_include? ClassyBase.methods, :classy_it.to_s
|
171
|
+
assert_not_include? ClassyBase.instance_methods, :classy_it.to_s
|
172
|
+
|
173
|
+
assert_equal [ClassySubWithoutIt, ClassyBase, Object, PP::ObjectMixin, Kernel], ClassySubWithoutIt.ancestors
|
174
|
+
|
175
|
+
assert_equal ClassyBase, ClassySubWithoutIt.ancestry_of_method(:classy_it)
|
176
|
+
assert_equal ClassyBase, ClassyBase.ancestry_of_method(:classy_it)
|
177
|
+
assert_equal ClassySubWithIt, ClassySubWithIt.ancestry_of_method(:classy_it)
|
178
|
+
end
|
179
|
+
def test_singleton_method
|
180
|
+
assert_equal $class_with_singleton_method, $class_with_singleton_method.ancestry_of_method(:classy_it)
|
181
|
+
assert_equal nil, $class_with_singleton_method.ancestry_of_instance_method(:classy_it)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
class BuiltInMethodsTest < Test::Unit::TestCase
|
186
|
+
def test_1
|
187
|
+
assert_equal [Object, PP::ObjectMixin, Kernel], Object.ancestors
|
188
|
+
assert_equal Kernel, Object.ancestry_of_method(:binding)
|
189
|
+
assert_equal Kernel, Object.ancestry_of_method(:require)
|
190
|
+
assert_equal Kernel, Object.ancestry_of_method(:proc)
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_looks_at_Class_Module_and_Kernel_instance_methods_if_necessary
|
194
|
+
# Sometimes it inherits a method NOT from any of its ancestors, but from Class (one of the *instance methods* of Class).
|
195
|
+
|
196
|
+
# (Class is *not* an ancestor of Object and yet that appears to be where these methods come from.)
|
197
|
+
assert_not_include? Object.ancestors, Class
|
198
|
+
assert_equal Class, Object.class
|
199
|
+
[
|
200
|
+
:superclass,
|
201
|
+
:new,
|
202
|
+
:allocate,
|
203
|
+
].each do |method|
|
204
|
+
assert_ancestry_of_method_is Object, method, Class
|
205
|
+
end
|
206
|
+
|
207
|
+
# superclass (and others) comes from Class, but instance_variable_get (and others) come from Module or Kernel.
|
208
|
+
# (See below for more examples)
|
209
|
+
assert_equal true, Class.instance_methods(false).include?('superclass')
|
210
|
+
assert_equal false, Class.instance_methods(false).include?('instance_variable_get')
|
211
|
+
assert_equal true, Kernel.instance_methods(false).include?('instance_variable_get')
|
212
|
+
|
213
|
+
# (Module is *not* an ancestor of Object and yet that appears to be where these methods come from.)
|
214
|
+
assert_not_include? Object.ancestors, Module
|
215
|
+
[
|
216
|
+
:instance_methods,
|
217
|
+
:included_modules,
|
218
|
+
:constants,
|
219
|
+
:ancestors,
|
220
|
+
:public_method_defined?,
|
221
|
+
].each do |method|
|
222
|
+
assert_equal Module, Object.ancestry_of_method(method), "Failed for #{method}"
|
223
|
+
end
|
224
|
+
|
225
|
+
# (Kernel *is* an ancestor of Object, but these methods come from Kernel.instance_methods and *not* from Kernel.methods.)
|
226
|
+
assert_include? Object.ancestors, Kernel
|
227
|
+
[
|
228
|
+
:puts,
|
229
|
+
:system,
|
230
|
+
:inspect,
|
231
|
+
:send,
|
232
|
+
:private_methods,
|
233
|
+
:instance_variable_get,
|
234
|
+
].each do |method|
|
235
|
+
assert_equal Kernel, Object.ancestry_of_method(method)
|
236
|
+
end
|
237
|
+
|
238
|
+
# proc apparently comes from *class method* Kernel.proc,
|
239
|
+
# while instance_variable_get comes from *intance method* Kernel.instance_variable_get
|
240
|
+
assert_equal true, Kernel.class_methods(false).include?('proc')
|
241
|
+
assert_equal false, Kernel.instance_methods(false).include?('proc')
|
242
|
+
assert_equal false, Kernel.class_methods(false).include?('instance_variable_get')
|
243
|
+
assert_equal true, Kernel.instance_methods(false).include?('instance_variable_get')
|
244
|
+
|
245
|
+
# Where does this method come from then???
|
246
|
+
[
|
247
|
+
:remove_class_variable,
|
248
|
+
:remove_const,
|
249
|
+
:remove_method,
|
250
|
+
:undef_method,
|
251
|
+
:nesting,
|
252
|
+
].each do |method|
|
253
|
+
assert_equal nil, Object.ancestry_of_method(method)
|
254
|
+
end
|
255
|
+
end
|
256
|
+
end
|
257
|
+
=end
|