You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +02:00
Refactor fail
method in matchers (#6517)
Accept a format and parameters just like `fmt.Sprintf` given almost all calls to `e.fail` follow this format.
This commit is contained in:
@@ -28,7 +28,7 @@ func (e *Expectation) ToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if !reflect.DeepEqual(e.actual, expected) {
|
if !reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,19 +36,19 @@ func (e *Expectation) NotToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if reflect.DeepEqual(e.actual, expected) {
|
if reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) ToBeNil() {
|
func (e *Expectation) ToBeNil() {
|
||||||
if e.actual != nil {
|
if e.actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nto be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) NotToBeNil() {
|
func (e *Expectation) NotToBeNil() {
|
||||||
if e.actual == nil {
|
if e.actual == nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nnot to be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +56,10 @@ func (e *Expectation) ToBeTrue() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if !a {
|
if !a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be true", e.actual))
|
e.fatalf("Expected\n\t%v\nto be true", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +67,10 @@ func (e *Expectation) ToBeFalse() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if a {
|
if a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be false", e.actual))
|
e.fatalf("Expected\n\t%v\nto be false", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,14 +80,14 @@ func (e *Expectation) NotToPanic() {
|
|||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if recovered := recover(); recovered != nil {
|
if recovered := recover(); recovered != nil {
|
||||||
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
|
e.fatalf("Expected panic\n\t%v\nto have not been raised", recovered)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
a()
|
a()
|
||||||
}()
|
}()
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-func value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,10 +95,10 @@ func (e *Expectation) ToSucceed() {
|
|||||||
switch actual := e.actual.(type) {
|
switch actual := e.actual.(type) {
|
||||||
case error:
|
case error:
|
||||||
if actual != nil {
|
if actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected error\n\t%v\nto have succeeded", actual))
|
e.fatalf("Expected error\n\t%v\nto have succeeded", actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nsucceeded", actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nsucceeded", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,20 +107,20 @@ func (e *Expectation) ToMatchError(expected interface{}) {
|
|||||||
|
|
||||||
actual, ok := e.actual.(error)
|
actual, ok := e.actual.(error)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nmatches error", e.actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nmatches error", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch expected := expected.(type) {
|
switch expected := expected.(type) {
|
||||||
case error:
|
case error:
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
if actual.Error() != expected {
|
if actual.Error() != expected {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected))
|
e.fatalf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !contained {
|
if !contained {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,7 +169,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
|
|
||||||
for j := 0; j < actualValue.Len(); j++ {
|
for j := 0; j < actualValue.Len(); j++ {
|
||||||
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
switch expectedKind {
|
switch expectedKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", expected))
|
e.fatalf("Expected\n\t%v\nto be an array", expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +209,12 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
actualKind := actualValue.Kind()
|
actualKind := actualValue.Kind()
|
||||||
|
|
||||||
if actualKind != expectedKind {
|
if actualKind != expectedKind {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if actualValue.Len() != expectedValue.Len() {
|
if actualValue.Len() != expectedValue.Len() {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,44 +246,44 @@ func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interfac
|
|||||||
if actual, ok := e.actual.(time.Time); ok {
|
if actual, ok := e.actual.(time.Time); ok {
|
||||||
ct, ok := compareTo.(time.Time)
|
ct, ok := compareTo.(time.Time)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot compare to non-temporal value\n\t%v", compareTo))
|
e.fatalf("Cannot compare to non-temporal value\n\t%v", compareTo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch matcher {
|
switch matcher {
|
||||||
case Before:
|
case Before:
|
||||||
if !actual.Before(ct) {
|
if !actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case BeforeOrSameTime:
|
case BeforeOrSameTime:
|
||||||
if actual.After(ct) {
|
if actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case After:
|
case After:
|
||||||
if !actual.After(ct) {
|
if !actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case AfterOrSameTime:
|
case AfterOrSameTime:
|
||||||
if actual.Before(ct) {
|
if actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail("Cannot compare times with unexpected temporal matcher")
|
e.fatalf("Cannot compare times with unexpected temporal matcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.fail(fmt.Sprintf("Cannot compare non-temporal value\n\t%v", e.actual))
|
e.fatalf("Cannot compare non-temporal value\n\t%v", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
||||||
if expected == nil {
|
if expected == nil {
|
||||||
e.fail("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
e.fatalf("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) fail(msg string) {
|
func (e *Expectation) fatalf(format string, a ...any) {
|
||||||
// Prune the stack trace so that it's easier to see relevant lines
|
// Prune the stack trace so that it's easier to see relevant lines
|
||||||
stack := strings.Split(string(debug.Stack()), "\n")
|
stack := strings.Split(string(debug.Stack()), "\n")
|
||||||
var prunedStack []string
|
var prunedStack []string
|
||||||
@@ -294,5 +294,6 @@ func (e *Expectation) fail(msg string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf(format, a...)
|
||||||
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,7 @@ func (e *Expectation) ToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if !reflect.DeepEqual(e.actual, expected) {
|
if !reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,19 +36,19 @@ func (e *Expectation) NotToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if reflect.DeepEqual(e.actual, expected) {
|
if reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) ToBeNil() {
|
func (e *Expectation) ToBeNil() {
|
||||||
if e.actual != nil {
|
if e.actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nto be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) NotToBeNil() {
|
func (e *Expectation) NotToBeNil() {
|
||||||
if e.actual == nil {
|
if e.actual == nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nnot to be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +56,10 @@ func (e *Expectation) ToBeTrue() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if !a {
|
if !a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be true", e.actual))
|
e.fatalf("Expected\n\t%v\nto be true", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +67,10 @@ func (e *Expectation) ToBeFalse() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if a {
|
if a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be false", e.actual))
|
e.fatalf("Expected\n\t%v\nto be false", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,14 +80,14 @@ func (e *Expectation) NotToPanic() {
|
|||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if recovered := recover(); recovered != nil {
|
if recovered := recover(); recovered != nil {
|
||||||
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
|
e.fatalf("Expected panic\n\t%v\nto have not been raised", recovered)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
a()
|
a()
|
||||||
}()
|
}()
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-func value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,10 +95,10 @@ func (e *Expectation) ToSucceed() {
|
|||||||
switch actual := e.actual.(type) {
|
switch actual := e.actual.(type) {
|
||||||
case error:
|
case error:
|
||||||
if actual != nil {
|
if actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected error\n\t%v\nto have succeeded", actual))
|
e.fatalf("Expected error\n\t%v\nto have succeeded", actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nsucceeded", actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nsucceeded", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,20 +107,20 @@ func (e *Expectation) ToMatchError(expected interface{}) {
|
|||||||
|
|
||||||
actual, ok := e.actual.(error)
|
actual, ok := e.actual.(error)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nmatches error", e.actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nmatches error", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch expected := expected.(type) {
|
switch expected := expected.(type) {
|
||||||
case error:
|
case error:
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
if actual.Error() != expected {
|
if actual.Error() != expected {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected))
|
e.fatalf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !contained {
|
if !contained {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,7 +169,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
|
|
||||||
for j := 0; j < actualValue.Len(); j++ {
|
for j := 0; j < actualValue.Len(); j++ {
|
||||||
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
switch expectedKind {
|
switch expectedKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", expected))
|
e.fatalf("Expected\n\t%v\nto be an array", expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +209,12 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
actualKind := actualValue.Kind()
|
actualKind := actualValue.Kind()
|
||||||
|
|
||||||
if actualKind != expectedKind {
|
if actualKind != expectedKind {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if actualValue.Len() != expectedValue.Len() {
|
if actualValue.Len() != expectedValue.Len() {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,44 +246,44 @@ func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interfac
|
|||||||
if actual, ok := e.actual.(time.Time); ok {
|
if actual, ok := e.actual.(time.Time); ok {
|
||||||
ct, ok := compareTo.(time.Time)
|
ct, ok := compareTo.(time.Time)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot compare to non-temporal value\n\t%v", compareTo))
|
e.fatalf("Cannot compare to non-temporal value\n\t%v", compareTo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch matcher {
|
switch matcher {
|
||||||
case Before:
|
case Before:
|
||||||
if !actual.Before(ct) {
|
if !actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case BeforeOrSameTime:
|
case BeforeOrSameTime:
|
||||||
if actual.After(ct) {
|
if actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case After:
|
case After:
|
||||||
if !actual.After(ct) {
|
if !actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case AfterOrSameTime:
|
case AfterOrSameTime:
|
||||||
if actual.Before(ct) {
|
if actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail("Cannot compare times with unexpected temporal matcher")
|
e.fatalf("Cannot compare times with unexpected temporal matcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.fail(fmt.Sprintf("Cannot compare non-temporal value\n\t%v", e.actual))
|
e.fatalf("Cannot compare non-temporal value\n\t%v", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
||||||
if expected == nil {
|
if expected == nil {
|
||||||
e.fail("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
e.fatalf("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) fail(msg string) {
|
func (e *Expectation) fatalf(format string, a ...any) {
|
||||||
// Prune the stack trace so that it's easier to see relevant lines
|
// Prune the stack trace so that it's easier to see relevant lines
|
||||||
stack := strings.Split(string(debug.Stack()), "\n")
|
stack := strings.Split(string(debug.Stack()), "\n")
|
||||||
var prunedStack []string
|
var prunedStack []string
|
||||||
@@ -294,5 +294,6 @@ func (e *Expectation) fail(msg string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf(format, a...)
|
||||||
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,7 @@ func (e *Expectation) ToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if !reflect.DeepEqual(e.actual, expected) {
|
if !reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,19 +36,19 @@ func (e *Expectation) NotToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if reflect.DeepEqual(e.actual, expected) {
|
if reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) ToBeNil() {
|
func (e *Expectation) ToBeNil() {
|
||||||
if e.actual != nil {
|
if e.actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nto be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) NotToBeNil() {
|
func (e *Expectation) NotToBeNil() {
|
||||||
if e.actual == nil {
|
if e.actual == nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nnot to be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +56,10 @@ func (e *Expectation) ToBeTrue() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if !a {
|
if !a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be true", e.actual))
|
e.fatalf("Expected\n\t%v\nto be true", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +67,10 @@ func (e *Expectation) ToBeFalse() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if a {
|
if a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be false", e.actual))
|
e.fatalf("Expected\n\t%v\nto be false", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,14 +80,14 @@ func (e *Expectation) NotToPanic() {
|
|||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if recovered := recover(); recovered != nil {
|
if recovered := recover(); recovered != nil {
|
||||||
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
|
e.fatalf("Expected panic\n\t%v\nto have not been raised", recovered)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
a()
|
a()
|
||||||
}()
|
}()
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-func value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,10 +95,10 @@ func (e *Expectation) ToSucceed() {
|
|||||||
switch actual := e.actual.(type) {
|
switch actual := e.actual.(type) {
|
||||||
case error:
|
case error:
|
||||||
if actual != nil {
|
if actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected error\n\t%v\nto have succeeded", actual))
|
e.fatalf("Expected error\n\t%v\nto have succeeded", actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nsucceeded", actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nsucceeded", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,20 +107,20 @@ func (e *Expectation) ToMatchError(expected interface{}) {
|
|||||||
|
|
||||||
actual, ok := e.actual.(error)
|
actual, ok := e.actual.(error)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nmatches error", e.actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nmatches error", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch expected := expected.(type) {
|
switch expected := expected.(type) {
|
||||||
case error:
|
case error:
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
if actual.Error() != expected {
|
if actual.Error() != expected {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected))
|
e.fatalf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !contained {
|
if !contained {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,7 +169,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
|
|
||||||
for j := 0; j < actualValue.Len(); j++ {
|
for j := 0; j < actualValue.Len(); j++ {
|
||||||
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
switch expectedKind {
|
switch expectedKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", expected))
|
e.fatalf("Expected\n\t%v\nto be an array", expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +209,12 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
actualKind := actualValue.Kind()
|
actualKind := actualValue.Kind()
|
||||||
|
|
||||||
if actualKind != expectedKind {
|
if actualKind != expectedKind {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if actualValue.Len() != expectedValue.Len() {
|
if actualValue.Len() != expectedValue.Len() {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,44 +246,44 @@ func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interfac
|
|||||||
if actual, ok := e.actual.(time.Time); ok {
|
if actual, ok := e.actual.(time.Time); ok {
|
||||||
ct, ok := compareTo.(time.Time)
|
ct, ok := compareTo.(time.Time)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot compare to non-temporal value\n\t%v", compareTo))
|
e.fatalf("Cannot compare to non-temporal value\n\t%v", compareTo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch matcher {
|
switch matcher {
|
||||||
case Before:
|
case Before:
|
||||||
if !actual.Before(ct) {
|
if !actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case BeforeOrSameTime:
|
case BeforeOrSameTime:
|
||||||
if actual.After(ct) {
|
if actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case After:
|
case After:
|
||||||
if !actual.After(ct) {
|
if !actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case AfterOrSameTime:
|
case AfterOrSameTime:
|
||||||
if actual.Before(ct) {
|
if actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail("Cannot compare times with unexpected temporal matcher")
|
e.fatalf("Cannot compare times with unexpected temporal matcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.fail(fmt.Sprintf("Cannot compare non-temporal value\n\t%v", e.actual))
|
e.fatalf("Cannot compare non-temporal value\n\t%v", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
||||||
if expected == nil {
|
if expected == nil {
|
||||||
e.fail("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
e.fatalf("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) fail(msg string) {
|
func (e *Expectation) fatalf(format string, a ...any) {
|
||||||
// Prune the stack trace so that it's easier to see relevant lines
|
// Prune the stack trace so that it's easier to see relevant lines
|
||||||
stack := strings.Split(string(debug.Stack()), "\n")
|
stack := strings.Split(string(debug.Stack()), "\n")
|
||||||
var prunedStack []string
|
var prunedStack []string
|
||||||
@@ -294,5 +294,6 @@ func (e *Expectation) fail(msg string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf(format, a...)
|
||||||
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
||||||
}
|
}
|
||||||
|
@@ -28,7 +28,7 @@ func (e *Expectation) ToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if !reflect.DeepEqual(e.actual, expected) {
|
if !reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,19 +36,19 @@ func (e *Expectation) NotToEqual(expected interface{}) {
|
|||||||
e.verifyExpectedNotNil(expected)
|
e.verifyExpectedNotNil(expected)
|
||||||
|
|
||||||
if reflect.DeepEqual(e.actual, expected) {
|
if reflect.DeepEqual(e.actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nnot to equal\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) ToBeNil() {
|
func (e *Expectation) ToBeNil() {
|
||||||
if e.actual != nil {
|
if e.actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nto be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) NotToBeNil() {
|
func (e *Expectation) NotToBeNil() {
|
||||||
if e.actual == nil {
|
if e.actual == nil {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to be nil", e.actual))
|
e.fatalf("Expected\n\t%v\nnot to be nil", e.actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,10 +56,10 @@ func (e *Expectation) ToBeTrue() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if !a {
|
if !a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be true", e.actual))
|
e.fatalf("Expected\n\t%v\nto be true", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,10 +67,10 @@ func (e *Expectation) ToBeFalse() {
|
|||||||
switch a := e.actual.(type) {
|
switch a := e.actual.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
if a {
|
if a {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be false", e.actual))
|
e.fatalf("Expected\n\t%v\nto be false", e.actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-bool value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-bool value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,14 +80,14 @@ func (e *Expectation) NotToPanic() {
|
|||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if recovered := recover(); recovered != nil {
|
if recovered := recover(); recovered != nil {
|
||||||
e.fail(fmt.Sprintf("Expected panic\n\t%v\nto have not been raised", recovered))
|
e.fatalf("Expected panic\n\t%v\nto have not been raised", recovered)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
a()
|
a()
|
||||||
}()
|
}()
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-func value\n\t%v\nis truthy", a))
|
e.fatalf("Cannot check if non-func value\n\t%v\nis truthy", a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,10 +95,10 @@ func (e *Expectation) ToSucceed() {
|
|||||||
switch actual := e.actual.(type) {
|
switch actual := e.actual.(type) {
|
||||||
case error:
|
case error:
|
||||||
if actual != nil {
|
if actual != nil {
|
||||||
e.fail(fmt.Sprintf("Expected error\n\t%v\nto have succeeded", actual))
|
e.fatalf("Expected error\n\t%v\nto have succeeded", actual)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nsucceeded", actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nsucceeded", actual)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,20 +107,20 @@ func (e *Expectation) ToMatchError(expected interface{}) {
|
|||||||
|
|
||||||
actual, ok := e.actual.(error)
|
actual, ok := e.actual.(error)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot check if non-error value\n\t%v\nmatches error", e.actual))
|
e.fatalf("Cannot check if non-error value\n\t%v\nmatches error", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch expected := expected.(type) {
|
switch expected := expected.(type) {
|
||||||
case error:
|
case error:
|
||||||
if !reflect.DeepEqual(actual, expected) {
|
if !reflect.DeepEqual(actual, expected) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
if actual.Error() != expected {
|
if actual.Error() != expected {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto match error\n\t%v", actual, expected))
|
e.fatalf("Expected\n\t%v\nto match error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected))
|
e.fatalf("Cannot match\n\t%v\nagainst non-error\n\t%v", actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -156,7 +156,7 @@ func (e *Expectation) ToContain(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !contained {
|
if !contained {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nto contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -169,7 +169,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
switch actualKind {
|
switch actualKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", e.actual))
|
e.fatalf("Expected\n\t%v\nto be an array", e.actual)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -187,7 +187,7 @@ func (e *Expectation) NotToContain(expected interface{}) {
|
|||||||
|
|
||||||
for j := 0; j < actualValue.Len(); j++ {
|
for j := 0; j < actualValue.Len(); j++ {
|
||||||
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
if reflect.DeepEqual(actualValue.Index(j).Interface(), expectedElem) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem))
|
e.fatalf("Expected\n\t%v\nnot to contain\n\t%v", e.actual, expectedElem)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
switch expectedKind {
|
switch expectedKind {
|
||||||
case reflect.Array, reflect.Slice:
|
case reflect.Array, reflect.Slice:
|
||||||
default:
|
default:
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be an array", expected))
|
e.fatalf("Expected\n\t%v\nto be an array", expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -209,12 +209,12 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
actualKind := actualValue.Kind()
|
actualKind := actualValue.Kind()
|
||||||
|
|
||||||
if actualKind != expectedKind {
|
if actualKind != expectedKind {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto be the same type as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if actualValue.Len() != expectedValue.Len() {
|
if actualValue.Len() != expectedValue.Len() {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto have the same length as\n\t%v", e.actual, expected)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,7 +237,7 @@ func (e *Expectation) ToMatchInAnyOrder(expected interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !found {
|
if !found {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected))
|
e.fatalf("Expected\n\t%v\nto contain the same elements as\n\t%v", e.actual, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -246,44 +246,44 @@ func (e *Expectation) ToBeTemporally(matcher TemporalMatcher, compareTo interfac
|
|||||||
if actual, ok := e.actual.(time.Time); ok {
|
if actual, ok := e.actual.(time.Time); ok {
|
||||||
ct, ok := compareTo.(time.Time)
|
ct, ok := compareTo.(time.Time)
|
||||||
if !ok {
|
if !ok {
|
||||||
e.fail(fmt.Sprintf("Cannot compare to non-temporal value\n\t%v", compareTo))
|
e.fatalf("Cannot compare to non-temporal value\n\t%v", compareTo)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
switch matcher {
|
switch matcher {
|
||||||
case Before:
|
case Before:
|
||||||
if !actual.Before(ct) {
|
if !actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case BeforeOrSameTime:
|
case BeforeOrSameTime:
|
||||||
if actual.After(ct) {
|
if actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally before or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case After:
|
case After:
|
||||||
if !actual.After(ct) {
|
if !actual.After(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
case AfterOrSameTime:
|
case AfterOrSameTime:
|
||||||
if actual.Before(ct) {
|
if actual.Before(ct) {
|
||||||
e.fail(fmt.Sprintf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo))
|
e.fatalf("Expected\n\t%v\nto be temporally after or at the same time as\n\t%v", e.actual, compareTo)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
e.fail("Cannot compare times with unexpected temporal matcher")
|
e.fatalf("Cannot compare times with unexpected temporal matcher")
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
e.fail(fmt.Sprintf("Cannot compare non-temporal value\n\t%v", e.actual))
|
e.fatalf("Cannot compare non-temporal value\n\t%v", e.actual)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
func (e *Expectation) verifyExpectedNotNil(expected interface{}) {
|
||||||
if expected == nil {
|
if expected == nil {
|
||||||
e.fail("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
e.fatalf("Refusing to compare with <nil>. Use `ToBeNil` or `NotToBeNil` instead.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Expectation) fail(msg string) {
|
func (e *Expectation) fatalf(format string, a ...any) {
|
||||||
// Prune the stack trace so that it's easier to see relevant lines
|
// Prune the stack trace so that it's easier to see relevant lines
|
||||||
stack := strings.Split(string(debug.Stack()), "\n")
|
stack := strings.Split(string(debug.Stack()), "\n")
|
||||||
var prunedStack []string
|
var prunedStack []string
|
||||||
@@ -294,5 +294,6 @@ func (e *Expectation) fail(msg string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
msg := fmt.Sprintf(format, a...)
|
||||||
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
e.t.Fatalf("\n%s\n%s\n", strings.Join(prunedStack, "\n"), msg)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user