github.com/GuanceCloud/cliutils@v1.1.21/testutil/t.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  // Package testutil wraps basic tools when building goalng test cases.
     7  package testutil
     8  
     9  import (
    10  	"reflect"
    11  )
    12  
    13  type TB interface {
    14  	Helper()
    15  	Fatalf(string, ...interface{})
    16  }
    17  
    18  func Assert(tb TB, condition bool, fmt string, a ...interface{}) {
    19  	tb.Helper()
    20  	if !condition {
    21  		tb.Fatalf("\033[31m"+fmt+"\033[39m\n", a...)
    22  	}
    23  }
    24  
    25  func Ok(tb TB, err error) {
    26  	tb.Helper()
    27  	if err != nil {
    28  		tb.Fatalf("\033[31munexpected error: %v\033[39m\n", err)
    29  	}
    30  }
    31  
    32  func NotOk(tb TB, err error, fmt string, a ...interface{}) {
    33  	tb.Helper()
    34  	if err == nil {
    35  		if len(a) != 0 {
    36  			tb.Fatalf("\033[31m"+fmt+": expected error, got none\033[39m", a...)
    37  		}
    38  		tb.Fatalf("\033[31mexpected error, got none\033[39m")
    39  	}
    40  }
    41  
    42  func Equals(tb TB, exp, act interface{}) {
    43  	tb.Helper()
    44  	if !reflect.DeepEqual(exp, act) {
    45  		tb.Fatalf("\033[31m\nexp: %#v\n\ngot: %#v\033[39m\n", exp, act)
    46  	}
    47  }