| 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/td" |
| 14 |
) |
| 15 |
|
| 16 |
func TestLax(t *testing.T) { |
| 17 |
checkOK(t, int64(1234), td.Lax(1234)) |
| 18 |
|
| 19 |
type MyInt int32 |
| 20 |
checkOK(t, int64(123), td.Lax(MyInt(123))) |
| 21 |
checkOK(t, MyInt(123), td.Lax(int64(123))) |
| 22 |
|
| 23 |
type gotStruct struct { |
| 24 |
name string |
| 25 |
age int |
| 26 |
} |
| 27 |
type expectedStruct struct { |
| 28 |
name string |
| 29 |
age int |
| 30 |
} |
| 31 |
checkOK(t, |
| 32 |
gotStruct{ |
| 33 |
name: "bob", |
| 34 |
age: 42, |
| 35 |
}, |
| 36 |
td.Lax(expectedStruct{ |
| 37 |
name: "bob", |
| 38 |
age: 42, |
| 39 |
})) |
| 40 |
checkOK(t, |
| 41 |
&gotStruct{ |
| 42 |
name: "bob", |
| 43 |
age: 42, |
| 44 |
}, |
| 45 |
td.Lax(&expectedStruct{ |
| 46 |
name: "bob", |
| 47 |
age: 42, |
| 48 |
})) |
| 49 |
|
| 50 |
checkError(t, int64(123), td.Between(120, 125), |
| 51 |
expectedError{ |
| 52 |
Message: mustBe("type mismatch"), |
| 53 |
}) |
| 54 |
checkOK(t, int64(123), td.Lax(td.Between(120, 125))) |
| 55 |
|
| 56 |
// nil cases |
| 57 |
checkOK(t, nil, td.Lax(nil)) |
| 58 |
checkOK(t, (*gotStruct)(nil), td.Lax((*expectedStruct)(nil))) |
| 59 |
checkOK(t, (*gotStruct)(nil), td.Lax(nil)) |
| 60 |
checkOK(t, (chan int)(nil), td.Lax(nil)) |
| 61 |
checkOK(t, (func())(nil), td.Lax(nil)) |
| 62 |
checkOK(t, (map[int]int)(nil), td.Lax(nil)) |
| 63 |
checkOK(t, ([]int)(nil), td.Lax(nil)) |
| 64 |
|
| 65 |
// |
| 66 |
// String |
| 67 |
test.EqualStr(t, td.Lax(6).String(), "Lax(6)") |
| 68 |
} |
| 69 |
|
| 70 |
func TestLaxTypeBehind(t *testing.T) { |
| 71 |
equalTypes(t, td.Lax(nil), nil) |
| 72 |
|
| 73 |
type MyBool bool |
| 74 |
equalTypes(t, td.Lax(MyBool(false)), false) |
| 75 |
equalTypes(t, td.Lax(0), int64(0)) |
| 76 |
equalTypes(t, td.Lax(uint8(0)), uint64(0)) |
| 77 |
equalTypes(t, td.Lax(float32(0)), float64(0)) |
| 78 |
equalTypes(t, td.Lax(complex64(complex(1, 1))), complex128(complex(1, 1))) |
| 79 |
type MyString string |
| 80 |
equalTypes(t, td.Lax(MyString("")), "") |
| 81 |
|
| 82 |
type MyBytes []byte |
| 83 |
equalTypes(t, td.Lax([]byte{}), []byte{}) |
| 84 |
equalTypes(t, td.Lax(MyBytes{}), MyBytes{}) |
| 85 |
|
| 86 |
// Another TestDeep operator delegation |
| 87 |
equalTypes(t, td.Lax(td.Struct(MyStruct{}, nil)), MyStruct{}) |
| 88 |
equalTypes(t, td.Lax(td.Any(1, 1.2)), nil) |
| 89 |
} |