Skip to content

Commit

Permalink
Move misplaced tests
Browse files Browse the repository at this point in the history
  • Loading branch information
amomchilov committed Jul 30, 2024
1 parent 2cc6546 commit 2159b53
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 59 deletions.
9 changes: 0 additions & 9 deletions core/hash/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,4 @@
h.hash.should == {x: [h]}.hash
# Like above, because h.eql?(x: [h])
end

ruby_version_is "3.1" do
it "allows omitting values" do
a = 1
b = 2

eval('{a:, b:}.should == { a: 1, b: 2 }')
end
end
end
98 changes: 48 additions & 50 deletions language/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
require_relative 'fixtures/hash_strings_usascii'

describe "Hash literal" do
it "{} should return an empty hash" do
it "{} should return an empty Hash" do
{}.size.should == 0
{}.should == {}
end

it "{} should return a new hash populated with the given elements" do
it "{} should return a new Hash populated with the given elements" do
h = {a: 'a', 'b' => 3, 44 => 2.3}
h.size.should == 3
h.should == {a: "a", "b" => 3, 44 => 2.3}
Expand Down Expand Up @@ -110,7 +110,7 @@
-> { eval("{:a?=> 1}") }.should raise_error(SyntaxError)
end

it "constructs a new hash with the given elements" do
it "constructs a new Hash with the given elements" do
{foo: 123}.should == {foo: 123}
h = {rbx: :cool, specs: 'fail_sometimes'}
{rbx: :cool, specs: 'fail_sometimes'}.should == h
Expand Down Expand Up @@ -232,6 +232,51 @@ def h.to_hash; {:b => 2, :c => 3}; end
ScratchPad.recorded.should == []
end
end

describe "with omitted values" do # a.k.a. "Hash punning" or "Shorthand Hash syntax"
it "accepts short notation 'key' for 'key: value' syntax" do
a, b, c = 1, 2, 3
h = eval('{a:}')
{a: 1}.should == h
h = eval('{a:, b:, c:}')
{a: 1, b: 2, c: 3}.should == h
end

it "ignores hanging comma on short notation" do
a, b, c = 1, 2, 3
h = eval('{a:, b:, c:,}')
{a: 1, b: 2, c: 3}.should == h
end

it "accepts mixed syntax" do
a, e = 1, 5
h = eval('{a:, b: 2, "c" => 3, :d => 4, e:}')
eval('{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}').should == h
end

it "works with methods and local vars" do
a = Class.new
a.class_eval(<<-RUBY)
def bar
"baz"
end
def foo(val)
{bar:, val:}
end
RUBY

a.new.foo(1).should == {bar: "baz", val: 1}
end

it "raises a SyntaxError when the Hash key ends with `!`" do
-> { eval("{a!:}") }.should raise_error(SyntaxError, /identifier a! is not valid to get/)
end

it "raises a SyntaxError when the Hash key ends with `?`" do
-> { eval("{a?:}") }.should raise_error(SyntaxError, /identifier a\? is not valid to get/)
end
end
end

describe "The ** operator" do
Expand All @@ -258,51 +303,4 @@ def m(h)
h.should == { one: 1, two: 2 }
end
end

ruby_version_is "3.1" do
describe "hash with omitted value" do
it "accepts short notation 'key' for 'key: value' syntax" do
a, b, c = 1, 2, 3
h = eval('{a:}')
{a: 1}.should == h
h = eval('{a:, b:, c:}')
{a: 1, b: 2, c: 3}.should == h
end

it "ignores hanging comma on short notation" do
a, b, c = 1, 2, 3
h = eval('{a:, b:, c:,}')
{a: 1, b: 2, c: 3}.should == h
end

it "accepts mixed syntax" do
a, e = 1, 5
h = eval('{a:, b: 2, "c" => 3, :d => 4, e:}')
eval('{a: 1, :b => 2, "c" => 3, "d": 4, e: 5}').should == h
end

it "works with methods and local vars" do
a = Class.new
a.class_eval(<<-RUBY)
def bar
"baz"
end
def foo(val)
{bar:, val:}
end
RUBY

a.new.foo(1).should == {bar: "baz", val: 1}
end

it "raises a SyntaxError when the hash key ends with `!`" do
-> { eval("{a!:}") }.should raise_error(SyntaxError, /identifier a! is not valid to get/)
end

it "raises a SyntaxError when the hash key ends with `?`" do
-> { eval("{a?:}") }.should raise_error(SyntaxError, /identifier a\? is not valid to get/)
end
end
end
end

0 comments on commit 2159b53

Please sign in to comment.