logtest: add Desc (#6796)
Fixes https://github.com/open-telemetry/opentelemetry-go/issues/6490 Fixes https://github.com/open-telemetry/opentelemetry-go/issues/6341
This commit is contained in:
parent
d2fff768dc
commit
3f85c35038
|
@ -26,6 +26,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||
The package contains semantic conventions from the `v1.32.0` version of the OpenTelemetry Semantic Conventions.
|
||||
See the [migration documentation](./semconv/v1.32.0/MIGRATION.md) for information on how to upgrade from `go.opentelemetry.io/otel/semconv/v1.31.0`(#6782)
|
||||
- Add `Transform` option in `go.opentelemetry.io/otel/log/logtest`. (#6794)
|
||||
- Add `Desc` option in `go.opentelemetry.io/otel/log/logtest`. (#6796)
|
||||
|
||||
### Removed
|
||||
|
||||
|
|
|
@ -45,7 +45,16 @@ func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOp
|
|||
cmpOpts = append(cmpOpts, cfg.cmpOpts...)
|
||||
|
||||
if diff := cmp.Diff(want, got, cmpOpts...); diff != "" {
|
||||
t.Errorf("mismatch (-want +got):\n%s", diff)
|
||||
msg := "mismatch (-want +got):\n%s"
|
||||
if cfg.msg != "" {
|
||||
msg = cfg.msg + "\n" + msg
|
||||
}
|
||||
|
||||
args := make([]any, 0, len(cfg.args)+1)
|
||||
args = append(args, cfg.args...)
|
||||
args = append(args, diff)
|
||||
|
||||
t.Errorf(msg, args...)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
@ -53,6 +62,8 @@ func assertEqual[T Recording | Record](t testingT, want, got T, opts ...AssertOp
|
|||
|
||||
type assertConfig struct {
|
||||
cmpOpts []cmp.Option
|
||||
msg string
|
||||
args []any
|
||||
}
|
||||
|
||||
// AssertOption allows for fine grain control over how AssertEqual operates.
|
||||
|
@ -75,3 +86,13 @@ func Transform[A, B any](f func(A) B) AssertOption {
|
|||
return cfg
|
||||
})
|
||||
}
|
||||
|
||||
// Desc prepends the given text to an assertion failure message.
|
||||
// The text is formatted with the args using fmt.Sprintf.
|
||||
func Desc(text string, args ...any) AssertOption {
|
||||
return fnOption(func(cfg assertConfig) assertConfig {
|
||||
cfg.msg = text
|
||||
cfg.args = args
|
||||
return cfg
|
||||
})
|
||||
}
|
||||
|
|
|
@ -5,10 +5,12 @@ package logtest
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"go.opentelemetry.io/otel/log"
|
||||
)
|
||||
|
@ -20,7 +22,7 @@ type mockTestingT struct {
|
|||
}
|
||||
|
||||
func (m *mockTestingT) Errorf(format string, args ...any) {
|
||||
m.errors = append(m.errors, format)
|
||||
m.errors = append(m.errors, fmt.Sprintf(format, args...))
|
||||
}
|
||||
|
||||
func TestAssertEqual(t *testing.T) {
|
||||
|
@ -186,3 +188,18 @@ func TestAssertEqualRecord(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDesc(t *testing.T) {
|
||||
mockT := &mockTestingT{}
|
||||
a := Record{
|
||||
Attributes: []log.KeyValue{log.String("foo", "bar")},
|
||||
}
|
||||
b := Record{
|
||||
Attributes: []log.KeyValue{log.Int("n", 1)},
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue