jactive_support 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
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 +24 -0
  7. data/lib/jactive_support.rb +6 -0
  8. data/lib/jactive_support/constantize.rb +19 -0
  9. data/lib/jactive_support/core_ext.rb +4 -0
  10. data/lib/jactive_support/core_ext/enum.rb +43 -0
  11. data/lib/jactive_support/core_ext/hash.rb +2 -0
  12. data/lib/jactive_support/core_ext/hash/assert_valid_params.rb +19 -0
  13. data/lib/jactive_support/core_ext/hash/with_keys_values.rb +9 -0
  14. data/lib/jactive_support/core_ext/locale.rb +28 -0
  15. data/lib/jactive_support/core_ext/module.rb +9 -0
  16. data/lib/jactive_support/core_ext/numeric.rb +7 -0
  17. data/lib/jactive_support/core_ext/ordering.rb +17 -0
  18. data/lib/jactive_support/core_ext/to_java.rb +9 -0
  19. data/lib/jactive_support/core_ext/to_java_date.rb +25 -0
  20. data/lib/jactive_support/core_ext/to_java_list.rb +11 -0
  21. data/lib/jactive_support/core_ext/to_java_map.rb +11 -0
  22. data/lib/jactive_support/java_ext.rb +4 -0
  23. data/lib/jactive_support/java_ext/date.rb +37 -0
  24. data/lib/jactive_support/java_ext/date/calculations.rb +43 -0
  25. data/lib/jactive_support/java_ext/date/conversions.rb +135 -0
  26. data/lib/jactive_support/java_ext/enum.rb +19 -0
  27. data/lib/jactive_support/java_ext/iterable.rb +74 -0
  28. data/lib/jactive_support/java_ext/iterator.rb +8 -0
  29. data/lib/jactive_support/java_ext/list.rb +65 -0
  30. data/lib/jactive_support/java_ext/list_iterator.rb +8 -0
  31. data/lib/jactive_support/java_ext/locale.rb +32 -0
  32. data/lib/jactive_support/java_ext/map.rb +6 -0
  33. data/lib/jactive_support/java_ext/map/constructor.rb +35 -0
  34. data/lib/jactive_support/java_ext/map/hash.rb +166 -0
  35. data/lib/jactive_support/java_ext/number.rb +91 -0
  36. data/lib/jactive_support/java_ext/sql_date.rb +7 -0
  37. data/lib/jactive_support/java_ext/sql_date/conversions.rb +33 -0
  38. data/lib/jactive_support/json.rb +1 -0
  39. data/lib/jactive_support/json/encoders/collection.rb +12 -0
  40. data/lib/jactive_support/json/encoders/locale.rb +6 -0
  41. data/lib/jactive_support/json/encoders/map.rb +55 -0
  42. data/lib/jactive_support/json/encoding.rb +5 -0
  43. data/lib/jactive_support/rescuable.rb +62 -0
  44. data/lib/jactive_support/version.rb +3 -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,13 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Hash#clone" do
4
+ it "copies instance variable but not the objects they refer to" do
5
+ hash = new_hash('key' => 'value')
6
+
7
+ clone = hash.clone
8
+
9
+ clone.should == hash
10
+ clone.object_id.should_not == hash.object_id
11
+ end
12
+ end
13
+
@@ -0,0 +1,111 @@
1
+ =begin compare_by_identity
2
+ ruby_version_is "1.9" do
3
+ describe "Hash#compare_by_identity" do
4
+ before(:each) do
5
+ @h = new_hash
6
+ @idh = new_hash.compare_by_identity
7
+ end
8
+
9
+ it "causes future comparisons on the receiver to be made by identity" do
10
+ @h["a"] = :a
11
+ @h["a"].should == :a
12
+ @h.compare_by_identity
13
+ @h["a"].should be_nil
14
+ end
15
+
16
+ it "causes #compare_by_identity? to return true" do
17
+ @idh.compare_by_identity?.should be_true
18
+ end
19
+
20
+ it "returns self" do
21
+ h = new_hash
22
+ h[:foo] = :bar
23
+ h.compare_by_identity.should == h
24
+ end
25
+
26
+ it "uses the semantics of BasicObject#equal? to determine key identity" do
27
+ 1.1.should_not equal(1.1)
28
+ @idh[1.1] = :a
29
+ @idh[1.1] = :b
30
+ [1].should_not equal([1])
31
+ @idh[[1]] = :c
32
+ @idh[[1]] = :d
33
+ :bar.should equal(:bar)
34
+ @idh[:bar] = :e
35
+ @idh[:bar] = :f
36
+ "bar".should_not equal('bar')
37
+ @idh["bar"] = :g
38
+ @idh["bar"] = :h
39
+ @idh.values.should == [:a, :b, :c, :d, :f, :g, :h]
40
+ end
41
+
42
+ it "doesn't call #equal? on keys to determine their identity" do
43
+ obj = mock('equal')
44
+ obj.should_not_receive(:equal?)
45
+ @idh[:foo] = :glark
46
+ @idh[obj] = :a
47
+ @idh[obj.dup].should == nil
48
+ end
49
+
50
+ it "regards #dup'd objects as having different identities" do
51
+ str = 'foo'
52
+ @idh[str.dup] = :str
53
+ @idh[str].should be_nil
54
+ end
55
+
56
+ it "regards #clone'd objects as having different identities" do
57
+ str = 'foo'
58
+ @idh[str.clone] = :str
59
+ @idh[str].should be_nil
60
+ end
61
+
62
+ it "regards references to the same object as having the same identity" do
63
+ o = Object.new
64
+ @h[o] = :o
65
+ @h[:a] = :a
66
+ @h[o].should == :o
67
+ end
68
+
69
+ it "raises a RuntimeError on frozen hashes" do
70
+ @h = @h.freeze
71
+ lambda { @h.compare_by_identity }.should raise_error(RuntimeError)
72
+ end
73
+
74
+ # Behaviour confirmed in bug #1871
75
+ it "perists over #dups" do
76
+ @idh['foo'] = :bar
77
+ @idh['foo'] = :glark
78
+ @idh.dup.should == @idh
79
+ @idh.dup.size.should == @idh.size
80
+ end
81
+
82
+ it "persists over #clones" do
83
+ @idh['foo'] = :bar
84
+ @idh['foo'] = :glark
85
+ @idh.clone.should == @idh
86
+ @idh.clone.size.should == @idh.size
87
+ end
88
+ end
89
+
90
+ describe "Hash#compare_by_identity?" do
91
+ it "returns false by default" do
92
+ h = new_hash
93
+ h.compare_by_identity?.should be_false
94
+ end
95
+
96
+ it "returns true once #compare_by_identity has been invoked on self" do
97
+ h = new_hash
98
+ h.compare_by_identity
99
+ h.compare_by_identity?.should be_true
100
+ end
101
+
102
+ it "returns true when called multiple times on the same ident hash" do
103
+ h = new_hash
104
+ h.compare_by_identity
105
+ h.compare_by_identity?.should be_true
106
+ h.compare_by_identity?.should be_true
107
+ h.compare_by_identity?.should be_true
108
+ end
109
+ end
110
+ end
111
+ =end
@@ -0,0 +1,54 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash.[]" do
5
+ it "creates a Hash; values can be provided as the argument list" do
6
+ hash_class[:a, 1, :b, 2].should == new_hash(:a => 1, :b => 2)
7
+ hash_class[].should == new_hash
8
+ hash_class[:a, 1, :b, new_hash(:c => 2)].should ==
9
+ new_hash(:a => 1, :b => new_hash(:c => 2))
10
+ end
11
+
12
+ it "creates a Hash; values can be provided as one single hash" do
13
+ hash_class[:a => 1, :b => 2].should == new_hash(:a => 1, :b => 2)
14
+ hash_class[new_hash(1 => 2, 3 => 4)].should == new_hash(1 => 2, 3 => 4)
15
+ hash_class[new_hash].should == new_hash
16
+ end
17
+
18
+ =begin nil
19
+ ruby_version_is '1.8.7' do
20
+ # Not officially documented yet, see http://redmine.ruby-lang.org/issues/show/1385
21
+ ruby_bug "[ruby-core:21249]", "1.8.7.167" do
22
+ it "creates a Hash; values can be provided as a list of value-pairs in an array" do
23
+ hash_class[[[:a, 1], [:b, 2]]].should == new_hash(:a => 1, :b => 2)
24
+ hash_class[[[:a, 1], [:b], 42, [:d, 2], [:e, 2, 3], []]].should == new_hash(:a => 1, :b => nil, :d => 2)
25
+ obj = mock('x')
26
+ def obj.to_ary() [:b, 2] end
27
+ hash_class[[[:a, 1], obj]].should == new_hash(:a => 1, :b => 2)
28
+ end
29
+ end
30
+ end
31
+ =end
32
+
33
+ it "raises an ArgumentError when passed an odd number of arguments" do
34
+ lambda { hash_class[1, 2, 3] }.should raise_error(ArgumentError)
35
+ lambda { hash_class[1, 2, new_hash(3 => 4)] }.should raise_error(ArgumentError)
36
+ end
37
+
38
+ ruby_version_is '1.8.7' do
39
+ it "calls to_hash" do
40
+ obj = mock('x')
41
+ def obj.to_hash() new_hash(1 => 2, 3 => 4) end
42
+ hash_class[obj].should == new_hash(1 => 2, 3 => 4)
43
+ end
44
+
45
+ it "returns an instance of a subclass when passed an Array" do
46
+ MyHash[[1,2,3,4]].should be_kind_of(MyHash)
47
+ end
48
+ end
49
+
50
+ it "returns an instance of the class it's called on" do
51
+ hash_class[MyHash[1, 2]].class.should == hash_class
52
+ MyHash[hash_class[1, 2]].should be_kind_of(MyHash)
53
+ end
54
+ end
@@ -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