| 1 |
// Copyright (c) 2019, 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 |
"testing" |
| 11 |
|
| 12 |
"github.com/maxatome/go-testdeep/internal/test" |
| 13 |
"github.com/maxatome/go-testdeep/internal/util" |
| 14 |
"github.com/maxatome/go-testdeep/td" |
| 15 |
) |
| 16 |
|
| 17 |
func TestTag(t *testing.T) { |
| 18 |
// expected value |
| 19 |
checkOK(t, 12, td.Tag("number", 12)) |
| 20 |
checkOK(t, nil, td.Tag("number", nil)) |
| 21 |
|
| 22 |
checkError(t, 8, td.Tag("number", 9), |
| 23 |
expectedError{ |
| 24 |
Message: mustBe("values differ"), |
| 25 |
Path: mustBe("DATA"), |
| 26 |
Got: mustBe("8"), |
| 27 |
Expected: mustBe("9"), |
| 28 |
}) |
| 29 |
|
| 30 |
// expected operator |
| 31 |
checkOK(t, 12, td.Tag("number", td.Between(9, 13))) |
| 32 |
|
| 33 |
checkError(t, 8, td.Tag("number", td.Between(9, 13)), |
| 34 |
expectedError{ |
| 35 |
Message: mustBe("values differ"), |
| 36 |
Path: mustBe("DATA"), |
| 37 |
Got: mustBe("8"), |
| 38 |
Expected: mustBe("9 ≤ got ≤ 13"), |
| 39 |
}) |
| 40 |
|
| 41 |
// |
| 42 |
// Bad usage |
| 43 |
checkError(t, "never tested", |
| 44 |
td.Tag("1badTag", td.Between(9, 13)), |
| 45 |
expectedError{ |
| 46 |
Message: mustBe("bad usage of Tag operator"), |
| 47 |
Path: mustBe("DATA"), |
| 48 |
Summary: mustBe(util.ErrTagInvalid.Error()), |
| 49 |
}) |
| 50 |
|
| 51 |
// |
| 52 |
// String |
| 53 |
test.EqualStr(t, |
| 54 |
td.Tag("foo", td.Gt(4)).String(), |
| 55 |
td.Gt(4).String()) |
| 56 |
test.EqualStr(t, td.Tag("foo", 8).String(), "8") |
| 57 |
test.EqualStr(t, td.Tag("foo", nil).String(), "nil") |
| 58 |
|
| 59 |
// Erroneous op |
| 60 |
test.EqualStr(t, td.Tag("1badTag", 12).String(), "Tag(<ERROR>)") |
| 61 |
} |
| 62 |
|
| 63 |
func TestTagTypeBehind(t *testing.T) { |
| 64 |
equalTypes(t, td.Tag("foo", 8), 0) |
| 65 |
equalTypes(t, td.Tag("foo", td.Gt(4)), 0) |
| 66 |
equalTypes(t, td.Tag("foo", nil), nil) |
| 67 |
|
| 68 |
// Erroneous op |
| 69 |
equalTypes(t, td.Tag("1badTag", 12), nil) |
| 70 |
} |