jactive_support 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rvmrc +63 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +20 -0
- data/Rakefile +45 -0
- data/jactive_support.gemspec +24 -0
- data/lib/jactive_support.rb +6 -0
- data/lib/jactive_support/constantize.rb +19 -0
- data/lib/jactive_support/core_ext.rb +4 -0
- data/lib/jactive_support/core_ext/enum.rb +43 -0
- data/lib/jactive_support/core_ext/hash.rb +2 -0
- data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
- data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
- data/lib/jactive_support/core_ext/locale.rb +28 -0
- data/lib/jactive_support/core_ext/module.rb +9 -0
- data/lib/jactive_support/core_ext/numeric.rb +7 -0
- data/lib/jactive_support/core_ext/ordering.rb +17 -0
- data/lib/jactive_support/core_ext/to_java.rb +9 -0
- data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
- data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
- data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
- data/lib/jactive_support/java_ext.rb +4 -0
- data/lib/jactive_support/java_ext/date.rb +37 -0
- data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
- data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
- data/lib/jactive_support/java_ext/enum.rb +19 -0
- data/lib/jactive_support/java_ext/iterable.rb +74 -0
- data/lib/jactive_support/java_ext/iterator.rb +8 -0
- data/lib/jactive_support/java_ext/list.rb +65 -0
- data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
- data/lib/jactive_support/java_ext/locale.rb +32 -0
- data/lib/jactive_support/java_ext/map.rb +6 -0
- data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
- data/lib/jactive_support/java_ext/map/hash.rb +166 -0
- data/lib/jactive_support/java_ext/number.rb +91 -0
- data/lib/jactive_support/java_ext/sql_date.rb +7 -0
- data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
- data/lib/jactive_support/json.rb +1 -0
- data/lib/jactive_support/json/encoders/collection.rb +12 -0
- data/lib/jactive_support/json/encoders/locale.rb +6 -0
- data/lib/jactive_support/json/encoders/map.rb +55 -0
- data/lib/jactive_support/json/encoding.rb +5 -0
- data/lib/jactive_support/rescuable.rb +62 -0
- data/lib/jactive_support/version.rb +3 -0
- data/spec/constantize_spec.rb +57 -0
- data/spec/core_ext/hash_spec.rb +34 -0
- data/spec/core_ext/module_spec.rb +19 -0
- data/spec/core_ext/ordering_spec.rb +12 -0
- data/spec/enum_spec.rb +67 -0
- data/spec/java_ext/iterable/clear_spec.rb +56 -0
- data/spec/java_ext/iterable/delete_if_spec.rb +71 -0
- data/spec/java_ext/iterable/empty_spec.rb +10 -0
- data/spec/java_ext/iterable/reject_spec.rb +76 -0
- data/spec/java_ext/iterable/shared/enumeratorize.rb +12 -0
- data/spec/java_ext/iterable/shared/fixtures.rb +90 -0
- data/spec/java_ext/iterable/shift_spec.rb +160 -0
- data/spec/java_ext/list/clear_spec.rb +56 -0
- data/spec/java_ext/list/collect_spec.rb +11 -0
- data/spec/java_ext/list/last_spec.rb +87 -0
- data/spec/java_ext/list/map_spec.rb +11 -0
- data/spec/java_ext/list/push_spec.rb +44 -0
- data/spec/java_ext/list/shared/collect.rb +133 -0
- data/spec/java_ext/list/shared/fixtures.rb +34 -0
- data/spec/java_ext/map/assoc_spec.rb +43 -0
- data/spec/java_ext/map/clear_spec.rb +49 -0
- data/spec/java_ext/map/clone_spec.rb +13 -0
- data/spec/java_ext/map/compare_by_identity_spec.rb +111 -0
- data/spec/java_ext/map/constructor_spec.rb +54 -0
- data/spec/java_ext/map/delete_if_spec.rb +54 -0
- data/spec/java_ext/map/delete_spec.rb +38 -0
- data/spec/java_ext/map/each_key_spec.rb +29 -0
- data/spec/java_ext/map/each_pair_spec.rb +38 -0
- data/spec/java_ext/map/each_spec.rb +9 -0
- data/spec/java_ext/map/each_value_spec.rb +28 -0
- data/spec/java_ext/map/element_reference_spec.rb +119 -0
- data/spec/java_ext/map/element_set_spec.rb +7 -0
- data/spec/java_ext/map/empty_spec.rb +15 -0
- data/spec/java_ext/map/eql_spec.rb +19 -0
- data/spec/java_ext/map/equal_value_spec.rb +18 -0
- data/spec/java_ext/map/fetch_spec.rb +35 -0
- data/spec/java_ext/map/fixtures/classes.rb +36 -0
- data/spec/java_ext/map/flatten_spec.rb +64 -0
- data/spec/java_ext/map/has_key_spec.rb +8 -0
- data/spec/java_ext/map/has_value_spec.rb +8 -0
- data/spec/java_ext/map/hash_spec.rb +53 -0
- data/spec/java_ext/map/include_spec.rb +7 -0
- data/spec/java_ext/map/index_spec.rb +7 -0
- data/spec/java_ext/map/indexes_spec.rb +9 -0
- data/spec/java_ext/map/indices_spec.rb +9 -0
- data/spec/java_ext/map/initialize_copy_spec.rb +13 -0
- data/spec/java_ext/map/initialize_spec.rb +64 -0
- data/spec/java_ext/map/inspect_spec.rb +9 -0
- data/spec/java_ext/map/invert_spec.rb +27 -0
- data/spec/java_ext/map/keep_if_spec.rb +33 -0
- data/spec/java_ext/map/key_spec.rb +14 -0
- data/spec/java_ext/map/keys_spec.rb +39 -0
- data/spec/java_ext/map/length_spec.rb +7 -0
- data/spec/java_ext/map/member_spec.rb +7 -0
- data/spec/java_ext/map/merge_spec.rb +69 -0
- data/spec/java_ext/map/rassoc_spec.rb +39 -0
- data/spec/java_ext/map/reject_spec.rb +123 -0
- data/spec/java_ext/map/replace_spec.rb +7 -0
- data/spec/java_ext/map/select_spec.rb +98 -0
- data/spec/java_ext/map/shared/each.rb +77 -0
- data/spec/java_ext/map/shared/eql.rb +224 -0
- data/spec/java_ext/map/shared/equal.rb +94 -0
- data/spec/java_ext/map/shared/index.rb +29 -0
- data/spec/java_ext/map/shared/iteration.rb +28 -0
- data/spec/java_ext/map/shared/key.rb +43 -0
- data/spec/java_ext/map/shared/length.rb +12 -0
- data/spec/java_ext/map/shared/replace.rb +82 -0
- data/spec/java_ext/map/shared/store.rb +68 -0
- data/spec/java_ext/map/shared/to_s.rb +68 -0
- data/spec/java_ext/map/shared/update.rb +95 -0
- data/spec/java_ext/map/shared/value.rb +16 -0
- data/spec/java_ext/map/shared/values_at.rb +12 -0
- data/spec/java_ext/map/shift_spec.rb +44 -0
- data/spec/java_ext/map/size_spec.rb +7 -0
- data/spec/java_ext/map/sort_spec.rb +17 -0
- data/spec/java_ext/map/store_spec.rb +7 -0
- data/spec/java_ext/map/to_a_spec.rb +29 -0
- data/spec/java_ext/map/to_hash_spec.rb +11 -0
- data/spec/java_ext/map/to_s_spec.rb +20 -0
- data/spec/java_ext/map/try_convert_spec.rb +32 -0
- data/spec/java_ext/map/update_spec.rb +7 -0
- data/spec/java_ext/map/value_spec.rb +8 -0
- data/spec/java_ext/map/values_at_spec.rb +7 -0
- data/spec/java_ext/map/values_spec.rb +10 -0
- data/spec/locale_spec.rb +77 -0
- data/spec/shared/fixtures.rb +7 -0
- data/spec/shared/version.rb +123 -0
- data/spec/spec_helper.rb +50 -0
- metadata +332 -0
@@ -0,0 +1,68 @@
|
|
1
|
+
shared_examples_for :hash_store do |method|
|
2
|
+
before { @method = method }
|
3
|
+
|
4
|
+
it "associates the key with the value and return the value" do
|
5
|
+
h = new_hash(:a => 1)
|
6
|
+
h.send(@method, :b, 2).should == 2
|
7
|
+
h.should == new_hash(:b=>2, :a=>1)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "duplicates string keys using dup semantics" do
|
11
|
+
# dup doesn't copy singleton methods
|
12
|
+
key = "foo"
|
13
|
+
def key.reverse() "bar" end
|
14
|
+
h = new_hash
|
15
|
+
h.send(@method, key, 0)
|
16
|
+
h.keys[0].reverse.should == "oof"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "stores unequal keys that hash to the same value" do
|
20
|
+
h = new_hash
|
21
|
+
k1 = ["x"]
|
22
|
+
k2 = ["y"]
|
23
|
+
# So they end up in the same bucket
|
24
|
+
k1.should_receive(:hash).and_return(0)
|
25
|
+
k2.should_receive(:hash).and_return(0)
|
26
|
+
|
27
|
+
h[k1] = 1
|
28
|
+
h[k2] = 2
|
29
|
+
h.size.should == 2
|
30
|
+
end
|
31
|
+
|
32
|
+
=begin frozen
|
33
|
+
it "duplicates and freezes string keys" do
|
34
|
+
key = "foo"
|
35
|
+
h = new_hash
|
36
|
+
h.send(@method, key, 0)
|
37
|
+
key << "bar"
|
38
|
+
|
39
|
+
h.should == new_hash("foo" => 0)
|
40
|
+
h.keys[0].frozen?.should == true
|
41
|
+
end
|
42
|
+
|
43
|
+
it "doesn't duplicate and freeze already frozen string keys" do
|
44
|
+
key = "foo".freeze
|
45
|
+
h = new_hash
|
46
|
+
h.send(@method, key, 0)
|
47
|
+
h.keys[0].should equal(key)
|
48
|
+
end
|
49
|
+
|
50
|
+
ruby_version_is ""..."1.9" do
|
51
|
+
it "raises a TypeError if called on a frozen instance" do
|
52
|
+
lambda { HashSpecs.frozen_hash.send(@method, 1, 2) }.should raise_error(TypeError)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
ruby_version_is "1.9" do
|
57
|
+
it "raises a RuntimeError if called on a frozen instance" do
|
58
|
+
lambda { HashSpecs.frozen_hash.send(@method, 1, 2) }.should raise_error(RuntimeError)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
=end
|
62
|
+
|
63
|
+
it "does not raise an exception if changing the value of an existing key during iteration" do
|
64
|
+
hash = {1 => 2, 3 => 4, 5 => 6}
|
65
|
+
hash.each { hash.send(@method, 1, :foo) }
|
66
|
+
hash.should == {1 => :foo, 3 => 4, 5 => 6}
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path('../../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
shared_examples_for :to_s do |method|
|
5
|
+
before { @method = method }
|
6
|
+
|
7
|
+
it "returns a string representation with same order as each()" do
|
8
|
+
h = new_hash(:a => [1, 2], :b => -2, :d => -6, nil => nil)
|
9
|
+
|
10
|
+
pairs = []
|
11
|
+
h.each do |key, value|
|
12
|
+
pairs << key.inspect + '=>' + value.inspect
|
13
|
+
end
|
14
|
+
|
15
|
+
str = '{' + pairs.join(', ') + '}'
|
16
|
+
h.send(@method).should == str
|
17
|
+
end
|
18
|
+
|
19
|
+
it "calls inspect on keys and values" do
|
20
|
+
key = mock('key')
|
21
|
+
val = mock('val')
|
22
|
+
key.should_receive(:inspect).and_return('key')
|
23
|
+
val.should_receive(:inspect).and_return('val')
|
24
|
+
|
25
|
+
new_hash(key => val).send(@method).should == '{key=>val}'
|
26
|
+
end
|
27
|
+
|
28
|
+
it "handles hashes with recursive values" do
|
29
|
+
x = new_hash
|
30
|
+
x[0] = x
|
31
|
+
x.send(@method).should == '{0=>{...}}'
|
32
|
+
|
33
|
+
x = new_hash
|
34
|
+
y = new_hash
|
35
|
+
x[0] = y
|
36
|
+
y[1] = x
|
37
|
+
x.send(@method).should == "{0=>{1=>{...}}}"
|
38
|
+
y.send(@method).should == "{1=>{0=>{...}}}"
|
39
|
+
end
|
40
|
+
|
41
|
+
# Recursive hash keys are disallowed on 1.9
|
42
|
+
ruby_version_is ""..."1.9" do
|
43
|
+
it "handles hashes with recursive keys" do
|
44
|
+
x = new_hash
|
45
|
+
x[x] = 0
|
46
|
+
x.send(@method).should == '{{...}=>0}'
|
47
|
+
|
48
|
+
x = new_hash
|
49
|
+
x[x] = x
|
50
|
+
x.send(@method).should == '{{...}=>{...}}'
|
51
|
+
|
52
|
+
|
53
|
+
x = new_hash
|
54
|
+
y = new_hash
|
55
|
+
x[y] = 0
|
56
|
+
y[x] = 1
|
57
|
+
x.send(@method).should == "{{{...}=>1}=>0}"
|
58
|
+
y.send(@method).should == "{{{...}=>0}=>1}"
|
59
|
+
|
60
|
+
x = new_hash
|
61
|
+
y = new_hash
|
62
|
+
x[y] = x
|
63
|
+
y[x] = y
|
64
|
+
x.send(@method).should == "{{{...}=>{...}}=>{...}}"
|
65
|
+
y.send(@method).should == "{{{...}=>{...}}=>{...}}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
shared_examples_for :hash_update do |method|
|
2
|
+
before { @method = method }
|
3
|
+
|
4
|
+
=begin self
|
5
|
+
it "returns self" do
|
6
|
+
h = new_hash(:_1 => 'a', :_2 => '3')
|
7
|
+
h.send(@method, :_1 => '9', :_9 => 2).should equal(h)
|
8
|
+
end
|
9
|
+
=end
|
10
|
+
|
11
|
+
it "adds the entries from other, overwriting duplicate keys" do
|
12
|
+
h = new_hash(:_1 => 'a', :_2 => '3')
|
13
|
+
h.send(@method, :_1 => '9', :_9 => 2)
|
14
|
+
h.should == new_hash(:_1 => "9", :_2 => "3", :_9 => 2)
|
15
|
+
end
|
16
|
+
|
17
|
+
=begin self
|
18
|
+
it "returns self with block" do
|
19
|
+
h1 = new_hash(:a => 2, :b => -1)
|
20
|
+
h2 = new_hash(:a => -2, :c => 1)
|
21
|
+
h1.send(@method, h2) { |k,x,y| 3.14 }.should equal(h1)
|
22
|
+
end
|
23
|
+
=end
|
24
|
+
|
25
|
+
=begin nil
|
26
|
+
it "sets any duplicate key to the value of block if passed a block" do
|
27
|
+
h1 = new_hash(:a => 2, :b => -1)
|
28
|
+
h2 = new_hash(:a => -2, :c => 1)
|
29
|
+
h1.send(@method, h2) { |k,x,y| 3.14 }
|
30
|
+
h1.should == new_hash(:c => 1, :b => -1, :a => 3.14)
|
31
|
+
|
32
|
+
h1.send(@method, h1) { nil }
|
33
|
+
h1.should == new_hash(:a => nil, :b => nil, :c => nil)
|
34
|
+
end
|
35
|
+
=end
|
36
|
+
|
37
|
+
it "tries to convert the passed argument to a hash using #to_hash" do
|
38
|
+
obj = mock('{1=>2}')
|
39
|
+
obj.should_receive(:to_hash).and_return(new_hash(1 => 2).to_hash)
|
40
|
+
new_hash(3 => 4).send(@method, obj).should == new_hash(1 => 2, 3 => 4)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "does not call to_hash on hash subclasses" do
|
44
|
+
new_hash(3 => 4).send(@method, ToHashHash[1 => 2]).should == new_hash(1 => 2, 3 => 4)
|
45
|
+
end
|
46
|
+
|
47
|
+
=begin nil
|
48
|
+
it "processes entries with same order as merge()" do
|
49
|
+
h = new_hash(1 => 2, 3 => 4, 5 => 6, "x" => nil, nil => 5, [] => [])
|
50
|
+
merge_bang_pairs = []
|
51
|
+
merge_pairs = []
|
52
|
+
h.merge(h) { |*arg| merge_pairs << arg }
|
53
|
+
h.send(@method, h) { |*arg| merge_bang_pairs << arg }
|
54
|
+
merge_bang_pairs.should == merge_pairs
|
55
|
+
end
|
56
|
+
=end
|
57
|
+
|
58
|
+
=begin frozen
|
59
|
+
ruby_version_is ""..."1.9" do
|
60
|
+
it "raises a TypeError if called on a non-empty, frozen instance" do
|
61
|
+
lambda { HashSpecs.frozen_hash.send(@method, 1 => 2) }.should raise_error(TypeError)
|
62
|
+
end
|
63
|
+
|
64
|
+
it "does not raise an exception on a frozen instance that would not be modified" do
|
65
|
+
hash = HashSpecs.frozen_hash.send(@method, HashSpecs.empty_frozen_hash)
|
66
|
+
hash.should == HashSpecs.frozen_hash
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
ruby_version_is "1.9" do
|
71
|
+
it "raises a RuntimeError on a frozen instance that is modified" do
|
72
|
+
lambda do
|
73
|
+
HashSpecs.frozen_hash.send(@method, 1 => 2)
|
74
|
+
end.should raise_error(RuntimeError)
|
75
|
+
end
|
76
|
+
|
77
|
+
it "checks frozen status before coercing an object with #to_hash" do
|
78
|
+
obj = mock("to_hash frozen")
|
79
|
+
# This is necessary because mock cleanup code cannot run on the frozen
|
80
|
+
# object.
|
81
|
+
def obj.to_hash() raise Exception, "should not receive #to_hash" end
|
82
|
+
obj.freeze
|
83
|
+
|
84
|
+
lambda { HashSpecs.frozen_hash.send(@method, obj) }.should raise_error(RuntimeError)
|
85
|
+
end
|
86
|
+
|
87
|
+
# see redmine #1571
|
88
|
+
it "raises a RuntimeError on a frozen instance that would not be modified" do
|
89
|
+
lambda do
|
90
|
+
HashSpecs.frozen_hash.send(@method, HashSpecs.empty_frozen_hash)
|
91
|
+
end.should raise_error(RuntimeError)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
=end
|
95
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
shared_examples_for :hash_value_p do |method|
|
2
|
+
before { @method = method }
|
3
|
+
|
4
|
+
it "returns true if the value exists in the hash" do
|
5
|
+
new_hash(:a => :b).send(@method, :a).should == false
|
6
|
+
new_hash(1 => 2).send(@method, 2).should == true
|
7
|
+
h = new_hash 5
|
8
|
+
h.send(@method, 5).should == false
|
9
|
+
h = new_hash { 5 }
|
10
|
+
h.send(@method, 5).should == false
|
11
|
+
end
|
12
|
+
|
13
|
+
it "uses == semantics for comparing values" do
|
14
|
+
new_hash(5 => 2.0).send(@method, 2).should == true
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
shared_examples_for :hash_values_at do |method|
|
2
|
+
before { @method = method }
|
3
|
+
|
4
|
+
it "returns an array of values for the given keys" do
|
5
|
+
h = new_hash(:a => 9, :b => 'a', :c => -10, :d => nil)
|
6
|
+
h.send(@method).should be_kind_of(Array)
|
7
|
+
h.send(@method).should == []
|
8
|
+
h.send(@method, :a, :d, :b).should be_kind_of(Array)
|
9
|
+
h.send(@method, :a, :d, :b).should == [9, nil, 'a']
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
describe "Hash#shift" do
|
5
|
+
=begin shift
|
6
|
+
it "removes a pair from hash and return it" do
|
7
|
+
h = new_hash(:a => 1, :b => 2, "c" => 3, nil => 4, [] => 5)
|
8
|
+
h2 = h.dup
|
9
|
+
|
10
|
+
h.size.times do |i|
|
11
|
+
r = h.shift
|
12
|
+
r.should be_kind_of(Array)
|
13
|
+
h2[r.first].should == r.last
|
14
|
+
h.size.should == h2.size - i - 1
|
15
|
+
end
|
16
|
+
|
17
|
+
h.should == new_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns nil from an empty hash " do
|
21
|
+
new_hash.shift.should == nil
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns (computed) default for empty hashes" do
|
25
|
+
new_hash(5).shift.should == 5
|
26
|
+
h = new_hash { |*args| args }
|
27
|
+
h.shift.should == [h, nil]
|
28
|
+
end
|
29
|
+
|
30
|
+
ruby_version_is "" ... "1.9" do
|
31
|
+
it "raises a TypeError if called on a frozen instance" do
|
32
|
+
lambda { HashSpecs.frozen_hash.shift }.should raise_error(TypeError)
|
33
|
+
lambda { HashSpecs.empty_frozen_hash.shift }.should raise_error(TypeError)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
ruby_version_is "1.9" do
|
38
|
+
it "raises a RuntimeError if called on a frozen instance" do
|
39
|
+
lambda { HashSpecs.frozen_hash.shift }.should raise_error(RuntimeError)
|
40
|
+
lambda { HashSpecs.empty_frozen_hash.shift }.should raise_error(RuntimeError)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
=end
|
44
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
describe "Hash#sort" do
|
5
|
+
it "converts self to a nested array of [key, value] arrays and sort with Array#sort" do
|
6
|
+
new_hash('a' => 'b', '1' => '2', 'b' => 'a').sort.should ==
|
7
|
+
[["1", "2"], ["a", "b"], ["b", "a"]]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "works when some of the keys are themselves arrays" do
|
11
|
+
new_hash([1,2] => 5, [1,1] => 5).sort.should == [[[1,1],5], [[1,2],5]]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "uses block to sort array if passed a block" do
|
15
|
+
new_hash(1 => 2, 2 => 9, 3 => 4).sort { |a,b| b <=> a }.should == [[3, 4], [2, 9], [1, 2]]
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
|
4
|
+
describe "Hash#to_a" do
|
5
|
+
it "returns a list of [key, value] pairs with same order as each()" do
|
6
|
+
h = new_hash(:a => 1, 1 => :a, 3 => :b, :b => 5)
|
7
|
+
pairs = []
|
8
|
+
|
9
|
+
h.each_pair do |key, value|
|
10
|
+
pairs << [key, value]
|
11
|
+
end
|
12
|
+
|
13
|
+
h.to_a.should be_kind_of(Array)
|
14
|
+
h.to_a.should == pairs
|
15
|
+
end
|
16
|
+
|
17
|
+
it "is called for Enumerable#entries" do
|
18
|
+
h = new_hash(:a => 1, 1 => :a, 3 => :b, :b => 5)
|
19
|
+
pairs = []
|
20
|
+
|
21
|
+
h.each_pair do |key, value|
|
22
|
+
pairs << [key, value]
|
23
|
+
end
|
24
|
+
|
25
|
+
ent = h.entries
|
26
|
+
ent.should be_kind_of(Array)
|
27
|
+
ent.should == pairs
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
3
|
+
require File.expand_path('../shared/to_s', __FILE__)
|
4
|
+
|
5
|
+
describe "Hash#to_s" do
|
6
|
+
|
7
|
+
ruby_version_is "1.9" do
|
8
|
+
it_behaves_like :to_s, :to_s
|
9
|
+
end
|
10
|
+
|
11
|
+
ruby_version_is ""..."1.9" do
|
12
|
+
it "returns a string by calling Hash#to_a and using Array#join with default separator" do
|
13
|
+
h = new_hash(:fun => 'x', 1 => 3, nil => "ok", [] => :y)
|
14
|
+
h.to_a.to_s.should == h.to_s
|
15
|
+
old, $, = $,, ':'
|
16
|
+
h.to_a.to_s.should == h.to_s
|
17
|
+
$, = old
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path('../../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
ruby_version_is "1.8.8" do
|
4
|
+
describe "Hash.try_convert" do
|
5
|
+
it "returns the argument if passed a Hash" do
|
6
|
+
h = {:foo => :glark}
|
7
|
+
Hash.try_convert(h).should == h
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns nil if the argument can't be coerced into a Hash" do
|
11
|
+
Hash.try_convert(Object.new).should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "does not rescue exceptions raised by #to_hash" do
|
15
|
+
obj = mock("to_hash raises")
|
16
|
+
obj.should_receive(:to_hash).and_raise(RuntimeError)
|
17
|
+
lambda { Hash.try_convert obj }.should raise_error(RuntimeError)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "coerces the argument with #to_hash" do
|
21
|
+
obj = mock('to_hash')
|
22
|
+
obj.should_receive(:to_hash).and_return({:foo => :bar})
|
23
|
+
Hash.try_convert(obj).should == {:foo => :bar}
|
24
|
+
end
|
25
|
+
|
26
|
+
it "raises a TypeError if #to_hash does not return a Hash" do
|
27
|
+
obj = mock("to_hash invalid")
|
28
|
+
obj.should_receive(:to_hash).and_return(:invalid_to_hash)
|
29
|
+
lambda { Hash.try_convert obj }.should raise_error(TypeError)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|