Debian Codegraph @

td/tuple_test.go

raw
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 "errors"
11 "testing"
12
13 "github.com/maxatome/go-testdeep/internal/test"
14 "github.com/maxatome/go-testdeep/td"
15 )
16
17 func TestTuple(t *testing.T) {
18 multi := func() (a int, b string, err error) {
19 return 12, "test", errors.New("err")
20 }
21
22 tuple := td.TupleFrom(multi())
23 test.EqualInt(t, tuple.Len(), 3)
24 test.EqualInt(t, tuple.Index(0).(int), 12)
25 test.EqualStr(t, tuple.Index(1).(string), "test")
26 test.EqualStr(t, tuple.Index(2).(error).Error(), "err")
27
28 td.Cmp(t,
29 td.TupleFrom(multi()),
30 td.TupleFrom(12, "test", td.Not(nil)),
31 )
32
33 price := func(p float64) (float64, string, error) {
34 if p < 0 {
35 return 0, "", errors.New("negative price not supported")
36 }
37 return p * 1.2, "€", nil
38 }
39
40 td.Cmp(t,
41 td.TupleFrom(price(10)),
42 td.TupleFrom(float64(12), "€", nil),
43 )
44 td.Cmp(t,
45 td.TupleFrom(price(-10)),
46 td.TupleFrom(float64(0), "", td.Not(nil)),
47 )
48
49 // With Flatten
50 td.Cmp(t,
51 td.TupleFrom(td.Flatten([]int64{1, 2, 3}), "OK", nil),
52 td.TupleFrom(int64(1), int64(2), int64(3), "OK", nil),
53 )
54 }