FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
jruby/spec/java_integration/types/map_spec.rb at test-latest_java8 · MSNexploder/jruby · GitHub
MSNexploder
/
jruby
Public
forked from
jruby/jruby
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
jruby
/
spec
/
java_integration
/
types
/
map_spec.rb
Copy path
More file actions
More file actions
Latest commit
History
History
History
283 lines (243 loc) · 8.35 KB
Breadcrumbs
jruby
/
spec
/
java_integration
/
types
/
map_spec.rb
Copy path
File metadata and controls
283 lines (243 loc) · 8.35 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require
File
.
dirname
(
__FILE__
)
+
"/../spec_helper"
describe
"A Java primitive Array of type"
do
if
{
}
.
respond_to?
:key
def
key
(
hash
,
value
)
hash
.
key
(
value
)
end
else
def
key
(
hash
,
value
)
hash
.
index
(
value
)
end
end
def
test_equal
(
a
,
b
)
b
.
should
==
a
end
def
test_ok
(
a
)
a
.
should
be_true
end
def
test_exception
(
a
,
&
b
)
lambda
{
b
.
call
}
.
should
raise_exception
(
a
)
end
def
test_no_exception
(
&
b
)
lambda
{
b
.
call
}
.
should_not
raise_exception
end
it
"supports Hash-like operations on java.util.Map classes"
do
h
=
java
.
util
.
HashMap
.
new
test_ok
(
h
.
kind_of?
java
.
util
.
Map
)
h
.
put
(
1
,
2
)
;
h
.
put
(
3
,
4
)
;
h
.
put
(
5
,
6
)
test_equal
(
{
1
=>
2
,
3
=>
4
,
5
=>
6
}
,
eval
(
h
.
inspect
)
)
test_equal
(
4
,
h
[
3
]
)
test_equal
(
nil
,
h
[
10
]
)
h
[
7
]
=
8
test_ok
(
{
3
=>
4
,
1
=>
2
,
7
=>
8
,
5
=>
6
}
==
h
)
test_equal
(
0
,
h
.
clear
.
size
)
test_equal
(
Java
::
JavaUtil
::
HashMap
,
h
.
class
)
h
.
put
(
"a"
,
100
)
;
h
.
put
(
"b"
,
200
)
;
h
.
put
(
"c"
,
300
)
test_equal
(
100
,
h
.
delete
(
"a"
)
)
test_equal
(
nil
,
h
.
delete
(
"z"
)
)
test_equal
(
"z not found"
,
h
.
delete
(
"z"
)
{
|
el
|
"
#{
el
}
not found"
}
)
test_equal
(
{
"c"
=>
300
}
,
h
.
delete_if
{
|
key
,
value
|
key
<=
"b"
}
)
h
=
java
.
util
.
LinkedHashMap
.
new
h
.
put
(
"a"
,
100
)
;
h
.
put
(
"b"
,
200
)
;
h
.
put
(
"c"
,
300
)
a1
=
[
]
;
a2
=
[
]
h
.
each
{
|
key
,
value
|
a1
<<
key
;
a2
<<
value
}
test_equal
(
[
"a"
,
"b"
,
"c"
]
,
a1
)
test_equal
(
[
100
,
200
,
300
]
,
a2
)
a1
=
[
]
;
a2
=
[
]
h
.
each_key
{
|
key
|
a1
<<
key
}
h
.
each_value
{
|
value
|
a2
<<
value
}
test_equal
(
[
"a"
,
"b"
,
"c"
]
,
a1
)
test_equal
(
[
100
,
200
,
300
]
,
a2
)
test_ok
(
h
.
clear
.
empty?
)
# Java 8 adds a replace method to Map that takes a key and value
if
ENV_JAVA
[
'java.specification.version'
]
<
'1.8'
h
.
replace
(
{
1
=>
100
}
)
else
h
.
clear
h
[
1
]
=
100
end
test_equal
(
{
1
=>
100
}
,
h
)
h
[
2
]
=
200
;
h
[
3
]
=
300
test_equal
(
300
,
h
.
fetch
(
3
)
)
test_exception
(
IndexError
)
{
h
.
fetch
(
10
)
}
test_equal
(
"hello"
,
h
.
fetch
(
10
,
"hello"
)
)
test_equal
(
"hello 10"
,
h
.
fetch
(
10
)
{
|
e
|
"hello
#{
e
}
"
}
)
test_ok
(
h
.
has_key?
(
1
)
)
test_ok
(
!
h
.
has_key?
(
0
)
)
test_ok
(
h
.
has_value?
(
300
)
)
test_ok
(
!
h
.
has_value?
(
-
1
)
)
test_ok
(
h
.
include?
(
2
)
)
test_equal
(
{
100
=>
1
,
200
=>
2
,
300
=>
3
}
,
h
.
invert
)
test_ok
(
!
h
.
key?
(
0
)
)
test_equal
(
[
1
,
2
,
3
]
,
h
.
keys
)
test_ok
(
!
h
.
value?
(
0.1
)
)
# java.util.Map has values method. Java's values() is used.
test_equal
(
"[100, 200, 300]"
,
h
.
values
.
to_a
.
inspect
)
test_equal
(
3
,
h
.
length
)
h
.
delete
(
1
)
test_equal
(
2
,
h
.
length
)
test_ok
(
h
.
member?
(
3
)
)
test_equal
(
Java
::
JavaUtil
::
LinkedHashMap
,
h
.
class
)
h1
=
java
.
util
.
LinkedHashMap
.
new
h1
.
put
(
"a"
,
100
)
;
h1
.
put
(
"b"
,
200
)
h2
=
java
.
util
.
LinkedHashMap
.
new
h2
.
put
(
"b"
,
254
)
;
h2
.
put
(
"c"
,
300
)
# Java 8 adds a merge method to Map used for merging multiple values for a given key in-place
if
ENV_JAVA
[
'java.specification.version'
]
<
'1.8'
test_equal
(
{
"a"
=>
100
,
"b"
=>
254
,
"c"
=>
300
}
,
h1
.
merge
(
h2
)
)
test_equal
(
{
"a"
=>
100
,
"b"
=>
454
,
"c"
=>
300
}
,
h1
.
merge
(
h2
)
{
|
k
,
o
,
n
|
o
+
n
}
)
test_equal
(
"{
\"
a
\"
=>100,
\"
b
\"
=>200}"
,
h1
.
inspect
)
end
h1
.
merge!
(
h2
)
{
|
k
,
o
,
n
|
o
}
test_equal
(
"{
\"
a
\"
=>100,
\"
b
\"
=>200,
\"
c
\"
=>300}"
,
h1
.
inspect
)
test_equal
(
Java
::
JavaUtil
::
LinkedHashMap
,
h1
.
class
)
h
.
clear
h
.
put
(
1
,
100
)
;
h
.
put
(
2
,
200
)
;
h
.
put
(
3
,
300
)
test_equal
(
{
1
=>
100
,
2
=>
200
}
,
h
.
reject
{
|
k
,
v
|
k
>
2
}
)
test_equal
(
"{1=>100, 2=>200, 3=>300}"
,
h
.
inspect
)
test_equal
(
{
1
=>
100
,
2
=>
200
}
,
h
.
reject!
{
|
k
,
v
|
k
>
2
}
)
test_equal
(
"{1=>100, 2=>200}"
,
h
.
inspect
)
# Java 8 adds a replace method to Map that takes a key and value
if
ENV_JAVA
[
'java.specification.version'
]
<
'1.8'
test_equal
(
{
"c"
=>
300
,
"d"
=>
400
,
"e"
=>
500
}
,
h
.
replace
(
{
"c"
=>
300
,
"d"
=>
400
,
"e"
=>
500
}
)
)
else
h
.
clear
h
.
put_all
(
{
"c"
=>
300
,
"d"
=>
400
,
"e"
=>
500
}
)
end
test_equal
(
Java
::
JavaUtil
::
LinkedHashMap
,
h
.
class
)
test_equal
(
{
"d"
=>
400
,
"e"
=>
500
}
,
h
.
select
{
|
k
,
v
|
k
>
"c"
}
)
test_equal
(
{
"c"
=>
300
}
,
h
.
select
{
|
k
,
v
|
v
<
400
}
)
# Java 8 adds a replace method to Map that takes a key and value
if
ENV_JAVA
[
'java.specification.version'
]
<
'1.8'
h
.
replace
(
{
"a"
=>
20
,
"d"
=>
10
,
"c"
=>
30
,
"b"
=>
0
}
)
else
h
.
clear
h
.
put_all
(
{
"a"
=>
20
,
"d"
=>
10
,
"c"
=>
30
,
"b"
=>
0
}
)
end
test_equal
(
[
[
"a"
,
20
]
,
[
"b"
,
0
]
,
[
"c"
,
30
]
,
[
"d"
,
10
]
]
,
h
.
sort
)
test_equal
(
[
[
"b"
,
0
]
,
[
"d"
,
10
]
,
[
"a"
,
20
]
,
[
"c"
,
30
]
]
,
h
.
sort
{
|
a
,
b
|
a
[
1
]
<=>
b
[
1
]
}
)
test_equal
(
20
,
h
.
store
(
"e"
,
20
)
)
test_equal
(
[
[
"a"
,
20
]
,
[
"d"
,
10
]
,
[
"c"
,
30
]
,
[
"b"
,
0
]
,
[
"e"
,
20
]
]
,
h
.
to_a
)
rh
=
h
.
to_hash
test_equal
(
Java
::
JavaUtil
::
LinkedHashMap
,
h
.
class
)
test_equal
(
Hash
,
rh
.
class
)
test_equal
(
"{
\"
a
\"
=>20,
\"
d
\"
=>10,
\"
c
\"
=>30,
\"
b
\"
=>0,
\"
e
\"
=>20}"
,
h
.
to_s
)
test_ok
(
h
.
all?
{
|
k
,
v
|
k
.
length
==
1
}
)
test_ok
(
!
h
.
all?
{
|
k
,
v
|
v
>
100
}
)
test_equal
(
[
[
"a"
,
20
]
,
[
"d"
,
10
]
,
[
"c"
,
30
]
,
[
"b"
,
0
]
,
[
"e"
,
20
]
]
,
h
.
map
{
|
a
|
a
}
)
test_equal
(
[
true
,
false
,
false
,
false
,
false
]
,
h
.
collect
{
|
k
,
v
|
k
==
"a"
}
)
test_equal
(
[
[
"a"
,
20
]
,
[
"d"
,
10
]
]
,
h
.
take
(
2
)
)
# Java 8 adds a replace method to Map that takes a key and value
if
ENV_JAVA
[
'java.specification.version'
]
<
'1.8'
h
.
replace
(
{
"a"
=>
100
,
"b"
=>
200
}
)
else
h
.
clear
h
.
put_all
(
{
"a"
=>
100
,
"b"
=>
200
}
)
end
h2
=
{
"b"
=>
254
,
"c"
=>
300
}
test_equal
(
{
"a"
=>
100
,
"b"
=>
200
,
"c"
=>
300
}
,
h
.
update
(
h2
)
{
|
k
,
o
,
n
|
o
}
)
test_equal
(
"{
\"
a
\"
=>100,
\"
b
\"
=>200,
\"
c
\"
=>300}"
,
h
.
inspect
)
test_equal
(
Java
::
JavaUtil
::
LinkedHashMap
,
h
.
class
)
test_equal
(
[
100
,
200
]
,
h
.
values_at
(
"a"
,
"b"
)
)
test_equal
(
[
100
,
200
,
nil
]
,
h
.
values_at
(
"a"
,
"b"
,
"z"
)
)
h
.
default
=
"cat"
test_equal
(
[
100
,
200
,
"cat"
]
,
h
.
values_at
(
"a"
,
"b"
,
"z"
)
)
h
=
java
.
util
.
HashMap
.
new
k1
=
[
1
]
h
[
k1
]
=
1
k1
[
0
]
=
100
test_equal
(
nil
,
h
[
k1
]
)
h
.
put
(
1
,
2
)
;
h
.
put
(
3
,
4
)
;
test_equal
(
1
,
key
(
h
,
2
)
)
test_equal
(
nil
,
key
(
h
,
10
)
)
test_equal
(
nil
,
h
.
default_proc
)
h
.
default
=
:hello
test_equal
(
nil
,
h
.
default_proc
)
test_equal
(
1
,
key
(
h
,
2
)
)
test_equal
(
nil
,
key
(
h
,
10
)
)
# java.util.HashMap can't have a block as an arg for its constructor
#h = Hash.new {|h,k| h[k] = k.to_i*10 }
#test_ok(!nil, h.default_proc)
#test_equal(100, h[10])
#test_equal(20, h.default(2))
#behavior change in 1.8.5 led to this:
h
=
java
.
util
.
HashMap
.
new
test_equal
(
nil
,
h
.
default
)
h
.
default
=
5
test_equal
(
5
,
h
.
default
)
test_equal
(
nil
,
h
.
default_proc
)
test_equal
(
5
,
h
[
12
]
)
###
# Maybe this test doens't work for a Java object.
#class << h
# def default(k); 2; end
#end
#test_equal(nil, h.default_proc)
#test_equal(2, h[30])
###
# test that extensions of the base classes are typed correctly
class
HashExt
<
java
.
util
.
HashMap
end
test_equal
(
HashExt
,
HashExt
.
new
.
class
)
# [] method of JavaProxy is used, and the test fails.
#test_equal(HashExt, HashExt[:foo => :bar].class)
###
# no need to test these against java.util.HashMap
# make sure hash yields look as expected (copied from MRI iterator test)
#class H
# def each
# yield [:key, :value]
# end
#end
#[{:key=>:value}, H.new].each {|h|
# h.each{|a| test_equal([:key, :value], a)}
# h.each{|*a| test_equal([[:key, :value]], a)}
# h.each{|k,v| test_equal([:key, :value], [k,v])}
#}
# each_pair should splat args correctly
#{:a=>:b}.each_pair do |*x|
# test_equal(:a,x[0])
# test_equal(:b,x[1])
#end
###
# Test hash coercion
class
ToHashImposter
def
initialize
(
hash
)
@hash
=
hash
end
def
to_hash
@hash
end
end
class
SubHash
<
Hash
end
x
=
java
.
util
.
HashMap
.
new
x
.
put
(
:a
,
1
)
;
x
.
put
(
:b
,
2
)
x
.
update
(
ToHashImposter
.
new
(
{
:a
=>
10
,
:b
=>
20
}
)
)
test_equal
(
10
,
x
[
:a
]
)
test_equal
(
20
,
x
[
:b
]
)
test_exception
(
TypeError
)
{
x
.
update
(
ToHashImposter
.
new
(
4
)
)
}
x
.
put
(
:a
,
1
)
;
x
.
put
(
:b
,
2
)
sub2
=
SubHash
.
new
(
)
sub2
[
:a
]
=
10
sub2
[
:b
]
=
20
x
.
update
(
ToHashImposter
.
new
(
sub2
)
)
test_equal
(
10
,
x
[
:a
]
)
test_equal
(
20
,
x
[
:b
]
)
x
.
put
(
:a
,
1
)
;
x
.
put
(
:b
,
2
)
# Java 8 adds a replace method to Map that takes a key and value
if
ENV_JAVA
[
'java.specification.version'
]
<
'1.8'
x
.
replace
(
ToHashImposter
.
new
(
{
:a
=>
10
,
:b
=>
20
}
)
)
test_equal
(
10
,
x
[
:a
]
)
test_equal
(
20
,
x
[
:b
]
)
test_exception
(
TypeError
)
{
x
.
replace
(
ToHashImposter
.
new
(
4
)
)
}
x
.
put
(
:a
,
1
)
;
x
.
put
(
:b
,
2
)
x
.
replace
(
ToHashImposter
.
new
(
sub2
)
)
test_equal
(
10
,
x
[
:a
]
)
test_equal
(
20
,
x
[
:b
]
)
end
class
H1
<
java
.
util
.
HashMap
end
test_no_exception
{
H1
.
new
.
clone
}
end
end
Back
|
FazBrowse Home
|
New Git URL