github.com/kaydxh/golang@v0.0.131/go/errors/errors_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package errors_test
    23  
    24  import (
    25  	"errors"
    26  	"fmt"
    27  	"testing"
    28  
    29  	errors_ "github.com/kaydxh/golang/go/errors"
    30  )
    31  
    32  func TestError(t *testing.T) {
    33  	var errs = []error{fmt.Errorf("error 1"), fmt.Errorf("error 2")}
    34  
    35  	var err error
    36  	//Aggregate  implemnet interface error 	Error() string
    37  	err = errors_.NewAggregate(errs)
    38  	//multiErrorStrings := err errors_.NewAggregate(errs).Errors()
    39  	multiErrorStrings := err.Error()
    40  	t.Logf("multiErrorStrings: %v", multiErrorStrings)
    41  
    42  }
    43  
    44  func TestErrorIs(t *testing.T) {
    45  	var ErrInternal = errors.New("internal error")
    46  	testCases := []struct {
    47  		err1     error
    48  		err2     error
    49  		expected bool
    50  	}{
    51  		{
    52  			err1:     ErrInternal,
    53  			err2:     ErrInternal,
    54  			expected: true,
    55  		},
    56  		{
    57  			// the same error messge is not mean the same error
    58  			err1:     errors.New("internal error"),
    59  			err2:     errors.New("internal error"),
    60  			expected: false,
    61  		},
    62  
    63  		{
    64  			err1:     errors.New("internal error1"),
    65  			err2:     errors.New("internal error2"),
    66  			expected: false,
    67  		},
    68  	}
    69  
    70  	for i, testCase := range testCases {
    71  		t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
    72  			err := errors_.NewAggregate([]error{testCase.err1})
    73  			if err.Is(testCase.err2) != testCase.expected {
    74  				t.Fatalf("err[%v] is not expected err2[%v]", err, testCase.err2)
    75  			}
    76  
    77  		})
    78  	}
    79  }
    80  
    81  func TestErrore(t *testing.T) {
    82  	var ErrInternal = errors.New("FailedOperation__Internal")
    83  	testCases := []struct {
    84  		err      error
    85  		code     int32
    86  		expected bool
    87  	}{
    88  		{
    89  			err:      ErrInternal,
    90  			code:     100,
    91  			expected: true,
    92  		},
    93  		{
    94  			err:      errors_.Errore(fmt.Errorf("failed to process,%d", 2), ErrInternal),
    95  			code:     100,
    96  			expected: true,
    97  		},
    98  		{
    99  			err:      fmt.Errorf("failed to process, %v", 2),
   100  			code:     100,
   101  			expected: false,
   102  		},
   103  	}
   104  
   105  	for i, testCase := range testCases {
   106  		t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
   107  			//err := errors_.Errore(testCase.code, testCase.err)
   108  			err := errors_.Errore(testCase.err)
   109  			if errors.Is(err, ErrInternal) != testCase.expected {
   110  				t.Fatalf("err[%v], epected[%v] test err[%v]", err, testCase.expected, testCase.err)
   111  			}
   112  			t.Logf("err[%v] expected[%v] test err[%v] ", err, testCase.expected, testCase.err)
   113  		})
   114  	}
   115  }