gopkg.in/hedzr/errors.v3@v3.3.1/causes_test.go (about)

     1  package errors
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"strconv"
     7  	"testing"
     8  )
     9  
    10  func TestAs_e1(t *testing.T) {
    11  	// As() on our error wrappers
    12  	err := New("xx")
    13  	var e1 *causes2
    14  	if As(err, &e1) {
    15  		t.Logf("e1: %v", e1)
    16  	} else {
    17  		t.Fail()
    18  	}
    19  }
    20  
    21  func TestAs_betterFormat(t *testing.T) {
    22  	err := New("Have errors").WithErrors(io.EOF, io.ErrShortWrite, io.ErrNoProgress)
    23  	t.Logf("%v\n", err)
    24  
    25  	nestNestErr := New("Errors FOUND:").WithErrors(err, io.EOF)
    26  	nnnErr := New("Nested Errors:").WithErrors(nestNestErr, strconv.ErrRange)
    27  	t.Logf("%v\n", nnnErr)
    28  	t.Logf("%+v\n", nnnErr)
    29  }
    30  
    31  func TestCauses2_WithCode(t *testing.T) {
    32  	err := &causes2{
    33  		Code:    Internal,
    34  		Causers: nil,
    35  		msg:     "ui err",
    36  	}
    37  	err.WithCode(NotFound).End()
    38  	t.Logf("failed: %+v", err)
    39  	t.Logf("failed: %+v", err.Cause())
    40  	t.Logf("failed: %+v", err.Causes())
    41  	t.Logf("failed: %+v", err.TypeIs(io.EOF))
    42  	t.Logf("failed: %+v", err.IsEmpty())
    43  }
    44  
    45  func TestCauses2_New(t *testing.T) {
    46  	err := &causes2{
    47  		Code:    Internal,
    48  		Causers: nil,
    49  		msg:     "ui err",
    50  	}
    51  
    52  	t.Logf("failed: %+v", err)
    53  
    54  	err = &causes2{
    55  		Code:    0,
    56  		Causers: nil,
    57  		msg:     "ui err",
    58  	}
    59  	_ = err.WithErrors(io.EOF, io.ErrClosedPipe)
    60  	t.Logf("failed: %+v", err)
    61  
    62  	err = &causes2{
    63  		Code:    Internal,
    64  		Causers: nil,
    65  		msg:     "ui err",
    66  	}
    67  	err.WithErrors(io.EOF, io.ErrClosedPipe).End()
    68  	t.Logf("failed: %+v", err)
    69  }
    70  
    71  func TestContainer(t *testing.T) {
    72  	// as a inner errors container
    73  	child := func() (err error) {
    74  		errContainer := &causes2{}
    75  
    76  		defer errContainer.Defer(&err)
    77  		for _, r := range []error{io.EOF, io.ErrShortWrite, io.ErrClosedPipe, Internal} {
    78  			errContainer.Attach(r)
    79  		}
    80  
    81  		return
    82  	}
    83  
    84  	err := child()
    85  	t.Logf("failed: %+v", err)
    86  
    87  	fmt.Printf("%+v\n", err)
    88  	fmt.Printf("%#v\n", err)
    89  	fmt.Printf("%v\n", err)
    90  	fmt.Printf("%s\n", err)
    91  	fmt.Printf("%q\n", err)
    92  
    93  	// fmt.Printf("%n", err) // need go1.13+
    94  }
    95  
    96  func TestAsAContainer(t *testing.T) {
    97  	// as a inner errors container
    98  	child := func() (err error) {
    99  		errContainer := New("")
   100  
   101  		defer errContainer.Defer(&err)
   102  		for _, r := range []error{io.EOF, io.ErrShortWrite, io.ErrClosedPipe, Internal} {
   103  			errContainer.Attach(r)
   104  		}
   105  
   106  		return
   107  	}
   108  
   109  	err := child()
   110  	t.Logf("failed: %+v", err)
   111  }
   112  
   113  func TestIsDescended(t *testing.T) {
   114  	err1 := &causes2{
   115  		Code:    Internal,
   116  		Causers: nil,
   117  		msg:     "ui err %v",
   118  	}
   119  
   120  	err2 := err1.FormatWith("1st")
   121  	if !IsDescended(err1, err2) {
   122  		t.Fatalf("bad test on IsDescended(err1, err2)")
   123  	}
   124  
   125  	err3 := New("any error tmpl with %v")
   126  	err4 := err3.FormatWith("huahua")
   127  	if !IsDescended(err3, err4) {
   128  		t.Fatalf("bad test on IsDescended(err3, err4)")
   129  	}
   130  }