-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtemplated_custom_namer.go
73 lines (58 loc) · 2.29 KB
/
templated_custom_namer.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
70
71
72
73
package approvals
import (
"path/filepath"
"strings"
"github.com/approvals/go-approval-tests/core"
)
type templatedCustomNamer struct {
template string
testSourceDirectory string
relativeTestSourceDirectory string
approvalsSubdirectory string
testFileName string
testCaseName string
}
func CreateTemplatedCustomNamerCreator(template string) core.ApprovalNamerCreator {
return func(t core.Failable) core.ApprovalNamer {
return NewTemplatedCustomNamer(t, template)
}
}
func NewTemplatedCustomNamer(t core.Failable, template string) *templatedCustomNamer {
namer := &templatedCustomNamer{
template: template,
}
name, fileName := getApprovalName(t)
namer.fillParts(name, fileName)
return namer
}
// auto testSourceDirectory = "{TestSourceDirectory}";
// auto relativeTestSourceDirectory = "{RelativeTestSourceDirectory}";
// auto approvalsSubdirectory = "{ApprovalsSubdirectory}";
// auto testFileName = "{TestFileName}";
// auto testCaseName = "{TestCaseName}";
func (s *templatedCustomNamer) fillParts(name, filename string) {
s.testSourceDirectory = filepath.Dir(filename)
s.relativeTestSourceDirectory = "."
s.approvalsSubdirectory = defaultFolder
s.testFileName = strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename))
s.testCaseName = name
}
func (s *templatedCustomNamer) getFileName(extWithDot, approvedOrReceived string) string {
out := strings.ReplaceAll(s.template, "{TestSourceDirectory}", s.testSourceDirectory)
out = strings.ReplaceAll(out, "{RelativeTestSourceDirectory}", s.relativeTestSourceDirectory)
out = strings.ReplaceAll(out, "{ApprovalsSubdirectory}", s.approvalsSubdirectory)
out = strings.ReplaceAll(out, "{TestFileName}", s.testFileName)
out = strings.ReplaceAll(out, "{TestCaseName}", s.testCaseName)
out = strings.ReplaceAll(out, "{ApprovedOrReceived}", approvedOrReceived)
out = strings.ReplaceAll(out, "{FileExtension}", strings.TrimPrefix(extWithDot, "."))
return out
}
func (s *templatedCustomNamer) GetName() string {
return s.testCaseName
}
func (s *templatedCustomNamer) GetReceivedFile(extWithDot string) string {
return s.getFileName(extWithDot, "received")
}
func (s *templatedCustomNamer) GetApprovalFile(extWithDot string) string {
return s.getFileName(extWithDot, "approved")
}