github.com/flavio/docker@v0.1.3-0.20170117145210-f63d1a6eec47/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  	"unicode"
    11  
    12  	"github.com/davecgh/go-spew/spew"
    13  )
    14  
    15  // TestingT is an interface which defines the methods of testing.T that are
    16  // required by this package
    17  type TestingT interface {
    18  	Fatalf(string, ...interface{})
    19  }
    20  
    21  // Equal compare the actual value to the expected value and fails the test if
    22  // they are not equal.
    23  func Equal(t TestingT, actual, expected interface{}) {
    24  	if expected != actual {
    25  		fatal(t, "Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual)
    26  	}
    27  }
    28  
    29  // EqualNormalizedString compare the actual value to the expected value after applying the specified
    30  // transform function. It fails the test if these two transformed string are not equal.
    31  // For example `EqualNormalizedString(t, RemoveSpace, "foo\n", "foo")` wouldn't fail the test as
    32  // spaces (and thus '\n') are removed before comparing the string.
    33  func EqualNormalizedString(t TestingT, transformFun func(rune) rune, actual, expected string) {
    34  	if strings.Map(transformFun, actual) != strings.Map(transformFun, expected) {
    35  		fatal(t, "Expected '%v' got '%v'", expected, expected, actual, actual)
    36  	}
    37  }
    38  
    39  // RemoveSpace returns -1 if the specified runes is considered as a space (unicode)
    40  // and the rune itself otherwise.
    41  func RemoveSpace(r rune) rune {
    42  	if unicode.IsSpace(r) {
    43  		return -1
    44  	}
    45  	return r
    46  }
    47  
    48  //EqualStringSlice compares two slices and fails the test if they do not contain
    49  // the same items.
    50  func EqualStringSlice(t TestingT, actual, expected []string) {
    51  	if len(actual) != len(expected) {
    52  		fatal(t, "Expected (length %d): %q\nActual (length %d): %q",
    53  			len(expected), expected, len(actual), actual)
    54  	}
    55  	for i, item := range actual {
    56  		if item != expected[i] {
    57  			fatal(t, "Slices differ at element %d, expected %q got %q",
    58  				i, expected[i], item)
    59  		}
    60  	}
    61  }
    62  
    63  // NilError asserts that the error is nil, otherwise it fails the test.
    64  func NilError(t TestingT, err error) {
    65  	if err != nil {
    66  		fatal(t, "Expected no error, got: %s", err.Error())
    67  	}
    68  }
    69  
    70  // DeepEqual compare the actual value to the expected value and fails the test if
    71  // they are not "deeply equal".
    72  func DeepEqual(t TestingT, actual, expected interface{}) {
    73  	if !reflect.DeepEqual(actual, expected) {
    74  		fatal(t, "Expected (%T):\n%v\n\ngot (%T):\n%s\n",
    75  			expected, spew.Sdump(expected), actual, spew.Sdump(actual))
    76  	}
    77  }
    78  
    79  // Error asserts that error is not nil, and contains the expected text,
    80  // otherwise it fails the test.
    81  func Error(t TestingT, err error, contains string) {
    82  	if err == nil {
    83  		fatal(t, "Expected an error, but error was nil")
    84  	}
    85  
    86  	if !strings.Contains(err.Error(), contains) {
    87  		fatal(t, "Expected error to contain '%s', got '%s'", contains, err.Error())
    88  	}
    89  }
    90  
    91  // Contains asserts that the string contains a substring, otherwise it fails the
    92  // test.
    93  func Contains(t TestingT, actual, contains string) {
    94  	if !strings.Contains(actual, contains) {
    95  		fatal(t, "Expected '%s' to contain '%s'", actual, contains)
    96  	}
    97  }
    98  
    99  // NotNil fails the test if the object is nil
   100  func NotNil(t TestingT, obj interface{}) {
   101  	if obj == nil {
   102  		fatal(t, "Expected non-nil value.")
   103  	}
   104  }
   105  
   106  func fatal(t TestingT, format string, args ...interface{}) {
   107  	t.Fatalf(errorSource()+format, args...)
   108  }
   109  
   110  // See testing.decorate()
   111  func errorSource() string {
   112  	_, filename, line, ok := runtime.Caller(3)
   113  	if !ok {
   114  		return ""
   115  	}
   116  	return fmt.Sprintf("%s:%d: ", filepath.Base(filename), line)
   117  }