Skip to content

Commit 870caf5

Browse files
committed
test: Completely override return handling
1 parent 2268163 commit 870caf5

File tree

2 files changed

+60
-43
lines changed

2 files changed

+60
-43
lines changed

internal/updater/update_test.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ func TestEncodeEscapeChars(t *testing.T) {
1717
},
1818
{
1919
input: `Hello\tworld`,
20-
expected: `Hello【\\】tworld`,
20+
expected: `Hello【&】tworld`,
2121
},
2222
{
2323
input: `This is a test\nwith new line`,
24-
expected: `This is a test【\\】nwith new line`,
24+
expected: `This is a test【&】nwith new line`,
2525
},
2626
{
2727
input: `Some\rtext`,
28-
expected: `Some【\\】rtext`,
28+
expected: `Some【&】rtext`,
2929
},
3030
{
3131
input: `\t\s\r\n`,
32-
expected: `【\\】t【\\】s【\\】r【\\】n`,
32+
expected: `【&】t【&】s【&】r【&】n`,
3333
},
3434
}
3535

@@ -51,19 +51,19 @@ func TestDecodeEscapeChars(t *testing.T) {
5151
expected: "",
5252
},
5353
{
54-
input: `Hello【\】tworld`,
54+
input: `Hello【&】tworld`,
5555
expected: `Hello\tworld`,
5656
},
5757
{
58-
input: `This is a test【\】nwith new line`,
58+
input: `This is a test【&】nwith new line`,
5959
expected: `This is a test\nwith new line`,
6060
},
6161
{
62-
input: `Some【\】rtext`,
62+
input: `Some【&】rtext`,
6363
expected: `Some\rtext`,
6464
},
6565
{
66-
input: `【\】t【\】r【\】n【\】s`,
66+
input: `【&】t【&】r【&】n【&】s`,
6767
expected: `\t\r\n\s`,
6868
},
6969
}
@@ -81,50 +81,62 @@ func TestFixReturn(t *testing.T) {
8181
input string
8282
expected string
8383
}{
84-
{
84+
{ // empty
8585
input: "",
8686
expected: "",
8787
},
88-
{
88+
{ // add quotes to strings
8989
input: "return 200 ok;",
9090
expected: "return 200 \"ok\";",
9191
},
92-
{
92+
{ // add quotes to strings
9393
input: "return 200 $content;",
9494
expected: "return 200 \"$content\";",
9595
},
96-
{
96+
{ // keep it as it is
9797
input: "return BACKEND\n;",
9898
expected: "return BACKEND;",
9999
},
100-
{
100+
{ // keep it as it is
101+
input: "return 200 \"BACKEND B:$uri\n\";",
102+
expected: "return 200 \"BACKEND B:$uri\n\";",
103+
},
104+
{ // keep it as it is
101105
input: "return 200;",
102106
expected: "return 200;",
103107
},
104-
{
108+
{ // trim
105109
input: "return 200 ;",
106110
expected: "return 200;",
107111
},
108-
{
109-
input: "return \"ok\";",
112+
{ // trim
113+
input: "return \"ok\" ; ",
110114
expected: "return \"ok\";",
111115
},
112-
{
113-
input: "return 200 \"ok\";",
116+
{ // trim
117+
input: "return 200 \"ok\" ; ",
114118
expected: "return 200 \"ok\";",
115119
},
116120
{
117-
input: "return 200 \"1\" ;",
118-
expected: "return 200 \"1\";",
121+
input: "return 200 \"1 1 1\" ;",
122+
expected: "return 200 \"1 1 1\";",
123+
},
124+
{
125+
input: "return \"1\n\" ;",
126+
expected: "return \"1\n\";",
127+
},
128+
{
129+
input: "return \"1\n 11\" ;",
130+
expected: "return \"1\n 11\";",
119131
},
120132
{
121-
input: "return \"1\" ;",
122-
expected: "return \"1\";",
133+
input: "return \"1\n\t\r 11\n\" ;",
134+
expected: "return \"1\n\t\r 11\n\";",
123135
},
124136
}
125137

126138
for _, tc := range testCases {
127-
output := updater.FixVars(updater.FixReturn(updater.EncodeEscapeChars(tc.input)))
139+
output := updater.FixReturn(updater.FixVars(updater.EncodeEscapeChars(tc.input)))
128140
if output != tc.expected {
129141
t.Errorf("Unexpected output. Input: %s, Expected: %s, Output: %s", tc.input, tc.expected, (updater.DecodeEscapeChars(output)))
130142
}

internal/updater/updater.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,53 +9,58 @@ import (
99
)
1010

1111
func EncodeEscapeChars(s string) string {
12-
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, `\t`, `【\\】t`), `\s`, `【\\】s`), `\r`, `【\\】r`), `\n`, `【\\】n`)
12+
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, `\t`, `【&】t`), `\s`, `【&】s`), `\r`, `【&】r`), `\n`, `【&】n`)
1313
}
1414

1515
func DecodeEscapeChars(s string) string {
16-
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, `【\】t`, `\t`), `【\】s`, `\s`), `【\】r`, `\r`), `【\】n`, `\n`)
16+
return strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(strings.ReplaceAll(s, `【&】t`, `\t`), `【&】s`, `\s`), `【&】r`, `\r`), `【&】n`, `\n`)
1717
}
1818

1919
func FixReturn(s string) string {
20-
s = strings.TrimSpace(s)
21-
if s == "" {
22-
return s
23-
}
2420

2521
var scene1 = regexp.MustCompile(`return\s+(\d+)\s+(\S+)\s*;`)
2622
var scene2 = regexp.MustCompile(`return\s+(\d+)\s+"(\S+)"\s*;`)
2723
var scene3 = regexp.MustCompile(`return\s+(\S+)\s*;`)
2824
var scene4 = regexp.MustCompile(`return\s+"(\S+)"\s*;`)
2925
var scene5 = regexp.MustCompile(`return\s+(\d+)\s*;`)
26+
var scene6 = regexp.MustCompile(`return\s+"(.+)"\s*;`)
27+
var scene7 = regexp.MustCompile(`return\s+(\d+)\s+"(.+)"\s*;`)
28+
var scene8 = regexp.MustCompile(`return\s+(\d+)\s+"([\s|\S|\n|\r|\t]+)"\s*;`)
29+
var scene9 = regexp.MustCompile(`return\s+"([\s|\S|\n|\r|\t]+)"\s*;`)
3030

3131
if scene1.MatchString(s) {
3232
if scene2.MatchString(s) { // eg: `return 200 "ok";`
33-
s = scene2.ReplaceAllString(s, "$1 \"$2\"")
33+
return strings.TrimSpace(scene2.ReplaceAllString(s, "return $1 \"$2\";"))
3434
} else { // eg: `return 200 $content;`
35-
s = scene1.ReplaceAllString(s, "$1 \"$2\"")
35+
return strings.TrimSpace(scene1.ReplaceAllString(s, "return $1 \"$2\";"))
3636
}
3737
} else if scene3.MatchString(s) {
38-
fmt.Println("!!")
3938
if scene5.MatchString(s) { // eg: `return 200;`
40-
s = scene5.ReplaceAllString(s, "$1")
41-
} else if scene4.MatchString(s) { // eg: `return "ok";`
42-
fmt.Println("!!!!!", s)
43-
44-
s = scene4.ReplaceAllString(s, "\"$1\"")
39+
return strings.TrimSpace(scene5.ReplaceAllString(s, "return $1;"))
40+
} else if scene6.MatchString(s) { // eg: `return "ok";`
41+
if scene4.MatchString(s) {
42+
return strings.TrimSpace(scene4.ReplaceAllString(s, "return \"$1\";"))
43+
} else {
44+
return strings.TrimSpace(scene6.ReplaceAllString(s, "return \"$1\";"))
45+
}
4546
} else { // eg: `return BACKEND\n;`
46-
4747
found := scene3.FindString(s)
4848
if !(strings.HasPrefix(found, `"`) && strings.HasSuffix(found, `"`)) {
49-
s = scene3.ReplaceAllString(s, "$1")
49+
return strings.TrimSpace(scene3.ReplaceAllString(s, "return $1;"))
5050
} else {
51-
s = scene3.ReplaceAllString(s, "\"$1\"")
51+
return strings.TrimSpace(scene3.ReplaceAllString(s, "return \"$1\";"))
5252
}
5353
}
5454
} else {
55-
fmt.Println("---", s)
56-
55+
if scene7.MatchString(s) {
56+
return strings.TrimSpace(scene7.ReplaceAllString(s, "return $1 \"$2\";"))
57+
} else if scene8.MatchString(s) {
58+
return strings.TrimSpace(scene8.ReplaceAllString(s, "return $1 \"$2\";"))
59+
} else if scene9.MatchString(s) {
60+
return strings.TrimSpace(scene9.ReplaceAllString(s, "return \"$1\";"))
61+
}
5762
}
58-
return "return " + strings.TrimSpace(s) + ";"
63+
return s
5964
}
6065

6166
func FixVars(s string) string {

0 commit comments

Comments
 (0)