-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhook_test.go
57 lines (50 loc) · 1.33 KB
/
hook_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
package anymapper
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type customType struct {
foo string
}
func (c *customType) MapFrom(m *Mapper, src reflect.Value) error {
return m.MapRefl(src, reflect.ValueOf(&c.foo))
}
func (c customType) MapTo(m *Mapper, dst reflect.Value) error {
return m.MapRefl(reflect.ValueOf(c.foo), dst)
}
func TestCustomType(t *testing.T) {
m := New()
m.Hooks = MappingInterfaceHooks
t.Run("mapFrom", func(t *testing.T) {
var dst customType
require.NoError(t, m.Map("foo", &dst))
assert.Equal(t, "foo", dst.foo)
})
t.Run("mapTo", func(t *testing.T) {
var dst string
require.NoError(t, m.Map(customType{foo: "foo"}, &dst))
assert.Equal(t, "foo", dst)
})
t.Run("mapFromPtr", func(t *testing.T) {
var dst *customType
require.NoError(t, m.Map("foo", &dst))
assert.Equal(t, "foo", dst.foo)
})
t.Run("mapToPtr", func(t *testing.T) {
var dst string
require.NoError(t, m.Map(&customType{foo: "foo"}, &dst))
assert.Equal(t, "foo", dst)
})
t.Run("mapToAny", func(t *testing.T) {
var dst any
require.NoError(t, m.Map(&customType{foo: "foo"}, &dst))
assert.Equal(t, "foo", dst)
})
t.Run("both", func(t *testing.T) {
var dst customType
require.NoError(t, m.Map(customType{foo: "foo"}, &dst))
assert.Equal(t, "foo", dst.foo)
})
}