1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-08-10 22:31:50 +02:00

log/logtest: change AssertEqual to accept TestingT for benchmark support (#6908)

This commit is contained in:
Flc゛
2025-07-07 18:13:53 +08:00
committed by GitHub
parent bd26298747
commit 186bd18db4
3 changed files with 19 additions and 13 deletions

View File

@@ -40,6 +40,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `RPCGRPCRequestMetadata`
- `RPCGRPCResponseMetadata`
### Changed
- Change `AssertEqual` in `go.opentelemetry.io/otel/log/logtest` to accept `TestingT` in order to support benchmarks and fuzz tests. (#6908)
<!-- Released section -->
<!-- Don't change this section unless doing release -->

View File

@@ -5,7 +5,6 @@ package logtest // import "go.opentelemetry.io/otel/log/logtest"
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
@@ -13,19 +12,14 @@ import (
"go.opentelemetry.io/otel/log"
)
// AssertEqual asserts that the two concrete data-types from the logtest package are equal.
func AssertEqual[T Recording | Record](t *testing.T, want, got T, opts ...AssertOption) bool {
t.Helper()
return assertEqual(t, want, got, opts...)
}
// testingT reports failure messages.
// TestingT reports failure messages.
// *testing.T implements this interface.
type testingT interface {
type TestingT interface {
Errorf(format string, args ...any)
}
func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOption) bool {
// AssertEqual asserts that the two concrete data-types from the logtest package are equal.
func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOption) bool {
if h, ok := t.(interface{ Helper() }); ok {
h.Helper()
}

View File

@@ -17,6 +17,14 @@ import (
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
// Compile-time check to ensure testing structs implement TestingT.
var (
_ TestingT = (*testing.T)(nil)
_ TestingT = (*testing.B)(nil)
_ TestingT = (*testing.F)(nil)
_ TestingT = (*mockTestingT)(nil)
)
type mockTestingT struct {
errors []string
}
@@ -114,7 +122,7 @@ func TestAssertEqualRecording(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mockT := &mockTestingT{}
result := assertEqual(mockT, tc.a, tc.b, tc.opts...)
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...)
if result != tc.want {
t.Errorf("AssertEqual() = %v, want %v", result, tc.want)
}
@@ -178,7 +186,7 @@ func TestAssertEqualRecord(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
mockT := &mockTestingT{}
result := assertEqual(mockT, tc.a, tc.b, tc.opts...)
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...)
if result != tc.want {
t.Errorf("AssertEqual() = %v, want %v", result, tc.want)
}
@@ -198,7 +206,7 @@ func TestDesc(t *testing.T) {
Attributes: []log.KeyValue{log.Int("n", 1)},
}
assertEqual(mockT, a, b, Desc("custom message, %s", "test"))
AssertEqual(mockT, a, b, Desc("custom message, %s", "test"))
require.Len(t, mockT.errors, 1, "expected one error")
assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message")