github.com/blend/go-sdk@v1.20220411.3/ex/is_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package ex
     9  
    10  import (
    11  	"errors"
    12  	"fmt"
    13  	"testing"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestIs(t *testing.T) {
    19  	it := assert.New(t)
    20  
    21  	stdLibErr := errors.New("sentinel")
    22  
    23  	testCases := []struct {
    24  		Err      interface{}
    25  		Cause    error
    26  		Expected bool
    27  	}{
    28  		{Err: Class("test class"), Cause: Class("test class"), Expected: true},
    29  		{Err: New("test class"), Cause: Class("test class"), Expected: true},
    30  		{Err: New("test class"), Cause: New("test class"), Expected: true},
    31  		{Err: New(stdLibErr), Cause: stdLibErr, Expected: true},
    32  		{Err: New(fmt.Errorf("outer err: %w", stdLibErr)), Cause: stdLibErr, Expected: true},
    33  		{Err: Multi([]error{New("test class"), Class("not test class")}), Cause: Class("not test class"), Expected: true},
    34  		{Err: Class("not test class"), Cause: New("test class"), Expected: false},
    35  		{Err: New("test class"), Cause: New("not test class"), Expected: false},
    36  		{Err: New("test class"), Cause: nil, Expected: false},
    37  		{Err: fmt.Errorf("outer err: %w", stdLibErr), Cause: stdLibErr, Expected: true},
    38  		{Err: nil, Cause: nil, Expected: false},
    39  		{Err: nil, Cause: Class("test class"), Expected: false},
    40  	}
    41  
    42  	for index, tc := range testCases {
    43  		it.Equal(tc.Expected, Is(tc.Err, tc.Cause), fmt.Sprintf("test case %d", index))
    44  	}
    45  }