Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assert: look for the right function call in the source #210

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 43 additions & 49 deletions assert/assert_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package assert
package assert_test

import (
"fmt"
"os"
"testing"

gocmp "github.com/google/go-cmp/cmp"
"gotest.tools/v3/assert"
"gotest.tools/v3/assert/cmp"
)

Expand All @@ -32,45 +33,45 @@ func (f *fakeTestingT) Helper() {}
func TestAssertWithBoolFailure(t *testing.T) {
fakeT := &fakeTestingT{}

Assert(fakeT, 1 == 6)
assert.Assert(fakeT, 1 == 6)
expectFailNowed(t, fakeT, "assertion failed: expression is false: 1 == 6")
}

func TestAssertWithBoolFailureNotEqual(t *testing.T) {
fakeT := &fakeTestingT{}

var err error
Assert(fakeT, err != nil)
assert.Assert(fakeT, err != nil)
expectFailNowed(t, fakeT, "assertion failed: err is nil")
}

func TestAssertWithBoolFailureNotTrue(t *testing.T) {
fakeT := &fakeTestingT{}

badNews := true
Assert(fakeT, !badNews)
assert.Assert(fakeT, !badNews)
expectFailNowed(t, fakeT, "assertion failed: badNews is true")
}

func TestAssertWithBoolFailureAndExtraMessage(t *testing.T) {
fakeT := &fakeTestingT{}

Assert(fakeT, 1 > 5, "sometimes things fail")
assert.Assert(fakeT, 1 > 5, "sometimes things fail")
expectFailNowed(t, fakeT,
"assertion failed: expression is false: 1 > 5: sometimes things fail")
}

func TestAssertWithBoolSuccess(t *testing.T) {
fakeT := &fakeTestingT{}

Assert(fakeT, 1 < 5)
assert.Assert(fakeT, 1 < 5)
expectSuccess(t, fakeT)
}

func TestAssertWithBoolMultiLineFailure(t *testing.T) {
fakeT := &fakeTestingT{}

Assert(fakeT, func() bool {
assert.Assert(fakeT, func() bool {
for range []int{1, 2, 3, 4} {
}
return false
Expand All @@ -95,23 +96,23 @@ func TestAssertWithComparisonSuccess(t *testing.T) {
fakeT := &fakeTestingT{}

cmp := exampleComparison{success: true}
Assert(fakeT, cmp.Compare)
assert.Assert(fakeT, cmp.Compare)
expectSuccess(t, fakeT)
}

func TestAssertWithComparisonFailure(t *testing.T) {
fakeT := &fakeTestingT{}

cmp := exampleComparison{message: "oops, not good"}
Assert(fakeT, cmp.Compare)
assert.Assert(fakeT, cmp.Compare)
expectFailNowed(t, fakeT, "assertion failed: oops, not good")
}

func TestAssertWithComparisonAndExtraMessage(t *testing.T) {
fakeT := &fakeTestingT{}

cmp := exampleComparison{message: "oops, not good"}
Assert(fakeT, cmp.Compare, "extra stuff %v", true)
assert.Assert(fakeT, cmp.Compare, "extra stuff %v", true)
expectFailNowed(t, fakeT, "assertion failed: oops, not good: extra stuff true")
}

Expand All @@ -130,41 +131,41 @@ func TestNilError(t *testing.T) {
t.Run("nil interface", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err error
NilError(fakeT, err)
assert.NilError(fakeT, err)
expectSuccess(t, fakeT)
})

t.Run("nil literal", func(t *testing.T) {
fakeT := &fakeTestingT{}
NilError(fakeT, nil)
assert.NilError(fakeT, nil)
expectSuccess(t, fakeT)
})

t.Run("interface with non-nil type", func(t *testing.T) {
fakeT := &fakeTestingT{}
var customErr *customError
NilError(fakeT, customErr)
expected := "assertion failed: error is not nil: error has type *assert.customError"
assert.NilError(fakeT, customErr)
expected := "assertion failed: error is not nil: error has type *assert_test.customError"
expectFailNowed(t, fakeT, expected)
})

t.Run("non-nil error", func(t *testing.T) {
fakeT := &fakeTestingT{}
NilError(fakeT, fmt.Errorf("this is the error"))
assert.NilError(fakeT, fmt.Errorf("this is the error"))
expectFailNowed(t, fakeT, "assertion failed: error is not nil: this is the error")
})

t.Run("non-nil error with struct type", func(t *testing.T) {
fakeT := &fakeTestingT{}
err := structError{}
NilError(fakeT, err)
assert.NilError(fakeT, err)
expectFailNowed(t, fakeT, "assertion failed: error is not nil: this is a struct")
})

t.Run("non-nil error with map type", func(t *testing.T) {
fakeT := &fakeTestingT{}
var err mapError
NilError(fakeT, err)
assert.NilError(fakeT, err)
expectFailNowed(t, fakeT, "assertion failed: error is not nil: ")
})
}
Expand All @@ -184,7 +185,7 @@ func (m mapError) Error() string {
func TestCheckFailure(t *testing.T) {
fakeT := &fakeTestingT{}

if Check(fakeT, 1 == 2) {
if assert.Check(fakeT, 1 == 2) {
t.Error("expected check to return false on failure")
}
expectFailed(t, fakeT, "assertion failed: expression is false: 1 == 2")
Expand All @@ -193,7 +194,7 @@ func TestCheckFailure(t *testing.T) {
func TestCheckSuccess(t *testing.T) {
fakeT := &fakeTestingT{}

if !Check(fakeT, true) {
if !assert.Check(fakeT, true) {
t.Error("expected check to return true on success")
}
expectSuccess(t, fakeT)
Expand All @@ -203,42 +204,40 @@ func TestCheckEqualFailure(t *testing.T) {
fakeT := &fakeTestingT{}

actual, expected := 5, 9
Check(fakeT, cmp.Equal(actual, expected))
assert.Check(fakeT, cmp.Equal(actual, expected))
expectFailed(t, fakeT, "assertion failed: 5 (actual int) != 9 (expected int)")
}

func TestCheck_MultipleFunctionsOnTheSameLine(t *testing.T) {
fakeT := &fakeTestingT{}

f := func(b bool) {}
f(Check(fakeT, false))
// TODO: update the expected when there is a more correct fix
expectFailed(t, fakeT,
"assertion failed: but assert failed to find the expression to print")
f(assert.Check(fakeT, false))
expectFailed(t, fakeT, "assertion failed: expression is false: false")
}

func TestEqualSuccess(t *testing.T) {
fakeT := &fakeTestingT{}

Equal(fakeT, 1, 1)
assert.Equal(fakeT, 1, 1)
expectSuccess(t, fakeT)

Equal(fakeT, "abcd", "abcd")
assert.Equal(fakeT, "abcd", "abcd")
expectSuccess(t, fakeT)
}

func TestEqualFailure(t *testing.T) {
fakeT := &fakeTestingT{}

actual, expected := 1, 3
Equal(fakeT, actual, expected)
assert.Equal(fakeT, actual, expected)
expectFailNowed(t, fakeT, "assertion failed: 1 (actual int) != 3 (expected int)")
}

func TestEqualFailureTypes(t *testing.T) {
fakeT := &fakeTestingT{}

Equal(fakeT, 3, uint(3))
assert.Equal(fakeT, 3, uint(3))
expectFailNowed(t, fakeT, `assertion failed: 3 (int) != 3 (uint)`)
}

Expand All @@ -250,7 +249,7 @@ func TestEqualFailureWithSelectorArgument(t *testing.T) {
}
var testcase = tc{expected: "foo"}

Equal(fakeT, "ok", testcase.expected)
assert.Equal(fakeT, "ok", testcase.expected)
expectFailNowed(t, fakeT,
"assertion failed: ok (string) != foo (testcase.expected string)")
}
Expand All @@ -259,15 +258,15 @@ func TestEqualFailureWithIndexExpr(t *testing.T) {
fakeT := &fakeTestingT{}

expected := map[string]string{"foo": "bar"}
Equal(fakeT, "ok", expected["foo"])
assert.Equal(fakeT, "ok", expected["foo"])
expectFailNowed(t, fakeT,
`assertion failed: ok (string) != bar (expected["foo"] string)`)
}

func TestEqualFailureWithCallExprArgument(t *testing.T) {
fakeT := &fakeTestingT{}
ce := customError{}
Equal(fakeT, "", ce.Error())
assert.Equal(fakeT, "", ce.Error())
expectFailNowed(t, fakeT,
"assertion failed: (string) != custom error (string)")
}
Expand All @@ -278,20 +277,19 @@ func TestAssertFailureWithOfflineComparison(t *testing.T) {
b := 2
// store comparison in a variable, so ast lookup can't find it
comparison := cmp.Equal(a, b)
Assert(fakeT, comparison)
assert.Assert(fakeT, comparison)
// expected value wont have variable names
expectFailNowed(t, fakeT, "assertion failed: 1 (int) != 2 (int)")
}

type testingT interface {
Errorf(msg string, args ...interface{})
Fatalf(msg string, args ...interface{})
Helper()
}

func expectFailNowed(t testingT, fakeT *fakeTestingT, expected string) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
t.Helper()
if fakeT.failed {
t.Errorf("should not have failed, got messages %s", fakeT.msgs)
}
Expand All @@ -305,9 +303,7 @@ func expectFailNowed(t testingT, fakeT *fakeTestingT, expected string) {

// nolint: unparam
func expectFailed(t testingT, fakeT *fakeTestingT, expected string) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
t.Helper()
if fakeT.failNowed {
t.Errorf("should not have failNowed, got messages %s", fakeT.msgs)
}
Expand All @@ -320,9 +316,7 @@ func expectFailed(t testingT, fakeT *fakeTestingT, expected string) {
}

func expectSuccess(t testingT, fakeT *fakeTestingT) {
if ht, ok := t.(helperT); ok {
ht.Helper()
}
t.Helper()
if fakeT.failNowed {
t.Errorf("should not have failNowed, got messages %s", fakeT.msgs)
}
Expand All @@ -341,7 +335,7 @@ func TestDeepEqualSuccess(t *testing.T) {
expected := stub{"ok", 1}

fakeT := &fakeTestingT{}
DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
assert.DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
expectSuccess(t, fakeT)
}

Expand All @@ -350,7 +344,7 @@ func TestDeepEqualFailure(t *testing.T) {
expected := stub{"ok", 2}

fakeT := &fakeTestingT{}
DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
assert.DeepEqual(fakeT, actual, expected, gocmp.AllowUnexported(stub{}))
if !fakeT.failNowed {
t.Fatal("should have failNowed")
}
Expand All @@ -361,14 +355,14 @@ func TestErrorFailure(t *testing.T) {
fakeT := &fakeTestingT{}

var err error
Error(fakeT, err, "this error")
assert.Error(fakeT, err, "this error")
expectFailNowed(t, fakeT, "assertion failed: expected an error, got nil")
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}

err := fmt.Errorf("the actual error")
Error(fakeT, err, "this error")
assert.Error(fakeT, err, "this error")
expected := `assertion failed: expected error "this error", got "the actual error"`
expectFailNowed(t, fakeT, expected)
})
Expand All @@ -379,14 +373,14 @@ func TestErrorContainsFailure(t *testing.T) {
fakeT := &fakeTestingT{}

var err error
ErrorContains(fakeT, err, "this error")
assert.ErrorContains(fakeT, err, "this error")
expectFailNowed(t, fakeT, "assertion failed: expected an error, got nil")
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}

err := fmt.Errorf("the actual error")
ErrorContains(fakeT, err, "this error")
assert.ErrorContains(fakeT, err, "this error")
expected := `assertion failed: expected error to contain "this error", got "the actual error"`
expectFailNowed(t, fakeT, expected)
})
Expand All @@ -397,14 +391,14 @@ func TestErrorTypeFailure(t *testing.T) {
fakeT := &fakeTestingT{}

var err error
ErrorType(fakeT, err, os.IsNotExist)
assert.ErrorType(fakeT, err, os.IsNotExist)
expectFailNowed(t, fakeT, "assertion failed: error is nil, not os.IsNotExist")
})
t.Run("different error", func(t *testing.T) {
fakeT := &fakeTestingT{}

err := fmt.Errorf("the actual error")
ErrorType(fakeT, err, os.IsNotExist)
assert.ErrorType(fakeT, err, os.IsNotExist)
expected := `assertion failed: error is the actual error (*errors.errorString), not os.IsNotExist`
expectFailNowed(t, fakeT, expected)
})
Expand Down
7 changes: 3 additions & 4 deletions internal/source/defers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,12 @@ func guessDefer(node ast.Node) (ast.Node, error) {
defers := collectDefers(node)
switch len(defers) {
case 0:
return nil, errors.New("failed to expression in defer")
return nil, errors.New("failed to find expression in defer")
case 1:
return defers[0].Call, nil
default:
return nil, errors.Errorf(
"ambiguous call expression: multiple (%d) defers in call block",
len(defers))
msg := "ambiguous call expression: multiple (%d) defers in call block"
return nil, errors.Errorf(msg, len(defers))
}
}

Expand Down
Loading