Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->

Expand Down
14 changes: 4 additions & 10 deletions log/logtest/assert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@ 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"

"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()
}
Expand Down
14 changes: 11 additions & 3 deletions log/logtest/assert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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")
Expand Down
Loading