jactive_support 1.0.2

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 +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,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
@@ -0,0 +1,64 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#initialize" do
5
+ =begin private
6
+ it "is private" do
7
+ hash_class.should have_private_instance_method("initialize")
8
+ end
9
+ =end
10
+
11
+ =begin default
12
+ it "can be used to reset default_proc" do
13
+ h = new_hash("foo" => 1, "bar" => 2)
14
+ h.default_proc.should == nil
15
+ h.instance_eval { initialize { |h, k| k * 2 } }
16
+ h.default_proc.should_not == nil
17
+ h["a"].should == "aa"
18
+ end
19
+ =end
20
+
21
+ it "receives the arguments passed to Hash#new" do
22
+ NewHash.new(:one, :two)[0].should == :one
23
+ NewHash.new(:one, :two)[1].should == :two
24
+ end
25
+
26
+ it "returns self" do
27
+ h = hash_class.new
28
+ h.send(:initialize).should equal(h)
29
+ end
30
+
31
+ =begin frozen
32
+ ruby_version_is ""..."1.9" do
33
+ it "raises a TypeError if called on a frozen instance" do
34
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize() }}
35
+ block.should raise_error(TypeError)
36
+
37
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize(nil) } }
38
+ block.should raise_error(TypeError)
39
+
40
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize(5) } }
41
+ block.should raise_error(TypeError)
42
+
43
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize { 5 } } }
44
+ block.should raise_error(TypeError)
45
+ end
46
+ end
47
+
48
+ ruby_version_is "1.9" do
49
+ it "raises a RuntimeError if called on a frozen instance" do
50
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize() }}
51
+ block.should raise_error(RuntimeError)
52
+
53
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize(nil) } }
54
+ block.should raise_error(RuntimeError)
55
+
56
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize(5) } }
57
+ block.should raise_error(RuntimeError)
58
+
59
+ block = lambda { HashSpecs.frozen_hash.instance_eval { initialize { 5 } } }
60
+ block.should raise_error(RuntimeError)
61
+ end
62
+ end
63
+ =end
64
+ 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/to_s', __FILE__)
4
+
5
+ describe "Hash#inspect" do
6
+ =begin stack level to deep
7
+ it_behaves_like :to_s, :inspect
8
+ =end
9
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#invert" do
5
+ it "returns a new hash where keys are values and vice versa" do
6
+ new_hash(1 => 'a', 2 => 'b', 3 => 'c').invert.should ==
7
+ new_hash('a' => 1, 'b' => 2, 'c' => 3)
8
+ end
9
+
10
+ it "handles collisions by overriding with the key coming later in keys()" do
11
+ h = new_hash(:a => 1, :b => 1)
12
+ override_key = h.keys.last
13
+ h.invert[1].should == override_key
14
+ end
15
+
16
+ it "compares new keys with eql? semantics" do
17
+ h = new_hash(:a => 1.0, :b => 1)
18
+ i = h.invert
19
+ i[1.0].should == :a
20
+ i[1].should == :b
21
+ end
22
+
23
+ it "does not return subclass instances for subclasses" do
24
+ MyHash[1 => 2, 3 => 4].invert.class.should == ruby_hash_class
25
+ MyHash[].invert.class.should == ruby_hash_class
26
+ end
27
+ end
@@ -0,0 +1,33 @@
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
+ ruby_version_is "1.9" do
6
+ describe "Hash#keep_if" do
7
+ it "yields two arguments: key and value" do
8
+ all_args = []
9
+ new_hash(1 => 2, 3 => 4).keep_if { |*args| all_args << args }
10
+ all_args.should == [[1, 2], [3, 4]]
11
+ end
12
+
13
+ it "keeps every entry for which block is true and returns self" do
14
+ h = new_hash(:a => 1, :b => 2, :c => 3, :d => 4)
15
+ h.keep_if { |k,v| v % 2 == 0 }.should equal(h.to_hash)
16
+ h.should == new_hash(:b => 2, :d => 4)
17
+ end
18
+
19
+ it "returns self even if unmodified" do
20
+ h = new_hash(1 => 2, 3 => 4)
21
+ h.keep_if { true }.should equal(h.to_hash)
22
+ end
23
+
24
+ =begin frozen
25
+ it "raises an RuntimeError if called on a frozen instance" do
26
+ lambda { HashSpecs.frozen_hash.keep_if { true } }.should raise_error(RuntimeError)
27
+ lambda { HashSpecs.empty_frozen_hash.keep_if { false } }.should raise_error(RuntimeError)
28
+ end
29
+ =end
30
+
31
+ it_behaves_like(:hash_iteration_no_block, :keep_if)
32
+ end
33
+ end