gorillib 0.0.7 → 0.0.8
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.
- data/.gitignore +49 -0
- data/.rspec +2 -0
- data/CHANGELOG.textile +49 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +36 -0
- data/README.textile +6 -1
- data/Rakefile +42 -9
- data/VERSION +1 -1
- data/gorillib.gemspec +110 -73
- data/lib/gorillib/array/deep_compact.rb +3 -12
- data/lib/gorillib/datetime/#flat.rb# +30 -0
- data/lib/gorillib/datetime/flat.rb +2 -10
- data/lib/gorillib/datetime/parse.rb +3 -1
- data/lib/gorillib/enumerable/sum.rb +37 -35
- data/lib/gorillib/hash/deep_compact.rb +3 -13
- data/lib/gorillib/logger/log.rb +1 -1
- data/lib/gorillib/numeric/clamp.rb +9 -0
- data/lib/gorillib/object/try.rb +10 -0
- data/lib/gorillib/object/try_dup.rb +44 -0
- data/lib/gorillib/string/human.rb +1 -3
- data/lib/gorillib/string/truncate.rb +2 -2
- data/spec/array/compact_blank_spec.rb +37 -0
- data/spec/array/extract_options_spec.rb +47 -0
- data/spec/datetime/flat_spec.rb +38 -0
- data/spec/datetime/parse_spec.rb +48 -0
- data/spec/enumerable/sum_spec.rb +56 -0
- data/spec/hash/compact_spec.rb +70 -0
- data/spec/hash/deep_compact_spec.rb +37 -0
- data/spec/hash/deep_merge_spec.rb +117 -0
- data/spec/hash/keys_spec.rb +111 -0
- data/spec/hash/reverse_merge_spec.rb +25 -0
- data/spec/hash/slice_spec.rb +43 -0
- data/spec/hash/zip_spec.rb +31 -0
- data/spec/logger/log_spec.rb +31 -0
- data/spec/metaprogramming/aliasing_spec.rb +177 -0
- data/spec/metaprogramming/cattr_accessor_spec.rb +41 -0
- data/spec/metaprogramming/class_attribute_spec.rb +75 -0
- data/spec/metaprogramming/delegation_spec.rb +166 -0
- data/spec/metaprogramming/mattr_accessor_spec.rb +44 -0
- data/spec/metaprogramming/singleton_class_spec.rb +9 -0
- data/spec/{numeric_spec.rb → numeric/clamp_spec.rb} +2 -2
- data/spec/object/blank_spec.rb +93 -0
- data/spec/object/try_dup_spec.rb +47 -0
- data/spec/object/try_spec.rb +20 -0
- data/spec/spec_helper.rb +13 -8
- data/spec/string/constantize_spec.rb +56 -0
- data/spec/string/human_spec.rb +66 -0
- data/spec/string/inflections_spec.rb +74 -0
- data/{test → spec}/string/inflector_test_cases.rb +0 -0
- data/spec/string/truncate_spec.rb +42 -0
- data/spec/support/kcode_test_helper.rb +16 -0
- metadata +209 -96
- data/.document +0 -5
- data/fiddle/hubahuba.rb +0 -62
- data/spec/blank_spec.rb +0 -86
- data/spec/deep_compact_spec.rb +0 -36
- data/spec/gorillib_spec.rb +0 -7
- data/spec/rcov.opts +0 -6
- data/spec/spec.opts +0 -4
- data/spec/spec_tasks.rake +0 -15
- data/test/abstract_unit.rb +0 -25
- data/test/array/compact_blank_test.rb +0 -33
- data/test/array/extract_options_test.rb +0 -39
- data/test/datetime/flat_test.rb +0 -0
- data/test/datetime/parse_test.rb +0 -0
- data/test/enumerable/sum_test.rb +0 -50
- data/test/hash/compact_test.rb +0 -38
- data/test/hash/deep_merge_test.rb +0 -30
- data/test/hash/keys_test.rb +0 -110
- data/test/hash/reverse_merge_test.rb +0 -20
- data/test/hash/slice_test.rb +0 -47
- data/test/hash/zip_test.rb +0 -0
- data/test/logger/log_test.rb +0 -0
- data/test/metaprogramming/aliasing_test.rb +0 -188
- data/test/metaprogramming/cattr_accessor_test.rb +0 -38
- data/test/metaprogramming/class_attribute_test.rb +0 -73
- data/test/metaprogramming/delegation_test.rb +0 -166
- data/test/metaprogramming/mattr_accessor_test.rb +0 -40
- data/test/metaprogramming/singleton_class_test.rb +0 -9
- data/test/object/blank_test.rb +0 -22
- data/test/string/constantize_test.rb +0 -30
- data/test/string/human_test.rb +0 -65
- data/test/string/inflections_test.rb +0 -57
- data/test/string/truncate_test.rb +0 -37
@@ -1,39 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/array/extract_options'
|
3
|
-
|
4
|
-
class ArrayExtractOptionsTests < Test::Unit::TestCase
|
5
|
-
class HashSubclass < Hash
|
6
|
-
end
|
7
|
-
|
8
|
-
class ExtractableHashSubclass < Hash
|
9
|
-
def extractable_options?
|
10
|
-
true
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_extract_options
|
15
|
-
assert_equal({}, [].extract_options!)
|
16
|
-
assert_equal({}, [1].extract_options!)
|
17
|
-
assert_equal({:a=>:b}, [{:a=>:b}].extract_options!)
|
18
|
-
assert_equal({:a=>:b}, [1, {:a=>:b}].extract_options!)
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_extract_options_doesnt_extract_hash_subclasses
|
22
|
-
hash = HashSubclass.new
|
23
|
-
hash[:foo] = 1
|
24
|
-
array = [hash]
|
25
|
-
options = array.extract_options!
|
26
|
-
assert_equal({}, options)
|
27
|
-
assert_equal [hash], array
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_extract_options_extracts_extractable_subclass
|
31
|
-
hash = ExtractableHashSubclass.new
|
32
|
-
hash[:foo] = 1
|
33
|
-
array = [hash]
|
34
|
-
options = array.extract_options!
|
35
|
-
assert_equal({:foo => 1}, options)
|
36
|
-
assert_equal [], array
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
data/test/datetime/flat_test.rb
DELETED
File without changes
|
data/test/datetime/parse_test.rb
DELETED
File without changes
|
data/test/enumerable/sum_test.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/enumerable/sum'
|
3
|
-
|
4
|
-
Payment = Struct.new(:price)
|
5
|
-
class SummablePayment < Payment
|
6
|
-
def +(p) self.class.new(price + p.price) end
|
7
|
-
end
|
8
|
-
|
9
|
-
class EnumerableTests < Test::Unit::TestCase
|
10
|
-
def test_sums
|
11
|
-
assert_equal 30, [5, 15, 10].sum
|
12
|
-
assert_equal 30, [5, 15, 10].sum { |i| i }
|
13
|
-
|
14
|
-
assert_equal 'abc', %w(a b c).sum
|
15
|
-
assert_equal 'abc', %w(a b c).sum { |i| i }
|
16
|
-
|
17
|
-
payments = [ Payment.new(5), Payment.new(15), Payment.new(10) ]
|
18
|
-
assert_equal 30, payments.sum(&:price)
|
19
|
-
assert_equal 60, payments.sum { |p| p.price * 2 }
|
20
|
-
|
21
|
-
payments = [ SummablePayment.new(5), SummablePayment.new(15) ]
|
22
|
-
assert_equal SummablePayment.new(20), payments.sum
|
23
|
-
assert_equal SummablePayment.new(20), payments.sum { |p| p }
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_nil_sums
|
27
|
-
expected_raise = TypeError
|
28
|
-
|
29
|
-
assert_raise(expected_raise) { [5, 15, nil].sum }
|
30
|
-
|
31
|
-
payments = [ Payment.new(5), Payment.new(15), Payment.new(10), Payment.new(nil) ]
|
32
|
-
assert_raise(expected_raise) { payments.sum(&:price) }
|
33
|
-
|
34
|
-
assert_equal 60, payments.sum { |p| p.price.to_i * 2 }
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_empty_sums
|
38
|
-
assert_equal 0, [].sum
|
39
|
-
assert_equal 0, [].sum { |i| i }
|
40
|
-
assert_equal Payment.new(0), [].sum(Payment.new(0))
|
41
|
-
end
|
42
|
-
|
43
|
-
def test_enumerable_sums
|
44
|
-
assert_equal 20, (1..4).sum { |i| i * 2 }
|
45
|
-
assert_equal 10, (1..4).sum
|
46
|
-
assert_equal 10, (1..4.5).sum
|
47
|
-
assert_equal 6, (1...4).sum
|
48
|
-
assert_equal 'abc', ('a'..'c').sum
|
49
|
-
end
|
50
|
-
end
|
data/test/hash/compact_test.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/hash/compact'
|
3
|
-
|
4
|
-
class HashCompactTests < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_compact_blank_with_empty
|
7
|
-
[ { 1 => nil}, { 1 => nil, 2 => false, 3 => {}, 4 => ""} ].each do |hsh|
|
8
|
-
assert_equal({}, hsh.compact_blank)
|
9
|
-
assert_not_equal(0, hsh.length)
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_compact_blank_with_empty
|
14
|
-
[ { 1 => nil}, { 1 => nil, 2 => false, 3 => {}, 4 => ""} ].each do |hsh|
|
15
|
-
assert_equal({}, hsh.compact_blank!)
|
16
|
-
assert_equal(0, hsh.length)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_compact_blank_with_full
|
21
|
-
assert_equal(
|
22
|
-
{ nil => 2 },
|
23
|
-
{ 1 => nil, nil => 2 }.compact_blank )
|
24
|
-
assert_equal(
|
25
|
-
{ 2 => :val_2, 4 => :val_4 },
|
26
|
-
{ 1 => nil, 2 => :val_2, 3 => {}, 4 => :val_4}.compact_blank )
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_compact_blank_bang_with_full
|
30
|
-
assert_equal(
|
31
|
-
{ nil => 2 },
|
32
|
-
{ 1 => nil, nil => 2 }.compact_blank! )
|
33
|
-
assert_equal(
|
34
|
-
{ 2 => :val_2, 4 => :val_4 },
|
35
|
-
{ 1 => nil, 2 => :val_2, 3 => {}, 4 => :val_4}.compact_blank! )
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/hash/deep_merge'
|
3
|
-
|
4
|
-
class HashDeepMergeTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_deep_merge
|
7
|
-
hash_1 = { :a => "a", :b => "b", :c => { :c1 => "c1", :c2 => "c2", :c3 => { :d1 => "d1" } } }
|
8
|
-
hash_2 = { :a => 1, :c => { :c1 => 2, :c3 => { :d2 => "d2" } } }
|
9
|
-
expected = { :a => 1, :b => "b", :c => { :c1 => 2, :c2 => "c2", :c3 => { :d1 => "d1", :d2 => "d2" } } }
|
10
|
-
assert_equal expected, hash_1.deep_merge(hash_2)
|
11
|
-
|
12
|
-
hash_1.deep_merge!(hash_2)
|
13
|
-
assert_equal expected, hash_1
|
14
|
-
end
|
15
|
-
|
16
|
-
# def test_deep_dup
|
17
|
-
# hash = { :a => { :b => 'b' } }
|
18
|
-
# dup = hash.deep_dup
|
19
|
-
# dup[:a][:c] = 'c'
|
20
|
-
# assert_equal nil, hash[:a][:c]
|
21
|
-
# assert_equal 'c', dup[:a][:c]
|
22
|
-
# end
|
23
|
-
#
|
24
|
-
# def test_deep_dup_initialize
|
25
|
-
# zero_hash = Hash.new 0
|
26
|
-
# hash = { :a => zero_hash }
|
27
|
-
# dup = hash.deep_dup
|
28
|
-
# assert_equal 0, dup[:a][44]
|
29
|
-
# end
|
30
|
-
end
|
data/test/hash/keys_test.rb
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/hash/keys'
|
3
|
-
|
4
|
-
class HashSymbolizeKeysTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
class IndifferentHash < HashWithIndifferentAccess
|
7
|
-
end
|
8
|
-
|
9
|
-
class SubclassingArray < Array
|
10
|
-
end
|
11
|
-
|
12
|
-
class SubclassingHash < Hash
|
13
|
-
end
|
14
|
-
|
15
|
-
|
16
|
-
def setup
|
17
|
-
@strings = { 'a' => 1, 'b' => 2 }
|
18
|
-
@symbols = { :a => 1, :b => 2 }
|
19
|
-
@mixed = { :a => 1, 'b' => 2 }
|
20
|
-
@fixnums = { 0 => 1, 1 => 2 }
|
21
|
-
if RUBY_VERSION < '1.9.0'
|
22
|
-
@illegal_symbols = { "\0" => 1, "" => 2, [] => 3 }
|
23
|
-
else
|
24
|
-
@illegal_symbols = { [] => 3 }
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_methods
|
29
|
-
h = {}
|
30
|
-
assert_respond_to h, :symbolize_keys
|
31
|
-
assert_respond_to h, :symbolize_keys!
|
32
|
-
assert_respond_to h, :stringify_keys
|
33
|
-
assert_respond_to h, :stringify_keys!
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_symbolize_keys
|
37
|
-
assert_equal @symbols, @symbols.symbolize_keys
|
38
|
-
assert_equal @symbols, @strings.symbolize_keys
|
39
|
-
assert_equal @symbols, @mixed.symbolize_keys
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_symbolize_keys!
|
43
|
-
assert_equal @symbols, @symbols.dup.symbolize_keys!
|
44
|
-
assert_equal @symbols, @strings.dup.symbolize_keys!
|
45
|
-
assert_equal @symbols, @mixed.dup.symbolize_keys!
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_symbolize_keys_preserves_keys_that_cant_be_symbolized
|
49
|
-
assert_equal @illegal_symbols, @illegal_symbols.symbolize_keys
|
50
|
-
assert_equal @illegal_symbols, @illegal_symbols.dup.symbolize_keys!
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_symbolize_keys_preserves_fixnum_keys
|
54
|
-
assert_equal @fixnums, @fixnums.symbolize_keys
|
55
|
-
assert_equal @fixnums, @fixnums.dup.symbolize_keys!
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_stringify_keys
|
59
|
-
assert_equal @strings, @symbols.stringify_keys
|
60
|
-
assert_equal @strings, @strings.stringify_keys
|
61
|
-
assert_equal @strings, @mixed.stringify_keys
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_stringify_keys!
|
65
|
-
assert_equal @strings, @symbols.dup.stringify_keys!
|
66
|
-
assert_equal @strings, @strings.dup.stringify_keys!
|
67
|
-
assert_equal @strings, @mixed.dup.stringify_keys!
|
68
|
-
end
|
69
|
-
|
70
|
-
def test_symbolize_keys_for_hash_with_indifferent_access
|
71
|
-
assert_instance_of Hash, @symbols.with_indifferent_access.symbolize_keys
|
72
|
-
assert_equal @symbols, @symbols.with_indifferent_access.symbolize_keys
|
73
|
-
assert_equal @symbols, @strings.with_indifferent_access.symbolize_keys
|
74
|
-
assert_equal @symbols, @mixed.with_indifferent_access.symbolize_keys
|
75
|
-
end
|
76
|
-
|
77
|
-
def test_symbolize_keys_bang_for_hash_with_indifferent_access
|
78
|
-
assert_raise(NoMethodError) { @symbols.with_indifferent_access.dup.symbolize_keys! }
|
79
|
-
assert_raise(NoMethodError) { @strings.with_indifferent_access.dup.symbolize_keys! }
|
80
|
-
assert_raise(NoMethodError) { @mixed.with_indifferent_access.dup.symbolize_keys! }
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_symbolize_keys_preserves_keys_that_cant_be_symbolized_for_hash_with_indifferent_access
|
84
|
-
assert_equal @illegal_symbols, @illegal_symbols.with_indifferent_access.symbolize_keys
|
85
|
-
assert_raise(NoMethodError) { @illegal_symbols.with_indifferent_access.dup.symbolize_keys! }
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_symbolize_keys_preserves_fixnum_keys_for_hash_with_indifferent_access
|
89
|
-
assert_equal @fixnums, @fixnums.with_indifferent_access.symbolize_keys
|
90
|
-
assert_raise(NoMethodError) { @fixnums.with_indifferent_access.dup.symbolize_keys! }
|
91
|
-
end
|
92
|
-
|
93
|
-
def test_hash_subclass
|
94
|
-
flash = { "foo" => SubclassingHash.new.tap { |h| h["bar"] = "baz" } }.with_indifferent_access
|
95
|
-
assert_kind_of SubclassingHash, flash["foo"]
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_assert_valid_keys
|
99
|
-
assert_nothing_raised do
|
100
|
-
{ :failure => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
|
101
|
-
{ :failure => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
|
102
|
-
end
|
103
|
-
|
104
|
-
assert_raise(ArgumentError, "Unknown key: failore") do
|
105
|
-
{ :failore => "stuff", :funny => "business" }.assert_valid_keys([ :failure, :funny ])
|
106
|
-
{ :failore => "stuff", :funny => "business" }.assert_valid_keys(:failure, :funny)
|
107
|
-
end
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/hash/reverse_merge'
|
3
|
-
|
4
|
-
class HashReverseMergeTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_reverse_merge
|
7
|
-
defaults = { :a => "x", :b => "y", :c => 10 }.freeze
|
8
|
-
options = { :a => 1, :b => 2 }
|
9
|
-
expected = { :a => 1, :b => 2, :c => 10 }
|
10
|
-
|
11
|
-
# Should merge defaults into options, creating a new hash.
|
12
|
-
assert_equal expected, options.reverse_merge(defaults)
|
13
|
-
assert_not_equal expected, options
|
14
|
-
|
15
|
-
# Should merge! defaults into options, replacing options.
|
16
|
-
merged = options.dup
|
17
|
-
assert_equal expected, merged.reverse_merge!(defaults)
|
18
|
-
assert_equal expected, merged
|
19
|
-
end
|
20
|
-
end
|
data/test/hash/slice_test.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/hash/slice'
|
3
|
-
|
4
|
-
class HashSliceTest < Test::Unit::TestCase
|
5
|
-
|
6
|
-
def test_slice
|
7
|
-
original = { :a => 'x', :b => 'y', :c => 10 }
|
8
|
-
expected = { :a => 'x', :b => 'y' }
|
9
|
-
|
10
|
-
# Should return a new hash with only the given keys.
|
11
|
-
assert_equal expected, original.slice(:a, :b)
|
12
|
-
assert_not_equal expected, original
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_slice_inplace
|
16
|
-
original = { :a => 'x', :b => 'y', :c => 10 }
|
17
|
-
expected = { :c => 10 }
|
18
|
-
|
19
|
-
# Should replace the hash with only the given keys.
|
20
|
-
assert_equal expected, original.slice!(:a, :b)
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_slice_with_an_array_key
|
24
|
-
original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
|
25
|
-
expected = { [:a, :b] => "an array key", :c => 10 }
|
26
|
-
|
27
|
-
# Should return a new hash with only the given keys when given an array key.
|
28
|
-
assert_equal expected, original.slice([:a, :b], :c)
|
29
|
-
assert_not_equal expected, original
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_slice_inplace_with_an_array_key
|
33
|
-
original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
|
34
|
-
expected = { :a => 'x', :b => 'y' }
|
35
|
-
|
36
|
-
# Should replace the hash with only the given keys when given an array key.
|
37
|
-
assert_equal expected, original.slice!([:a, :b], :c)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_slice_with_splatted_keys
|
41
|
-
original = { :a => 'x', :b => 'y', :c => 10, [:a, :b] => "an array key" }
|
42
|
-
expected = { :a => 'x', :b => "y" }
|
43
|
-
|
44
|
-
# Should grab each of the splatted keys.
|
45
|
-
assert_equal expected, original.slice(*[:a, :b])
|
46
|
-
end
|
47
|
-
end
|
data/test/hash/zip_test.rb
DELETED
File without changes
|
data/test/logger/log_test.rb
DELETED
File without changes
|
@@ -1,188 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/aliasing'
|
3
|
-
|
4
|
-
module BarMethodAliaser
|
5
|
-
def self.included(foo_class)
|
6
|
-
foo_class.class_eval do
|
7
|
-
include BarMethods
|
8
|
-
alias_method_chain :bar, :baz
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
module BarMethods
|
14
|
-
def bar_with_baz
|
15
|
-
bar_without_baz << '_with_baz'
|
16
|
-
end
|
17
|
-
|
18
|
-
def quux_with_baz!
|
19
|
-
quux_without_baz! << '_with_baz'
|
20
|
-
end
|
21
|
-
|
22
|
-
def quux_with_baz?
|
23
|
-
false
|
24
|
-
end
|
25
|
-
|
26
|
-
def quux_with_baz=(v)
|
27
|
-
send(:quux_without_baz=, v) << '_with_baz'
|
28
|
-
end
|
29
|
-
|
30
|
-
def duck_with_orange
|
31
|
-
duck_without_orange << '_with_orange'
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
class MethodAliasingTest < Test::Unit::TestCase
|
36
|
-
def setup
|
37
|
-
Object.const_set :FooClassWithBarMethod, Class.new { def bar() 'bar' end }
|
38
|
-
@instance = FooClassWithBarMethod.new
|
39
|
-
end
|
40
|
-
|
41
|
-
def teardown
|
42
|
-
Object.instance_eval { remove_const :FooClassWithBarMethod }
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_alias_method_chain
|
46
|
-
assert @instance.respond_to?(:bar)
|
47
|
-
feature_aliases = [:bar_with_baz, :bar_without_baz]
|
48
|
-
|
49
|
-
feature_aliases.each do |method|
|
50
|
-
assert !@instance.respond_to?(method)
|
51
|
-
end
|
52
|
-
|
53
|
-
assert_equal 'bar', @instance.bar
|
54
|
-
|
55
|
-
FooClassWithBarMethod.class_eval { include BarMethodAliaser }
|
56
|
-
|
57
|
-
feature_aliases.each do |method|
|
58
|
-
assert_respond_to @instance, method
|
59
|
-
end
|
60
|
-
|
61
|
-
assert_equal 'bar_with_baz', @instance.bar
|
62
|
-
assert_equal 'bar', @instance.bar_without_baz
|
63
|
-
end
|
64
|
-
|
65
|
-
def test_alias_method_chain_with_punctuation_method
|
66
|
-
FooClassWithBarMethod.class_eval do
|
67
|
-
def quux!; 'quux' end
|
68
|
-
end
|
69
|
-
|
70
|
-
assert !@instance.respond_to?(:quux_with_baz!)
|
71
|
-
FooClassWithBarMethod.class_eval do
|
72
|
-
include BarMethodAliaser
|
73
|
-
alias_method_chain :quux!, :baz
|
74
|
-
end
|
75
|
-
assert_respond_to @instance, :quux_with_baz!
|
76
|
-
|
77
|
-
assert_equal 'quux_with_baz', @instance.quux!
|
78
|
-
assert_equal 'quux', @instance.quux_without_baz!
|
79
|
-
end
|
80
|
-
|
81
|
-
def test_alias_method_chain_with_same_names_between_predicates_and_bang_methods
|
82
|
-
FooClassWithBarMethod.class_eval do
|
83
|
-
def quux!; 'quux!' end
|
84
|
-
def quux?; true end
|
85
|
-
def quux=(v); 'quux=' end
|
86
|
-
end
|
87
|
-
|
88
|
-
assert !@instance.respond_to?(:quux_with_baz!)
|
89
|
-
assert !@instance.respond_to?(:quux_with_baz?)
|
90
|
-
assert !@instance.respond_to?(:quux_with_baz=)
|
91
|
-
|
92
|
-
FooClassWithBarMethod.class_eval { include BarMethodAliaser }
|
93
|
-
assert_respond_to @instance, :quux_with_baz!
|
94
|
-
assert_respond_to @instance, :quux_with_baz?
|
95
|
-
assert_respond_to @instance, :quux_with_baz=
|
96
|
-
|
97
|
-
|
98
|
-
FooClassWithBarMethod.alias_method_chain :quux!, :baz
|
99
|
-
assert_equal 'quux!_with_baz', @instance.quux!
|
100
|
-
assert_equal 'quux!', @instance.quux_without_baz!
|
101
|
-
|
102
|
-
FooClassWithBarMethod.alias_method_chain :quux?, :baz
|
103
|
-
assert_equal false, @instance.quux?
|
104
|
-
assert_equal true, @instance.quux_without_baz?
|
105
|
-
|
106
|
-
FooClassWithBarMethod.alias_method_chain :quux=, :baz
|
107
|
-
assert_equal 'quux=_with_baz', @instance.send(:quux=, 1234)
|
108
|
-
assert_equal 'quux=', @instance.send(:quux_without_baz=, 1234)
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_alias_method_chain_with_feature_punctuation
|
112
|
-
FooClassWithBarMethod.class_eval do
|
113
|
-
def quux; 'quux' end
|
114
|
-
def quux?; 'quux?' end
|
115
|
-
include BarMethodAliaser
|
116
|
-
alias_method_chain :quux, :baz!
|
117
|
-
end
|
118
|
-
|
119
|
-
assert_nothing_raised do
|
120
|
-
assert_equal 'quux_with_baz', @instance.quux_with_baz!
|
121
|
-
end
|
122
|
-
|
123
|
-
assert_raise(NameError) do
|
124
|
-
FooClassWithBarMethod.alias_method_chain :quux?, :baz!
|
125
|
-
end
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_alias_method_chain_yields_target_and_punctuation
|
129
|
-
args = nil
|
130
|
-
|
131
|
-
FooClassWithBarMethod.class_eval do
|
132
|
-
def quux?; end
|
133
|
-
include BarMethods
|
134
|
-
|
135
|
-
FooClassWithBarMethod.alias_method_chain :quux?, :baz do |target, punctuation|
|
136
|
-
args = [target, punctuation]
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
assert_not_nil args
|
141
|
-
assert_equal 'quux', args[0]
|
142
|
-
assert_equal '?', args[1]
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_alias_method_chain_preserves_private_method_status
|
146
|
-
FooClassWithBarMethod.class_eval do
|
147
|
-
def duck; 'duck' end
|
148
|
-
include BarMethodAliaser
|
149
|
-
private :duck
|
150
|
-
alias_method_chain :duck, :orange
|
151
|
-
end
|
152
|
-
|
153
|
-
assert_raise NoMethodError do
|
154
|
-
@instance.duck
|
155
|
-
end
|
156
|
-
|
157
|
-
assert_equal 'duck_with_orange', @instance.instance_eval { duck }
|
158
|
-
assert FooClassWithBarMethod.private_method_defined?(:duck)
|
159
|
-
end
|
160
|
-
|
161
|
-
def test_alias_method_chain_preserves_protected_method_status
|
162
|
-
FooClassWithBarMethod.class_eval do
|
163
|
-
def duck; 'duck' end
|
164
|
-
include BarMethodAliaser
|
165
|
-
protected :duck
|
166
|
-
alias_method_chain :duck, :orange
|
167
|
-
end
|
168
|
-
|
169
|
-
assert_raise NoMethodError do
|
170
|
-
@instance.duck
|
171
|
-
end
|
172
|
-
|
173
|
-
assert_equal 'duck_with_orange', @instance.instance_eval { duck }
|
174
|
-
assert FooClassWithBarMethod.protected_method_defined?(:duck)
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_alias_method_chain_preserves_public_method_status
|
178
|
-
FooClassWithBarMethod.class_eval do
|
179
|
-
def duck; 'duck' end
|
180
|
-
include BarMethodAliaser
|
181
|
-
public :duck
|
182
|
-
alias_method_chain :duck, :orange
|
183
|
-
end
|
184
|
-
|
185
|
-
assert_equal 'duck_with_orange', @instance.duck
|
186
|
-
assert FooClassWithBarMethod.public_method_defined?(:duck)
|
187
|
-
end
|
188
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/metaprogramming/cattr_accessor'
|
3
|
-
|
4
|
-
class ClassAttributeAccessorTest < Test::Unit::TestCase
|
5
|
-
def setup
|
6
|
-
@class = Class.new do
|
7
|
-
cattr_accessor :foo
|
8
|
-
cattr_accessor :bar, :instance_writer => false
|
9
|
-
cattr_reader :shaq, :instance_reader => false
|
10
|
-
end
|
11
|
-
@object = @class.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_should_use_mattr_default
|
15
|
-
assert_nil @class.foo
|
16
|
-
assert_nil @object.foo
|
17
|
-
end
|
18
|
-
|
19
|
-
def test_should_set_mattr_value
|
20
|
-
@class.foo = :test
|
21
|
-
assert_equal :test, @object.foo
|
22
|
-
|
23
|
-
@object.foo = :test2
|
24
|
-
assert_equal :test2, @class.foo
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_should_not_create_instance_writer
|
28
|
-
assert_respond_to @class, :foo
|
29
|
-
assert_respond_to @class, :foo=
|
30
|
-
assert_respond_to @object, :bar
|
31
|
-
assert !@object.respond_to?(:bar=)
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_should_not_create_instance_reader
|
35
|
-
assert_respond_to @class, :shaq
|
36
|
-
assert !@object.respond_to?(:shaq)
|
37
|
-
end
|
38
|
-
end
|
@@ -1,73 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../abstract_unit'
|
2
|
-
require 'gorillib/metaprogramming/class_attribute'
|
3
|
-
|
4
|
-
class ClassAttributeTest < ActiveSupport::TestCase
|
5
|
-
def setup
|
6
|
-
@klass = Class.new { class_attribute :setting }
|
7
|
-
@sub = Class.new(@klass)
|
8
|
-
end
|
9
|
-
|
10
|
-
test 'defaults to nil' do
|
11
|
-
assert_nil @klass.setting
|
12
|
-
assert_nil @sub.setting
|
13
|
-
end
|
14
|
-
|
15
|
-
test 'inheritable' do
|
16
|
-
@klass.setting = 1
|
17
|
-
assert_equal 1, @sub.setting
|
18
|
-
end
|
19
|
-
|
20
|
-
test 'overridable' do
|
21
|
-
@sub.setting = 1
|
22
|
-
assert_nil @klass.setting
|
23
|
-
|
24
|
-
@klass.setting = 2
|
25
|
-
assert_equal 1, @sub.setting
|
26
|
-
|
27
|
-
assert_equal 1, Class.new(@sub).setting
|
28
|
-
end
|
29
|
-
|
30
|
-
test 'query method' do
|
31
|
-
assert_equal false, @klass.setting?
|
32
|
-
@klass.setting = 1
|
33
|
-
assert_equal true, @klass.setting?
|
34
|
-
end
|
35
|
-
|
36
|
-
test 'instance reader delegates to class' do
|
37
|
-
assert_nil @klass.new.setting
|
38
|
-
|
39
|
-
@klass.setting = 1
|
40
|
-
assert_equal 1, @klass.new.setting
|
41
|
-
end
|
42
|
-
|
43
|
-
test 'instance override' do
|
44
|
-
object = @klass.new
|
45
|
-
object.setting = 1
|
46
|
-
assert_nil @klass.setting
|
47
|
-
@klass.setting = 2
|
48
|
-
assert_equal 1, object.setting
|
49
|
-
end
|
50
|
-
|
51
|
-
test 'instance query' do
|
52
|
-
object = @klass.new
|
53
|
-
assert_equal false, object.setting?
|
54
|
-
object.setting = 1
|
55
|
-
assert_equal true, object.setting?
|
56
|
-
end
|
57
|
-
|
58
|
-
test 'disabling instance writer' do
|
59
|
-
object = Class.new { class_attribute :setting, :instance_writer => false }.new
|
60
|
-
assert_raise(NoMethodError) { object.setting = 'boom' }
|
61
|
-
end
|
62
|
-
|
63
|
-
test 'works well with singleton classes' do
|
64
|
-
object = @klass.new
|
65
|
-
object.singleton_class.setting = 'foo'
|
66
|
-
assert_equal 'foo', object.setting
|
67
|
-
end
|
68
|
-
|
69
|
-
test 'setter returns set value' do
|
70
|
-
val = @klass.send(:setting=, 1)
|
71
|
-
assert_equal 1, val
|
72
|
-
end
|
73
|
-
end
|