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