Beispiel #1
0
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
	if !reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
		tb.FailNow()
	}
}
Beispiel #2
0
// equals fails the test if exp is not equal to act.
func equals(tb testing.TB, exp, act interface{}) {
	if !reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("%s:%d: expected: %#v got: %#v\n", filepath.Base(file), line, exp, act)
		tb.FailNow()
	}
}
Beispiel #3
0
// Assert fails the test and displays 'msg', if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
	if !condition {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("%s:%d: "+msg+"\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
		tb.FailNow()
	}
}
Beispiel #4
0
// AssertNotNil fails the test if exp is not equal to act.
func AssertNotNil(tb testing.TB, exp interface{}) {
	if exp == nil {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d: expecting not nil but got nil\033[39m\n\n", filepath.Base(file), line)
		tb.FailNow()
	}
}
Beispiel #5
0
// ok fails the test if an err is not nil.
func ok(tb testing.TB, err error) {
	if err != nil {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
		tb.FailNow()
	}
}
Beispiel #6
0
func AssertJSONBody(tb testing.TB, exp, act interface{}) {
	red := ansi.ColorCode("red+h:black")
	green := ansi.ColorCode("green+h:black")
	yellow := ansi.ColorFunc("yellow+h")
	reset := ansi.ColorCode("reset")

	var actBuf bytes.Buffer
	err := json.Indent(&actBuf, []byte(act.(string)), "", " ")
	if err != nil {
		fmt.Println(red, "Invalid json: ", act, reset)
	}
	act = string(actBuf.Bytes())

	var expBuf bytes.Buffer
	err = json.Indent(&expBuf, []byte(exp.(string)), "", " ")
	if err != nil {
		fmt.Println(red, "Invalid json: ", exp, reset)
	}
	exp = string(expBuf.Bytes())

	if !reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(1)

		fmt.Println(yellow(fmt.Sprintf("%s:%d", filepath.Base(file), line)))
		fmt.Println(green, "Expected: ", exp, reset)
		fmt.Println(red, "     Got: ", act, reset)

		tb.FailNow()
	}
}
// Equals fails the test if exp is not equal to act.
// Code was copied from https://github.com/benbjohnson/testing MIT license
func Equals(tb testing.TB, exp, act interface{}) {
	if !reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("%s%s:%d:\n\n\texp: %#v\n\n\tgot: %#v%s\n\n", colors[red], filepath.Base(file), line, exp, act, colors[reset])
		tb.FailNow()
	}
}
Beispiel #8
0
// AssertEQ fails the test and displays 'msg', if exp is not equal to act.
func AssertEQ(tb testing.TB, exp, act interface{}, msg string) {
	if !reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("%s:%d: %s\n\n\texp: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, msg, exp, act)
		tb.FailNow()
	}
}
Beispiel #9
0
func notOk(tb testing.TB, err error) {
	if err == nil {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d: expected error but got nil instead\n\n", filepath.Base(file), line)
		tb.FailNow()
	}
}
Beispiel #10
0
// notNilUp is like notNil, but used inside helper functions, to ensure that the
// file and line number reported by failures corresponds to one or more levels
// up the stack.
func notNilUp(obtained interface{}, t testing.TB, caller int) {
	if _isNil(obtained) {
		_, file, line, _ := runtime.Caller(caller + 1)
		fmt.Printf("%s:%d: expected non-nil, got: %v\n", filepath.Base(file), line, obtained)
		t.FailNow()
	}
}
Beispiel #11
0
func AssertError(tb testing.TB, err error) {
	if err == nil {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d: expecting error but got nil\033[39m\n\n", filepath.Base(file), line)
		tb.FailNow()
	}
}
Beispiel #12
0
// equalsUp is like equals, but used inside helper functions, to ensure that the
// file and line number reported by failures corresponds to one or more levels
// up the stack.
func equalsUp(exp, act interface{}, t testing.TB, caller int) {
	if !reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(caller + 1)
		fmt.Printf("%s:%d: exp: %v (%T), got: %v (%T)\n",
			filepath.Base(file), line, exp, exp, act, act)
		t.FailNow()
	}
}
Beispiel #13
0
// assertUp is like assert, but used inside helper functions, to ensure that
// the file and line number reported by failures corresponds to one or more
// levels up the stack.
func assertUp(condition bool, t testing.TB, caller int, msg string, v ...interface{}) {
	if !condition {
		_, file, line, _ := runtime.Caller(caller + 1)
		v = append([]interface{}{filepath.Base(file), line}, v...)
		fmt.Printf("%s:%d: "+msg+"\n", v...)
		t.FailNow()
	}
}
Beispiel #14
0
func contains(tb testing.TB, s []string, e string) {
	for _, a := range s {
		if a == e {
			return
		}
	}
	fmt.Printf("Expected to contain %s\n", e)
	tb.FailNow()
}
Beispiel #15
0
// assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, v ...interface{}) {
	if !condition {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\n\033[31m%s:%d: failure!!!\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
		tb.FailNow()
	} else {
		fmt.Printf("\033[32m.\033[39m")
	}
}
Beispiel #16
0
func AssertNotEquals(tb testing.TB, exp, act interface{}) {
	if reflect.DeepEqual(exp, act) {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\n\033[31m%s:%d:\n\n\texpected: %#v\n\n\tnot to eq: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
		tb.FailNow()
	} else {
		fmt.Printf("\033[32m.\033[39m")
	}
}
Beispiel #17
0
func assertNext(tb testing.TB, exp string, ch chan string, timeout time.Duration) {
	select {
	case act := <-ch:
		equals(tb, exp, act)
	case <-time.After(timeout):
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d: timed out after %v, exp: %s\033[39m\n\n", filepath.Base(file), line, timeout, exp)
		tb.FailNow()
	}
}
Beispiel #18
0
// equals fails the test if got is not equal to want.
func equals(t testing.TB, got, want interface{}) {
	if want == nil && (got == nil || reflect.ValueOf(got).IsNil()) {
		// Accept any nil interface value
		return
	}
	if !reflect.DeepEqual(got, want) {
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\t%s:%d: got: %#v; want: %#v\n", filepath.Base(file), line, got, want)
		t.FailNow()
	}
}
Beispiel #19
0
// OKs fails the test if any error from errs is not nil.
func OKs(tb testing.TB, errs []error) {
	errFound := false
	for _, err := range errs {
		if err != nil {
			errFound = true
			_, file, line, _ := runtime.Caller(1)
			fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
		}
	}
	if errFound {
		tb.FailNow()
	}
}
Beispiel #20
0
func f테스트_같음(t testing.TB, 값 interface{}, 비교값_모음 ...interface{}) {
	if len(비교값_모음) == 0 {
		New에러("비교값이 없습니다.")
		t.FailNow()
	}

	문자열이_같으면_같은_값 := false

	switch 값.(type) {
	case *big.Int, *big.Rat, decimal.Decimal,
		int, int8, int16, int32, int64,
		uint, uint8, uint16, uint32, uint64,
		float32, float64: //*big.Float:
		문자열이_같으면_같은_값 = true
	}
Beispiel #21
0
// approx tests approximate equality of floats.
// Note that this is a fraught topic. This is a very naive comparison.
func approx(t testing.TB, got, want float64) {
	f1 := got
	f2 := want
	if f1 == f2 {
		return
	} else if f1 > f2 {
		f1, f2 = f2, f1
	}
	delta := (f2 - f1) / f1
	if delta > 0.0001 { // Accept want up to 0.01% greater than got
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\t%s:%d: got %v; want %v\n", filepath.Base(file), line, got, want)
		t.FailNow()
	}
}
Beispiel #22
0
func AssertEqual(tb testing.TB, exp, act interface{}) {
	if !reflect.DeepEqual(exp, act) {
		yellow := ansi.ColorFunc("yellow+h")
		green := ansi.ColorCode("green+h:black")
		red := ansi.ColorCode("red+h:black")
		reset := ansi.ColorCode("reset")

		_, file, line, _ := runtime.Caller(1)

		fmt.Println(yellow(fmt.Sprintf("%s:%d", filepath.Base(file), line)))
		fmt.Println(green, "Expected: ", exp, reset)
		fmt.Println(red, "     Got: ", act, reset)

		tb.FailNow()
	}
}
Beispiel #23
0
// panics calls f, which must be a function of no arguments, and fails unless it panics.
func panics(tb testing.TB, f interface{}) {
	rv := reflect.ValueOf(f)
	rt := rv.Type()
	if rt.Kind() != reflect.Func || rt.NumIn() != 0 {
		panic("panics() must be called with a function of 0 arguments")
	}

	defer func() {
		if err := recover(); err == nil {
			_, file, line, _ := runtime.Caller(2)
			fmt.Printf("\033[31m%s:%d:\n\n\texpected to panic but did not\033[39m\n\n", filepath.Base(file), line)
			tb.FailNow()
		}
	}()

	rv.Call(nil)
}
Beispiel #24
0
func assertNextMatch(tb testing.TB, exp string, ch chan string, timeout time.Duration) {
	select {
	case act := <-ch:
		matches, err := regexp.MatchString("^"+exp+"$", act)
		ok(tb, err)

		if matches {
			return
		}

		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
		tb.FailNow()

	case <-time.After(timeout):
		_, file, line, _ := runtime.Caller(1)
		fmt.Printf("\033[31m%s:%d: timed out after %v, exp: %s\033[39m\n\n", filepath.Base(file), line, timeout, exp)
		tb.FailNow()
	}
}
Beispiel #25
0
func handshakeTest(t testing.TB, ia []byte, aData, bData string) {
	a, b := net.Pipe()
	wg := sync.WaitGroup{}
	wg.Add(2)
	go func() {
		defer wg.Done()
		a, err := InitiateHandshake(a, []byte("yep"), ia)
		if err != nil {
			t.Fatal(err)
			return
		}
		go a.Write([]byte(aData))

		var msg [20]byte
		n, _ := a.Read(msg[:])
		if n != len(bData) {
			t.FailNow()
		}
		// t.Log(string(msg[:n]))
	}()
	go func() {
		defer wg.Done()
		b, err := ReceiveHandshake(b, [][]byte{[]byte("nope"), []byte("yep"), []byte("maybe")})
		if err != nil {
			t.Fatal(err)
			return
		}
		go b.Write([]byte(bData))
		// Need to be exact here, as there are several reads, and net.Pipe is
		// most synchronous.
		msg := make([]byte, len(ia)+len(aData))
		n, _ := io.ReadFull(b, msg[:])
		if n != len(msg) {
			t.FailNow()
		}
		// t.Log(string(msg[:n]))
	}()
	wg.Wait()
	a.Close()
	b.Close()
}
Beispiel #26
0
// newMetricSet instantiates a new MetricSet using the given configuration.
// The ModuleFactory and MetricSetFactory are obtained from the global
// Registry.
func newMetricSet(t testing.TB, config interface{}) mb.MetricSet {
	c, err := common.NewConfigFrom(config)
	if err != nil {
		t.Fatal(err)
	}
	m, err := mb.NewModules([]*common.Config{c}, mb.Registry)
	if err != nil {
		t.Fatal(err)
	}
	if !assert.Len(t, m, 1) {
		t.FailNow()
	}

	var metricSet mb.MetricSet
	for _, v := range m {
		if !assert.Len(t, v, 1) {
			t.FailNow()
		}

		metricSet = v[0]
		break
	}

	if !assert.NotNil(t, metricSet) {
		t.FailNow()
	}
	return metricSet
}
Beispiel #27
0
func (e *callRecord) assert(t testing.TB, name string, params ...interface{}) {
	if name != e.name {
		t.Logf("Expected call to %s%s", e.name, paramsToString(e.params))
		t.Logf(" got call to %s%s", name, paramsToString(params))
		showStack(t)
		t.Fail()
		return
	}
	if len(params) != len(e.params) {
		t.Logf("Call to (%s) unexpected parameters", name)
		t.Logf(" expected %s", paramsToString(e.params))
		t.Logf("      got %s", paramsToString(params))
		showStack(t)
		t.FailNow()
		return
	}
	for i, ap := range params {
		ep := e.params[i]

		if ap == nil && ep == nil {
			continue
		}

		switch ep := ep.(type) {
		case func(actual interface{}):
			ep(ap)
		default:
			if !reflect.DeepEqual(ap, ep) {
				t.Logf("Call to %s parameter %d unexpected", name, i)
				t.Logf("  expected %#v (%T)", ep, ep)
				t.Logf("       got %#v (%T)", ap, ap)
				showStack(t)
				t.Fail()
			}
		}
	}
}
Beispiel #28
0
func fail(tb testing.TB, reason string) {
	_, file, line, _ := runtime.Caller(1)
	fmt.Printf("%s:%d: %s\n\n", filepath.Base(file), line, reason)
	tb.FailNow()
}
// IsNil ensures that the act interface is nil
// otherwise an error is raised.
func IsNil(tb testing.TB, act interface{}) {
	if act != nil {
		tb.Error("expected nil", act)
		tb.FailNow()
	}
}