Debian Codegraph @

td/must_118_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.18
8 // +build go1.18
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 TestMust(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 test.EqualInt(t, td.Must(fn(true)), 42)
29
30 test.CheckPanic(t, func() { td.Must(fn(false)) }, "error")
31 }
32
33 func TestMust2(t *testing.T) {
34 fn := func(ok bool) (int, string, error) {
35 if ok {
36 return 42, "pipo", nil
37 }
38 return 0, "", errors.New("error")
39 }
40
41 val1, val2 := td.Must2(fn(true))
42 test.EqualInt(t, val1, 42)
43 test.EqualStr(t, val2, "pipo")
44
45 test.CheckPanic(t, func() { td.Must2(fn(false)) }, "error")
46 }
47
48 func TestMust3(t *testing.T) {
49 fn := func(ok bool) (int, string, bool, error) {
50 if ok {
51 return 42, "pipo", true, nil
52 }
53 return 0, "", false, errors.New("error")
54 }
55
56 val1, val2, val3 := td.Must3(fn(true))
57 test.EqualInt(t, val1, 42)
58 test.EqualStr(t, val2, "pipo")
59 test.EqualBool(t, val3, true)
60
61 test.CheckPanic(t, func() { td.Must3(fn(false)) }, "error")
62 }