File: serialization_test.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (66 lines) | stat: -rw-r--r-- 1,774 bytes parent folder | download | duplicates (2)
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))
		})
	}
}