github.com/sereiner/library@v0.0.0-20200518095232-1fa3e640cc5f/ut/ut.go (about)

     1  package ut
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  )
     7  
     8  type test interface {
     9  	Errorf(format string, args ...interface{})
    10  	FailNow()
    11  }
    12  
    13  func Expect(t test, a interface{}, b interface{}) {
    14  	a1 := a
    15  	b1 := b
    16  	val := reflect.ValueOf(a)
    17  	if val.Kind() == reflect.Interface || val.Kind() == reflect.Ptr {
    18  		val = val.Elem()
    19  	}
    20  	switch val.Kind() {
    21  	case reflect.Map, reflect.Struct, reflect.Slice:
    22  		a1 = fmt.Sprintf("%+v", a)
    23  	}
    24  	val = reflect.ValueOf(b)
    25  	if val.Kind() == reflect.Interface || val.Kind() == reflect.Ptr {
    26  		val = val.Elem()
    27  	}
    28  	switch val.Kind() {
    29  	case reflect.Map, reflect.Struct, reflect.Slice:
    30  		b1 = fmt.Sprintf("%+v", b)
    31  	}
    32  	if a1 != b1 {
    33  		t.Errorf("Expected %v (type %v) - Got %v (type %v)", b1, reflect.TypeOf(b1), a1, reflect.TypeOf(a1))
    34  	}
    35  }
    36  func ExpectSkip(t test, a interface{}, b interface{}) bool {
    37  	if a != b {
    38  		t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
    39  		t.FailNow()
    40  		return true
    41  	}
    42  	return false
    43  }
    44  
    45  func Refute(t test, a interface{}, b interface{}) {
    46  	if a == b {
    47  		t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
    48  	}
    49  }
    50  func RefuteSkip(t test, a interface{}, b interface{}) {
    51  	if a == b {
    52  		t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
    53  		t.FailNow()
    54  	}
    55  }