| 1 |
// Copyright (c) 2020, 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 TestDelay(t *testing.T) { |
| 17 |
called := 0 |
| 18 |
op := td.Delay(func() td.TestDeep { |
| 19 |
called++ |
| 20 |
return td.Lt(13) |
| 21 |
}) |
| 22 |
test.EqualInt(t, called, 0) |
| 23 |
checkOK(t, 12, op) |
| 24 |
test.EqualInt(t, called, 1) |
| 25 |
checkOK(t, 12, op) |
| 26 |
test.EqualInt(t, called, 1) |
| 27 |
|
| 28 |
delayNil := td.Delay(td.Nil) |
| 29 |
checkOK(t, nil, delayNil) |
| 30 |
|
| 31 |
test.EqualStr(t, delayNil.String(), "nil") |
| 32 |
|
| 33 |
checkError(t, 8, |
| 34 |
td.Delay( |
| 35 |
func() td.TestDeep { |
| 36 |
return td.Gt(13) |
| 37 |
}, |
| 38 |
), |
| 39 |
expectedError{ |
| 40 |
Message: mustBe("values differ"), |
| 41 |
Path: mustBe("DATA"), |
| 42 |
Got: mustBe("8"), |
| 43 |
Expected: mustBe("> 13"), |
| 44 |
}) |
| 45 |
|
| 46 |
// Bad usage |
| 47 |
checkError(t, "never tested", |
| 48 |
td.Delay(nil), |
| 49 |
expectedError{ |
| 50 |
Message: mustBe("bad usage of Delay operator"), |
| 51 |
Path: mustBe("DATA"), |
| 52 |
Summary: mustBe("Delay(DELAYED): DELAYED must be non-nil"), |
| 53 |
}) |
| 54 |
|
| 55 |
// Erroneous op |
| 56 |
test.EqualStr(t, td.Delay(nil).String(), "Delay(<ERROR>)") |
| 57 |
} |
| 58 |
|
| 59 |
func TestDelayTypeBehind(t *testing.T) { |
| 60 |
equalTypes(t, td.Delay(func() td.TestDeep { return td.String("x") }), nil) |
| 61 |
equalTypes(t, td.Delay(func() td.TestDeep { return td.Gt(16) }), 42) |
| 62 |
|
| 63 |
// Erroneous op |
| 64 |
equalTypes(t, td.Delay(nil), nil) |
| 65 |
} |