...

Source file src/go.mongodb.org/mongo-driver/internal/assert/assertion_mongo_test.go

Documentation: go.mongodb.org/mongo-driver/internal/assert

     1  // Copyright (C) MongoDB, Inc. 2023-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package assert
     8  
     9  import (
    10  	"testing"
    11  
    12  	"go.mongodb.org/mongo-driver/bson"
    13  )
    14  
    15  func TestDifferentAddressRanges(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	slice := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    19  
    20  	testCases := []struct {
    21  		name string
    22  		a    []byte
    23  		b    []byte
    24  		want bool
    25  	}{
    26  		{
    27  			name: "distinct byte slices",
    28  			a:    []byte{0, 1, 2, 3},
    29  			b:    []byte{0, 1, 2, 3},
    30  			want: true,
    31  		},
    32  		{
    33  			name: "same byte slice",
    34  			a:    slice,
    35  			b:    slice,
    36  			want: false,
    37  		},
    38  		{
    39  			name: "whole and subslice",
    40  			a:    slice,
    41  			b:    slice[:4],
    42  			want: false,
    43  		},
    44  		{
    45  			name: "two subslices",
    46  			a:    slice[1:2],
    47  			b:    slice[3:4],
    48  			want: false,
    49  		},
    50  		{
    51  			name: "empty",
    52  			a:    []byte{0, 1, 2, 3},
    53  			b:    []byte{},
    54  			want: true,
    55  		},
    56  		{
    57  			name: "nil",
    58  			a:    []byte{0, 1, 2, 3},
    59  			b:    nil,
    60  			want: true,
    61  		},
    62  	}
    63  
    64  	for _, tc := range testCases {
    65  		tc := tc // Capture range variable.
    66  
    67  		t.Run(tc.name, func(t *testing.T) {
    68  			t.Parallel()
    69  
    70  			got := DifferentAddressRanges(new(testing.T), tc.a, tc.b)
    71  			if got != tc.want {
    72  				t.Errorf("DifferentAddressRanges(%p, %p) = %v, want %v", tc.a, tc.b, got, tc.want)
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestEqualBSON(t *testing.T) {
    79  	t.Parallel()
    80  
    81  	testCases := []struct {
    82  		name     string
    83  		expected interface{}
    84  		actual   interface{}
    85  		want     bool
    86  	}{
    87  		{
    88  			name:     "equal bson.Raw",
    89  			expected: bson.Raw{5, 0, 0, 0, 0},
    90  			actual:   bson.Raw{5, 0, 0, 0, 0},
    91  			want:     true,
    92  		},
    93  		{
    94  			name:     "different bson.Raw",
    95  			expected: bson.Raw{8, 0, 0, 0, 10, 120, 0, 0},
    96  			actual:   bson.Raw{5, 0, 0, 0, 0},
    97  			want:     false,
    98  		},
    99  		{
   100  			name:     "invalid bson.Raw",
   101  			expected: bson.Raw{99, 99, 99, 99},
   102  			actual:   bson.Raw{5, 0, 0, 0, 0},
   103  			want:     false,
   104  		},
   105  		{
   106  			name:     "nil bson.Raw",
   107  			expected: bson.Raw(nil),
   108  			actual:   bson.Raw(nil),
   109  			want:     true,
   110  		},
   111  	}
   112  
   113  	for _, tc := range testCases {
   114  		tc := tc // Capture range variable.
   115  
   116  		t.Run(tc.name, func(t *testing.T) {
   117  			t.Parallel()
   118  
   119  			got := EqualBSON(new(testing.T), tc.expected, tc.actual)
   120  			if got != tc.want {
   121  				t.Errorf("EqualBSON(%#v, %#v) = %v, want %v", tc.expected, tc.actual, got, tc.want)
   122  			}
   123  		})
   124  	}
   125  }
   126  

View as plain text