github.com/MetalBlockchain/metalgo@v1.11.9/snow/engine/common/error_test.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package common 5 6 import ( 7 "errors" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 // Tests the invariant that AppErrors are matched against their error codes 14 func TestAppErrorEqual(t *testing.T) { 15 tests := []struct { 16 name string 17 err1 *AppError 18 err2 error 19 expected bool 20 }{ 21 { 22 name: "is - equal", 23 err1: &AppError{ 24 Code: 1, 25 }, 26 err2: &AppError{ 27 Code: 1, 28 }, 29 expected: true, 30 }, 31 { 32 name: "is - same error code different messages", 33 err1: &AppError{ 34 Code: 1, 35 Message: "foo", 36 }, 37 err2: &AppError{ 38 Code: 1, 39 Message: "bar", 40 }, 41 expected: true, 42 }, 43 { 44 name: "not is - different error code", 45 err1: &AppError{ 46 Code: 1, 47 }, 48 err2: &AppError{ 49 Code: 2, 50 }, 51 }, 52 { 53 name: "not is - different type", 54 err1: &AppError{ 55 Code: 1, 56 }, 57 err2: errors.New("foobar"), 58 }, 59 } 60 61 for _, tt := range tests { 62 t.Run(tt.name, func(t *testing.T) { 63 require.Equal(t, tt.expected, errors.Is(tt.err1, tt.err2)) 64 }) 65 } 66 } 67 68 // Tests reserved error types 69 func TestErrorCode(t *testing.T) { 70 tests := []struct { 71 name string 72 code int32 73 expected *AppError 74 }{ 75 { 76 name: "undefined", 77 code: 0, 78 expected: ErrUndefined, 79 }, 80 { 81 name: "undefined", 82 code: -1, 83 expected: ErrTimeout, 84 }, 85 } 86 87 for _, tt := range tests { 88 t.Run(tt.name, func(t *testing.T) { 89 require.ErrorIs(t, tt.expected, &AppError{Code: tt.code}) 90 }) 91 } 92 }