File: multierr_test.go

package info (click to toggle)
golang-github-containers-image 5.36.1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 5,152 kB
  • sloc: sh: 267; makefile: 100
file content (39 lines) | stat: -rw-r--r-- 936 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
package multierr

import (
	"errors"
	"testing"

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

type errA struct{}

func (errA) Error() string { return "A" }

func TestFormat(t *testing.T) {
	errB := errors.New("B")

	// Single-item Format preserves the error type
	res := Format("[", ",", "]", []error{errA{}})
	assert.Equal(t, "A", res.Error())
	var aTarget errA
	assert.ErrorAs(t, res, &aTarget)

	// Single-item Format preserves the error identity
	res = Format("[", ",", "]", []error{errB})
	assert.Equal(t, "B", res.Error())
	assert.ErrorIs(t, res, errB)

	// Multi-item Format preserves both
	res = Format("[", ",", "]", []error{errA{}, errB})
	assert.Equal(t, "[A,B]", res.Error())
	assert.ErrorAs(t, res, &aTarget)
	assert.ErrorIs(t, res, errB)

	// This is invalid, but make sure we don’t misleadingly suceeed
	res = Format("[", ",", "]", []error{})
	assert.Error(t, res)
	res = Format("[", ",", "]", nil)
	assert.Error(t, res)
}