From 82a937d5177938817ae994286de741915d0eda71 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Thu, 31 Jul 2025 20:10:45 +0800 Subject: [PATCH] chore: refactor unit tests (#5041) --- rest/httpx/responses.go | 4 ++-- rest/httpx/responses_test.go | 23 ++++++++++++----------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/rest/httpx/responses.go b/rest/httpx/responses.go index 4f3aaf8cd..28b806275 100644 --- a/rest/httpx/responses.go +++ b/rest/httpx/responses.go @@ -177,8 +177,8 @@ func doMarshalJson(v any) ([]byte, error) { // why not use json.Marshal? https://github.com/golang/go/issues/28453 // it change the behavior of json.Marshal, like & -> \u0026, < -> \u003c, > -> \u003e // which is not what we want in logic response - buf := &bytes.Buffer{} - enc := json.NewEncoder(buf) + var buf bytes.Buffer + enc := json.NewEncoder(&buf) enc.SetEscapeHTML(false) if err := enc.Encode(v); err != nil { return nil, err diff --git a/rest/httpx/responses_test.go b/rest/httpx/responses_test.go index b8492326b..6c6bc0a02 100644 --- a/rest/httpx/responses_test.go +++ b/rest/httpx/responses_test.go @@ -12,10 +12,9 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/zeromicro/go-zero/core/logx" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - - "github.com/zeromicro/go-zero/core/logx" ) type message struct { @@ -449,6 +448,7 @@ func Test_doMarshalJson(t *testing.T) { type args struct { v any } + tests := []struct { name string args args @@ -532,15 +532,16 @@ func Test_doMarshalJson(t *testing.T) { wantErr: assert.NoError, }, } + for _, tt := range tests { - t.Run( - tt.name, func(t *testing.T) { - got, err := doMarshalJson(tt.args.v) - if !tt.wantErr(t, err, fmt.Sprintf("doMarshalJson(%v)", tt.args.v)) { - return - } - assert.Equalf(t, string(tt.want), string(got), "doMarshalJson(%v)", tt.args.v) - }, - ) + tt := tt + t.Run(tt.name, func(t *testing.T) { + got, err := doMarshalJson(tt.args.v) + if !tt.wantErr(t, err, fmt.Sprintf("doMarshalJson(%v)", tt.args.v)) { + return + } + + assert.Equalf(t, string(tt.want), string(got), "doMarshalJson(%v)", tt.args.v) + }) } }