-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnaming_test.go
More file actions
109 lines (96 loc) · 2.56 KB
/
Copy pathnaming_test.go
File metadata and controls
109 lines (96 loc) · 2.56 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package morph_test
import (
"testing"
"github.com/seeruk/morph"
"github.com/seeruk/morph/spec"
"github.com/seeruk/morph/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestMapperName(t *testing.T) {
input := morph.NameInput{
Source: types.Type{
Name: "User",
Package: types.PackageRef{Name: "source"},
},
Target: types.Type{
Name: "Person",
Package: types.PackageRef{Name: "target"},
},
Signature: spec.MapperSignature{
Accepts: spec.ParameterKindPointer,
Returns: spec.ParameterKindValue,
},
}
tests := []struct {
name string
templ string
want string
}{
{
name: "renders type names",
templ: "Map{{ .Source.Type }}To{{ .Target.Type }}",
want: "MapUserToPerson",
},
{
name: "renders package and type names",
templ: "Map{{ .Source.Package }}{{ .Source.Type }}To{{ .Target.Package }}{{ .Target.Type }}",
want: "MapSourceUserToTargetPerson",
},
{
name: "renders signature names as function name parts",
templ: "Map{{ .Source.Type }}{{ .Signature.Accepts }}To{{ .Target.Type }}{{ .Signature.Returns }}",
want: "MapUserPointerToPersonValue",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := morph.MapperName(input, tt.templ)
require.NoError(t, err)
assert.Equal(t, tt.want, got)
})
}
t.Run("should use zero-value signature parts as value", func(t *testing.T) {
input := morph.NameInput{
Source: types.Type{Name: "User"},
Target: types.Type{Name: "Person"},
}
got, err := morph.MapperName(input, "Map{{ .Source.Type }}{{ .Signature.Accepts }}To{{ .Target.Type }}{{ .Signature.Returns }}")
require.NoError(t, err)
assert.Equal(t, "MapUserValueToPersonValue", got)
})
}
func TestMapperName_Error(t *testing.T) {
input := morph.NameInput{
Source: types.Type{Name: "User"},
Target: types.Type{Name: "Person"},
Signature: spec.MapperSignature{
Accepts: spec.ParameterKindPointer,
Returns: spec.ParameterKindValue,
},
}
tests := []struct {
name string
templ string
wantErrMsg string
}{
{
name: "returns parse errors",
templ: "Map{{ if }}",
wantErrMsg: "failed to parse template",
},
{
name: "returns execute errors",
templ: "Map{{ .Source.Name }}To{{ .Target.Name }}",
wantErrMsg: "failed to execute template",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := morph.MapperName(input, tt.templ)
require.Error(t, err)
assert.Empty(t, got)
assert.ErrorContains(t, err, tt.wantErrMsg)
})
}
}