github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/testhelpers/assert/helpers.go (about)

     1  package assert
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/onsi/ginkgo"
     6  	"runtime"
     7  	"strings"
     8  )
     9  
    10  // Fail reports a failure through
    11  func Fail(failureMessage string, msgAndArgs ...interface{}) bool {
    12  
    13  	message := messageFromMsgAndArgs(msgAndArgs...)
    14  
    15  	if len(message) > 0 {
    16  		ginkgo.Fail(fmt.Sprintf("\r%s\r\tLocation:\t%s\n\r\tError:\t\t%s\n\r\tMessages:\t%s\n\r", getWhitespaceString(), CallerInfo(), failureMessage, message))
    17  	} else {
    18  		ginkgo.Fail(fmt.Sprintf("\r%s\r\tLocation:\t%s\n\r\tError:\t\t%s\n\r", getWhitespaceString(), CallerInfo(), failureMessage))
    19  	}
    20  
    21  	return false
    22  }
    23  
    24  /* CallerInfo is necessary because the assert functions use the testing object
    25  internally, causing it to print the file:line of the assert method, rather than where
    26  the problem actually occured in calling code.*/
    27  
    28  // CallerInfo returns a string containing the file and line number of the assert call
    29  // that failed.
    30  func CallerInfo() string {
    31  
    32  	file := ""
    33  	line := 0
    34  	ok := false
    35  
    36  	for i := 0; ; i++ {
    37  		_, file, line, ok = runtime.Caller(i)
    38  		if !ok {
    39  			return ""
    40  		}
    41  		parts := strings.Split(file, "/")
    42  		dir := parts[len(parts)-2]
    43  		file = parts[len(parts)-1]
    44  		if (dir != "assert" && dir != "mock") || file == "mock_test.go" {
    45  			break
    46  		}
    47  	}
    48  
    49  	return fmt.Sprintf("%s:%d", file, line)
    50  }
    51  
    52  // Takes a slice of errors and asserts that there were none provided
    53  // When failing, appends error messages together on newlines and
    54  // provides a count of how many errors were passed in
    55  func AssertNoErrors(errs []error) {
    56  	if len(errs) > 0 {
    57  		var concatErrors string
    58  		for _, err := range errs {
    59  			concatErrors = concatErrors + err.Error() + "\n"
    60  		}
    61  		ginkgo.Fail(fmt.Sprintf("Expected no errors, but there were %d.\n%s", len(errs), concatErrors))
    62  	}
    63  }
    64  
    65  func AssertPanic(panicValue interface{}, callback func()) {
    66  	defer func() {
    67  		value := recover()
    68  		if value != panicValue {
    69  			panic(value)
    70  		}
    71  	}()
    72  
    73  	callback()
    74  	ginkgo.Fail("Expected a panic, but got none!")
    75  }
    76  
    77  // getWhitespaceString returns a string that is long enough to overwrite the default
    78  // output from the go testing framework.
    79  func getWhitespaceString() string {
    80  
    81  	_, file, line, ok := runtime.Caller(1)
    82  	if !ok {
    83  		return ""
    84  	}
    85  	parts := strings.Split(file, "/")
    86  	file = parts[len(parts)-1]
    87  
    88  	return strings.Repeat(" ", len(fmt.Sprintf("%s:%d:      ", file, line)))
    89  
    90  }
    91  
    92  func messageFromMsgAndArgs(msgAndArgs ...interface{}) string {
    93  	if len(msgAndArgs) == 0 || msgAndArgs == nil {
    94  		return ""
    95  	}
    96  	if len(msgAndArgs) == 1 {
    97  		return msgAndArgs[0].(string)
    98  	}
    99  	if len(msgAndArgs) > 1 {
   100  		return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...)
   101  	}
   102  	return ""
   103  }