Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gob_test.go « git2go « internal - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e18a10961c1f943654537ffcfbe84285902694a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package git2go

import (
	"bytes"
	"encoding/gob"
	"errors"
	"fmt"
	"testing"

	"github.com/stretchr/testify/require"
)

func TestSerializableError(t *testing.T) {
	for _, tc := range []struct {
		desc          string
		input         error
		output        error
		containsTyped bool
	}{
		{
			desc:   "plain error",
			input:  errors.New("plain error"),
			output: wrapError{Message: "plain error"},
		},
		{
			desc:   "wrapped plain error",
			input:  fmt.Errorf("error wrapper: %w", errors.New("plain error")),
			output: wrapError{Message: "error wrapper: plain error", Err: wrapError{Message: "plain error"}},
		},
		{
			desc:          "wrapped typed error",
			containsTyped: true,
			input:         fmt.Errorf("error wrapper: %w", InvalidArgumentError("typed error")),
			output:        wrapError{Message: "error wrapper: typed error", Err: InvalidArgumentError("typed error")},
		},
		{
			desc:          "typed wrapper",
			containsTyped: true,
			input: wrapError{
				Message: "error wrapper: typed error 1: typed error 2",
				Err: wrapError{
					Message: "typed error 1: typed error 2",
					Err:     InvalidArgumentError("typed error 2"),
				},
			},
			output: wrapError{
				Message: "error wrapper: typed error 1: typed error 2",
				Err: wrapError{
					Message: "typed error 1: typed error 2",
					Err:     InvalidArgumentError("typed error 2"),
				},
			},
		},
	} {
		t.Run(tc.desc, func(t *testing.T) {
			encoded := &bytes.Buffer{}
			require.NoError(t, gob.NewEncoder(encoded).Encode(SerializableError(tc.input)))
			var err wrapError
			require.NoError(t, gob.NewDecoder(encoded).Decode(&err))
			require.Equal(t, tc.output, err)

			var typedErr InvalidArgumentError
			require.Equal(t, tc.containsTyped, errors.As(err, &typedErr))
		})
	}
}