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
70
71
72
73
74
75
76
77
|
package mapper
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type unregisteredType struct{}
type testObject struct {
GivenName string
FamilyName string
}
type testModel struct {
Name string
}
func TestMapper(t *testing.T) {
Register[*testObject, *testModel](func(item *testObject) *testModel {
return &testModel{
Name: fmt.Sprintf("%v %v", item.GivenName, item.FamilyName),
}
})
t.Run("MapFrom", func(t *testing.T) {
t.Run("when the mapping is registered", func(t *testing.T) {
item := &testObject{
GivenName: "Tsuyoshi",
FamilyName: "Garret",
}
model := MapFrom[*testObject, *testModel](item)
require.NotNil(t, model)
assert.Equal(t, "Tsuyoshi Garret", model.Name)
})
t.Run("When the mapping is not registered", func(t *testing.T) {
item := &unregisteredType{}
model := MapFrom[*unregisteredType, *testModel](item)
assert.Nil(t, model)
})
})
t.Run("MapEachFrom", func(t *testing.T) {
t.Run("when the mapping is registered", func(t *testing.T) {
datum := []*testObject{
{GivenName: "Tsuyoshi", FamilyName: "Garret"},
{GivenName: "Takashi", FamilyName: "Shirogane"},
}
results := MapEachFrom[*testObject, *testModel](datum)
require.NotNil(t, results)
require.Equal(t, 2, len(results))
assert.Equal(t, "Tsuyoshi Garret", results[0].Name)
assert.Equal(t, "Takashi Shirogane", results[1].Name)
})
t.Run("when the mapping is not registered", func(t *testing.T) {
datum := []*unregisteredType{
{},
}
results := MapEachFrom[*unregisteredType, *testModel](datum)
require.NotNil(t, results)
assert.Equal(t, 0, len(results))
})
})
}
|