jactive_support 1.0.0-universal-java-1.6

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 +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,56 @@
1
+ require 'jactive_support/java_ext/list'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+
4
+ describe "java::util::List#clear" do
5
+ it "removes all elements" do
6
+ a = List[1, 2, 3, 4]
7
+ a.clear.should equal(a)
8
+ a.should == List[]
9
+ end
10
+
11
+ it "returns self" do
12
+ a = List[1]
13
+ oid = a.object_id
14
+ a.clear.object_id.should == oid
15
+ end
16
+
17
+ it "leaves the List empty" do
18
+ a = List[1]
19
+ a.clear
20
+ a.empty?.should == true
21
+ end
22
+
23
+ it "keeps tainted status" do
24
+ a = List[1]
25
+ a.taint
26
+ a.tainted?.should be_true
27
+ a.clear
28
+ a.tainted?.should be_true
29
+ end
30
+
31
+ it "does not accept any arguments" do
32
+ lambda { List[1].clear(true) }.should raise_error(ArgumentError)
33
+ end
34
+
35
+ ruby_version_is '1.9' do
36
+ it "keeps untrusted status" do
37
+ a = List[1]
38
+ a.untrust
39
+ a.untrusted?.should be_true
40
+ a.clear
41
+ a.untrusted?.should be_true
42
+ end
43
+ end
44
+
45
+ ruby_version_is '' ... '1.9' do
46
+ it "raises a TypeError on a frozen array" do
47
+ lambda { ListSpecs.frozen_list.clear }.should raise_error(TypeError)
48
+ end
49
+ end
50
+
51
+ ruby_version_is '1.9' do
52
+ it "raises a RuntimeError on a frozen array" do
53
+ lambda { ListSpecs.frozen_list.clear }.should raise_error(RuntimeError)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,11 @@
1
+ require 'jactive_support/java_ext/list'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+ require File.expand_path('../shared/collect', __FILE__)
4
+
5
+ describe "java::util::List#collect" do
6
+ it_behaves_like(:list_collect, :collect)
7
+ end
8
+
9
+ describe "java::util::List#collect!" do
10
+ it_behaves_like(:list_collect_b, :collect!)
11
+ end
@@ -0,0 +1,87 @@
1
+ require 'jactive_support/java_ext/list'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+
4
+ describe "java::util::List#last" do
5
+ it "returns the last element" do
6
+ List[1, 1, 1, 1, 2].last.should == 2
7
+ end
8
+
9
+ it "returns nil if self is empty" do
10
+ List[].last.should == nil
11
+ end
12
+
13
+ it "returns the last count elements if given a count" do
14
+ List[1, 2, 3, 4, 5, 9].last(3).should == [4, 5, 9]
15
+ end
16
+
17
+ it "returns an empty list when passed a count on an empty list" do
18
+ List[].last(0).should == []
19
+ List[].last(1).should == []
20
+ end
21
+
22
+ it "returns an empty list when count == 0" do
23
+ List[1, 2, 3, 4, 5].last(0).should == []
24
+ end
25
+
26
+ it "returns an list containing the last element when passed count == 1" do
27
+ List[1, 2, 3, 4, 5].last(1).should == [5]
28
+ end
29
+
30
+ it "raises an ArgumentError when count is negative" do
31
+ lambda { List[1, 2].last(-1) }.should raise_error(ArgumentError)
32
+ end
33
+
34
+ it "returns the entire list when count > length" do
35
+ List[1, 2, 3, 4, 5, 9].last(10).should == [1, 2, 3, 4, 5, 9]
36
+ end
37
+
38
+ it "returns an list which is independent to the original when passed count" do
39
+ ary = List[1, 2, 3, 4, 5]
40
+ ary.last(0).replace([1,2])
41
+ ary.should == List[1, 2, 3, 4, 5]
42
+ ary.last(1).replace([1,2])
43
+ ary.should == List[1, 2, 3, 4, 5]
44
+ ary.last(6).replace([1,2])
45
+ ary.should == List[1, 2, 3, 4, 5]
46
+ end
47
+
48
+ it "properly handles recursive lists" do
49
+ empty = ListSpecs.empty_recursive_list
50
+ empty.last.should equal(empty)
51
+
52
+ list = ListSpecs.recursive_list
53
+ list.last.should equal(list)
54
+ end
55
+
56
+ it "tries to convert the passed argument to an Integer usinig #to_int" do
57
+ obj = mock('to_int')
58
+ obj.should_receive(:to_int).and_return(2)
59
+ List[1, 2, 3, 4, 5].last(obj).should == [4, 5]
60
+ end
61
+
62
+ it "raises a TypeError if the passed argument is not numeric" do
63
+ lambda { List[1,2].last(nil) }.should raise_error(TypeError)
64
+ lambda { List[1,2].last("a") }.should raise_error(TypeError)
65
+
66
+ obj = mock("nonnumeric")
67
+ lambda { List[1,2].last(obj) }.should raise_error(TypeError)
68
+ end
69
+
70
+ it "does not return subclass instance on List subclasses" do
71
+ List[].last(0).should be_kind_of(Array)
72
+ List[].last(2).should be_kind_of(Array)
73
+ List[1, 2, 3].last(0).should be_kind_of(Array)
74
+ List[1, 2, 3].last(1).should be_kind_of(Array)
75
+ List[1, 2, 3].last(2).should be_kind_of(Array)
76
+ end
77
+
78
+ it "is not destructive" do
79
+ a = List[1, 2, 3]
80
+ a.last
81
+ a.should == List[1, 2, 3]
82
+ a.last(2)
83
+ a.should == List[1, 2, 3]
84
+ a.last(3)
85
+ a.should == List[1, 2, 3]
86
+ end
87
+ end
@@ -0,0 +1,11 @@
1
+ require 'jactive_support/java_ext/list'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+ require File.expand_path('../shared/collect', __FILE__)
4
+
5
+ describe "java::util::List#map" do
6
+ it_behaves_like(:list_collect, :map)
7
+ end
8
+
9
+ describe "java::util::List#map!" do
10
+ it_behaves_like(:list_collect_b, :map!)
11
+ end
@@ -0,0 +1,44 @@
1
+ require 'jactive_support/java_ext/list'
2
+ require File.expand_path('../shared/fixtures', __FILE__)
3
+
4
+ describe "java::util::List#push" do
5
+ it "appends the arguments to the list" do
6
+ a = List[ "a", "b", "c" ]
7
+ a.push("d", "e", "f").should equal(a)
8
+ a.push().should == List["a", "b", "c", "d", "e", "f"]
9
+ a.push(5)
10
+ a.should == List["a", "b", "c", "d", "e", "f", 5]
11
+ end
12
+
13
+ it "isn't confused by previous shift" do
14
+ a = List[ "a", "b", "c" ]
15
+ a.shift
16
+ a.push("foo")
17
+ a.should == List["b", "c", "foo"]
18
+ end
19
+
20
+ it "properly handles recursive lists" do
21
+ empty = ListSpecs.empty_recursive_list
22
+ empty.push(:last).should == List[empty, :last]
23
+
24
+ list = ListSpecs.recursive_list
25
+ list.push(:last).should == List[1, 'two', 3.0, list, list, list, list, list, :last]
26
+ end
27
+
28
+ ruby_version_is "" ... "1.9" do
29
+ it "raises a TypeError on a frozen list if modification takes place" do
30
+ lambda { ListSpecs.frozen_list.push(1) }.should raise_error(TypeError)
31
+ end
32
+
33
+ it "does not raise on a frozen list if no modification is made" do
34
+ ListSpecs.frozen_list.push.should == List[1, 2, 3]
35
+ end
36
+ end
37
+
38
+ ruby_version_is "1.9" do
39
+ it "raises a RuntimeError on a frozen list" do
40
+ lambda { ListSpecs.frozen_list.push(1) }.should raise_error(RuntimeError)
41
+ lambda { ListSpecs.frozen_list.push }.should raise_error(RuntimeError)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,133 @@
1
+ shared_examples_for :list_collect do |method|
2
+ it "returns a copy of list with each element replaced by the value returned by block" do
3
+ a = List['a', 'b', 'c', 'd']
4
+ b = a.send(method) { |i| i + '!' }
5
+ b.should == ["a!", "b!", "c!", "d!"]
6
+ b.object_id.should_not == a.object_id
7
+ end
8
+
9
+ it "does not return subclass instance" do
10
+ List[1, 2, 3].send(method) { |x| x + 1 }.should be_kind_of(Array)
11
+ end
12
+
13
+ it "does not change self" do
14
+ a = List['a', 'b', 'c', 'd']
15
+ b = a.send(method) { |i| i + '!' }
16
+ a.should == List['a', 'b', 'c', 'd']
17
+ end
18
+
19
+ it "returns the evaluated value of block if it broke in the block" do
20
+ a = List['a', 'b', 'c', 'd']
21
+ b = a.send(method) {|i|
22
+ if i == 'c'
23
+ break 0
24
+ else
25
+ i + '!'
26
+ end
27
+ }
28
+ b.should == 0
29
+ end
30
+
31
+ ruby_version_is '' ... '1.9' do
32
+ it 'returns a copy of self if no block given' do
33
+ a = List[1, 2, 3]
34
+
35
+ copy = a.send(method)
36
+ copy.should == a.to_a
37
+ copy.should_not equal(a)
38
+ end
39
+ end
40
+ ruby_version_is '1.9' do
41
+ it "returns an Enumerator when no block given" do
42
+ a = List[1, 2, 3]
43
+ a.send(method).should be_an_instance_of(enumerator_class)
44
+ end
45
+ end
46
+
47
+ it "does not copy tainted status" do
48
+ a = List[1, 2, 3]
49
+ a.taint
50
+ a.send(method){|x| x}.tainted?.should be_false
51
+ end
52
+
53
+ ruby_version_is '1.9' do
54
+ it "does not copy untrusted status" do
55
+ a = List[1, 2, 3]
56
+ a.untrust
57
+ a.send(method){|x| x}.untrusted?.should be_false
58
+ end
59
+ end
60
+ end
61
+
62
+ shared_examples_for :list_collect_b do |method|
63
+ it "replaces each element with the value returned by block" do
64
+ a = List[7, 9, 3, 5]
65
+ a.send(method) { |i| i - 1 }.should equal(a)
66
+ a.should == List[6, 8, 2, 4]
67
+ end
68
+
69
+ it "returns self" do
70
+ a = List[1, 2, 3, 4, 5]
71
+ b = a.send(method) {|i| i+1 }
72
+ a.object_id.should == b.object_id
73
+ end
74
+
75
+ it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do
76
+ a = List['a', 'b', 'c', 'd']
77
+ b = a.send(method) {|i|
78
+ if i == 'c'
79
+ break 0
80
+ else
81
+ i + '!'
82
+ end
83
+ }
84
+ b.should == 0
85
+ a.should == List['a!', 'b!', 'c', 'd']
86
+ end
87
+
88
+ ruby_version_is '' ... '1.8.7' do
89
+ it 'raises LocalJumpError if no block given' do
90
+ a = List[1, 2, 3]
91
+ lambda { a.send(method) }.should raise_error(LocalJumpError)
92
+ end
93
+ end
94
+
95
+ ruby_version_is '1.8.7' do
96
+ it "returns an Enumerator when no block given, and the enumerator can modify the original list" do
97
+ a = List[1, 2, 3]
98
+ enum = a.send(method)
99
+ enum.should be_an_instance_of(enumerator_class)
100
+ enum.each{|i| "#{i}!" }
101
+ a.should == List["1!", "2!", "3!"]
102
+ end
103
+ end
104
+
105
+ it "keeps tainted status" do
106
+ a = List[1, 2, 3]
107
+ a.taint
108
+ a.tainted?.should be_true
109
+ a.send(method){|x| x}
110
+ a.tainted?.should be_true
111
+ end
112
+
113
+ ruby_version_is '1.9' do
114
+ it "keeps untrusted status" do
115
+ a = List[1, 2, 3]
116
+ a.untrust
117
+ a.send(method){|x| x}
118
+ a.untrusted?.should be_true
119
+ end
120
+ end
121
+
122
+ ruby_version_is '' ... '1.9' do
123
+ it "raises a TypeError on a frozen list" do
124
+ lambda { ListSpecs.frozen_list.send(method) {} }.should raise_error(TypeError)
125
+ end
126
+ end
127
+
128
+ ruby_version_is '1.9' do
129
+ it "raises a RuntimeError on a frozen list" do
130
+ lambda { ListSpecs.frozen_list.send(method) {} }.should raise_error(RuntimeError)
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../../../../shared/version', __FILE__)
2
+ require File.expand_path('../../../../shared/fixtures', __FILE__)
3
+
4
+ class List < java::util::ArrayList
5
+ def self.[](*args)
6
+ new(args)
7
+ end
8
+ end
9
+
10
+ module ListSpecs
11
+ def self.frozen_list
12
+ frozen_array = List[1,2,3]
13
+ frozen_array.freeze
14
+ frozen_array
15
+ end
16
+
17
+ def self.empty_frozen_list
18
+ frozen_array = List[]
19
+ frozen_array.freeze
20
+ frozen_array
21
+ end
22
+
23
+ def self.recursive_list
24
+ a = List[1, 'two', 3.0]
25
+ 5.times { a << a }
26
+ a
27
+ end
28
+
29
+ def self.empty_recursive_list
30
+ a = List[]
31
+ a << a
32
+ a
33
+ end
34
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Hash#assoc" do
5
+ before(:each) do
6
+ @h = {:apple => :green, :orange => :orange, :grape => :green, :banana => :yellow}
7
+ end
8
+
9
+ it "returns an Array if the argument is == to a key of the Hash" do
10
+ @h.assoc(:apple).should be_an_instance_of(Array)
11
+ end
12
+
13
+ it "returns a 2-element Array if the argument is == to a key of the Hash" do
14
+ @h.assoc(:grape).size.should == 2
15
+ end
16
+
17
+ it "sets the first element of the Array to the located key" do
18
+ @h.assoc(:banana).first.should == :banana
19
+ end
20
+
21
+ it "sets the last element of the Array to the value of the located key" do
22
+ @h.assoc(:banana).last.should == :yellow
23
+ end
24
+
25
+ it "only returns the first matching key-value pair for identity hashes" do
26
+ @h.compare_by_identity
27
+ @h['pear'] = :red
28
+ @h['pear'] = :green
29
+ @h.keys.grep(/pear/).size.should == 2
30
+ @h.assoc('pear').should == ['pear', :red]
31
+ end
32
+
33
+ it "uses #== to compare the argument to the keys" do
34
+ @h[1.0] = :value
35
+ 1.should == 1.0
36
+ @h.assoc(1).should == [1.0, :value]
37
+ end
38
+
39
+ it "returns nil if the argument is not a key of the Hash" do
40
+ @h.assoc(:green).should be_nil
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Hash#clear" do
5
+ #it "returns itself" do
6
+ # h = new_hash(1 => 2, 3 => 4)
7
+ # h.clear.should equal(h)
8
+ #end
9
+
10
+ it "removes all key, value pairs" do
11
+ h = new_hash(1 => 2, 3 => 4)
12
+ h.clear
13
+ h.should == new_hash
14
+ end
15
+
16
+ =begin default
17
+ it "does not remove default values" do
18
+ h = new_hash 5
19
+ h.clear
20
+ h.default.should == 5
21
+
22
+ h = new_hash("a" => 100, "b" => 200)
23
+ h.default = "Go fish"
24
+ h.clear
25
+ h["z"].should == "Go fish"
26
+ end
27
+
28
+ it "does not remove default procs" do
29
+ h = new_hash { 5 }
30
+ h.clear
31
+ h.default_proc.should_not == nil
32
+ end
33
+ =end
34
+
35
+ =begin frozen
36
+ ruby_version_is ""..."1.9" do
37
+ it "raises a TypeError if called on a frozen instance" do
38
+ lambda { HashSpecs.frozen_hash.clear }.should raise_error(TypeError)
39
+ lambda { HashSpecs.empty_frozen_hash.clear }.should raise_error(TypeError)
40
+ end
41
+ end
42
+ ruby_version_is "1.9" do
43
+ it "raises a RuntimeError if called on a frozen instance" do
44
+ lambda { HashSpecs.frozen_hash.clear }.should raise_error(RuntimeError)
45
+ lambda { HashSpecs.empty_frozen_hash.clear }.should raise_error(RuntimeError)
46
+ end
47
+ end
48
+ =end
49
+ end
@@ -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