github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/utils/assert/assert.go (about)

     1  package assert
     2  
     3  import (
     4  	"bytes"
     5  	"reflect"
     6  	"runtime"
     7  	"testing"
     8  )
     9  
    10  func NoError(t testing.TB, err error) {
    11  	if err != nil {
    12  		_, file, line, _ := runtime.Caller(1)
    13  		t.Logf("%s:%d: %v\n", file, line, err)
    14  		t.FailNow()
    15  	}
    16  }
    17  
    18  func Equal[T comparable](t testing.TB, expected, actual T) {
    19  	if expected != actual {
    20  		_, file, line, _ := runtime.Caller(1)
    21  		t.Logf("%s:%d: expected %v, but got %v\n", file, line, expected, actual)
    22  	}
    23  }
    24  func MustEqual[T comparable](t testing.TB, expected, actual T) {
    25  	if expected != actual {
    26  		_, file, line, _ := runtime.Caller(1)
    27  		t.Logf("%s:%d: expected %v, but got %v\n", file, line, expected, actual)
    28  		t.FailNow()
    29  	}
    30  }
    31  
    32  func ObjectsAreEqual(expected, actual any) bool {
    33  	if expected == nil || actual == nil {
    34  		return expected == actual
    35  	}
    36  
    37  	exp, ok := expected.([]byte)
    38  	if !ok {
    39  		return reflect.DeepEqual(expected, actual)
    40  	}
    41  
    42  	act, ok := actual.([]byte)
    43  	if !ok {
    44  		return false
    45  	}
    46  	if exp == nil || act == nil {
    47  		return exp == nil && act == nil
    48  	}
    49  	return bytes.Equal(exp, act)
    50  }