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