gobot.io/x/gobot/v2@v2.1.0/gobottest/gobottest.go (about)

     1  package gobottest
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"reflect"
     8  	"runtime"
     9  	"strings"
    10  	"testing"
    11  )
    12  
    13  var errFunc = func(t *testing.T, message string) {
    14  	t.Errorf(message)
    15  }
    16  
    17  func logFailure(t *testing.T, message string) {
    18  	_, file, line, _ := runtime.Caller(2)
    19  	s := strings.Split(file, "/")
    20  	errFunc(t, fmt.Sprintf("%v:%v: %v", s[len(s)-1], line, message))
    21  }
    22  
    23  // Assert checks if a and b are equal, emits a t.Errorf if they are not equal.
    24  func Assert(t *testing.T, a interface{}, b interface{}) {
    25  	if !reflect.DeepEqual(a, b) {
    26  		logFailure(t, fmt.Sprintf("%v - \"%v\", should equal,  %v - \"%v\"",
    27  			a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
    28  	}
    29  }
    30  
    31  // Refute checks if a and b are equal, emits a t.Errorf if they are equal.
    32  func Refute(t *testing.T, a interface{}, b interface{}) {
    33  	if reflect.DeepEqual(a, b) {
    34  		logFailure(t, fmt.Sprintf("%v - \"%v\", should not equal,  %v - \"%v\"",
    35  			a, reflect.TypeOf(a), b, reflect.TypeOf(b)))
    36  	}
    37  }
    38  
    39  func ExecCommand(command string, args ...string) *exec.Cmd {
    40  	cs := []string{"-test.run=TestHelperProcess", "--", command}
    41  	cs = append(cs, args...)
    42  	cmd := exec.Command(os.Args[0], cs...)
    43  	cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"}
    44  	return cmd
    45  }