-
Notifications
You must be signed in to change notification settings - Fork 2
/
stringmapper_test.go
132 lines (119 loc) · 3.82 KB
/
stringmapper_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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package goredirect
import (
"errors"
"testing"
)
type stringPrefixMapperTestCase struct {
InputPattern string
OutputPattern string
Mappings []stringPrefixMapperMapping
}
type stringPrefixMapperMapping struct {
Input string
Mapped string
Matched string
Tail string
Err error
}
func TestMapStringPrefix(t *testing.T) {
testCases := []stringPrefixMapperTestCase{
{
InputPattern: "/(?P<first>.+)/(?P<second>.+)/",
OutputPattern: "{{.second}}/{{.first}}",
Mappings: []stringPrefixMapperMapping{
{"/first/second/", "second/first", "/first/second/", "", nil},
},
},
{
InputPattern: "/prefix/(?P<first>.+)/(?P<second>.+)/",
OutputPattern: "/newprefix/{{.second}}/{{.first}}",
Mappings: []stringPrefixMapperMapping{
{"/prefix/first/second/", "/newprefix/second/first", "/prefix/first/second/", "", nil},
{"/prefix/first/", "", "", "", errors.New("Error: prefix not matched")},
{"/prefix/first/second/third", "/newprefix/second/first", "/prefix/first/second/", "third", nil},
},
},
{
InputPattern: "/hardcodedprefix/",
OutputPattern: "/newprefix/",
Mappings: []stringPrefixMapperMapping{
{"/hardcodedprefix/", "/newprefix/", "/hardcodedprefix/", "", nil},
{"/foo/", "", "", "", errors.New("Error: prefix not matched")},
{"/hardcodedprefix/sub/path", "/newprefix/", "/hardcodedprefix/", "sub/path", nil},
},
},
}
for _, testCase := range testCases {
t.Logf(`Testing "%s" -> "%s"`, testCase.InputPattern, testCase.OutputPattern)
m, err := NewStringMapper(testCase.InputPattern, testCase.OutputPattern)
if err != nil {
t.Fatalf("Unable to create new StringMapper due to error: %s", err.Error())
}
for _, mapping := range testCase.Mappings {
t.Logf(" mapping string %s", mapping.Input)
actualMapped, actualMatched, actualTail, err := m.MapStringPrefix(mapping.Input)
if err == nil && mapping.Err == nil {
if actualMapped != mapping.Mapped {
t.Errorf("Mapped: %s != %s", mapping.Mapped, actualMapped)
}
if actualMatched != mapping.Matched {
t.Errorf("Matched: %s != %s", mapping.Matched, actualMatched)
}
if actualTail != mapping.Tail {
t.Errorf("Tail: %s != %s", mapping.Tail, actualTail)
}
} else if err == nil || mapping.Err == nil || err.Error() != mapping.Err.Error() {
if err != nil {
t.Errorf("Did not expect error: %s", err.Error())
}
if mapping.Err != nil {
t.Errorf("Expected error: %s", mapping.Err.Error())
}
}
}
}
}
type stringMapperTestCase struct {
InputPattern string
OutputPattern string
Mappings []stringMapperMapping
}
type stringMapperMapping struct {
Input string
Output string
Err error
}
func TestMapString(t *testing.T) {
testCases := []stringMapperTestCase{
{
InputPattern: "/prefix/(?P<first>.+)/(?P<second>.+)/",
OutputPattern: "/newprefix/{{.second}}/{{.first}}",
Mappings: []stringMapperMapping{
{"/prefix/first/second/third", "", errors.New("Error: not matched")},
},
},
}
for _, testCase := range testCases {
t.Logf(`Testing "%s" -> "%s"`, testCase.InputPattern, testCase.OutputPattern)
m, err := NewStringMapper(testCase.InputPattern, testCase.OutputPattern)
if err != nil {
t.Fatalf("Unable to create new StringMapper due to error: %s", err.Error())
}
for _, mapping := range testCase.Mappings {
t.Logf(" mapping string %s", mapping.Input)
actualOutput, err := m.MapString(mapping.Input)
if err == nil && mapping.Err == nil {
if actualOutput != mapping.Output {
t.Errorf("Expected %s but was %s", mapping.Output, actualOutput)
}
} else if err == nil || mapping.Err == nil || err.Error() != mapping.Err.Error() {
if err != nil {
t.Errorf("Did not expect error: %s", err.Error())
}
if mapping.Err != nil {
t.Errorf("Expected error: %s", mapping.Err.Error())
}
}
}
}
}