github.com/muxinc/mux-go@v1.1.1/examples/common/lazytest.go (about)

     1  package common
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  )
     8  
     9  func AssertNoError(err error) {
    10  	if err != nil {
    11  		fmt.Printf("err: %s \n\n", err)
    12  		os.Exit(255)
    13  	}
    14  }
    15  
    16  // Copied from Testify under MIT license.
    17  // containsKind checks if a specified kind in the slice of kinds.
    18  func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool {
    19  	for i := 0; i < len(kinds); i++ {
    20  		if kind == kinds[i] {
    21  			return true
    22  		}
    23  	}
    24  
    25  	return false
    26  }
    27  
    28  // Copied from Testify under MIT license.
    29  func AssertNotNil(o interface{}) {
    30  	if o == nil {
    31  		fmt.Println("Object was nil!")
    32  		os.Exit(255)
    33  	}
    34  
    35  	v := reflect.ValueOf(o)
    36  	kind := v.Kind()
    37  	isNilableKind := containsKind(
    38  		[]reflect.Kind{
    39  			reflect.Chan, reflect.Func,
    40  			reflect.Interface, reflect.Map,
    41  			reflect.Ptr, reflect.Slice},
    42  		kind)
    43  
    44  	if isNilableKind && v.IsNil() {
    45  		fmt.Println("Object was nil!")
    46  		os.Exit(255)
    47  	}
    48  }
    49  
    50  // Inverse of above
    51  func AssertNil(o interface{}) {
    52  	if o == nil {
    53  		return
    54  	}
    55  
    56  	v := reflect.ValueOf(o)
    57  	kind := v.Kind()
    58  	isNilableKind := containsKind(
    59  		[]reflect.Kind{
    60  			reflect.Chan, reflect.Func,
    61  			reflect.Interface, reflect.Map,
    62  			reflect.Ptr, reflect.Slice},
    63  		kind)
    64  
    65  	if isNilableKind && v.IsNil() {
    66  		return
    67  	}
    68  
    69  	fmt.Println("Object nil!")
    70  	os.Exit(255)
    71  }
    72  
    73  func AssertStringEqualsValue(a string, b string) {
    74  	if a != b {
    75  		fmt.Println("Strings weren't equal!")
    76  		os.Exit(255)
    77  	}
    78  }
    79  
    80  func AssertStringNotEqualsValue(a string, b string) {
    81  	if a == b {
    82  		fmt.Println("Strings weren't supposed to be equal!")
    83  		os.Exit(255)
    84  	}
    85  }
    86  
    87  func AssertIntEqualsValue(a int, b int) {
    88  	if a != b {
    89  		fmt.Println("Ints weren't equal!")
    90  		os.Exit(255)
    91  	}
    92  }
    93  
    94  func AssertIntGreaterThanZero(a int) {
    95  	if a <= 0 {
    96  		fmt.Println("Int was <= 0!")
    97  		os.Exit(255)
    98  	}
    99  }