44require_relative 'fixtures/hash_strings_usascii'
55
66describe "Hash literal" do
7- it "{} should return an empty hash " do
7+ it "{} should return an empty Hash " do
88 { } . size . should == 0
99 { } . should == { }
1010 end
1111
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
1313 h = { a : 'a' , 'b' => 3 , 44 => 2.3 }
1414 h . size . should == 3
1515 h . should == { a : "a" , "b" => 3 , 44 => 2.3 }
110110 -> { eval ( "{:a?=> 1}" ) } . should raise_error ( SyntaxError )
111111 end
112112
113- it "constructs a new hash with the given elements" do
113+ it "constructs a new Hash with the given elements" do
114114 { foo : 123 } . should == { foo : 123 }
115115 h = { rbx : :cool , specs : 'fail_sometimes' }
116116 { rbx : :cool , specs : 'fail_sometimes' } . should == h
@@ -232,6 +232,51 @@ def h.to_hash; {:b => 2, :c => 3}; end
232232 ScratchPad . recorded . should == [ ]
233233 end
234234 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
235280end
236281
237282describe "The ** operator" do
@@ -258,51 +303,4 @@ def m(h)
258303 h . should == { one : 1 , two : 2 }
259304 end
260305 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
308306end
0 commit comments