Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
48 lines (42 loc) · 1.72 KB
/
Copy patherrors_test.go
File metadata and controls
48 lines (42 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package function
import (
"context"
"encoding/json"
"errors"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestErrParseArgString_ErrorWithoutValue(t *testing.T) {
// The value is in the ArgValue field but not repeated in the message,
// because it can be large or private and the scanner error describes it
w := MustReflectWrapper(func(n int) {}, "n")
value := strings.Repeat("x", 50)
_, err := w.CallWithStrings(context.Background(), value)
var errArg ErrParseArgString
require.ErrorAs(t, err, &errArg)
assert.Equal(t, value, errArg.ArgValue)
assert.Equal(t, "n", errArg.ArgName)
assert.Equal(t, 1, strings.Count(err.Error(), value), "value only in the scanner error: %s", err)
}
func TestErrParseArgsJSON_WithoutJSON(t *testing.T) {
err := NewErrParseArgsJSON(errors.New("invalid"), VoidFuncWrapper(nil))
assert.EqualError(t, err, "error unmarshalling JSON object for arguments of function func(): invalid")
}
func TestErrParseArgJSON_ErrorAndUnwrap(t *testing.T) {
// The JSON errors name the argument and unwrap to the error of the
// JSON decoder, so callers can find out why the JSON was rejected
w := MustReflectWrapper(func(amount int) {}, "amount")
_, err := w.CallWithJSON(context.Background(), []byte(`{"amount":"x"}`))
var errArg ErrParseArgJSON
require.ErrorAs(t, err, &errArg)
assert.Equal(t, "amount", errArg.ArgName)
assert.ErrorContains(t, err, `error unmarshalling JSON for argument "amount" of function`)
var typeErr *json.UnmarshalTypeError
assert.ErrorAs(t, err, &typeErr)
_, err = w.CallWithJSON(context.Background(), []byte(`{`))
assert.ErrorAs(t, err, new(ErrParseArgsJSON))
var syntaxErr *json.SyntaxError
assert.ErrorAs(t, err, &syntaxErr)
}