-
Notifications
You must be signed in to change notification settings - Fork 1.2k
log/logtest: Add AssertEqual and remove AssertRecordEqual #6662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
7872ec1
log/logtest: Remove AssertRecordEqual
pellared 1d96eca
log/logtest: Add AssertEqual
pellared 32fdc15
Add changelog entries
pellared 67789a5
Fix changelog
pellared 24230c0
fmt
pellared 2f6df16
Merge branch 'main' into logtest-assert
pellared 7ebd0d6
Merge branch 'main' into logtest-assert
pellared d0d4881
Merge branch 'main' into logtest-assert
pellared 841bd5e
Merge branch 'main' into logtest-assert
pellared d1cc2be
Merge branch 'main' into logtest-assert
pellared 88abdec
Merge branch 'main' into logtest-assert
pellared 1aa31d5
Merge branch 'main' into logtest-assert
pellared 11df802
Merge branch 'main' into logtest-assert
pellared 952e1f4
Merge branch 'main' into logtest-assert
pellared c0948aa
Merge branch 'main' into logtest-assert
pellared 2d4396f
Change AssertEqual signature to accept *testing.T
pellared 72717ea
Remove unused code
pellared 7758e7c
Fix TestAssertEqual
pellared 4fcb5ae
Merge branch 'main' into logtest-assert
pellared de514a9
Merge branch 'main' into logtest-assert
pellared e125c4b
Fix lint error
pellared 609eec8
Merge branch 'main' into logtest-assert
pellared File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package logtest // import "go.opentelemetry.io/otel/log/logtest" | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
|
||
"go.opentelemetry.io/otel/log" | ||
) | ||
|
||
// TestingT reports failure messages. | ||
// [testing.T] implements this interface. | ||
type TestingT interface { | ||
Errorf(format string, args ...any) | ||
} | ||
|
||
// 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() | ||
} | ||
|
||
cmpOpts := []cmp.Option{ | ||
cmp.Comparer(func(x, y context.Context) bool { return x == y }), // Compare context. | ||
cmpopts.SortSlices( | ||
func(a, b log.KeyValue) bool { return a.Key < b.Key }, | ||
), // Unordered compare of the key values. | ||
cmpopts.EquateEmpty(), // Empty and nil collections are equal. | ||
} | ||
|
||
cfg := newAssertConfig(opts) | ||
if cfg.ignoreTimestamp { | ||
cmpOpts = append(cmpOpts, cmpopts.IgnoreTypes(time.Time{})) // Ignore Timestamps. | ||
} | ||
|
||
if diff := cmp.Diff(want, got, cmpOpts...); diff != "" { | ||
t.Errorf("mismatch (-want +got):\n%s", diff) | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
type assertConfig struct { | ||
ignoreTimestamp bool | ||
} | ||
|
||
func newAssertConfig(opts []AssertOption) assertConfig { | ||
var cfg assertConfig | ||
for _, opt := range opts { | ||
cfg = opt.apply(cfg) | ||
} | ||
return cfg | ||
} | ||
|
||
// AssertOption allows for fine grain control over how AssertEqual operates. | ||
type AssertOption interface { | ||
apply(cfg assertConfig) assertConfig | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package logtest | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"go.opentelemetry.io/otel/log" | ||
) | ||
|
||
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) | ||
|
||
type mockTestingT struct { | ||
errors []string | ||
} | ||
|
||
func (m *mockTestingT) Errorf(format string, args ...any) { | ||
m.errors = append(m.errors, format) | ||
} | ||
|
||
func TestAssertEqualRecording(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
a Recording | ||
b Recording | ||
opts []AssertOption | ||
want bool | ||
}{ | ||
{ | ||
name: "equal recordings", | ||
a: Recording{ | ||
Scope{Name: t.Name()}: []Record{ | ||
{ | ||
Timestamp: y2k, | ||
Context: context.Background(), | ||
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, | ||
}, | ||
}, | ||
}, | ||
b: Recording{ | ||
Scope{Name: t.Name()}: []Record{ | ||
{ | ||
Timestamp: y2k, | ||
Context: context.Background(), | ||
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, | ||
}, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "different recordings", | ||
a: Recording{ | ||
Scope{Name: t.Name()}: []Record{ | ||
{Attributes: []log.KeyValue{log.String("foo", "bar")}}, | ||
}, | ||
}, | ||
b: Recording{ | ||
Scope{Name: t.Name()}: []Record{ | ||
{Attributes: []log.KeyValue{log.Int("n", 1)}}, | ||
}, | ||
}, | ||
want: false, | ||
}, | ||
{ | ||
name: "equal empty scopes", | ||
a: Recording{ | ||
Scope{Name: t.Name()}: nil, | ||
}, | ||
b: Recording{ | ||
Scope{Name: t.Name()}: []Record{}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "equal empty attributes", | ||
a: Recording{ | ||
Scope{Name: t.Name()}: []Record{ | ||
{Body: log.StringValue("msg"), Attributes: []log.KeyValue{}}, | ||
}, | ||
}, | ||
b: Recording{ | ||
Scope{Name: t.Name()}: []Record{ | ||
{Body: log.StringValue("msg"), Attributes: nil}, | ||
}, | ||
}, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockT := &mockTestingT{} | ||
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...) | ||
if result != tc.want { | ||
t.Errorf("AssertEqual() = %v, want %v", result, tc.want) | ||
} | ||
if !tc.want && len(mockT.errors) == 0 { | ||
t.Errorf("expected Errorf call but got none") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestAssertEqualRecord(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
a Record | ||
b Record | ||
opts []AssertOption | ||
want bool | ||
}{ | ||
{ | ||
name: "equal records", | ||
a: Record{ | ||
Timestamp: y2k, | ||
Context: context.Background(), | ||
Attributes: []log.KeyValue{log.Int("n", 1), log.String("foo", "bar")}, | ||
}, | ||
b: Record{ | ||
Timestamp: y2k, | ||
Context: context.Background(), | ||
Attributes: []log.KeyValue{log.String("foo", "bar"), log.Int("n", 1)}, | ||
}, | ||
want: true, | ||
}, | ||
{ | ||
name: "different records", | ||
a: Record{ | ||
Attributes: []log.KeyValue{log.String("foo", "bar")}, | ||
}, | ||
b: Record{ | ||
Attributes: []log.KeyValue{log.Int("n", 1)}, | ||
}, | ||
want: false, | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(tc.name, func(t *testing.T) { | ||
mockT := &mockTestingT{} | ||
result := AssertEqual(mockT, tc.a, tc.b, tc.opts...) | ||
if result != tc.want { | ||
t.Errorf("AssertEqual() = %v, want %v", result, tc.want) | ||
} | ||
if !tc.want && len(mockT.errors) == 0 { | ||
t.Errorf("expected Errorf call but got none") | ||
} | ||
}) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.