jactive_support 1.0.0-universal-java-1.6

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.
Files changed (133) hide show
  1. data/.gitignore +4 -0
  2. data/.rvmrc +63 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +20 -0
  5. data/Rakefile +45 -0
  6. data/jactive_support.gemspec +25 -0
  7. data/lib/jactive_support/constantize.rb +19 -0
  8. data/lib/jactive_support/core_ext/enum.rb +43 -0
  9. data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
  10. data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
  11. data/lib/jactive_support/core_ext/hash.rb +2 -0
  12. data/lib/jactive_support/core_ext/locale.rb +28 -0
  13. data/lib/jactive_support/core_ext/module.rb +9 -0
  14. data/lib/jactive_support/core_ext/numeric.rb +7 -0
  15. data/lib/jactive_support/core_ext/ordering.rb +17 -0
  16. data/lib/jactive_support/core_ext/to_java.rb +9 -0
  17. data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
  18. data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
  19. data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
  20. data/lib/jactive_support/core_ext.rb +4 -0
  21. data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
  22. data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
  23. data/lib/jactive_support/java_ext/date.rb +37 -0
  24. data/lib/jactive_support/java_ext/enum.rb +19 -0
  25. data/lib/jactive_support/java_ext/iterable.rb +74 -0
  26. data/lib/jactive_support/java_ext/iterator.rb +8 -0
  27. data/lib/jactive_support/java_ext/list.rb +65 -0
  28. data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
  29. data/lib/jactive_support/java_ext/locale.rb +32 -0
  30. data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
  31. data/lib/jactive_support/java_ext/map/hash.rb +166 -0
  32. data/lib/jactive_support/java_ext/map.rb +6 -0
  33. data/lib/jactive_support/java_ext/number.rb +91 -0
  34. data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
  35. data/lib/jactive_support/java_ext/sql_date.rb +7 -0
  36. data/lib/jactive_support/java_ext.rb +4 -0
  37. data/lib/jactive_support/json/encoders/collection.rb +12 -0
  38. data/lib/jactive_support/json/encoders/locale.rb +6 -0
  39. data/lib/jactive_support/json/encoders/map.rb +55 -0
  40. data/lib/jactive_support/json/encoding.rb +5 -0
  41. data/lib/jactive_support/json.rb +1 -0
  42. data/lib/jactive_support/rescuable.rb +62 -0
  43. data/lib/jactive_support/version.rb +3 -0
  44. data/lib/jactive_support.rb +5 -0
  45. data/spec/constantize_spec.rb +57 -0
  46. data/spec/core_ext/hash_spec.rb +34 -0
  47. data/spec/core_ext/module_spec.rb +19 -0
  48. data/spec/core_ext/ordering_spec.rb +12 -0
  49. data/spec/enum_spec.rb +67 -0
  50. data/spec/java_ext/iterable/clear_spec.rb +56 -0
  51. data/spec/java_ext/iterable/delete_if_spec.rb +71 -0
  52. data/spec/java_ext/iterable/empty_spec.rb +10 -0
  53. data/spec/java_ext/iterable/reject_spec.rb +76 -0
  54. data/spec/java_ext/iterable/shared/enumeratorize.rb +12 -0
  55. data/spec/java_ext/iterable/shared/fixtures.rb +90 -0
  56. data/spec/java_ext/iterable/shift_spec.rb +160 -0
  57. data/spec/java_ext/list/clear_spec.rb +56 -0
  58. data/spec/java_ext/list/collect_spec.rb +11 -0
  59. data/spec/java_ext/list/last_spec.rb +87 -0
  60. data/spec/java_ext/list/map_spec.rb +11 -0
  61. data/spec/java_ext/list/push_spec.rb +44 -0
  62. data/spec/java_ext/list/shared/collect.rb +133 -0
  63. data/spec/java_ext/list/shared/fixtures.rb +34 -0
  64. data/spec/java_ext/map/assoc_spec.rb +43 -0
  65. data/spec/java_ext/map/clear_spec.rb +49 -0
  66. data/spec/java_ext/map/clone_spec.rb +13 -0
  67. data/spec/java_ext/map/compare_by_identity_spec.rb +111 -0
  68. data/spec/java_ext/map/constructor_spec.rb +54 -0
  69. data/spec/java_ext/map/delete_if_spec.rb +54 -0
  70. data/spec/java_ext/map/delete_spec.rb +38 -0
  71. data/spec/java_ext/map/each_key_spec.rb +29 -0
  72. data/spec/java_ext/map/each_pair_spec.rb +38 -0
  73. data/spec/java_ext/map/each_spec.rb +9 -0
  74. data/spec/java_ext/map/each_value_spec.rb +28 -0
  75. data/spec/java_ext/map/element_reference_spec.rb +119 -0
  76. data/spec/java_ext/map/element_set_spec.rb +7 -0
  77. data/spec/java_ext/map/empty_spec.rb +15 -0
  78. data/spec/java_ext/map/eql_spec.rb +19 -0
  79. data/spec/java_ext/map/equal_value_spec.rb +18 -0
  80. data/spec/java_ext/map/fetch_spec.rb +35 -0
  81. data/spec/java_ext/map/fixtures/classes.rb +36 -0
  82. data/spec/java_ext/map/flatten_spec.rb +64 -0
  83. data/spec/java_ext/map/has_key_spec.rb +8 -0
  84. data/spec/java_ext/map/has_value_spec.rb +8 -0
  85. data/spec/java_ext/map/hash_spec.rb +53 -0
  86. data/spec/java_ext/map/include_spec.rb +7 -0
  87. data/spec/java_ext/map/index_spec.rb +7 -0
  88. data/spec/java_ext/map/indexes_spec.rb +9 -0
  89. data/spec/java_ext/map/indices_spec.rb +9 -0
  90. data/spec/java_ext/map/initialize_copy_spec.rb +13 -0
  91. data/spec/java_ext/map/initialize_spec.rb +64 -0
  92. data/spec/java_ext/map/inspect_spec.rb +9 -0
  93. data/spec/java_ext/map/invert_spec.rb +27 -0
  94. data/spec/java_ext/map/keep_if_spec.rb +33 -0
  95. data/spec/java_ext/map/key_spec.rb +14 -0
  96. data/spec/java_ext/map/keys_spec.rb +39 -0
  97. data/spec/java_ext/map/length_spec.rb +7 -0
  98. data/spec/java_ext/map/member_spec.rb +7 -0
  99. data/spec/java_ext/map/merge_spec.rb +69 -0
  100. data/spec/java_ext/map/rassoc_spec.rb +39 -0
  101. data/spec/java_ext/map/reject_spec.rb +123 -0
  102. data/spec/java_ext/map/replace_spec.rb +7 -0
  103. data/spec/java_ext/map/select_spec.rb +98 -0
  104. data/spec/java_ext/map/shared/each.rb +77 -0
  105. data/spec/java_ext/map/shared/eql.rb +224 -0
  106. data/spec/java_ext/map/shared/equal.rb +94 -0
  107. data/spec/java_ext/map/shared/index.rb +29 -0
  108. data/spec/java_ext/map/shared/iteration.rb +28 -0
  109. data/spec/java_ext/map/shared/key.rb +43 -0
  110. data/spec/java_ext/map/shared/length.rb +12 -0
  111. data/spec/java_ext/map/shared/replace.rb +82 -0
  112. data/spec/java_ext/map/shared/store.rb +68 -0
  113. data/spec/java_ext/map/shared/to_s.rb +68 -0
  114. data/spec/java_ext/map/shared/update.rb +95 -0
  115. data/spec/java_ext/map/shared/value.rb +16 -0
  116. data/spec/java_ext/map/shared/values_at.rb +12 -0
  117. data/spec/java_ext/map/shift_spec.rb +44 -0
  118. data/spec/java_ext/map/size_spec.rb +7 -0
  119. data/spec/java_ext/map/sort_spec.rb +17 -0
  120. data/spec/java_ext/map/store_spec.rb +7 -0
  121. data/spec/java_ext/map/to_a_spec.rb +29 -0
  122. data/spec/java_ext/map/to_hash_spec.rb +11 -0
  123. data/spec/java_ext/map/to_s_spec.rb +20 -0
  124. data/spec/java_ext/map/try_convert_spec.rb +32 -0
  125. data/spec/java_ext/map/update_spec.rb +7 -0
  126. data/spec/java_ext/map/value_spec.rb +8 -0
  127. data/spec/java_ext/map/values_at_spec.rb +7 -0
  128. data/spec/java_ext/map/values_spec.rb +10 -0
  129. data/spec/locale_spec.rb +77 -0
  130. data/spec/shared/fixtures.rb +7 -0
  131. data/spec/shared/version.rb +123 -0
  132. data/spec/spec_helper.rb +50 -0
  133. metadata +332 -0
@@ -0,0 +1,54 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/iteration', __FILE__)
4
+
5
+ describe "Hash#delete_if" do
6
+ it "yields two arguments: key and value" do
7
+ all_args = []
8
+ new_hash(1 => 2, 3 => 4).delete_if { |*args| all_args << args }
9
+ all_args.sort.should == [[1, 2], [3, 4]]
10
+ end
11
+
12
+ =begin self
13
+ it "returns self" do
14
+ h = new_hash(:a => 1, :b => 2, :c => 3, :d => 4)
15
+ h.delete_if { |k,v| v % 2 == 1 }.should equal(h)
16
+ end
17
+ =end
18
+
19
+ it "removes every entry for which block is true" do
20
+ h = new_hash(:a => 1, :b => 2, :c => 3, :d => 4)
21
+ h.delete_if { |k,v| v % 2 == 1 }
22
+ h.should == new_hash(:b => 2, :d => 4)
23
+ end
24
+
25
+ it "processes entries with the same order as each()" do
26
+ h = new_hash(:a => 1, :b => 2, :c => 3, :d => 4)
27
+
28
+ each_pairs = []
29
+ delete_pairs = []
30
+
31
+ h.each_pair { |k,v| each_pairs << [k, v] }
32
+ h.delete_if { |k,v| delete_pairs << [k,v] }
33
+
34
+ each_pairs.should == delete_pairs
35
+ end
36
+
37
+ =begin frozen
38
+ ruby_version_is "" ... "1.9" do
39
+ it "raises an TypeError if called on a frozen instance" do
40
+ lambda { HashSpecs.frozen_hash.delete_if { false } }.should raise_error(TypeError)
41
+ lambda { HashSpecs.empty_frozen_hash.delete_if { true } }.should raise_error(TypeError)
42
+ end
43
+ end
44
+
45
+ ruby_version_is "1.9" do
46
+ it "raises an RuntimeError if called on a frozen instance" do
47
+ lambda { HashSpecs.frozen_hash.delete_if { false } }.should raise_error(RuntimeError)
48
+ lambda { HashSpecs.empty_frozen_hash.delete_if { true } }.should raise_error(RuntimeError)
49
+ end
50
+ end
51
+ =end
52
+
53
+ it_behaves_like(:hash_iteration_no_block, :delete_if)
54
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#delete" do
5
+ it "removes the entry and returns the deleted value" do
6
+ h = new_hash(:a => 5, :b => 2)
7
+ h.delete(:b).should == 2
8
+ h.should == new_hash(:a => 5)
9
+ end
10
+
11
+ it "calls supplied block if the key is not found" do
12
+ new_hash(:a => 1, :b => 10, :c => 100).delete(:d) { 5 }.should == 5
13
+ new_hash(:default).delete(:d) { 5 }.should == 5
14
+ new_hash { :defualt }.delete(:d) { 5 }.should == 5
15
+ end
16
+
17
+ it "returns nil if the key is not found when no block is given" do
18
+ new_hash(:a => 1, :b => 10, :c => 100).delete(:d).should == nil
19
+ new_hash(:default).delete(:d).should == nil
20
+ new_hash { :defualt }.delete(:d).should == nil
21
+ end
22
+
23
+ =begin frozen
24
+ ruby_version_is "" ... "1.9" do
25
+ it "raises a TypeError if called on a frozen instance" do
26
+ lambda { HashSpecs.frozen_hash.delete("foo") }.should raise_error(TypeError)
27
+ lambda { HashSpecs.empty_frozen_hash.delete("foo") }.should raise_error(TypeError)
28
+ end
29
+ end
30
+
31
+ ruby_version_is "1.9" do
32
+ it "raises a RuntimeError if called on a frozen instance" do
33
+ lambda { HashSpecs.frozen_hash.delete("foo") }.should raise_error(RuntimeError)
34
+ lambda { HashSpecs.empty_frozen_hash.delete("foo") }.should raise_error(RuntimeError)
35
+ end
36
+ end
37
+ =end
38
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/iteration', __FILE__)
4
+
5
+ describe "Hash#each_key" do
6
+
7
+ =begin self
8
+ it "returns self" do
9
+ h = new_hash(1 => -1, 2 => -2, 3 => -3, 4 => -4)
10
+ h.each_key { |k| }.should equal(h)
11
+ end
12
+ =end
13
+
14
+ it "calls block once for each key, passing key" do
15
+ r = new_hash
16
+ h = new_hash(1 => -1, 2 => -2, 3 => -3, 4 => -4)
17
+ h.each_key { |k| r[k] = k }
18
+ r.should == new_hash(1 => 1, 2 => 2, 3 => 3, 4 => 4)
19
+ end
20
+
21
+ it "processes keys in the same order as keys()" do
22
+ keys = []
23
+ h = new_hash(1 => -1, 2 => -2, 3 => -3, 4 => -4)
24
+ h.each_key { |k| keys << k }
25
+ keys.should == h.keys
26
+ end
27
+
28
+ it_behaves_like(:hash_iteration_no_block, :each_key)
29
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/iteration', __FILE__)
4
+ require File.expand_path('../shared/each', __FILE__)
5
+
6
+ describe "Hash#each_pair" do
7
+ ruby_version_is ""..."1.9" do
8
+ =begin self
9
+ it "returns self" do
10
+ h = new_hash(1 => 2, 3 => 4)
11
+ h.each_pair { |key, value| }.should equal(h)
12
+ end
13
+ =end
14
+
15
+ it "yields the key and value of each pair to a block expecting |key, value|" do
16
+ all_args = []
17
+
18
+ h = new_hash(1 => 2, 3 => 4)
19
+ h.each_pair { |key, value| all_args << [key, value] }
20
+
21
+ all_args.sort.should == [[1, 2], [3, 4]]
22
+ end
23
+
24
+ it "yields a [key, value] Array for each pair to a block expecting |*args|" do
25
+ all_args = []
26
+
27
+ h = new_hash(1 => 2, 3 => 4)
28
+ h.each_pair { |*args| all_args << args }
29
+
30
+ all_args.sort.should == [[1, 2], [3, 4]]
31
+ end
32
+ end
33
+
34
+ ruby_version_is "1.9" do
35
+ it_behaves_like(:hash_each, :each_pair)
36
+ end
37
+ it_behaves_like(:hash_iteration_no_block, :each_pair)
38
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/iteration', __FILE__)
4
+ require File.expand_path('../shared/each', __FILE__)
5
+
6
+ describe "Hash#each" do
7
+ it_behaves_like(:hash_each, :each)
8
+ it_behaves_like(:hash_iteration_no_block, :each)
9
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/iteration', __FILE__)
4
+
5
+ describe "Hash#each_value" do
6
+ =begin self
7
+ it "returns self" do
8
+ h = new_hash(:a => -5, :b => -3, :c => -2, :d => -1, :e => -1)
9
+ h.each_value { |v| }.should equal(h)
10
+ end
11
+ =end
12
+
13
+ it "calls block once for each key, passing value" do
14
+ r = []
15
+ h = new_hash(:a => -5, :b => -3, :c => -2, :d => -1, :e => -1)
16
+ h.each_value { |v| r << v }
17
+ r.sort.should == [-5, -3, -2, -1, -1]
18
+ end
19
+
20
+ it "processes values in the same order as values()" do
21
+ values = []
22
+ h = new_hash(:a => -5, :b => -3, :c => -2, :d => -1, :e => -1)
23
+ h.each_value { |v| values << v }
24
+ values.should == h.values.to_a
25
+ end
26
+
27
+ it_behaves_like(:hash_iteration_no_block, :each_value)
28
+ end
@@ -0,0 +1,119 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#[]" do
5
+ it "returns the value for key" do
6
+ obj = mock('x')
7
+ h = new_hash(1 => 2, 3 => 4, "foo" => "bar", obj => obj, [] => "baz")
8
+ h[1].should == 2
9
+ h[3].should == 4
10
+ h["foo"].should == "bar"
11
+ h[obj].should == obj
12
+ h[[]].should == "baz"
13
+ end
14
+
15
+ it "returns nil as default default value" do
16
+ new_hash(0 => 0)[5].should == nil
17
+ end
18
+
19
+ it "returns the default (immediate) value for missing keys" do
20
+ h = new_hash 5
21
+ h[:a].should == 5
22
+ h[:a] = 0
23
+ h[:a].should == 0
24
+ h[:b].should == 5
25
+ end
26
+
27
+ =begin default
28
+ it "calls subclass implementations of default" do
29
+ h = DefaultHash.new
30
+ h[:nothing].should == 100
31
+ end
32
+ =end
33
+
34
+ it "does not create copies of the immediate default value" do
35
+ str = "foo"
36
+ h = new_hash(str)
37
+ a = h[:a]
38
+ b = h[:b]
39
+ a << "bar"
40
+
41
+ a.should equal(b)
42
+ a.should == "foobar"
43
+ b.should == "foobar"
44
+ end
45
+
46
+ =begin default
47
+ it "returns the default (dynamic) value for missing keys" do
48
+ h = new_hash { |hsh, k| k.kind_of?(Numeric) ? hsh[k] = k + 2 : hsh[k] = k }
49
+ h[1].should == 3
50
+ h['this'].should == 'this'
51
+ h.should == new_hash(1 => 3, 'this' => 'this')
52
+
53
+ i = 0
54
+ h = new_hash { |hsh, key| i += 1 }
55
+ h[:foo].should == 1
56
+ h[:foo].should == 2
57
+ h[:bar].should == 3
58
+ end
59
+ =end
60
+
61
+ =begin nil
62
+ it "does not return default values for keys with nil values" do
63
+ h = new_hash 5
64
+ h[:a] = nil
65
+ h[:a].should == nil
66
+
67
+ h = new_hash() { 5 }
68
+ h[:a] = nil
69
+ h[:a].should == nil
70
+ end
71
+ =end
72
+
73
+ it "compares keys with eql? semantics" do
74
+ new_hash(1.0 => "x")[1].should == nil
75
+ new_hash(1.0 => "x")[1.0].should == "x"
76
+ new_hash(1 => "x")[1.0].should == nil
77
+ new_hash(1 => "x")[1].should == "x"
78
+ end
79
+
80
+ it "compares key via hash" do
81
+ x = mock('0')
82
+ x.should_receive(:hash).and_return(0)
83
+
84
+ h = new_hash
85
+ # 1.9 only calls #hash if the hash had at least one entry beforehand.
86
+ h[:foo] = :bar
87
+ h[x].should == nil
88
+ end
89
+
90
+ =begin hashCode equals
91
+ it "does not compare keys with different #hashCode values via #equals" do
92
+ x = mock('x')
93
+ x.should_not_receive(:equals)
94
+ x.stub!(:hashCode).and_return(0)
95
+
96
+ y = mock('y')
97
+ y.should_not_receive(:equals)
98
+ y.stub!(:hashCode).and_return(1)
99
+
100
+ new_hash(y => 1)[x].should == nil
101
+ end
102
+
103
+ it "compares keys with the same #hashCode value via #equals" do
104
+ x = mock('x')
105
+ x.should_receive(:equals).and_return(true)
106
+ x.stub!(:hashCode).and_return(42)
107
+
108
+ y = mock('y')
109
+ y.should_not_receive(:equals)
110
+ y.stub!(:hashCode).and_return(42)
111
+
112
+ new_hash(y => 1)[x].should == 1
113
+ end
114
+ =end
115
+ end
116
+
117
+ describe "Hash.[]" do
118
+ it "needs to be reviewed for spec completeness"
119
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/store', __FILE__)
4
+
5
+ describe "Hash#[]=" do
6
+ it_behaves_like(:hash_store, :[]=)
7
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#empty?" do
5
+ it "returns true if the hash has no entries" do
6
+ new_hash.empty?.should == true
7
+ new_hash(1 => 1).empty?.should == false
8
+ end
9
+
10
+ it "returns true if the hash has no entries and has a default value" do
11
+ new_hash(5).empty?.should == true
12
+ new_hash { 5 }.empty?.should == true
13
+ new_hash { |hsh, k| hsh[k] = k }.empty?.should == true
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/eql', __FILE__)
4
+
5
+ # Do not use #should_receive(:eql?) mocks in these specs
6
+ # because MSpec uses Hash for mocks and Hash calls #eql?.
7
+
8
+ describe "Hash#eql?" do
9
+ it_behaves_like :hash_eql, :eql?
10
+
11
+ ruby_version_is '1.8.7' do
12
+ it_behaves_like :hash_eql_additional, :eql?
13
+ end
14
+
15
+ ruby_version_is '1.9' do
16
+ it_behaves_like :hash_eql_additional_more, :eql?
17
+ end
18
+
19
+ end
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/eql', __FILE__)
4
+
5
+ describe "Hash#==" do
6
+ it_behaves_like :hash_eql, :==
7
+ it_behaves_like :hash_eql_additional, :==
8
+ it_behaves_like :hash_eql_additional_more, :==
9
+
10
+ it "compares values with == semantics" do
11
+ l_val = mock("left")
12
+ r_val = mock("right")
13
+
14
+ l_val.should_receive(:==).with(r_val).and_return(true)
15
+
16
+ (new_hash(1 => l_val) == new_hash(1 => r_val)).should be_true
17
+ end
18
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#fetch" do
5
+ it "returns the value for key" do
6
+ new_hash(:a => 1, :b => -1).fetch(:b).should == -1
7
+ end
8
+
9
+ it "raises an IndexError if key is not found" do
10
+ lambda { new_hash.fetch(:a) }.should raise_error(IndexError)
11
+ lambda { new_hash(5).fetch(:a) }.should raise_error(IndexError)
12
+ lambda { new_hash { 5 }.fetch(:a) }.should raise_error(IndexError)
13
+ end
14
+
15
+ it "returns default if key is not found when passed a default" do
16
+ new_hash.fetch(:a, nil).should == nil
17
+ new_hash.fetch(:a, 'not here!').should == "not here!"
18
+ =begin nil
19
+ new_hash(:a => nil).fetch(:a, 'not here!').should == nil
20
+ =end
21
+ end
22
+
23
+ it "returns value of block if key is not found when passed a block" do
24
+ new_hash.fetch('a') { |k| k + '!' }.should == "a!"
25
+ end
26
+
27
+ it "gives precedence to the default block over the default argument when passed both" do
28
+ new_hash.fetch(9, :foo) { |i| i * i }.should == 81
29
+ end
30
+
31
+ it "raises an ArgumentError when not passed one or two arguments" do
32
+ lambda { new_hash.fetch() }.should raise_error(ArgumentError)
33
+ lambda { new_hash.fetch(1, 2, 3) }.should raise_error(ArgumentError)
34
+ end
35
+ end
@@ -0,0 +1,36 @@
1
+ class MyHash < hash_class; end
2
+
3
+ class NewHash < hash_class
4
+ def initialize(*args)
5
+ super(0)
6
+ args.each_with_index do |val, index|
7
+ self[index] = val
8
+ end
9
+ end
10
+ end
11
+
12
+ class DefaultHash < hash_class
13
+ def default(key)
14
+ 100
15
+ end
16
+ end
17
+
18
+ class ToHashHash < Hash
19
+ def to_hash
20
+ new_hash "to_hash" => "was", "called!" => "duh."
21
+ end
22
+ end
23
+
24
+ module HashSpecs
25
+ def self.empty_frozen_hash
26
+ @empty ||= new_hash
27
+ @empty.freeze
28
+ @empty
29
+ end
30
+
31
+ def self.frozen_hash
32
+ @hash ||= new_hash(1 => 2, 3 => 4)
33
+ @hash.freeze
34
+ @hash
35
+ end
36
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Hash#flatten" do
5
+
6
+ before(:each) do
7
+ @h = {:plato => :greek,
8
+ :witgenstein => [:austrian, :british],
9
+ :russell => :welsh}
10
+ end
11
+
12
+ it "returns an Array" do
13
+ new_hash.flatten.should be_an_instance_of(Array)
14
+ end
15
+
16
+ it "returns an empty Array for an empty Hash" do
17
+ new_hash.flatten.should == []
18
+ end
19
+
20
+ it "sets each even index of the Array to a key of the Hash" do
21
+ a = @h.flatten
22
+ a[0].should == :plato
23
+ a[2].should == :witgenstein
24
+ a[4].should == :russell
25
+ end
26
+
27
+ it "sets each odd index of the Array to the value corresponding to the previous element" do
28
+ a = @h.flatten
29
+ a[1].should == :greek
30
+ a[3].should == [:austrian, :british]
31
+ a[5].should == :welsh
32
+ end
33
+
34
+ it "does not recursively flatten Array values when called without arguments" do
35
+ a = @h.flatten
36
+ a[3].should == [:austrian, :british]
37
+ end
38
+
39
+ it "does not recursively flatten Hash values when called without arguments" do
40
+ @h[:russell] = {:born => :wales, :influenced_by => :mill }
41
+ a = @h.flatten
42
+ a[5].should_not == {:born => :wales, :influenced_by => :mill }.flatten
43
+ end
44
+
45
+ it "recursively flattens Array values when called with an argument >= 2" do
46
+ a = @h.flatten(2)
47
+ a[3].should == :austrian
48
+ a[4].should == :british
49
+ end
50
+
51
+ it "recursively flattens Array values to the given depth" do
52
+ @h[:russell] = [[:born, :wales], [:influenced_by, :mill]]
53
+ a = @h.flatten(2)
54
+ a[6].should == [:born, :wales]
55
+ a[7].should == [:influenced_by, :mill]
56
+ end
57
+
58
+ it "raises an TypeError if given a non-Integer argument" do
59
+ lambda do
60
+ @h.flatten(Object.new)
61
+ end.should raise_error(TypeError)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/key', __FILE__)
4
+
5
+ describe "Hash#has_key?" do
6
+ it_behaves_like(:hash_key_p, :has_key?)
7
+ end
8
+
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/value', __FILE__)
4
+
5
+ describe "Hash#has_value?" do
6
+ it_behaves_like(:hash_value_p, :has_value?)
7
+ end
8
+
@@ -0,0 +1,53 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Hash" do
4
+ it "includes Enumerable" do
5
+ hash_class.include?(Enumerable).should == true
6
+ end
7
+ end
8
+
9
+ describe "Hash#hash" do
10
+ # prior to 1.8.7, there were no sensible Hash#hash defined at all
11
+ ruby_version_is "1.8.7" do
12
+ ruby_bug "#", "1.8.7.10" do
13
+ it "returns a value which doesn't depend on the hash order" do
14
+ new_hash(0=>2, 11=>1).hash.should == new_hash(11=>1, 0=>2).hash
15
+ end
16
+ end
17
+ end
18
+
19
+ it "generates a hash for recursive hash structures" do
20
+ h = new_hash
21
+ h[:a] = h
22
+ (h.hash == h[:a].hash).should == true
23
+ end
24
+
25
+ =begin recursive hash
26
+ ruby_bug "redmine #1852", "1.9.1" do
27
+ it "returns the same hash for recursive hashes" do
28
+ h = {} ; h[:x] = h
29
+ h.hash.should == {:x => h}.hash
30
+ h.hash.should == {:x => {:x => h}}.hash
31
+ # This is because h.eql?(:x => h)
32
+ # Remember that if two objects are eql?
33
+ # then the need to have the same hash.
34
+ # Check the Hash#eql? specs!
35
+ end
36
+
37
+ it "returns the same hash for recursive hashes through arrays" do
38
+ h = {} ; rec = [h] ; h[:x] = rec
39
+ h.hash.should == {:x => rec}.hash
40
+ h.hash.should == {:x => [h]}.hash
41
+ # Like above, because h.eql?(:x => [h])
42
+ end
43
+ end
44
+ =end
45
+
46
+ ruby_version_is "" .. "1.8.6" do
47
+ it "computes recursive hash keys with identical hashes" do
48
+ h = new_hash
49
+ h[h] = h
50
+ (h.hash == h[h].hash).should == true
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/key', __FILE__)
4
+
5
+ describe "Hash#include?" do
6
+ it_behaves_like(:hash_key_p, :include?)
7
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/index', __FILE__)
4
+
5
+ describe "Hash#index" do
6
+ it_behaves_like :hash_index, :index
7
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/values_at', __FILE__)
4
+
5
+ ruby_version_is ""..."1.9" do
6
+ describe "Hash#indexes" do
7
+ it_behaves_like(:hash_values_at, :indexes)
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/values_at', __FILE__)
4
+
5
+ ruby_version_is ""..."1.9" do
6
+ describe "Hash#indices" do
7
+ it_behaves_like(:hash_values_at, :indices)
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/replace', __FILE__)
4
+
5
+ describe "Hash#initialize_copy" do
6
+ =begin private
7
+ it "is private" do
8
+ hash_class.should have_private_instance_method("initialize_copy")
9
+ end
10
+ =end
11
+
12
+ it_behaves_like(:hash_replace, :initialize_copy)
13
+ end