go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/assert/its_equal.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package assert
     9  
    10  import (
    11  	"reflect"
    12  	"testing"
    13  )
    14  
    15  // ItsEqual is a test helper to verify that two arguments are equal.
    16  //
    17  // You can use it to build up other assertions, such as for length or not-nil.
    18  func ItsEqual(t *testing.T, expected, actual any, message ...any) {
    19  	t.Helper()
    20  	if !areEqual(expected, actual) {
    21  		Fatalf(t, "expected %v to equal %v", []any{actual, expected}, message)
    22  	}
    23  }
    24  
    25  // ItsZero is a test helper to verify that an argument is zero.
    26  func ItsZero(t *testing.T, actual any, message ...any) {
    27  	t.Helper()
    28  	ItsEqual(t, 0, actual, message...)
    29  }
    30  
    31  // ItsNotEqual is a test helper to verify that two arguments are not equal.
    32  //
    33  // You can use it to build up other assertions, such as for length or not-nil.
    34  func ItsNotEqual(t *testing.T, expected, actual any, message ...any) {
    35  	t.Helper()
    36  	if areEqual(expected, actual) {
    37  		Fatalf(t, "expected %v not to equal %v", []any{actual, expected}, message)
    38  	}
    39  }
    40  
    41  // ItsNotZero is a test helper to verify that an argument is not zero.
    42  func ItsNotZero(t *testing.T, actual any, message ...any) {
    43  	t.Helper()
    44  	ItsNotEqual(t, 0, actual, message...)
    45  }
    46  
    47  // ItsTrue is a helper that tests a value expected to be true.
    48  func ItsTrue(t *testing.T, expectedTrue bool, message ...any) {
    49  	t.Helper()
    50  	if !expectedTrue {
    51  		Fatalf(t, "expected %v to be true", []any{expectedTrue}, message)
    52  	}
    53  }
    54  
    55  // ItsFalse is a helper that tests a value expected to be true.
    56  func ItsFalse(t *testing.T, expectedFalse bool, message ...any) {
    57  	t.Helper()
    58  	if expectedFalse {
    59  		Fatalf(t, "expected %v to be false", []any{expectedFalse}, message)
    60  	}
    61  }
    62  
    63  func areEqual(expected, actual any) bool {
    64  	if Nil(expected) && Nil(actual) {
    65  		return true
    66  	}
    67  	if (Nil(expected) && !Nil(actual)) || (!Nil(expected) && Nil(actual)) {
    68  		return false
    69  	}
    70  	actualType := reflect.TypeOf(actual)
    71  	if actualType == nil {
    72  		return false
    73  	}
    74  	expectedValue := reflect.ValueOf(expected)
    75  	if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) {
    76  		return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual)
    77  	}
    78  	return reflect.DeepEqual(expected, actual)
    79  }