Debian Codegraph @

error_test.go

raw
1 // Licensed under the MIT license, see LICENSE file for details.
2
3 package qt_test
4
5 import (
6 "errors"
7 "fmt"
8 "testing"
9
10 "github.com/go-quicktest/qt"
11 )
12
13 func TestBadCheckf(t *testing.T) {
14 err := qt.BadCheckf("bad %s", "wolf")
15 expectedMessage := "bad check: bad wolf"
16 if err.Error() != expectedMessage {
17 t.Fatalf("error:\ngot %q\nwant %q", err, expectedMessage)
18 }
19 }
20
21 func TestIsBadCheck(t *testing.T) {
22 err := qt.BadCheckf("bad wolf")
23 assertBool(t, qt.IsBadCheck(err), true)
24 err = errors.New("bad wolf")
25 assertBool(t, qt.IsBadCheck(err), false)
26 }
27
28 var errBadWolf = &errTest{
29 msg: "bad wolf",
30 formatted: true,
31 }
32
33 var errBadWolfMultiLine = &errTest{
34 msg: "bad wolf\nfaulty logic",
35 formatted: true,
36 }
37
38 // errTest is an error type used in tests.
39 type errTest struct {
40 msg string
41 formatted bool
42 }
43
44 // Error implements error.
45 func (err *errTest) Error() string {
46 return err.msg
47 }
48
49 // Format implements fmt.Formatter.
50 func (err *errTest) Format(f fmt.State, c rune) {
51 if !f.Flag('+') || c != 'v' {
52 fmt.Fprint(f, "unexpected verb for formatting the error")
53 }
54 fmt.Fprint(f, err.Error())
55 if err.formatted {
56 fmt.Fprint(f, "\n file:line")
57 }
58 }