4
4
require_relative 'fixtures/hash_strings_usascii'
5
5
6
6
describe "Hash literal" do
7
- it "{} should return an empty hash " do
7
+ it "{} should return an empty Hash " do
8
8
{ } . size . should == 0
9
9
{ } . should == { }
10
10
end
11
11
12
- it "{} should return a new hash populated with the given elements" do
12
+ it "{} should return a new Hash populated with the given elements" do
13
13
h = { a : 'a' , 'b' => 3 , 44 => 2.3 }
14
14
h . size . should == 3
15
15
h . should == { a : "a" , "b" => 3 , 44 => 2.3 }
110
110
-> { eval ( "{:a?=> 1}" ) } . should raise_error ( SyntaxError )
111
111
end
112
112
113
- it "constructs a new hash with the given elements" do
113
+ it "constructs a new Hash with the given elements" do
114
114
{ foo : 123 } . should == { foo : 123 }
115
115
h = { rbx : :cool , specs : 'fail_sometimes' }
116
116
{ rbx : :cool , specs : 'fail_sometimes' } . should == h
@@ -232,6 +232,51 @@ def h.to_hash; {:b => 2, :c => 3}; end
232
232
ScratchPad . recorded . should == [ ]
233
233
end
234
234
end
235
+
236
+ describe "with omitted values" do # a.k.a. "Hash punning" or "Shorthand Hash syntax"
237
+ it "accepts short notation 'key' for 'key: value' syntax" do
238
+ a , b , c = 1 , 2 , 3
239
+ h = eval ( '{a:}' )
240
+ { a : 1 } . should == h
241
+ h = eval ( '{a:, b:, c:}' )
242
+ { a : 1 , b : 2 , c : 3 } . should == h
243
+ end
244
+
245
+ it "ignores hanging comma on short notation" do
246
+ a , b , c = 1 , 2 , 3
247
+ h = eval ( '{a:, b:, c:,}' )
248
+ { a : 1 , b : 2 , c : 3 } . should == h
249
+ end
250
+
251
+ it "accepts mixed syntax" do
252
+ a , e = 1 , 5
253
+ h = eval ( '{a:, b: 2, "c" => 3, :d => 4, e:}' )
254
+ eval ( '{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}' ) . should == h
255
+ end
256
+
257
+ it "works with methods and local vars" do
258
+ a = Class . new
259
+ a . class_eval ( <<-RUBY )
260
+ def bar
261
+ "baz"
262
+ end
263
+
264
+ def foo(val)
265
+ {bar:, val:}
266
+ end
267
+ RUBY
268
+
269
+ a . new . foo ( 1 ) . should == { bar : "baz" , val : 1 }
270
+ end
271
+
272
+ it "raises a SyntaxError when the Hash key ends with `!`" do
273
+ -> { eval ( "{a!:}" ) } . should raise_error ( SyntaxError , /identifier a! is not valid to get/ )
274
+ end
275
+
276
+ it "raises a SyntaxError when the Hash key ends with `?`" do
277
+ -> { eval ( "{a?:}" ) } . should raise_error ( SyntaxError , /identifier a\? is not valid to get/ )
278
+ end
279
+ end
235
280
end
236
281
237
282
describe "The ** operator" do
@@ -258,51 +303,4 @@ def m(h)
258
303
h . should == { one : 1 , two : 2 }
259
304
end
260
305
end
261
-
262
- ruby_version_is "3.1" do
263
- describe "hash with omitted value" do
264
- it "accepts short notation 'key' for 'key: value' syntax" do
265
- a , b , c = 1 , 2 , 3
266
- h = eval ( '{a:}' )
267
- { a : 1 } . should == h
268
- h = eval ( '{a:, b:, c:}' )
269
- { a : 1 , b : 2 , c : 3 } . should == h
270
- end
271
-
272
- it "ignores hanging comma on short notation" do
273
- a , b , c = 1 , 2 , 3
274
- h = eval ( '{a:, b:, c:,}' )
275
- { a : 1 , b : 2 , c : 3 } . should == h
276
- end
277
-
278
- it "accepts mixed syntax" do
279
- a , e = 1 , 5
280
- h = eval ( '{a:, b: 2, "c" => 3, :d => 4, e:}' )
281
- eval ( '{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}' ) . should == h
282
- end
283
-
284
- it "works with methods and local vars" do
285
- a = Class . new
286
- a . class_eval ( <<-RUBY )
287
- def bar
288
- "baz"
289
- end
290
-
291
- def foo(val)
292
- {bar:, val:}
293
- end
294
- RUBY
295
-
296
- a . new . foo ( 1 ) . should == { bar : "baz" , val : 1 }
297
- end
298
-
299
- it "raises a SyntaxError when the hash key ends with `!`" do
300
- -> { eval ( "{a!:}" ) } . should raise_error ( SyntaxError , /identifier a! is not valid to get/ )
301
- end
302
-
303
- it "raises a SyntaxError when the hash key ends with `?`" do
304
- -> { eval ( "{a?:}" ) } . should raise_error ( SyntaxError , /identifier a\? is not valid to get/ )
305
- end
306
- end
307
- end
308
306
end
0 commit comments