forked from cloudwego/kitex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): perf optimize and log loc correct (cloudwego#1455)
- Loading branch information
1 parent
d499a63
commit a2e29ea
Showing
3 changed files
with
141 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright 2024 CloudWeGo Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
type mockTesting struct { | ||
t *testing.T | ||
|
||
expect0 string | ||
expect1 string | ||
|
||
helper bool | ||
} | ||
|
||
func (m *mockTesting) Reset() { | ||
m.expect0 = "" | ||
m.expect1 = "" | ||
m.helper = false | ||
} | ||
|
||
func (m *mockTesting) ExpectFatal(args ...interface{}) { | ||
m.expect0 = fmt.Sprint(args...) | ||
} | ||
|
||
func (m *mockTesting) ExpectFatalf(format string, args ...interface{}) { | ||
m.expect1 = fmt.Sprintf(format, args...) | ||
} | ||
|
||
func (m *mockTesting) Fatal(args ...interface{}) { | ||
t := m.t | ||
t.Helper() | ||
if !m.helper { | ||
t.Fatal("need to call Helper before calling Fatal") | ||
} | ||
if s := fmt.Sprint(args...); s != m.expect0 { | ||
t.Fatalf("got %q expect %q", s, m.expect0) | ||
} | ||
} | ||
|
||
func (m *mockTesting) Fatalf(format string, args ...interface{}) { | ||
t := m.t | ||
t.Helper() | ||
if !m.helper { | ||
t.Fatal("need to call Helper before calling Fatalf") | ||
} | ||
if s := fmt.Sprintf(format, args...); s != m.expect1 { | ||
t.Fatalf("got %q expect %q", s, m.expect1) | ||
} | ||
} | ||
|
||
func (m *mockTesting) Helper() { m.helper = true } | ||
|
||
func TestAssert(t *testing.T) { | ||
m := &mockTesting{t: t} | ||
|
||
m.Reset() | ||
m.ExpectFatal("assertion failed") | ||
Assert(m, false) | ||
|
||
m.Reset() | ||
m.ExpectFatal("assertion failed: hello") | ||
Assert(m, false, "hello") | ||
|
||
m.Reset() | ||
m.ExpectFatalf("assert: %s", "hello") | ||
Assertf(m, false, "assert: %s", "hello") | ||
|
||
m.Reset() | ||
m.ExpectFatalf("assertion failed: 1 != 2") | ||
DeepEqual(m, 1, 2) | ||
|
||
m.Reset() | ||
m.ExpectFatal("") | ||
Panic(m, func() { panic("hello") }) | ||
|
||
m.Reset() | ||
m.ExpectFatal("assertion failed: did not panic") | ||
Panic(m, func() {}) | ||
|
||
m.Reset() | ||
m.ExpectFatal("assertion failed: did not panic") | ||
PanicAt(m, func() {}, func(err interface{}) bool { return true }) | ||
|
||
m.Reset() | ||
m.ExpectFatal("assertion failed: panic but not expected") | ||
PanicAt(m, func() { panic("hello") }, func(err interface{}) bool { return false }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters