Skip to content

Commit

Permalink
Add specs for hash key ends with ! or ?
Browse files Browse the repository at this point in the history
This PR adds several spec for the following syntax errors:

```console
$ ruby -cve '{a!:}'
ruby 3.4.0dev (2024-04-18T03:46:51Z master b3c59370ca) [x86_64-darwin23]
-e:1: identifier a! is not valid to get
ruby: compile error (SyntaxError)

$ ruby -cve '{a?:}'
ruby 3.4.0dev (2024-04-18T03:46:51Z master b3c59370ca) [x86_64-darwin23]
-e:1: identifier a? is not valid to get
ruby: compile error (SyntaxError)
```

This addition supplements some specs for the non-omitting value hash notation.
  • Loading branch information
koic committed Apr 18, 2024
1 parent 0ce0633 commit 89f8820
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions language/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,30 @@
-> { eval("{:a ==> 1}") }.should raise_error(SyntaxError)
end

it "recognizes '!' at the end of the key" do
eval("{:a! =>1}").should == {:"a!" => 1}
eval("{:a! => 1}").should == {:"a!" => 1}

eval("{a!:1}").should == {:"a!" => 1}
eval("{a!: 1}").should == {:"a!" => 1}
end

it "raises a SyntaxError if there is no space between `!` and `=>`" do
-> { eval("{:a!=> 1}") }.should raise_error(SyntaxError)
end

it "recognizes '?' at the end of the key" do
eval("{:a? =>1}").should == {:"a?" => 1}
eval("{:a? => 1}").should == {:"a?" => 1}

eval("{a?:1}").should == {:"a?" => 1}
eval("{a?: 1}").should == {:"a?" => 1}
end

it "raises a SyntaxError if there is no space between `?` and `=>`" do
-> { eval("{:a?=> 1}") }.should raise_error(SyntaxError)
end

it "constructs a new hash with the given elements" do
{foo: 123}.should == {foo: 123}
h = {rbx: :cool, specs: 'fail_sometimes'}
Expand Down Expand Up @@ -271,6 +295,14 @@ def foo(val)

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 89f8820

Please sign in to comment.