1793 lines · 45.0 KB · Go
RawDownload
package assert import (	"bytes"	"encoding/json"	"errors"	"fmt"	"io"	"math"	"os"	"reflect"	"regexp"	"runtime"	"strings"	"testing"	"time") var (	i     interface{}	zeros = []interface{}{		false,		byte(0),		complex64(0),		complex128(0),		float32(0),		float64(0),		int(0),		int8(0),		int16(0),		int32(0),		int64(0),		rune(0),		uint(0),		uint8(0),		uint16(0),		uint32(0),		uint64(0),		uintptr(0),		"",		[0]interface{}{},		[]interface{}(nil),		struct{ x int }{},		(*interface{})(nil),		(func())(nil),		nil,		interface{}(nil),		map[interface{}]interface{}(nil),		(chan interface{})(nil),		(<-chan interface{})(nil),		(chan<- interface{})(nil),	}	nonZeros = []interface{}{		true,		byte(1),		complex64(1),		complex128(1),		float32(1),		float64(1),		int(1),		int8(1),		int16(1),		int32(1),		int64(1),		rune(1),		uint(1),		uint8(1),		uint16(1),		uint32(1),		uint64(1),		uintptr(1),		"s",		[1]interface{}{1},		[]interface{}{},		struct{ x int }{1},		(*interface{})(&i),		(func())(func() {}),		interface{}(1),		map[interface{}]interface{}{},		(chan interface{})(make(chan interface{})),		(<-chan interface{})(make(chan interface{})),		(chan<- interface{})(make(chan interface{})),	}) // AssertionTesterInterface defines an interface to be used for testing assertion methodstype AssertionTesterInterface interface {	TestMethod()} // AssertionTesterConformingObject is an object that conforms to the AssertionTesterInterface interfacetype AssertionTesterConformingObject struct {} func (a *AssertionTesterConformingObject) TestMethod() {} // AssertionTesterNonConformingObject is an object that does not conform to the AssertionTesterInterface interfacetype AssertionTesterNonConformingObject struct {} func TestObjectsAreEqual(t *testing.T) { 	if !ObjectsAreEqual("Hello World", "Hello World") {		t.Error("objectsAreEqual should return true")	}	if !ObjectsAreEqual(123, 123) {		t.Error("objectsAreEqual should return true")	}	if !ObjectsAreEqual(123.5, 123.5) {		t.Error("objectsAreEqual should return true")	}	if !ObjectsAreEqual([]byte("Hello World"), []byte("Hello World")) {		t.Error("objectsAreEqual should return true")	}	if !ObjectsAreEqual(nil, nil) {		t.Error("objectsAreEqual should return true")	}	if ObjectsAreEqual(map[int]int{5: 10}, map[int]int{10: 20}) {		t.Error("objectsAreEqual should return false")	}	if ObjectsAreEqual('x', "x") {		t.Error("objectsAreEqual should return false")	}	if ObjectsAreEqual("x", 'x') {		t.Error("objectsAreEqual should return false")	}	if ObjectsAreEqual(0, 0.1) {		t.Error("objectsAreEqual should return false")	}	if ObjectsAreEqual(0.1, 0) {		t.Error("objectsAreEqual should return false")	}	if ObjectsAreEqual(uint32(10), int32(10)) {		t.Error("objectsAreEqual should return false")	}	if !ObjectsAreEqualValues(uint32(10), int32(10)) {		t.Error("ObjectsAreEqualValues should return true")	}	if ObjectsAreEqualValues(0, nil) {		t.Fail()	}	if ObjectsAreEqualValues(nil, 0) {		t.Fail()	} } func TestImplements(t *testing.T) { 	mockT := new(testing.T) 	if !Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterConformingObject)) {		t.Error("Implements method should return true: AssertionTesterConformingObject implements AssertionTesterInterface")	}	if Implements(mockT, (*AssertionTesterInterface)(nil), new(AssertionTesterNonConformingObject)) {		t.Error("Implements method should return false: AssertionTesterNonConformingObject does not implements AssertionTesterInterface")	}	if Implements(mockT, (*AssertionTesterInterface)(nil), nil) {		t.Error("Implements method should return false: nil does not implement AssertionTesterInterface")	} } func TestIsType(t *testing.T) { 	mockT := new(testing.T) 	if !IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {		t.Error("IsType should return true: AssertionTesterConformingObject is the same type as AssertionTesterConformingObject")	}	if IsType(mockT, new(AssertionTesterConformingObject), new(AssertionTesterNonConformingObject)) {		t.Error("IsType should return false: AssertionTesterConformingObject is not the same type as AssertionTesterNonConformingObject")	} } func TestEqual(t *testing.T) { 	mockT := new(testing.T) 	if !Equal(mockT, "Hello World", "Hello World") {		t.Error("Equal should return true")	}	if !Equal(mockT, 123, 123) {		t.Error("Equal should return true")	}	if !Equal(mockT, 123.5, 123.5) {		t.Error("Equal should return true")	}	if !Equal(mockT, []byte("Hello World"), []byte("Hello World")) {		t.Error("Equal should return true")	}	if !Equal(mockT, nil, nil) {		t.Error("Equal should return true")	}	if !Equal(mockT, int32(123), int32(123)) {		t.Error("Equal should return true")	}	if !Equal(mockT, uint64(123), uint64(123)) {		t.Error("Equal should return true")	}	if !Equal(mockT, &struct{}{}, &struct{}{}) {		t.Error("Equal should return true (pointer equality is based on equality of underlying value)")	}	var m map[string]interface{}	if Equal(mockT, m["bar"], "something") {		t.Error("Equal should return false")	}} // bufferT implements TestingT. Its implementation of Errorf writes the output that would be produced by// testing.T.Errorf to an internal bytes.Buffer.type bufferT struct {	buf bytes.Buffer} func (t *bufferT) Errorf(format string, args ...interface{}) {	// implementation of decorate is copied from testing.T	decorate := func(s string) string {		_, file, line, ok := runtime.Caller(3) // decorate + log + public function.		if ok {			// Truncate file name at last file name separator.			if index := strings.LastIndex(file, "/"); index >= 0 {				file = file[index+1:]			} else if index = strings.LastIndex(file, "\\"); index >= 0 {				file = file[index+1:]			}		} else {			file = "???"			line = 1		}		buf := new(bytes.Buffer)		// Every line is indented at least one tab.		buf.WriteByte('\t')		fmt.Fprintf(buf, "%s:%d: ", file, line)		lines := strings.Split(s, "\n")		if l := len(lines); l > 1 && lines[l-1] == "" {			lines = lines[:l-1]		}		for i, line := range lines {			if i > 0 {				// Second and subsequent lines are indented an extra tab.				buf.WriteString("\n\t\t")			}			buf.WriteString(line)		}		buf.WriteByte('\n')		return buf.String()	}	t.buf.WriteString(decorate(fmt.Sprintf(format, args...)))} func TestStringEqual(t *testing.T) {	for i, currCase := range []struct {		equalWant  string		equalGot   string		msgAndArgs []interface{}		want       string	}{		{equalWant: "hi, \nmy name is", equalGot: "what,\nmy name is", want: "\tassertions.go:\\d+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"hi, \\\\nmy name is\"\n\\s+actual\\s+: \"what,\\\\nmy name is\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1,2 \\+1,2 @@\n\\s+-hi, \n\\s+\\+what,\n\\s+my name is"},	} {		mockT := &bufferT{}		Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)		Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i)	}} func TestEqualFormatting(t *testing.T) {	for i, currCase := range []struct {		equalWant  string		equalGot   string		msgAndArgs []interface{}		want       string	}{		{equalWant: "want", equalGot: "got", want: "\tassertions.go:\\d+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n"},		{equalWant: "want", equalGot: "got", msgAndArgs: []interface{}{"hello, %v!", "world"}, want: "\tassertions.go:[0-9]+: \n\t+Error Trace:\t\n\t+Error:\\s+Not equal:\\s+\n\\s+expected: \"want\"\n\\s+actual\\s+: \"got\"\n\\s+Diff:\n\\s+-+ Expected\n\\s+\\++ Actual\n\\s+@@ -1 \\+1 @@\n\\s+-want\n\\s+\\+got\n\\s+Messages:\\s+hello, world!\n"},	} {		mockT := &bufferT{}		Equal(mockT, currCase.equalWant, currCase.equalGot, currCase.msgAndArgs...)		Regexp(t, regexp.MustCompile(currCase.want), mockT.buf.String(), "Case %d", i)	}} func TestFormatUnequalValues(t *testing.T) {	expected, actual := formatUnequalValues("foo", "bar")	Equal(t, `"foo"`, expected, "value should not include type")	Equal(t, `"bar"`, actual, "value should not include type") 	expected, actual = formatUnequalValues(123, 123)	Equal(t, `123`, expected, "value should not include type")	Equal(t, `123`, actual, "value should not include type") 	expected, actual = formatUnequalValues(int64(123), int32(123))	Equal(t, `int64(123)`, expected, "value should include type")	Equal(t, `int32(123)`, actual, "value should include type") 	expected, actual = formatUnequalValues(int64(123), nil)	Equal(t, `int64(123)`, expected, "value should include type")	Equal(t, `<nil>(<nil>)`, actual, "value should include type") 	type testStructType struct {		Val string	} 	expected, actual = formatUnequalValues(&testStructType{Val: "test"}, &testStructType{Val: "test"})	Equal(t, `&assert.testStructType{Val:"test"}`, expected, "value should not include type annotation")	Equal(t, `&assert.testStructType{Val:"test"}`, actual, "value should not include type annotation")} func TestNotNil(t *testing.T) { 	mockT := new(testing.T) 	if !NotNil(mockT, new(AssertionTesterConformingObject)) {		t.Error("NotNil should return true: object is not nil")	}	if NotNil(mockT, nil) {		t.Error("NotNil should return false: object is nil")	}	if NotNil(mockT, (*struct{})(nil)) {		t.Error("NotNil should return false: object is (*struct{})(nil)")	} } func TestNil(t *testing.T) { 	mockT := new(testing.T) 	if !Nil(mockT, nil) {		t.Error("Nil should return true: object is nil")	}	if !Nil(mockT, (*struct{})(nil)) {		t.Error("Nil should return true: object is (*struct{})(nil)")	}	if Nil(mockT, new(AssertionTesterConformingObject)) {		t.Error("Nil should return false: object is not nil")	} } func TestTrue(t *testing.T) { 	mockT := new(testing.T) 	if !True(mockT, true) {		t.Error("True should return true")	}	if True(mockT, false) {		t.Error("True should return false")	} } func TestFalse(t *testing.T) { 	mockT := new(testing.T) 	if !False(mockT, false) {		t.Error("False should return true")	}	if False(mockT, true) {		t.Error("False should return false")	} } func TestExactly(t *testing.T) { 	mockT := new(testing.T) 	a := float32(1)	b := float64(1)	c := float32(1)	d := float32(2) 	if Exactly(mockT, a, b) {		t.Error("Exactly should return false")	}	if Exactly(mockT, a, d) {		t.Error("Exactly should return false")	}	if !Exactly(mockT, a, c) {		t.Error("Exactly should return true")	} 	if Exactly(mockT, nil, a) {		t.Error("Exactly should return false")	}	if Exactly(mockT, a, nil) {		t.Error("Exactly should return false")	} } func TestNotEqual(t *testing.T) { 	mockT := new(testing.T) 	if !NotEqual(mockT, "Hello World", "Hello World!") {		t.Error("NotEqual should return true")	}	if !NotEqual(mockT, 123, 1234) {		t.Error("NotEqual should return true")	}	if !NotEqual(mockT, 123.5, 123.55) {		t.Error("NotEqual should return true")	}	if !NotEqual(mockT, []byte("Hello World"), []byte("Hello World!")) {		t.Error("NotEqual should return true")	}	if !NotEqual(mockT, nil, new(AssertionTesterConformingObject)) {		t.Error("NotEqual should return true")	}	funcA := func() int { return 23 }	funcB := func() int { return 42 }	if NotEqual(mockT, funcA, funcB) {		t.Error("NotEqual should return false")	} 	if NotEqual(mockT, "Hello World", "Hello World") {		t.Error("NotEqual should return false")	}	if NotEqual(mockT, 123, 123) {		t.Error("NotEqual should return false")	}	if NotEqual(mockT, 123.5, 123.5) {		t.Error("NotEqual should return false")	}	if NotEqual(mockT, []byte("Hello World"), []byte("Hello World")) {		t.Error("NotEqual should return false")	}	if NotEqual(mockT, new(AssertionTesterConformingObject), new(AssertionTesterConformingObject)) {		t.Error("NotEqual should return false")	}	if NotEqual(mockT, &struct{}{}, &struct{}{}) {		t.Error("NotEqual should return false")	}} type A struct {	Name, Value string} func TestContains(t *testing.T) { 	mockT := new(testing.T)	list := []string{"Foo", "Bar"}	complexList := []*A{		{"b", "c"},		{"d", "e"},		{"g", "h"},		{"j", "k"},	}	simpleMap := map[interface{}]interface{}{"Foo": "Bar"} 	if !Contains(mockT, "Hello World", "Hello") {		t.Error("Contains should return true: \"Hello World\" contains \"Hello\"")	}	if Contains(mockT, "Hello World", "Salut") {		t.Error("Contains should return false: \"Hello World\" does not contain \"Salut\"")	} 	if !Contains(mockT, list, "Bar") {		t.Error("Contains should return true: \"[\"Foo\", \"Bar\"]\" contains \"Bar\"")	}	if Contains(mockT, list, "Salut") {		t.Error("Contains should return false: \"[\"Foo\", \"Bar\"]\" does not contain \"Salut\"")	}	if !Contains(mockT, complexList, &A{"g", "h"}) {		t.Error("Contains should return true: complexList contains {\"g\", \"h\"}")	}	if Contains(mockT, complexList, &A{"g", "e"}) {		t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")	}	if Contains(mockT, complexList, &A{"g", "e"}) {		t.Error("Contains should return false: complexList contains {\"g\", \"e\"}")	}	if !Contains(mockT, simpleMap, "Foo") {		t.Error("Contains should return true: \"{\"Foo\": \"Bar\"}\" contains \"Foo\"")	}	if Contains(mockT, simpleMap, "Bar") {		t.Error("Contains should return false: \"{\"Foo\": \"Bar\"}\" does not contains \"Bar\"")	}} func TestNotContains(t *testing.T) { 	mockT := new(testing.T)	list := []string{"Foo", "Bar"}	simpleMap := map[interface{}]interface{}{"Foo": "Bar"} 	if !NotContains(mockT, "Hello World", "Hello!") {		t.Error("NotContains should return true: \"Hello World\" does not contain \"Hello!\"")	}	if NotContains(mockT, "Hello World", "Hello") {		t.Error("NotContains should return false: \"Hello World\" contains \"Hello\"")	} 

Lines 1–500 of 1793.