github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/errors/errors_test.go (about)

     1  package errors
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestWrap(t *testing.T) {
    11  	err := errors.New("0")
    12  	err1 := Wrap(err, "1")
    13  	err2 := Wrap(err1, "2")
    14  	err3 := Wrap(err2)
    15  
    16  	if got := Root(err1); got != err {
    17  		t.Fatalf("Root(%v)=%v want %v", err1, got, err)
    18  	}
    19  
    20  	if got := Root(err2); got != err {
    21  		t.Fatalf("Root(%v)=%v want %v", err2, got, err)
    22  	}
    23  
    24  	if err2.Error() != "2: 1: 0" {
    25  		t.Fatalf("err msg = %s want '2: 1: 0'", err2.Error())
    26  	}
    27  
    28  	if err3.Error() != "2: 1: 0" {
    29  		t.Fatalf("err msg = %s want '2: 1: 0'", err3.Error())
    30  	}
    31  
    32  	stack := Stack(err1)
    33  	if len(stack) == 0 {
    34  		t.Fatalf("len(stack) = %v want > 0", len(stack))
    35  	}
    36  	if !strings.Contains(stack[0].String(), "TestWrap") {
    37  		t.Fatalf("first stack frame should contain \"TestWrap\": %v", stack[0].String())
    38  	}
    39  
    40  	if !reflect.DeepEqual(Stack(err2), Stack(err1)) {
    41  		t.Errorf("err2 stack got %v want %v", Stack(err2), Stack(err1))
    42  	}
    43  
    44  	if !reflect.DeepEqual(Stack(err3), Stack(err1)) {
    45  		t.Errorf("err3 stack got %v want %v", Stack(err3), Stack(err1))
    46  	}
    47  }
    48  
    49  func TestWrapNil(t *testing.T) {
    50  	var err error
    51  
    52  	err1 := Wrap(err, "1")
    53  	if err1 != nil {
    54  		t.Fatal("wrapping nil error should yield nil")
    55  	}
    56  }
    57  
    58  func TestWrapf(t *testing.T) {
    59  	err := errors.New("0")
    60  	err1 := Wrapf(err, "there are %d errors being wrapped", 1)
    61  	if err1.Error() != "there are 1 errors being wrapped: 0" {
    62  		t.Fatalf("err msg = %s want 'there are 1 errors being wrapped: 0'", err1.Error())
    63  	}
    64  }
    65  
    66  func TestWrapMsg(t *testing.T) {
    67  	err := errors.New("rooti")
    68  	err1 := Wrap(err, "cherry", " ", "guava")
    69  	if err1.Error() != "cherry guava: rooti" {
    70  		t.Fatalf("err msg = %s want 'cherry guava: rooti'", err1.Error())
    71  	}
    72  }
    73  
    74  func TestDetail(t *testing.T) {
    75  	root := errors.New("foo")
    76  	cases := []struct {
    77  		err     error
    78  		detail  string
    79  		message string
    80  	}{
    81  		{root, "foo", "foo"},
    82  		{WithDetail(root, "bar"), "bar", "bar: foo"},
    83  		{WithDetail(WithDetail(root, "bar"), "baz"), "bar; baz", "baz: bar: foo"},
    84  		{Wrap(WithDetail(root, "bar"), "baz"), "bar", "baz: bar: foo"},
    85  	}
    86  
    87  	for _, test := range cases {
    88  		if got := Detail(test.err); got != test.detail {
    89  			t.Errorf("Detail(%v) = %v want %v", test.err, got, test.detail)
    90  		}
    91  		if got := Root(test.err); got != root {
    92  			t.Errorf("Root(%v) = %v want %v", test.err, got, root)
    93  		}
    94  		if got := test.err.Error(); got != test.message {
    95  			t.Errorf("(%v).Error() = %v want %v", test.err, got, test.message)
    96  		}
    97  	}
    98  }
    99  
   100  func TestData(t *testing.T) {
   101  	root := errors.New("foo")
   102  	cases := []struct {
   103  		err  error
   104  		data interface{}
   105  	}{
   106  		{WithData(root, "a", "b"), map[string]interface{}{"a": "b"}},
   107  		{WithData(WithData(root, "a", "b"), "c", "d"), map[string]interface{}{"a": "b", "c": "d"}},
   108  		{Wrap(WithData(root, "a", "b"), "baz"), map[string]interface{}{"a": "b"}},
   109  	}
   110  
   111  	for _, test := range cases {
   112  		if got := Data(test.err); !reflect.DeepEqual(got, test.data) {
   113  			t.Errorf("Data(%#v) = %v want %v", test.err, got, test.data)
   114  		}
   115  		if got := Root(test.err); got != root {
   116  			t.Errorf("Root(%#v) = %v want %v", test.err, got, root)
   117  		}
   118  	}
   119  }
   120  
   121  func TestSub(t *testing.T) {
   122  	x := errors.New("x")
   123  	y := errors.New("y")
   124  	cases := []struct{ new, old, want error }{
   125  		{nil, nil, nil},
   126  		{x, nil, nil},
   127  		{nil, Wrap(y), nil},
   128  		{Wrap(x), nil, nil},
   129  		{nil, y, nil},
   130  		{x, y, errors.New("y: x")},
   131  		{Wrap(x), y, errors.New("y: x")},
   132  		{x, Wrap(y), errors.New("y: x")},
   133  		{Wrap(x, "z"), Wrap(y), errors.New("y: z: x")},
   134  	}
   135  
   136  	for _, test := range cases {
   137  		got := Sub(test.new, test.old)
   138  		if !(got == nil && test.want == nil || got.Error() == test.want.Error()) {
   139  			t.Errorf("Sub(%#v, %#v) = %v, want %v", test.new, test.old, got, test.want)
   140  		}
   141  	}
   142  }