Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove pND params #767

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions compiler/parser/method_definition_parsing_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package parser

import (
"github.com/goby-lang/goby/compiler/lexer"
"testing"

"github.com/goby-lang/goby/compiler/lexer"
)

func TestDefStatement(t *testing.T) {
Expand All @@ -15,8 +16,6 @@ func TestDefStatement(t *testing.T) {
123;
end

def bar(x = 10, y: ); end

def baz(z: 100, *s); end
`

Expand Down Expand Up @@ -47,14 +46,9 @@ func TestDefStatement(t *testing.T) {
secondExpression.IsIntegerLiteral(t).ShouldEqualTo(123)

thirdStmt := program.NthStmt(3).IsDefStmt(t)
thirdStmt.ShouldHaveName("bar")
thirdStmt.ShouldHaveOptionalParam("x")
thirdStmt.ShouldHaveRequiredKeywordParam("y")

fourthStmt := program.NthStmt(4).IsDefStmt(t)
fourthStmt.ShouldHaveName("baz")
fourthStmt.ShouldHaveOptionalKeywordParam("z")
fourthStmt.ShouldHaveSplatParam("s")
thirdStmt.ShouldHaveName("baz")
thirdStmt.ShouldHaveOptionalKeywordParam("z")
thirdStmt.ShouldHaveSplatParam("s")
}

func TestDefStatementWithYield(t *testing.T) {
Expand Down Expand Up @@ -83,3 +77,35 @@ func TestDefStatementWithYield(t *testing.T) {
secondExp := stmt.MethodBody().NthStmt(2).IsExpression(t)
secondExp.IsYieldExpression(t)
}

func TestInvalidParameterWithDefaultFail(t *testing.T) {
tests := []struct {
input string
error string
}{
{`def foo(x = 1); end`, `unexpected '=' Line: 0`},
{`def foo(x=1); end`, `unexpected '=' Line: 0`},
{`def foo(x =1); end`, `unexpected '=' Line: 0`},
{`def foo(x= 1); end`, `unexpected '=' Line: 0`},
{`def foo(x=1, y=2); end`, `unexpected '=' Line: 0`},
{`def foo(x=1, y: 2); end`, `unexpected '=' Line: 0`},
{`def foo(x: 1, y=2); end`, `unexpected '=' Line: 0`},
}

for _, tt := range tests {
l := lexer.New(tt.input)
p := New(l)
_, err := p.ParseProgram()

if err == nil {
t.Fatal("expected '=' in parameters are invalid, but got no error")
} else {
if err.Message != tt.error {
t.Log("Expected parsing error")
t.Log("expect: ", tt.error)
t.Fatal("actual: ", err.Message)
}
}

}
}