-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconvert_test.go
69 lines (58 loc) · 1.26 KB
/
convert_test.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package argmapper
import (
"reflect"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestConvert(t *testing.T) {
cases := []struct {
Name string
Args []Arg
Target interface{}
Expected interface{}
}{
{
"primitive to primitive",
[]Arg{
Typed("42"),
Converter(func(v string) (int, error) { return strconv.Atoi(v) }),
},
(*int)(nil),
int(42),
},
{
"primitive to interface type",
[]Arg{
Typed("42"),
Converter(func(v string) testInterface { return &testInterfaceImpl{} }),
},
(*testInterface)(nil),
&testInterfaceImpl{},
},
{
"primitive to interface implementation",
[]Arg{
Typed("42"),
Converter(func(v string) *testInterfaceImpl { return &testInterfaceImpl{} }),
},
(*testInterface)(nil),
&testInterfaceImpl{},
},
}
for _, tt := range cases {
t.Run(tt.Name, func(t *testing.T) {
require := require.New(t)
result, err := Convert(reflect.TypeOf(tt.Target).Elem(), tt.Args...)
require.NoError(err)
require.Equal(tt.Expected, result)
})
}
}
type testInterface interface {
error
}
type testInterfaceImpl struct{}
func (*testInterfaceImpl) Error() string { return "hello" }