github.com/noxiouz/docker@v0.7.3-0.20160629055221-3d231c78e8c5/pkg/testutil/assert/assert.go (about) 1 // Package assert contains functions for making assertions in unit tests 2 package assert 3 4 import ( 5 "strings" 6 ) 7 8 // TestingT is an interface which defines the methods of testing.T that are 9 // required by this package 10 type TestingT interface { 11 Fatalf(string, ...interface{}) 12 } 13 14 // Equal compare the actual value to the expected value and fails the test if 15 // they are not equal. 16 func Equal(t TestingT, actual, expected interface{}) { 17 if expected != actual { 18 t.Fatalf("Expected '%v' (%T) got '%v' (%T)", expected, expected, actual, actual) 19 } 20 } 21 22 // NilError asserts that the error is nil, otherwise it fails the test. 23 func NilError(t TestingT, err error) { 24 if err != nil { 25 t.Fatalf("Expected no error, got: %s", err.Error()) 26 } 27 } 28 29 // Error asserts that error is not nil, and contains the expected text, 30 // otherwise it fails the test. 31 func Error(t TestingT, err error, contains string) { 32 if err == nil { 33 t.Fatalf("Expected an error, but error was nil") 34 } 35 36 if !strings.Contains(err.Error(), contains) { 37 t.Fatalf("Expected error to contain '%s', got '%s'", contains, err.Error()) 38 } 39 } 40 41 // Contains asserts that the string contains a substring, otherwise it fails the 42 // test. 43 func Contains(t TestingT, actual, contains string) { 44 if !strings.Contains(actual, contains) { 45 t.Fatalf("Expected '%s' to contain '%s'", actual, contains) 46 } 47 }