| 1 |
// Licensed under the MIT license, see LICENSE file for details. |
| 2 |
|
| 3 |
package quicktest_test |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"testing" |
| 8 |
|
| 9 |
qt "github.com/frankban/quicktest" |
| 10 |
) |
| 11 |
|
| 12 |
var formatTests = []struct { |
| 13 |
about string |
| 14 |
value interface{} |
| 15 |
want string |
| 16 |
}{{ |
| 17 |
about: "error value", |
| 18 |
value: errBadWolf, |
| 19 |
want: "bad wolf\n file:line", |
| 20 |
}, { |
| 21 |
about: "error value: not formatted", |
| 22 |
value: &errTest{ |
| 23 |
msg: "exterminate!", |
| 24 |
}, |
| 25 |
want: `e"exterminate!"`, |
| 26 |
}, { |
| 27 |
about: "error value: with quotes", |
| 28 |
value: &errTest{ |
| 29 |
msg: `cannot open "/no/such/file"`, |
| 30 |
}, |
| 31 |
want: "e`cannot open \"/no/such/file\"`", |
| 32 |
}, { |
| 33 |
about: "error value: multi-line", |
| 34 |
value: &errTest{ |
| 35 |
msg: `err: |
| 36 |
"these are the voyages"`, |
| 37 |
}, |
| 38 |
want: `e"err:\n\"these are the voyages\""`, |
| 39 |
}, { |
| 40 |
about: "error value: with backquotes", |
| 41 |
value: &errTest{ |
| 42 |
msg: "cannot `open` \"file\"", |
| 43 |
}, |
| 44 |
want: `e"cannot ` + "`open`" + ` \"file\""`, |
| 45 |
}, { |
| 46 |
about: "error value: not guarding against nil", |
| 47 |
value: (*errTest)(nil), |
| 48 |
want: `e<nil>`, |
| 49 |
}, { |
| 50 |
about: "stringer", |
| 51 |
value: bytes.NewBufferString("I am a stringer"), |
| 52 |
want: `s"I am a stringer"`, |
| 53 |
}, { |
| 54 |
about: "stringer: with quotes", |
| 55 |
value: bytes.NewBufferString(`I say "hello"`), |
| 56 |
want: "s`I say \"hello\"`", |
| 57 |
}, { |
| 58 |
about: "stringer: not guarding against nil", |
| 59 |
value: (*nilStringer)(nil), |
| 60 |
want: "s<nil>", |
| 61 |
}, { |
| 62 |
about: "string", |
| 63 |
value: "these are the voyages", |
| 64 |
want: `"these are the voyages"`, |
| 65 |
}, { |
| 66 |
about: "string: with quotes", |
| 67 |
value: `here is a quote: "`, |
| 68 |
want: "`here is a quote: \"`", |
| 69 |
}, { |
| 70 |
about: "string: multi-line", |
| 71 |
value: `foo |
| 72 |
"bar" |
| 73 |
`, |
| 74 |
want: `"foo\n\"bar\"\n"`, |
| 75 |
}, { |
| 76 |
about: "string: with backquotes", |
| 77 |
value: `"` + "`", |
| 78 |
want: `"\"` + "`\"", |
| 79 |
}, { |
| 80 |
about: "slice", |
| 81 |
value: []int{1, 2, 3}, |
| 82 |
want: "[]int{1, 2, 3}", |
| 83 |
}, { |
| 84 |
about: "bytes", |
| 85 |
value: []byte("hello"), |
| 86 |
want: `[]uint8("hello")`, |
| 87 |
}, { |
| 88 |
about: "custom bytes type", |
| 89 |
value: myBytes("hello"), |
| 90 |
want: `quicktest_test.myBytes("hello")`, |
| 91 |
}, { |
| 92 |
about: "bytes with backquote", |
| 93 |
value: []byte(`a "b" c`), |
| 94 |
want: "[]uint8(`a \"b\" c`)", |
| 95 |
}, { |
| 96 |
about: "bytes with invalid utf-8", |
| 97 |
value: []byte("\xff"), |
| 98 |
want: "[]uint8{0xff}", |
| 99 |
}, { |
| 100 |
about: "nil byte slice", |
| 101 |
value: []byte(nil), |
| 102 |
want: "[]uint8(nil)", |
| 103 |
}, { |
| 104 |
about: "time", |
| 105 |
value: goTime, |
| 106 |
want: `s"2012-03-28 00:00:00 +0000 UTC"`, |
| 107 |
}, { |
| 108 |
about: "struct with byte slice", |
| 109 |
value: struct{ X []byte }{[]byte("x")}, |
| 110 |
want: "struct { X []uint8 }{\n X: {0x78},\n}", |
| 111 |
}, { |
| 112 |
about: "uint64", |
| 113 |
value: uint64(17), |
| 114 |
want: "uint64(17)", |
| 115 |
}, { |
| 116 |
about: "uint32", |
| 117 |
value: uint32(17898), |
| 118 |
want: "uint32(17898)", |
| 119 |
}, { |
| 120 |
about: "uintptr", |
| 121 |
value: uintptr(13), |
| 122 |
want: "uintptr(13)", |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
func TestFormat(t *testing.T) { |
| 127 |
for _, test := range formatTests { |
| 128 |
t.Run(test.about, func(t *testing.T) { |
| 129 |
got := qt.Format(test.value) |
| 130 |
if got != test.want { |
| 131 |
t.Fatalf("format:\ngot %q\nwant %q", got, test.want) |
| 132 |
} |
| 133 |
}) |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
type myBytes []byte |
| 138 |
|
| 139 |
// nilStringer is a stringer not guarding against nil. |
| 140 |
type nilStringer struct { |
| 141 |
msg string |
| 142 |
} |
| 143 |
|
| 144 |
func (s *nilStringer) String() string { |
| 145 |
return s.msg |
| 146 |
} |