Skip to content

Commit

Permalink
Improved message
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 18, 2016
1 parent eca549e commit 385e27c
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
// ErrPlain is the default error that is returned for functions in this package.
var ErrPlain = errors.New("error")

////////////////////////////////////////////////////////////////

func fileline(i int) string {
_, file, line, ok := runtime.Caller(i)
if !ok {
Expand All @@ -28,42 +30,52 @@ func trace() string {
return "\r\t" + strings.Repeat(" ", len(fmt.Sprintf("%s:", trace2))) + "\r\t" + trace3
}

func message(empty string, msgs ...interface{}) string {
msg := fmt.Sprintln(msgs...)
if len(msg) == 0 {
msg = empty + "\n"
}
return msg
}

func printable(s string) string {
s = strings.Replace(s, "\n", `\n`, -1)
s = strings.Replace(s, "\r", `\r`, -1)
s = strings.Replace(s, "\t", `\t`, -1)
return s
}

func That(t *testing.T, condition bool, msg ...interface{}) {
////////////////////////////////////////////////////////////////

func That(t *testing.T, condition bool, msgs ...interface{}) {
if !condition {
t.Errorf("%s: %s\n", trace(), fmt.Sprint(msg...))
t.Errorf("%s: %s", trace(), message("bad assertion", msgs...))
}
}

func Error(t *testing.T, err, expected error, msg ...interface{}) {
func Error(t *testing.T, err, expected error, msgs ...interface{}) {
if err != expected {
t.Errorf("%s: %s\n error: %v\nexpected: %v\n", trace(), fmt.Sprint(msg...), err, expected)
t.Errorf("%s: %s error: %v\nexpected: %v\n", trace(), message("", msgs...), err, expected)
}
}

func String(t *testing.T, output, expected string, msg ...interface{}) {
func String(t *testing.T, output, expected string, msgs ...interface{}) {
if output != expected {
t.Errorf("%s: %s\n output: %s\nexpected: %s\n", trace(), fmt.Sprint(msg...), printable(output), printable(expected))
t.Errorf("%s: %s output: %s\nexpected: %s\n", trace(), message("", msgs...), printable(output), printable(expected))
}
}

func Bytes(t *testing.T, output, expected []byte, msg ...interface{}) {
func Bytes(t *testing.T, output, expected []byte, msgs ...interface{}) {
if !bytes.Equal(output, expected) {
t.Errorf("%s: %s\n output: %s\nexpected: %s\n", trace(), fmt.Sprint(msg...), printable(string(output)), printable(string(expected)))
t.Errorf("%s: %s output: %s\nexpected: %s\n", trace(), message("", msgs...), printable(string(output)), printable(string(expected)))
}
}

func Minify(t *testing.T, input string, err error, output, expected string, msg ...interface{}) {
func Minify(t *testing.T, input string, err error, output, expected string, msgs ...interface{}) {
if err != nil {
t.Errorf("%s: %s\n given: %s\n error: %v\n", trace(), fmt.Sprint(msg...), printable(input), err)
t.Errorf("%s: %s given: %s\n error: %v\n", trace(), message("", msgs...), printable(input), err)
}
if output != expected {
t.Errorf("%s: %s\n given: %s\nminified: %s\nexpected: %s\n", trace(), fmt.Sprint(msg...), printable(input), printable(output), printable(expected))
t.Errorf("%s: %s given: %s\nminified: %s\nexpected: %s\n", trace(), message("", msgs...), printable(input), printable(output), printable(expected))
}
}

0 comments on commit 385e27c

Please sign in to comment.