AdGuardHome/internal/agherr/agherr_test.go
Ainar Garipov 73c30590e0 Pull request: all: fix lint and naming issues vol. 4
Merge in DNS/adguard-home from 2276-fix-lint-4 to master

Updates #2276.

Squashed commit of the following:

commit 15d49184cd8ce1f8701bf3221e69418ca1778b36
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 8 15:51:34 2020 +0300

    util: fix naming

commit 3b9a86a0feb8c6e0b167e6e23105e8137b0dda76
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Tue Dec 8 15:41:10 2020 +0300

    all: fix lint and naming issues vol. 4
2020-12-08 16:01:13 +03:00

80 lines
1.6 KiB
Go

package agherr
import (
"errors"
"fmt"
"testing"
"github.com/AdguardTeam/AdGuardHome/internal/testutil"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
testutil.DiscardLogOutput(m)
}
func TestError_Error(t *testing.T) {
testCases := []struct {
name string
want string
err error
}{{
name: "simple",
want: "a",
err: Many("a"),
}, {
name: "wrapping",
want: "a: b",
err: Many("a", errors.New("b")),
}, {
name: "wrapping several",
want: "a: b (hidden: c, d)",
err: Many("a", errors.New("b"), errors.New("c"), errors.New("d")),
}, {
name: "wrapping wrapper",
want: "a: b: c (hidden: d)",
err: Many("a", Many("b", errors.New("c"), errors.New("d"))),
}}
for _, tc := range testCases {
assert.Equal(t, tc.want, tc.err.Error(), tc.name)
}
}
func TestError_Unwrap(t *testing.T) {
var _ wrapper = &manyError{}
const (
errSimple = iota
errWrapped
errNil
)
errs := []error{
errSimple: errors.New("a"),
errWrapped: fmt.Errorf("err: %w", errors.New("nested")),
errNil: nil,
}
testCases := []struct {
name string
want error
wrapped error
}{{
name: "simple",
want: errs[errSimple],
wrapped: Many("a", errs[errSimple]),
}, {
name: "nested",
want: errs[errWrapped],
wrapped: Many("b", errs[errWrapped]),
}, {
name: "nil passed",
want: errs[errNil],
wrapped: Many("c", errs[errNil]),
}, {
name: "nil not passed",
want: nil,
wrapped: Many("d"),
}}
for _, tc := range testCases {
assert.Equal(t, tc.want, errors.Unwrap(tc.wrapped), tc.name)
}
}