| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package logtest // import "go.opentelemetry.io/otel/log/logtest" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
|
| 9 |
"github.com/google/go-cmp/cmp" |
| 10 |
"github.com/google/go-cmp/cmp/cmpopts" |
| 11 |
|
| 12 |
"go.opentelemetry.io/otel/log" |
| 13 |
) |
| 14 |
|
| 15 |
// TestingT reports failure messages. |
| 16 |
// *testing.T implements this interface. |
| 17 |
type TestingT interface { |
| 18 |
Errorf(format string, args ...any) |
| 19 |
} |
| 20 |
|
| 21 |
// AssertEqual asserts that the two concrete data-types from the logtest package are equal. |
| 22 |
func AssertEqual[T Recording | Record](t TestingT, want, got T, opts ...AssertOption) bool { |
| 23 |
if h, ok := t.(interface{ Helper() }); ok { |
| 24 |
h.Helper() |
| 25 |
} |
| 26 |
|
| 27 |
var cfg assertConfig |
| 28 |
for _, opt := range opts { |
| 29 |
cfg = opt.apply(cfg) |
| 30 |
} |
| 31 |
|
| 32 |
cmpOpts := []cmp.Option{ |
| 33 |
cmp.Comparer(func(x, y context.Context) bool { return x == y }), // Compare context. |
| 34 |
cmpopts.SortSlices( |
| 35 |
func(a, b log.KeyValue) bool { return a.Key < b.Key }, |
| 36 |
), // Unordered compare of the key values. |
| 37 |
cmpopts.EquateEmpty(), // Empty and nil collections are equal. |
| 38 |
} |
| 39 |
cmpOpts = append(cmpOpts, cfg.cmpOpts...) |
| 40 |
|
| 41 |
if diff := cmp.Diff(want, got, cmpOpts...); diff != "" { |
| 42 |
msg := "mismatch (-want +got):\n%s" |
| 43 |
if cfg.msg != "" { |
| 44 |
msg = cfg.msg + "\n" + msg |
| 45 |
} |
| 46 |
|
| 47 |
args := make([]any, 0, len(cfg.args)+1) |
| 48 |
args = append(args, cfg.args...) |
| 49 |
args = append(args, diff) |
| 50 |
|
| 51 |
t.Errorf(msg, args...) |
| 52 |
return false |
| 53 |
} |
| 54 |
return true |
| 55 |
} |
| 56 |
|
| 57 |
type assertConfig struct { |
| 58 |
cmpOpts []cmp.Option |
| 59 |
msg string |
| 60 |
args []any |
| 61 |
} |
| 62 |
|
| 63 |
// AssertOption allows for fine grain control over how AssertEqual operates. |
| 64 |
type AssertOption interface { |
| 65 |
apply(cfg assertConfig) assertConfig |
| 66 |
} |
| 67 |
|
| 68 |
type fnOption func(cfg assertConfig) assertConfig |
| 69 |
|
| 70 |
func (fn fnOption) apply(cfg assertConfig) assertConfig { |
| 71 |
return fn(cfg) |
| 72 |
} |
| 73 |
|
| 74 |
// Transform applies a transformation f function that |
| 75 |
// converts values of a certain type into that of another. |
| 76 |
// f must not mutate A in any way. |
| 77 |
func Transform[A, B any](f func(A) B) AssertOption { |
| 78 |
return fnOption(func(cfg assertConfig) assertConfig { |
| 79 |
cfg.cmpOpts = append(cfg.cmpOpts, cmp.Transformer("", f)) |
| 80 |
return cfg |
| 81 |
}) |
| 82 |
} |
| 83 |
|
| 84 |
// Desc prepends the given text to an assertion failure message. |
| 85 |
// The text is formatted with the args using fmt.Sprintf. |
| 86 |
func Desc(text string, args ...any) AssertOption { |
| 87 |
return fnOption(func(cfg assertConfig) assertConfig { |
| 88 |
cfg.msg = text |
| 89 |
cfg.args = args |
| 90 |
return cfg |
| 91 |
}) |
| 92 |
} |