"Fossies" - the Fresh Open Source Software Archive

Member "AdGuardHome-0.104.3/internal/agherr/agherr_test.go" (19 Nov 2020, 1561 Bytes) of package /linux/misc/dns/AdGuardHome-0.104.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "agherr_test.go": 0.104.1_vs_0.104.3.

    1 package agherr
    2 
    3 import (
    4     "errors"
    5     "fmt"
    6     "testing"
    7 
    8     "github.com/AdguardTeam/AdGuardHome/internal/testutil"
    9     "github.com/stretchr/testify/assert"
   10 )
   11 
   12 func TestMain(m *testing.M) {
   13     testutil.DiscardLogOutput(m)
   14 }
   15 
   16 func TestError_Error(t *testing.T) {
   17     testCases := []struct {
   18         name string
   19         want string
   20         err  error
   21     }{{
   22         name: "simple",
   23         want: "a",
   24         err:  Many("a"),
   25     }, {
   26         name: "wrapping",
   27         want: "a: b",
   28         err:  Many("a", errors.New("b")),
   29     }, {
   30         name: "wrapping several",
   31         want: "a: b (hidden: c, d)",
   32         err:  Many("a", errors.New("b"), errors.New("c"), errors.New("d")),
   33     }, {
   34         name: "wrapping wrapper",
   35         want: "a: b: c (hidden: d)",
   36         err:  Many("a", Many("b", errors.New("c"), errors.New("d"))),
   37     }}
   38     for _, tc := range testCases {
   39         assert.Equal(t, tc.want, tc.err.Error(), tc.name)
   40     }
   41 }
   42 
   43 func TestError_Unwrap(t *testing.T) {
   44     const (
   45         errSimple = iota
   46         errWrapped
   47         errNil
   48     )
   49     errs := []error{
   50         errSimple:  errors.New("a"),
   51         errWrapped: fmt.Errorf("err: %w", errors.New("nested")),
   52         errNil:     nil,
   53     }
   54     testCases := []struct {
   55         name    string
   56         want    error
   57         wrapped error
   58     }{{
   59         name:    "simple",
   60         want:    errs[errSimple],
   61         wrapped: Many("a", errs[errSimple]),
   62     }, {
   63         name:    "nested",
   64         want:    errs[errWrapped],
   65         wrapped: Many("b", errs[errWrapped]),
   66     }, {
   67         name:    "nil passed",
   68         want:    errs[errNil],
   69         wrapped: Many("c", errs[errNil]),
   70     }, {
   71         name:    "nil not passed",
   72         want:    nil,
   73         wrapped: Many("d"),
   74     }}
   75     for _, tc := range testCases {
   76         assert.Equal(t, tc.want, errors.Unwrap(tc.wrapped), tc.name)
   77     }
   78 }