-
Notifications
You must be signed in to change notification settings - Fork 891
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GODRIVER-2725 Allow setting Encoder and Decoder options on a Client. (#…
…1282) Co-authored-by: Preston Vasquez <[email protected]>
- Loading branch information
1 parent
836d408
commit 41ebbc3
Showing
31 changed files
with
1,716 additions
and
697 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,90 @@ | ||
// Copyright (C) MongoDB, Inc. 2023-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// assertion_mongo.go contains MongoDB-specific extensions to the "assert" | ||
// package. | ||
|
||
package assert | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"unsafe" | ||
) | ||
|
||
// DifferentAddressRanges asserts that two byte slices reference distinct memory | ||
// address ranges, meaning they reference different underlying byte arrays. | ||
func DifferentAddressRanges(t TestingT, a, b []byte) (ok bool) { | ||
if h, ok := t.(tHelper); ok { | ||
h.Helper() | ||
} | ||
|
||
if len(a) == 0 || len(b) == 0 { | ||
return true | ||
} | ||
|
||
// Find the start and end memory addresses for the underlying byte array for | ||
// each input byte slice. | ||
sliceAddrRange := func(b []byte) (uintptr, uintptr) { | ||
sh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) | ||
return sh.Data, sh.Data + uintptr(sh.Cap-1) | ||
} | ||
aStart, aEnd := sliceAddrRange(a) | ||
bStart, bEnd := sliceAddrRange(b) | ||
|
||
// If "b" starts after "a" ends or "a" starts after "b" ends, there is no | ||
// overlap. | ||
if bStart > aEnd || aStart > bEnd { | ||
return true | ||
} | ||
|
||
// Otherwise, calculate the overlap start and end and print the memory | ||
// overlap error message. | ||
min := func(a, b uintptr) uintptr { | ||
if a < b { | ||
return a | ||
} | ||
return b | ||
} | ||
max := func(a, b uintptr) uintptr { | ||
if a > b { | ||
return a | ||
} | ||
return b | ||
} | ||
overlapLow := max(aStart, bStart) | ||
overlapHigh := min(aEnd, bEnd) | ||
|
||
t.Errorf("Byte slices point to the same underlying byte array:\n"+ | ||
"\ta addresses:\t%d ... %d\n"+ | ||
"\tb addresses:\t%d ... %d\n"+ | ||
"\toverlap:\t%d ... %d", | ||
aStart, aEnd, | ||
bStart, bEnd, | ||
overlapLow, overlapHigh) | ||
|
||
return false | ||
} | ||
|
||
// EqualBSON asserts that the expected and actual BSON binary values are equal. | ||
// If the values are not equal, it prints both the binary and Extended JSON diff | ||
// of the BSON values. The provided BSON value types must implement the | ||
// fmt.Stringer interface. | ||
func EqualBSON(t TestingT, expected, actual interface{}) bool { | ||
if h, ok := t.(tHelper); ok { | ||
h.Helper() | ||
} | ||
|
||
return Equal(t, | ||
expected, | ||
actual, | ||
`expected and actual BSON values do not match | ||
As Extended JSON: | ||
Expected: %s | ||
Actual : %s`, | ||
expected.(fmt.Stringer).String(), | ||
actual.(fmt.Stringer).String()) | ||
} |
This file contains 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,125 @@ | ||
// Copyright (C) MongoDB, Inc. 2023-present. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. You may obtain | ||
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
package assert | ||
|
||
import ( | ||
"testing" | ||
|
||
"go.mongodb.org/mongo-driver/bson" | ||
) | ||
|
||
func TestDifferentAddressRanges(t *testing.T) { | ||
t.Parallel() | ||
|
||
slice := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} | ||
|
||
testCases := []struct { | ||
name string | ||
a []byte | ||
b []byte | ||
want bool | ||
}{ | ||
{ | ||
name: "distinct byte slices", | ||
a: []byte{0, 1, 2, 3}, | ||
b: []byte{0, 1, 2, 3}, | ||
want: true, | ||
}, | ||
{ | ||
name: "same byte slice", | ||
a: slice, | ||
b: slice, | ||
want: false, | ||
}, | ||
{ | ||
name: "whole and subslice", | ||
a: slice, | ||
b: slice[:4], | ||
want: false, | ||
}, | ||
{ | ||
name: "two subslices", | ||
a: slice[1:2], | ||
b: slice[3:4], | ||
want: false, | ||
}, | ||
{ | ||
name: "empty", | ||
a: []byte{0, 1, 2, 3}, | ||
b: []byte{}, | ||
want: true, | ||
}, | ||
{ | ||
name: "nil", | ||
a: []byte{0, 1, 2, 3}, | ||
b: nil, | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc // Capture range variable. | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := DifferentAddressRanges(new(testing.T), tc.a, tc.b) | ||
if got != tc.want { | ||
t.Errorf("DifferentAddressRanges(%p, %p) = %v, want %v", tc.a, tc.b, got, tc.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestEqualBSON(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := []struct { | ||
name string | ||
expected interface{} | ||
actual interface{} | ||
want bool | ||
}{ | ||
{ | ||
name: "equal bson.Raw", | ||
expected: bson.Raw{5, 0, 0, 0, 0}, | ||
actual: bson.Raw{5, 0, 0, 0, 0}, | ||
want: true, | ||
}, | ||
{ | ||
name: "different bson.Raw", | ||
expected: bson.Raw{8, 0, 0, 0, 10, 120, 0, 0}, | ||
actual: bson.Raw{5, 0, 0, 0, 0}, | ||
want: false, | ||
}, | ||
{ | ||
name: "invalid bson.Raw", | ||
expected: bson.Raw{99, 99, 99, 99}, | ||
actual: bson.Raw{5, 0, 0, 0, 0}, | ||
want: false, | ||
}, | ||
{ | ||
name: "nil bson.Raw", | ||
expected: bson.Raw(nil), | ||
actual: bson.Raw(nil), | ||
want: true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc // Capture range variable. | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
got := EqualBSON(new(testing.T), tc.expected, tc.actual) | ||
if got != tc.want { | ||
t.Errorf("EqualBSON(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.