From 54b1bb7145eb4510915e5e2e390adcd6bc745cb7 Mon Sep 17 00:00:00 2001 From: guoguangwu Date: Thu, 25 May 2023 09:40:19 +0800 Subject: [PATCH] code optimization --- .gitignore | 2 ++ mock/mock.go | 2 +- mock/mock_test.go | 16 +++++++--------- suite/suite_test.go | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 5aacdb7cc..874c47329 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ _testmain.go *.exe .DS_Store + +.idea/ diff --git a/mock/mock.go b/mock/mock.go index f4b42e44f..3ef86e8a7 100644 --- a/mock/mock.go +++ b/mock/mock.go @@ -455,7 +455,7 @@ func (m *Mock) Called(arguments ...interface{}) Arguments { // For Ex: github_com_docker_libkv_store_mock.WatchTree.pN39_github_com_docker_libkv_store_mock.Mock // uses interface information unlike golang github.com/docker/libkv/store/mock.(*Mock).WatchTree // With GCCGO we need to remove interface information starting from pN
. - re := regexp.MustCompile("\\.pN\\d+_") + re := regexp.MustCompile(`\.pN\d+_`) if re.MatchString(functionPath) { functionPath = re.Split(functionPath, -1)[0] } diff --git a/mock/mock_test.go b/mock/mock_test.go index 9ceebbc44..9ec86ac8c 100644 --- a/mock/mock_test.go +++ b/mock/mock_test.go @@ -1624,9 +1624,7 @@ func TestIsArgsEqual(t *testing.T) { assert.False(t, isArgsEqual(expected, args)) var arr = make([]interface{}, 6) - for i := 0; i < len(expected); i++ { - arr[i] = expected[i] - } + copy(arr, expected) assert.True(t, isArgsEqual(expected, arr)) } @@ -1939,20 +1937,20 @@ func TestLoggingAssertExpectations(t *testing.T) { AssertExpectationsForObjects(tcl, m, new(TestExampleImplementation)) require.Equal(t, 1, len(tcl.errs)) - assert.Regexp(t, regexp.MustCompile("(?s)FAIL: 0 out of 1 expectation\\(s\\) were met.*The code you are testing needs to make 1 more call\\(s\\).*"), tcl.errs[0]) + assert.Regexp(t, regexp.MustCompile(`(?s)FAIL: 0 out of 1 expectation\(s\) were met.*The code you are testing needs to make 1 more call\(s\).*`), tcl.errs[0]) require.Equal(t, 2, len(tcl.logs)) - assert.Regexp(t, regexp.MustCompile("(?s)FAIL:\tGetTime\\(int\\).*"), tcl.logs[0]) + assert.Regexp(t, regexp.MustCompile(`(?s)FAIL:\tGetTime\(int\).*`), tcl.logs[0]) require.Equal(t, "Expectations didn't match for Mock: *mock.timer", tcl.logs[1]) } func TestAfterTotalWaitTimeWhileExecution(t *testing.T) { waitDuration := 1 - total, waitMs := 5, time.Millisecond*time.Duration(waitDuration) + total, wait := 5, time.Millisecond*time.Duration(waitDuration) aTimer := new(timer) for i := 0; i < total; i++ { - aTimer.On("GetTime", i).After(waitMs).Return(fmt.Sprintf("Time%d", i)).Once() + aTimer.On("GetTime", i).After(wait).Return(fmt.Sprintf("Time%d", i)).Once() } - time.Sleep(waitMs) + time.Sleep(wait) start := time.Now() var results []string @@ -1962,7 +1960,7 @@ func TestAfterTotalWaitTimeWhileExecution(t *testing.T) { end := time.Now() elapsedTime := end.Sub(start) - assert.True(t, elapsedTime > waitMs, fmt.Sprintf("Total elapsed time:%v should be atleast greater than %v", elapsedTime, waitMs)) + assert.True(t, elapsedTime > wait, fmt.Sprintf("Total elapsed time:%v should be atleast greater than %v", elapsedTime, wait)) assert.Equal(t, total, len(results)) for i := range results { assert.Equal(t, fmt.Sprintf("Time%d", i), results[i], "Return value of method should be same") diff --git a/suite/suite_test.go b/suite/suite_test.go index d684f52b9..f3094ee62 100644 --- a/suite/suite_test.go +++ b/suite/suite_test.go @@ -4,7 +4,7 @@ import ( "bytes" "errors" "flag" - "io/ioutil" + "io" "math/rand" "os" "os/exec" @@ -429,7 +429,7 @@ func (sc *StdoutCapture) StopCapture() (string, error) { } os.Stdout.Close() os.Stdout = sc.oldStdout - bytes, err := ioutil.ReadAll(sc.readPipe) + bytes, err := io.ReadAll(sc.readPipe) if err != nil { return "", err }