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

Omit package name in internal test failure output #174

Closed
wants to merge 1 commit into from
Closed
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
66 changes: 66 additions & 0 deletions internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package quicktest

import (
"bytes"
"fmt"
"strings"
"testing"
)

func TestInternal(t *testing.T) {
tt := &testingT{}
c := New(tt)
c.Assert(internal{internal0{42}}, DeepEquals, internal{internal0{47}})

got := tt.fatalString()
// Quicktest package is not displayed in the failure output.
want := `
got:
internal{
Int: internal0{Num:42},
}
want:
internal{
Int: internal0{Num:47},
}
`
if !strings.Contains(got, want) {
t.Fatalf(`
got %q
want %q
-------------------- got --------------------
%s
-------------------- want -------------------
%s
---------------------------------------------`, got, want, got, want)
}
}

type internal struct {
Int internal0
}

type internal0 struct {
Num int
}

// testingT can be passed to qt.New for testing purposes.
type testingT struct {
testing.TB

fatalBuf bytes.Buffer
}

// Fatal overrides *testing.T.Fatal so that messages are collected and the
// goroutine is not killed.
func (t *testingT) Fatal(a ...interface{}) {
fmt.Fprint(&t.fatalBuf, a...)
}

// Helper implements testing.TB.Helper.
func (t *testingT) Helper() {}

// fatalString returns the fatal error message.
func (t *testingT) fatalString() string {
return t.fatalBuf.String()
}
22 changes: 22 additions & 0 deletions report.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func writeError(w io.Writer, err error, p reportParams) {
ptrs := make(map[string]interface{})
values := make(map[string]string)

pc, _, _, _ := runtime.Caller(4)
fn := runtime.FuncForPC(pc).Name()
pkg := fn[:strings.LastIndexByte(fn, '.')]
pkgPrefix := pkg[strings.LastIndexByte(pkg, '/')+1:] + "."

printPair := func(key string, value interface{}) {
fmt.Fprintln(w, key+":")
var v string
Expand Down Expand Up @@ -100,6 +105,12 @@ func writeError(w io.Writer, err error, p reportParams) {
}
}

// Check wether we can remove the package prefix from the output.
if pkgPath(value) == pkg && strings.HasPrefix(v, pkgPrefix) {
// TODO(frankban): This is best effort and suboptimal.
v = strings.ReplaceAll(v[len(pkgPrefix):], ": "+pkgPrefix, ": ")
}

values[v] = key
fmt.Fprint(w, prefixf(prefix, "%s", v))
}
Expand Down Expand Up @@ -246,3 +257,14 @@ type note struct {

// prefix is the string used to indent blocks of output.
const prefix = " "

func pkgPath(v interface{}) string {
if s, ok := v.(SuppressedIfLong); ok {
v = s.Value
}
t := reflect.TypeOf(v)
if t == nil {
return ""
}
return t.PkgPath()
}
Loading