Debian Codegraph @

errcapture/do_test.go

raw symbols SCIP
1 // Copyright (c) The EfficientGo Authors.
2 // Licensed under the Apache License 2.0.
3
4 // Initially copied from Thanos
5 //
6 // Copyright (c) The Thanos Authors.
7 // Licensed under the Apache License 2.0.
8
9 package errcapture
10
11 import (
12 "io"
13 "testing"
14
15 "github.com/efficientgo/core/errors"
16 )
17
18 type testCloser struct {
19 err error
20 }
21
22 func (c testCloser) Close() error {
23 return c.err
24 }
25
26 func TestDo(t *testing.T) {
27 for _, tcase := range []struct {
28 err error
29 closer io.Closer
30
31 expectedErrStr string
32 }{
33 {
34 err: nil,
35 closer: testCloser{err: nil},
36 expectedErrStr: "",
37 },
38 {
39 err: errors.New("test"),
40 closer: testCloser{err: nil},
41 expectedErrStr: "test",
42 },
43 {
44 err: nil,
45 closer: testCloser{err: errors.New("test")},
46 expectedErrStr: "close: test",
47 },
48 {
49 err: errors.New("test"),
50 closer: testCloser{err: errors.New("test")},
51 expectedErrStr: "2 errors: test; close: test",
52 },
53 } {
54 if ok := t.Run("", func(t *testing.T) {
55 ret := tcase.err
56 Do(&ret, tcase.closer.Close, "close")
57
58 if tcase.expectedErrStr == "" {
59 if ret != nil {
60 t.Error("Expected error to be nil")
61 t.Fail()
62 }
63 } else {
64 if ret == nil {
65 t.Error("Expected error to be not nil")
66 t.Fail()
67 }
68
69 if tcase.expectedErrStr != ret.Error() {
70 t.Errorf("%s != %s", tcase.expectedErrStr, ret.Error())
71 t.Fail()
72 }
73 }
74
75 }); !ok {
76 return
77 }
78 }
79 }