godog_and_gomega/godog_and_gomega_helpers.go

77 строки
1,5 КиБ
Go

package godog_and_gomega
import (
"errors"
"fmt"
"runtime"
"strconv"
"time"
. "github.com/onsi/gomega"
)
var Be = Equal
func NoErr(err error) {
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
var Ok = NoErr
func Err(err error) {
ExpectWithOffset(1, err).To(HaveOccurred())
}
func ErrIs(err, target error) {
ExpectWithOffset(1, err).To(HaveOccurred())
v := errors.Is(err, target)
s := fmt.Sprintf("ErrIs: '%v' != '%v'", err, target)
ExpectWithOffset(1, v).To(BeTrue(), s)
}
func Yes(b bool) {
ExpectWithOffset(1, b).To(BeTrue())
}
func YesText(b bool, text string) {
ExpectWithOffset(1, b).To(BeTrue(), text)
}
func Atoi(in string) int {
res, err := strconv.Atoi(in)
Ok(err)
return res
}
func Atof(in string) float64 {
res, err := strconv.ParseFloat(in, 64)
Ok(err)
return res
}
func Истина(in string) bool {
switch in {
case "+", "да", "Да", "ДА", "t", "true", "True", "TRUE":
return true
}
return false
}
func SetTestDeadlockProtection(seconds int) {
go checkTestTimeout(time.Duration(seconds) * time.Second)
}
func checkTestTimeout(t time.Duration) {
<-time.After(t)
panic("Test Suite Timeout")
}
func SetTestDeadlockProtection_WithStackDump(seconds int) {
go checkTestTimeout_WithStackDump(time.Duration(seconds) * time.Second)
}
func checkTestTimeout_WithStackDump(t time.Duration) {
<-time.After(t)
PrintGoroutineStacks()
panic("Test Suite Timeout")
}
func PrintGoroutineStacks() {
buf := make([]byte, 1<<16)
stackSize := runtime.Stack(buf, true)
fmt.Printf("Goroutine stack trace:\n%s\n", buf[:stackSize])
}