github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/common/apperr/error_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package apperr
    20  
    21  import (
    22  	"testing"
    23  
    24  	"github.com/pkg/errors"
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func TestError(t *testing.T) {
    29  
    30  	var (
    31  		baseErr   = errors.New("base error")
    32  		commonErr = errors.New("common error")
    33  
    34  		ErrFirst  = ErrorWithCode("FIRST_ERROR", "first error", baseErr)
    35  		ErrSecond = ErrorWithCode("SECOND_ERROR", "second error", commonErr)
    36  	)
    37  
    38  	t.Run("error", func(t *testing.T) {
    39  		Convey("", t, func(ctx C) {
    40  
    41  			firstError := ErrFirst
    42  			ctx.So(errors.Is(firstError, baseErr), ShouldBeTrue)
    43  			ctx.So(errors.Is(firstError, commonErr), ShouldBeFalse)
    44  
    45  			secondError := ErrSecond
    46  			ctx.So(errors.Is(secondError, commonErr), ShouldBeTrue)
    47  			ctx.So(errors.Is(secondError, baseErr), ShouldBeFalse)
    48  
    49  			ctx.So(ErrFirst.Error(), ShouldEqual, "base error: first error")
    50  			ctx.So(ErrSecond.Error(), ShouldEqual, "common error: second error")
    51  
    52  			ctx.So(Code(ErrFirst), ShouldEqual, "FIRST_ERROR")
    53  			ctx.So(Message(ErrFirst), ShouldEqual, "first error")
    54  			ctx.So(Root(ErrFirst), ShouldEqual, "base error")
    55  
    56  			err := GetError(ErrFirst)
    57  			ctx.So(err.Code(), ShouldEqual, "FIRST_ERROR")
    58  			ctx.So(err.Message(), ShouldEqual, "first error")
    59  			ctx.So(err.Root(), ShouldEqual, "base error")
    60  
    61  			ctx.So(errors.Is(secondError, secondError), ShouldBeTrue)
    62  			ctx.So(errors.Is(firstError, firstError), ShouldBeTrue)
    63  			ctx.So(errors.Is(secondError, firstError), ShouldBeFalse)
    64  		})
    65  	})
    66  }