github.com/haraldrudell/parl@v0.4.176/perrors/errorglue/publics_test.go (about)

     1  /*
     2  © 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
     3  ISC License
     4  */
     5  
     6  package errorglue
     7  
     8  import (
     9  	"errors"
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/haraldrudell/parl/pruntime"
    14  )
    15  
    16  func TestErrorChainSlice(t *testing.T) {
    17  	err := errors.New("x")
    18  	err2 := fmt.Errorf("%w", err)
    19  	errLen := 2
    20  
    21  	var errs []error
    22  
    23  	errs = ErrorChainSlice(nil)
    24  	if len(errs) != 0 {
    25  		t.Errorf("errs len not 0: %d", len(errs))
    26  	}
    27  
    28  	errs = ErrorChainSlice(err2)
    29  	if len(errs) != errLen {
    30  		t.Errorf("errs len not %d: %d", errLen, len(errs))
    31  	}
    32  }
    33  
    34  func TestGetInnerMostStack(t *testing.T) {
    35  	var errNoStack = errors.New("x")
    36  	var errOneStack = NewErrorStack(errNoStack, pruntime.NewStack(0))
    37  	var stack1 = errOneStack.(ErrorCallStacker).StackTrace()
    38  	var errTwoStacks = NewErrorStack(errOneStack, pruntime.NewStack(1))
    39  	var stack2 = errTwoStacks.(ErrorCallStacker).StackTrace()
    40  	if len(stack1.Frames()) == len(stack2.Frames()) {
    41  		t.Error("stacks same")
    42  	}
    43  
    44  	var stack pruntime.Stack
    45  
    46  	// nil error should return nil stack
    47  	stack = GetInnerMostStack(nil)
    48  	if stack != nil {
    49  		t.Errorf("stack not nil")
    50  	}
    51  
    52  	// error without stack should return nil
    53  	stack = GetInnerMostStack(errNoStack)
    54  	if stack != nil {
    55  		t.Errorf("stack not nil")
    56  	}
    57  
    58  	stack = GetInnerMostStack(errOneStack)
    59  	if len(stack.Frames()) == 0 {
    60  		t.Error("stack3 len 0")
    61  	}
    62  	if len(stack.Frames()) != len(stack1.Frames()) {
    63  		t.Errorf("stack3 bad length %d exp %d", len(stack.Frames()), len(stack1.Frames()))
    64  	}
    65  
    66  	stack = GetInnerMostStack(errTwoStacks)
    67  	if len(stack.Frames()) == 0 {
    68  		t.Error("stack4 len 0")
    69  	}
    70  	if len(stack.Frames()) != len(stack1.Frames()) {
    71  		t.Errorf("stack4 bad length %d exp %d", len(stack.Frames()), len(stack1.Frames()))
    72  	}
    73  }
    74  
    75  func TestGetStackTrace(t *testing.T) {
    76  	var errNoStack = errors.New("x")
    77  	var errOneStack = NewErrorStack(errNoStack, pruntime.NewStack(0))
    78  	var stack1 = errOneStack.(ErrorCallStacker).StackTrace()
    79  	var errTwoStacks = NewErrorStack(errOneStack, pruntime.NewStack(1))
    80  	var stack2 = errTwoStacks.(ErrorCallStacker).StackTrace()
    81  	if len(stack1.Frames()) == len(stack2.Frames()) {
    82  		t.Error("stacks same")
    83  	}
    84  
    85  	var stack pruntime.Stack
    86  
    87  	// nil error should return nil
    88  	stack = GetStackTrace(nil)
    89  	if stack != nil {
    90  		t.Errorf("stack not nil")
    91  	}
    92  
    93  	// no-stack error should return nil
    94  	stack = GetStackTrace(errNoStack)
    95  	if stack != nil {
    96  		t.Errorf("stack not nil")
    97  	}
    98  
    99  	stack = GetStackTrace(errOneStack)
   100  	if len(stack.Frames()) == 0 {
   101  		t.Error("stack3 len 0")
   102  	}
   103  	if len(stack.Frames()) != len(stack1.Frames()) {
   104  		t.Errorf("stack3 bad length %d exp %d", len(stack.Frames()), len(stack1.Frames()))
   105  	}
   106  
   107  	stack = GetStackTrace(errTwoStacks)
   108  	if len(stack.Frames()) == 0 {
   109  		t.Error("stack4 len 0")
   110  	}
   111  	if len(stack.Frames()) != len(stack2.Frames()) {
   112  		t.Errorf("stack4 bad length %d exp %d", len(stack.Frames()), len(stack2.Frames()))
   113  	}
   114  }
   115  
   116  func TestDumpChain(t *testing.T) {
   117  	var err = errors.New("x")
   118  	errType := fmt.Sprintf("%T", err)
   119  	var err2 = fmt.Errorf("%w", err)
   120  	err2Type := fmt.Sprintf("%T", err2)
   121  
   122  	type args struct {
   123  		err error
   124  	}
   125  	tests := []struct {
   126  		name          string
   127  		args          args
   128  		wantTypeNames string
   129  	}{
   130  		{"nil", args{nil}, ""},
   131  		{"1", args{err}, errType},
   132  		{"2", args{err2}, err2Type + "\x20" + errType},
   133  	}
   134  	for _, tt := range tests {
   135  		t.Run(tt.name, func(t *testing.T) {
   136  			if gotTypeNames := DumpChain(tt.args.err); gotTypeNames != tt.wantTypeNames {
   137  				t.Errorf("DumpChain() = %v, want %v", gotTypeNames, tt.wantTypeNames)
   138  			}
   139  		})
   140  	}
   141  }
   142  
   143  func TestDumpGo(t *testing.T) {
   144  	var err = errors.New("x")
   145  	errType := fmt.Sprintf("%T %#[1]v", err)
   146  	var err2 = fmt.Errorf("%w", err)
   147  	err2Type := fmt.Sprintf("%T %#[1]v", err2)
   148  
   149  	type args struct {
   150  		err error
   151  	}
   152  	tests := []struct {
   153  		name          string
   154  		args          args
   155  		wantTypeNames string
   156  	}{
   157  		{"nil", args{nil}, ""},
   158  		{"1", args{err}, errType},
   159  		{"2", args{err2}, err2Type + "\n" + errType},
   160  	}
   161  	for _, tt := range tests {
   162  		t.Run(tt.name, func(t *testing.T) {
   163  			if gotTypeNames := DumpGo(tt.args.err); gotTypeNames != tt.wantTypeNames {
   164  				t.Errorf("DumpGo() = %v, want %v", gotTypeNames, tt.wantTypeNames)
   165  			}
   166  		})
   167  	}
   168  }