You've already forked opentelemetry-go
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:
@@ -40,6 +40,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
- `RPCGRPCRequestMetadata`
|
- `RPCGRPCRequestMetadata`
|
||||||
- `RPCGRPCResponseMetadata`
|
- `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 -->
|
<!-- Released section -->
|
||||||
<!-- Don't change this section unless doing release -->
|
<!-- Don't change this section unless doing release -->
|
||||||
|
|
||||||
|
@@ -5,7 +5,6 @@ package logtest // import "go.opentelemetry.io/otel/log/logtest"
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/google/go-cmp/cmp"
|
"github.com/google/go-cmp/cmp"
|
||||||
"github.com/google/go-cmp/cmp/cmpopts"
|
"github.com/google/go-cmp/cmp/cmpopts"
|
||||||
@@ -13,19 +12,14 @@ import (
|
|||||||
"go.opentelemetry.io/otel/log"
|
"go.opentelemetry.io/otel/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AssertEqual asserts that the two concrete data-types from the logtest package are equal.
|
// TestingT reports failure messages.
|
||||||
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.
|
|
||||||
// *testing.T implements this interface.
|
// *testing.T implements this interface.
|
||||||
type testingT interface {
|
type TestingT interface {
|
||||||
Errorf(format string, args ...any)
|
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 {
|
if h, ok := t.(interface{ Helper() }); ok {
|
||||||
h.Helper()
|
h.Helper()
|
||||||
}
|
}
|
||||||
|
@@ -17,6 +17,14 @@ import (
|
|||||||
|
|
||||||
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
|
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 {
|
type mockTestingT struct {
|
||||||
errors []string
|
errors []string
|
||||||
}
|
}
|
||||||
@@ -114,7 +122,7 @@ func TestAssertEqualRecording(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
mockT := &mockTestingT{}
|
mockT := &mockTestingT{}
|
||||||
result := assertEqual(mockT, tc.a, tc.b, tc.opts...)
|
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...)
|
||||||
if result != tc.want {
|
if result != tc.want {
|
||||||
t.Errorf("AssertEqual() = %v, want %v", 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 {
|
for _, tc := range tests {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
mockT := &mockTestingT{}
|
mockT := &mockTestingT{}
|
||||||
result := assertEqual(mockT, tc.a, tc.b, tc.opts...)
|
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...)
|
||||||
if result != tc.want {
|
if result != tc.want {
|
||||||
t.Errorf("AssertEqual() = %v, want %v", 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)},
|
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")
|
require.Len(t, mockT.errors, 1, "expected one error")
|
||||||
assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message")
|
assert.Contains(t, mockT.errors[0], "custom message, test\n", "expected custom message")
|
||||||
|
Reference in New Issue
Block a user