...

Source file src/go.mongodb.org/mongo-driver/internal/assert/assertion_compare_go1.17_test.go

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

     1  // Copied from https://github.com/stretchr/testify/blob/1333b5d3bda8cf5aedcf3e1aaa95cac28aaab892/assert/assertion_compare_go1.17_test.go
     2  
     3  // Copyright 2020 Mat Ryer, Tyler Bunnell and all contributors. All rights reserved.
     4  // Use of this source code is governed by an MIT-style license that can be found in
     5  // the THIRD-PARTY-NOTICES file.
     6  
     7  //go:build go1.17
     8  // +build go1.17
     9  
    10  package assert
    11  
    12  import (
    13  	"bytes"
    14  	"reflect"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  func TestCompare17(t *testing.T) {
    20  	type customTime time.Time
    21  	type customBytes []byte
    22  	for _, currCase := range []struct {
    23  		less    interface{}
    24  		greater interface{}
    25  		cType   string
    26  	}{
    27  		{less: time.Now(), greater: time.Now().Add(time.Hour), cType: "time.Time"},
    28  		{less: customTime(time.Now()), greater: customTime(time.Now().Add(time.Hour)), cType: "time.Time"},
    29  		{less: []byte{1, 1}, greater: []byte{1, 2}, cType: "[]byte"},
    30  		{less: customBytes([]byte{1, 1}), greater: customBytes([]byte{1, 2}), cType: "[]byte"},
    31  	} {
    32  		resLess, isComparable := compare(currCase.less, currCase.greater, reflect.ValueOf(currCase.less).Kind())
    33  		if !isComparable {
    34  			t.Error("object should be comparable for type " + currCase.cType)
    35  		}
    36  
    37  		if resLess != compareLess {
    38  			t.Errorf("object less (%v) should be less than greater (%v) for type "+currCase.cType,
    39  				currCase.less, currCase.greater)
    40  		}
    41  
    42  		resGreater, isComparable := compare(currCase.greater, currCase.less, reflect.ValueOf(currCase.less).Kind())
    43  		if !isComparable {
    44  			t.Error("object are comparable for type " + currCase.cType)
    45  		}
    46  
    47  		if resGreater != compareGreater {
    48  			t.Errorf("object greater should be greater than less for type " + currCase.cType)
    49  		}
    50  
    51  		resEqual, isComparable := compare(currCase.less, currCase.less, reflect.ValueOf(currCase.less).Kind())
    52  		if !isComparable {
    53  			t.Error("object are comparable for type " + currCase.cType)
    54  		}
    55  
    56  		if resEqual != 0 {
    57  			t.Errorf("objects should be equal for type " + currCase.cType)
    58  		}
    59  	}
    60  }
    61  
    62  func TestGreater17(t *testing.T) {
    63  	mockT := new(testing.T)
    64  
    65  	if !Greater(mockT, 2, 1) {
    66  		t.Error("Greater should return true")
    67  	}
    68  
    69  	if Greater(mockT, 1, 1) {
    70  		t.Error("Greater should return false")
    71  	}
    72  
    73  	if Greater(mockT, 1, 2) {
    74  		t.Error("Greater should return false")
    75  	}
    76  
    77  	// Check error report
    78  	for _, currCase := range []struct {
    79  		less    interface{}
    80  		greater interface{}
    81  		msg     string
    82  	}{
    83  		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than "[1 2]"`},
    84  		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than "0001-01-01 01:00:00 +0000 UTC"`},
    85  	} {
    86  		out := &outputT{buf: bytes.NewBuffer(nil)}
    87  		False(t, Greater(out, currCase.less, currCase.greater))
    88  		Contains(t, out.buf.String(), currCase.msg)
    89  		Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.Greater")
    90  	}
    91  }
    92  
    93  func TestGreaterOrEqual17(t *testing.T) {
    94  	mockT := new(testing.T)
    95  
    96  	if !GreaterOrEqual(mockT, 2, 1) {
    97  		t.Error("GreaterOrEqual should return true")
    98  	}
    99  
   100  	if !GreaterOrEqual(mockT, 1, 1) {
   101  		t.Error("GreaterOrEqual should return true")
   102  	}
   103  
   104  	if GreaterOrEqual(mockT, 1, 2) {
   105  		t.Error("GreaterOrEqual should return false")
   106  	}
   107  
   108  	// Check error report
   109  	for _, currCase := range []struct {
   110  		less    interface{}
   111  		greater interface{}
   112  		msg     string
   113  	}{
   114  		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 1]" is not greater than or equal to "[1 2]"`},
   115  		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 00:00:00 +0000 UTC" is not greater than or equal to "0001-01-01 01:00:00 +0000 UTC"`},
   116  	} {
   117  		out := &outputT{buf: bytes.NewBuffer(nil)}
   118  		False(t, GreaterOrEqual(out, currCase.less, currCase.greater))
   119  		Contains(t, out.buf.String(), currCase.msg)
   120  		Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.GreaterOrEqual")
   121  	}
   122  }
   123  
   124  func TestLess17(t *testing.T) {
   125  	mockT := new(testing.T)
   126  
   127  	if !Less(mockT, 1, 2) {
   128  		t.Error("Less should return true")
   129  	}
   130  
   131  	if Less(mockT, 1, 1) {
   132  		t.Error("Less should return false")
   133  	}
   134  
   135  	if Less(mockT, 2, 1) {
   136  		t.Error("Less should return false")
   137  	}
   138  
   139  	// Check error report
   140  	for _, currCase := range []struct {
   141  		less    interface{}
   142  		greater interface{}
   143  		msg     string
   144  	}{
   145  		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than "[1 1]"`},
   146  		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than "0001-01-01 00:00:00 +0000 UTC"`},
   147  	} {
   148  		out := &outputT{buf: bytes.NewBuffer(nil)}
   149  		False(t, Less(out, currCase.greater, currCase.less))
   150  		Contains(t, out.buf.String(), currCase.msg)
   151  		Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.Less")
   152  	}
   153  }
   154  
   155  func TestLessOrEqual17(t *testing.T) {
   156  	mockT := new(testing.T)
   157  
   158  	if !LessOrEqual(mockT, 1, 2) {
   159  		t.Error("LessOrEqual should return true")
   160  	}
   161  
   162  	if !LessOrEqual(mockT, 1, 1) {
   163  		t.Error("LessOrEqual should return true")
   164  	}
   165  
   166  	if LessOrEqual(mockT, 2, 1) {
   167  		t.Error("LessOrEqual should return false")
   168  	}
   169  
   170  	// Check error report
   171  	for _, currCase := range []struct {
   172  		less    interface{}
   173  		greater interface{}
   174  		msg     string
   175  	}{
   176  		{less: []byte{1, 1}, greater: []byte{1, 2}, msg: `"[1 2]" is not less than or equal to "[1 1]"`},
   177  		{less: time.Time{}, greater: time.Time{}.Add(time.Hour), msg: `"0001-01-01 01:00:00 +0000 UTC" is not less than or equal to "0001-01-01 00:00:00 +0000 UTC"`},
   178  	} {
   179  		out := &outputT{buf: bytes.NewBuffer(nil)}
   180  		False(t, LessOrEqual(out, currCase.greater, currCase.less))
   181  		Contains(t, out.buf.String(), currCase.msg)
   182  		Contains(t, out.helpers, "go.mongodb.org/mongo-driver/internal/assert.LessOrEqual")
   183  	}
   184  }
   185  

View as plain text