Skip to content

Commit

Permalink
fill error types
Browse files Browse the repository at this point in the history
  • Loading branch information
slushie committed Apr 21, 2020
1 parent 0b05571 commit 7f822de
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
49 changes: 41 additions & 8 deletions checker/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ func cleanup(value string) string {
return result
}

func makePos(line, col int) lexer.Position { //nolint:unparam
return lexer.Position{
Filename: "<stdin>",
Line: line,
Column: col,
}
}

func TestChecker_Check(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -308,7 +316,11 @@ func TestChecker_Check(t *testing.T) {
run "echo hi" with opts
}
`,
ErrWrongArgType{},
ErrWrongArgType{
Pos: makePos(2, 8),
Expected: "option::run",
Found: "string",
},
}, /*{
"variadic options with bad method type",
`
Expand All @@ -335,7 +347,11 @@ func TestChecker_Check(t *testing.T) {
run "echo hi" with opts
}
`,
ErrWrongArgType{},
ErrWrongArgType{
Pos: makePos(2, 23),
Expected: "option::run",
Found: "string",
},
}, {
"func call with bad arg count",
`
Expand All @@ -347,7 +363,13 @@ func TestChecker_Check(t *testing.T) {
run cmd
}
`,
ErrNumArgs{},
ErrNumArgs{
Expected: 1,
CallStmt: &parser.CallStmt{
Pos: makePos(2, 1),
Args: make([]*parser.Expr, 2),
},
},
}, {
"func call with bad arg type: basic literal",
`
Expand All @@ -359,7 +381,11 @@ func TestChecker_Check(t *testing.T) {
run cmd
}
`,
ErrWrongArgType{},
ErrWrongArgType{
Pos: makePos(2, 8),
Expected: "string",
Found: "int",
},
}, /*{
"func call with bad arg type: basic ident",
`
Expand Down Expand Up @@ -394,7 +420,11 @@ func TestChecker_Check(t *testing.T) {
run cmd
}
`,
ErrWrongArgType{},
ErrWrongArgType{
Pos: makePos(2, 8),
Expected: "string",
Found: "fs",
},
}, {
"func call with bad subtype",
`
Expand All @@ -407,7 +437,11 @@ func TestChecker_Check(t *testing.T) {
run cmd
}
`,
ErrWrongArgType{},
ErrWrongArgType{
Pos: makePos(2, 1),
Expected: "fs",
Found: "option::run",
},
}, /*{
"func call with bad option type",
`
Expand Down Expand Up @@ -499,7 +533,6 @@ func TestChecker_Check(t *testing.T) {
}
}


func TestChecker_CheckSelectors(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -572,7 +605,7 @@ func validateError(t *testing.T, expectedError error, actualError error) {
// assume if we got a semantic error we really want
// to validate the underlying error
if semErr, ok := actualError.(ErrSemantic); ok {
require.IsType(t, expectedError, semErr.Errs[0])
require.IsType(t, expectedError, semErr.Errs[0], "type %T", semErr.Errs[0])
require.Equal(t, expectedError.Error(), semErr.Errs[0].Error(), "error: %v", actualError)
} else {
require.IsType(t, expectedError, actualError, "error: %v", actualError)
Expand Down
3 changes: 3 additions & 0 deletions checker/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ type ErrNumArgs struct {
}

func (e ErrNumArgs) Error() string {
if e.CallStmt == nil {
return "<invalid ErrNumArgs>"
}
return fmt.Sprintf("%s expected %d args, found %d", FormatPos(e.CallStmt.Pos), e.Expected, len(e.CallStmt.Args))
}

Expand Down

0 comments on commit 7f822de

Please sign in to comment.