@@ -5,10 +5,80 @@ import (
5
5
"testing"
6
6
)
7
7
8
- type stringMapperMapping struct {
9
- Input string
10
- Output string
11
- Err error
8
+ type stringPrefixMapperTestCase struct {
9
+ InputPattern string
10
+ OutputPattern string
11
+ Mappings []stringPrefixMapperMapping
12
+ }
13
+
14
+ type stringPrefixMapperMapping struct {
15
+ Input string
16
+ Mapped string
17
+ Matched string
18
+ Tail string
19
+ Err error
20
+ }
21
+
22
+ func TestMapStringPrefix (t * testing.T ) {
23
+ testCases := []stringPrefixMapperTestCase {
24
+ {
25
+ InputPattern : "/(?P<first>.+)/(?P<second>.+)/" ,
26
+ OutputPattern : "{{.second}}/{{.first}}" ,
27
+ Mappings : []stringPrefixMapperMapping {
28
+ {"/first/second/" , "second/first" , "/first/second/" , "" , nil },
29
+ },
30
+ },
31
+ {
32
+ InputPattern : "/prefix/(?P<first>.+)/(?P<second>.+)/" ,
33
+ OutputPattern : "/newprefix/{{.second}}/{{.first}}" ,
34
+ Mappings : []stringPrefixMapperMapping {
35
+ {"/prefix/first/second/" , "/newprefix/second/first" , "/prefix/first/second/" , "" , nil },
36
+ {"/prefix/first/" , "" , "" , "" , errors .New ("Error: prefix not matched" )},
37
+ {"/prefix/first/second/third" , "/newprefix/second/first" , "/prefix/first/second/" , "third" , nil },
38
+ },
39
+ },
40
+ {
41
+ InputPattern : "/hardcodedprefix/" ,
42
+ OutputPattern : "/newprefix/" ,
43
+ Mappings : []stringPrefixMapperMapping {
44
+ {"/hardcodedprefix/" , "/newprefix/" , "/hardcodedprefix/" , "" , nil },
45
+ {"/foo/" , "" , "" , "" , errors .New ("Error: prefix not matched" )},
46
+ {"/hardcodedprefix/sub/path" , "/newprefix/" , "/hardcodedprefix/" , "sub/path" , nil },
47
+ },
48
+ },
49
+ }
50
+
51
+ for _ , testCase := range testCases {
52
+ t .Logf (`Testing "%s" -> "%s"` , testCase .InputPattern , testCase .OutputPattern )
53
+ m , err := NewStringMapper (testCase .InputPattern , testCase .OutputPattern )
54
+ if err != nil {
55
+ t .Fatalf ("Unable to create new StringMapper due to error: %s" , err .Error ())
56
+ }
57
+
58
+ for _ , mapping := range testCase .Mappings {
59
+ t .Logf (" mapping string %s" , mapping .Input )
60
+
61
+ actualMapped , actualMatched , actualTail , err := m .MapStringPrefix (mapping .Input )
62
+ if err == nil && mapping .Err == nil {
63
+ if actualMapped != mapping .Mapped {
64
+ t .Errorf ("Mapped: %s != %s" , mapping .Mapped , actualMapped )
65
+ }
66
+ if actualMatched != mapping .Matched {
67
+ t .Errorf ("Matched: %s != %s" , mapping .Matched , actualMatched )
68
+ }
69
+ if actualTail != mapping .Tail {
70
+ t .Errorf ("Tail: %s != %s" , mapping .Tail , actualTail )
71
+ }
72
+ } else if err == nil || mapping .Err == nil || err .Error () != mapping .Err .Error () {
73
+ if err != nil {
74
+ t .Errorf ("Did not expect error: %s" , err .Error ())
75
+ }
76
+ if mapping .Err != nil {
77
+ t .Errorf ("Expected error: %s" , mapping .Err .Error ())
78
+ }
79
+ }
80
+ }
81
+ }
12
82
}
13
83
14
84
type stringMapperTestCase struct {
@@ -17,21 +87,18 @@ type stringMapperTestCase struct {
17
87
Mappings []stringMapperMapping
18
88
}
19
89
90
+ type stringMapperMapping struct {
91
+ Input string
92
+ Output string
93
+ Err error
94
+ }
95
+
20
96
func TestMapString (t * testing.T ) {
21
97
testCases := []stringMapperTestCase {
22
- {
23
- InputPattern : "/(?P<first>.+)/(?P<second>.+)/" ,
24
- OutputPattern : "{{.second}}/{{.first}}" ,
25
- Mappings : []stringMapperMapping {
26
- {"/first/second/" , "second/first" , nil },
27
- },
28
- },
29
98
{
30
99
InputPattern : "/prefix/(?P<first>.+)/(?P<second>.+)/" ,
31
100
OutputPattern : "/newprefix/{{.second}}/{{.first}}" ,
32
101
Mappings : []stringMapperMapping {
33
- {"/prefix/first/second/" , "/newprefix/second/first" , nil },
34
- {"/prefix/first/" , "" , errors .New ("Error: prefix not matched" )},
35
102
{"/prefix/first/second/third" , "" , errors .New ("Error: not matched" )},
36
103
},
37
104
},
0 commit comments