Debian Codegraph @

td/t_must_127_test.go

raw
1 // Copyright (c) 2026, 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 //go:build go1.27
8 // +build go1.27
9
10 package td_test
11
12 import (
13 "errors"
14 "testing"
15
16 "github.com/maxatome/go-testdeep/internal/test"
17 "github.com/maxatome/go-testdeep/td"
18 )
19
20 func TestTMust(t *testing.T) {
21 fn := func(ok bool) (int, error) {
22 if ok {
23 return 42, nil
24 }
25 return 0, errors.New("error")
26 }
27
28 ttt := test.NewTestingTB(t.Name())
29 assert := td.NewT(ttt)
30
31 test.EqualInt(t, assert.Must(fn(true)), 42)
32
33 test.MatchStr(t, ttt.CatchFatal(func() { assert.Must(fn(false)) }),
34 `Failed test
35 DATA: should NOT be an error
36 got: \S+\(error\)
37 expected: nil
38 `)
39 }
40
41 func TestTMust2(t *testing.T) {
42 fn := func(ok bool) (int, string, error) {
43 if ok {
44 return 42, "pipo", nil
45 }
46 return 0, "", errors.New("error")
47 }
48
49 ttt := test.NewTestingTB(t.Name())
50 assert := td.NewT(ttt)
51
52 val1, val2 := assert.Must2(fn(true))
53 test.EqualInt(t, val1, 42)
54 test.EqualStr(t, val2, "pipo")
55
56 test.MatchStr(t, ttt.CatchFatal(func() { assert.Must2(fn(false)) }),
57 `Failed test
58 DATA: should NOT be an error
59 got: \S+\(error\)
60 expected: nil
61 `)
62 }
63
64 func TestTMust3(t *testing.T) {
65 fn := func(ok bool) (int, string, bool, error) {
66 if ok {
67 return 42, "pipo", true, nil
68 }
69 return 0, "", false, errors.New("error")
70 }
71
72 ttt := test.NewTestingTB(t.Name())
73 assert := td.NewT(ttt)
74
75 val1, val2, val3 := assert.Must3(fn(true))
76 test.EqualInt(t, val1, 42)
77 test.EqualStr(t, val2, "pipo")
78 test.IsTrue(t, val3)
79
80 test.MatchStr(t, ttt.CatchFatal(func() { assert.Must3(fn(false)) }),
81 `Failed test
82 DATA: should NOT be an error
83 got: \S+\(error\)
84 expected: nil
85 `)
86 }