github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/pkg/testutil/assert/assert.go (about)

     1  // Package assert contains functions for making assertions in unit tests
     2  package assert
     3  
     4  import (
     5  	"fmt"
     6  	"path/filepath"
     7  	"reflect"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  // TestingT is an interface which defines the methods of testing.T that are
    13  // required by this package
    14  type TestingT interface {
    15  	Fatalf(string, ...interface{})
    16  }
    17  
    18  // Equal compare the actual value to the expected value and fails the test if
    19  // they are not equal.
    20  func Equal(t TestingT, actual, expected interface{}) {
    21  	if expected != actual {
    22  		fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
    23  	}
    24  }
    25  
    26  //EqualStringSlice compares two slices and fails the test if they do not contain
    27  // the same items.
    28  func EqualStringSlice(t TestingT, actual, expected []string) {
    29  	if len(actual) != len(expected) {
    30  		fatal(t, "Expected (length %d): %q\nActual (length %d): %q",
    31  			len(expected), expected, len(actual), actual)
    32  	}
    33  	for i, item := range actual {
    34  		if item != expected[i] {
    35  			fatal(t, "Slices differ at element %d, expected %q got %q",
    36  				i, expected[i], item)
    37  		}
    38  	}
    39  }
    40  
    41  // NilError asserts that the error is nil, otherwise it fails the test.
    42  func NilError(t TestingT, err error) {
    43  	if err != nil {
    44  		fatal(t, "Expected no error, got: %s", err.Error())
    45  	}
    46  }
    47  
    48  // DeepEqual compare the actual value to the expected value and fails the test if
    49  // they are not "deeply equal".
    50  func DeepEqual(t TestingT, actual, expected interface{}) {
    51  	if !reflect.DeepEqual(actual, expected) {
    52  		fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
    53  	}
    54  }
    55  
    56  // Error asserts that error is not nil, and contains the expected text,
    57  // otherwise it fails the test.
    58  func Error(t TestingT, err error, contains string) {
    59  	if err == nil {
    60  		fatal(t, "Expected an error, but error was nil")
    61  	}
    62  
    63  	if !strings.Contains(err.Error(), contains) {
    64  		fatal(t, "Expected error to contain '%s', got '%s'", contains, err.Error())
    65  	}
    66  }
    67  
    68  // Contains asserts that the string contains a substring, otherwise it fails the
    69  // test.
    70  func Contains(t TestingT, actual, contains string) {
    71  	if !strings.Contains(actual, contains) {
    72  		fatal(t, "Expected '%s' to contain '%s'", actual, contains)
    73  	}
    74  }
    75  
    76  // NotNil fails the test if the object is nil
    77  func NotNil(t TestingT, obj interface{}) {
    78  	if obj == nil {
    79  		fatal(t, "Expected non-nil value.")
    80  	}
    81  }
    82  
    83  func fatal(t TestingT, format string, args ...interface{}) {
    84  	t.Fatalf(errorSource()+format, args...)
    85  }
    86  
    87  // See testing.decorate()
    88  func errorSource() string {
    89  	_, filename, line, ok := runtime.Caller(3)
    90  	if !ok {
    91  		return ""
    92  	}
    93  	return fmt.Sprintf("%s:%d: ", filepath.Base(filename), line)
    94  }