Skip to content

Commit 82a937d

Browse files
authored
chore: refactor unit tests (#5041)
1 parent 93c11a7 commit 82a937d

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

rest/httpx/responses.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ func doMarshalJson(v any) ([]byte, error) {
177177
// why not use json.Marshal? https://github.com/golang/go/issues/28453
178178
// it change the behavior of json.Marshal, like & -> \u0026, < -> \u003c, > -> \u003e
179179
// which is not what we want in logic response
180-
buf := &bytes.Buffer{}
181-
enc := json.NewEncoder(buf)
180+
var buf bytes.Buffer
181+
enc := json.NewEncoder(&buf)
182182
enc.SetEscapeHTML(false)
183183
if err := enc.Encode(v); err != nil {
184184
return nil, err

rest/httpx/responses_test.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@ import (
1212
"testing"
1313

1414
"github.com/stretchr/testify/assert"
15+
"github.com/zeromicro/go-zero/core/logx"
1516
"google.golang.org/grpc/codes"
1617
"google.golang.org/grpc/status"
17-
18-
"github.com/zeromicro/go-zero/core/logx"
1918
)
2019

2120
type message struct {
@@ -449,6 +448,7 @@ func Test_doMarshalJson(t *testing.T) {
449448
type args struct {
450449
v any
451450
}
451+
452452
tests := []struct {
453453
name string
454454
args args
@@ -532,15 +532,16 @@ func Test_doMarshalJson(t *testing.T) {
532532
wantErr: assert.NoError,
533533
},
534534
}
535+
535536
for _, tt := range tests {
536-
t.Run(
537-
tt.name, func(t *testing.T) {
538-
got, err := doMarshalJson(tt.args.v)
539-
if !tt.wantErr(t, err, fmt.Sprintf("doMarshalJson(%v)", tt.args.v)) {
540-
return
541-
}
542-
assert.Equalf(t, string(tt.want), string(got), "doMarshalJson(%v)", tt.args.v)
543-
},
544-
)
537+
tt := tt
538+
t.Run(tt.name, func(t *testing.T) {
539+
got, err := doMarshalJson(tt.args.v)
540+
if !tt.wantErr(t, err, fmt.Sprintf("doMarshalJson(%v)", tt.args.v)) {
541+
return
542+
}
543+
544+
assert.Equalf(t, string(tt.want), string(got), "doMarshalJson(%v)", tt.args.v)
545+
})
545546
}
546547
}

0 commit comments

Comments
 (0)