| 1 |
// Copyright (c) 2018, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/maxatome/go-testdeep/td" |
| 14 |
) |
| 15 |
|
| 16 |
func TestString(t *testing.T) { |
| 17 |
checkOK(t, "foobar", td.String("foobar")) |
| 18 |
checkOK(t, []byte("foobar"), td.String("foobar")) |
| 19 |
|
| 20 |
type MyBytes []byte |
| 21 |
checkOK(t, MyBytes("foobar"), td.String("foobar")) |
| 22 |
|
| 23 |
type MyString string |
| 24 |
checkOK(t, MyString("foobar"), td.String("foobar")) |
| 25 |
|
| 26 |
// error interface |
| 27 |
checkOK(t, errors.New("pipo bingo"), td.String("pipo bingo")) |
| 28 |
// fmt.Stringer interface |
| 29 |
checkOK(t, MyStringer{}, td.String("pipo bingo")) |
| 30 |
|
| 31 |
checkError(t, "foo bar test", td.String("pipo"), |
| 32 |
expectedError{ |
| 33 |
Message: mustBe("does not match"), |
| 34 |
Path: mustBe("DATA"), |
| 35 |
Got: mustContain(`"foo bar test"`), |
| 36 |
Expected: mustContain(`"pipo"`), |
| 37 |
}) |
| 38 |
|
| 39 |
checkError(t, []int{1, 2}, td.String("bar"), |
| 40 |
expectedError{ |
| 41 |
Message: mustBe("bad type"), |
| 42 |
Path: mustBe("DATA"), |
| 43 |
Got: mustBe("[]int"), |
| 44 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 45 |
}) |
| 46 |
|
| 47 |
checkError(t, 12, td.String("bar"), |
| 48 |
expectedError{ |
| 49 |
Message: mustBe("bad type"), |
| 50 |
Path: mustBe("DATA"), |
| 51 |
Got: mustBe("int"), |
| 52 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 53 |
}) |
| 54 |
} |
| 55 |
|
| 56 |
func TestHasPrefix(t *testing.T) { |
| 57 |
checkOK(t, "foobar", td.HasPrefix("foo")) |
| 58 |
checkOK(t, []byte("foobar"), td.HasPrefix("foo")) |
| 59 |
|
| 60 |
type MyBytes []byte |
| 61 |
checkOK(t, MyBytes("foobar"), td.HasPrefix("foo")) |
| 62 |
|
| 63 |
type MyString string |
| 64 |
checkOK(t, MyString("foobar"), td.HasPrefix("foo")) |
| 65 |
|
| 66 |
// error interface |
| 67 |
checkOK(t, errors.New("pipo bingo"), td.HasPrefix("pipo")) |
| 68 |
// fmt.Stringer interface |
| 69 |
checkOK(t, MyStringer{}, td.HasPrefix("pipo")) |
| 70 |
|
| 71 |
checkError(t, "foo bar test", td.HasPrefix("pipo"), |
| 72 |
expectedError{ |
| 73 |
Message: mustBe("has not prefix"), |
| 74 |
Path: mustBe("DATA"), |
| 75 |
Got: mustContain(`"foo bar test"`), |
| 76 |
Expected: mustMatch(`^HasPrefix\(.*"pipo"`), |
| 77 |
}) |
| 78 |
|
| 79 |
checkError(t, []int{1, 2}, td.HasPrefix("bar"), |
| 80 |
expectedError{ |
| 81 |
Message: mustBe("bad type"), |
| 82 |
Path: mustBe("DATA"), |
| 83 |
Got: mustBe("[]int"), |
| 84 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 85 |
}) |
| 86 |
|
| 87 |
checkError(t, 12, td.HasPrefix("bar"), |
| 88 |
expectedError{ |
| 89 |
Message: mustBe("bad type"), |
| 90 |
Path: mustBe("DATA"), |
| 91 |
Got: mustBe("int"), |
| 92 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 93 |
}) |
| 94 |
} |
| 95 |
|
| 96 |
func TestHasSuffix(t *testing.T) { |
| 97 |
checkOK(t, "foobar", td.HasSuffix("bar")) |
| 98 |
checkOK(t, []byte("foobar"), td.HasSuffix("bar")) |
| 99 |
|
| 100 |
type MyBytes []byte |
| 101 |
checkOK(t, MyBytes("foobar"), td.HasSuffix("bar")) |
| 102 |
|
| 103 |
type MyString string |
| 104 |
checkOK(t, MyString("foobar"), td.HasSuffix("bar")) |
| 105 |
|
| 106 |
// error interface |
| 107 |
checkOK(t, errors.New("pipo bingo"), td.HasSuffix("bingo")) |
| 108 |
// fmt.Stringer interface |
| 109 |
checkOK(t, MyStringer{}, td.HasSuffix("bingo")) |
| 110 |
|
| 111 |
checkError(t, "foo bar test", td.HasSuffix("pipo"), |
| 112 |
expectedError{ |
| 113 |
Message: mustBe("has not suffix"), |
| 114 |
Path: mustBe("DATA"), |
| 115 |
Got: mustContain(`"foo bar test"`), |
| 116 |
Expected: mustMatch(`^HasSuffix\(.*"pipo"`), |
| 117 |
}) |
| 118 |
|
| 119 |
checkError(t, []int{1, 2}, td.HasSuffix("bar"), |
| 120 |
expectedError{ |
| 121 |
Message: mustBe("bad type"), |
| 122 |
Path: mustBe("DATA"), |
| 123 |
Got: mustBe("[]int"), |
| 124 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 125 |
}) |
| 126 |
|
| 127 |
checkError(t, 12, td.HasSuffix("bar"), |
| 128 |
expectedError{ |
| 129 |
Message: mustBe("bad type"), |
| 130 |
Path: mustBe("DATA"), |
| 131 |
Got: mustBe("int"), |
| 132 |
Expected: mustBe("string (convertible) OR []byte (convertible) OR fmt.Stringer OR error"), |
| 133 |
}) |
| 134 |
} |
| 135 |
|
| 136 |
func TestStringTypeBehind(t *testing.T) { |
| 137 |
equalTypes(t, td.String("x"), nil) |
| 138 |
equalTypes(t, td.HasPrefix("x"), nil) |
| 139 |
equalTypes(t, td.HasSuffix("x"), nil) |
| 140 |
} |