github.com/stellar/go-xdr@v0.0.0-20231122183749-b53fb00bcac2/xdr3/error_test.go (about) 1 /* 2 * Copyright (c) 2014 Dave Collins <dave@davec.name> 3 * 4 * Permission to use, copy, modify, and distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 package xdr_test 18 19 import ( 20 "errors" 21 "io" 22 "testing" 23 24 . "github.com/stellar/go-xdr/xdr3" 25 ) 26 27 // TestErrorCodeStringer tests the stringized output for the ErrorCode type. 28 func TestErrorCodeStringer(t *testing.T) { 29 tests := []struct { 30 in ErrorCode 31 want string 32 }{ 33 {ErrBadArguments, "ErrBadArguments"}, 34 {ErrUnsupportedType, "ErrUnsupportedType"}, 35 {ErrBadEnumValue, "ErrBadEnumValue"}, 36 {ErrNotSettable, "ErrNotSettable"}, 37 {ErrOverflow, "ErrOverflow"}, 38 {ErrNilInterface, "ErrNilInterface"}, 39 {ErrIO, "ErrIO"}, 40 {ErrParseTime, "ErrParseTime"}, 41 {0xffff, "Unknown ErrorCode (65535)"}, 42 } 43 44 for i, test := range tests { 45 result := test.in.String() 46 if result != test.want { 47 t.Errorf("String #%d\n got: %s want: %s", i, result, 48 test.want) 49 continue 50 } 51 } 52 } 53 54 // TestUnmarshalError tests the error output for the UnmarshalError type. 55 func TestUnmarshalError(t *testing.T) { 56 tests := []struct { 57 in UnmarshalError 58 want string 59 }{ 60 { 61 UnmarshalError{ 62 ErrorCode: ErrIO, 63 Func: "test", 64 Description: "EOF while decoding 5 bytes", 65 Value: "testval", 66 }, 67 "xdr:test: EOF while decoding 5 bytes - read: 'testval'", 68 }, 69 { 70 UnmarshalError{ 71 ErrorCode: ErrIO, 72 Func: "test", 73 Description: "underlying io error", 74 Value: "testval", 75 Err: io.ErrShortBuffer, 76 }, 77 "xdr:test: underlying io error - read: 'testval'", 78 }, 79 { 80 UnmarshalError{ 81 ErrorCode: ErrBadEnumValue, 82 Func: "test", 83 Description: "invalid enum", 84 Value: "testenum", 85 }, 86 "xdr:test: invalid enum - read: 'testenum'", 87 }, 88 { 89 UnmarshalError{ 90 ErrorCode: ErrNilInterface, 91 Func: "test", 92 Description: "can't unmarshal to nil interface", 93 Value: nil, 94 }, 95 "xdr:test: can't unmarshal to nil interface", 96 }, 97 } 98 99 for i, test := range tests { 100 result := test.in.Error() 101 if result != test.want { 102 t.Errorf("Error #%d\n got: %s want: %s", i, result, 103 test.want) 104 continue 105 } 106 if test.in.Err != nil && !errors.Is(&test.in, test.in.Err) { 107 t.Errorf("Error #%d\n is not is-able on underlying error", i) 108 continue 109 } 110 } 111 } 112 113 // TestMarshalError tests the error output for the MarshalError type. 114 func TestMarshalError(t *testing.T) { 115 tests := []struct { 116 in MarshalError 117 want string 118 }{ 119 { 120 MarshalError{ 121 ErrorCode: ErrIO, 122 Func: "test", 123 Description: "EOF while encoding 5 bytes", 124 Value: []byte{0x01, 0x02}, 125 }, 126 "xdr:test: EOF while encoding 5 bytes - wrote: '[1 2]'", 127 }, 128 { 129 MarshalError{ 130 ErrorCode: ErrIO, 131 Func: "test", 132 Description: "underlying io error", 133 Value: []byte{0x01, 0x02}, 134 Err: io.ErrShortWrite, 135 }, 136 "xdr:test: underlying io error - wrote: '[1 2]'", 137 }, 138 { 139 MarshalError{ 140 ErrorCode: ErrBadEnumValue, 141 Func: "test", 142 Description: "invalid enum", 143 Value: "testenum", 144 }, 145 "xdr:test: invalid enum - value: 'testenum'", 146 }, 147 { 148 MarshalError{ 149 ErrorCode: ErrNilInterface, 150 Func: "test", 151 Description: "can't marshal to nil interface", 152 Value: nil, 153 }, 154 "xdr:test: can't marshal to nil interface", 155 }, 156 } 157 158 for i, test := range tests { 159 result := test.in.Error() 160 if result != test.want { 161 t.Errorf("Error #%d\n got: %s want: %s", i, result, 162 test.want) 163 continue 164 } 165 if test.in.Err != nil && !errors.Is(&test.in, test.in.Err) { 166 t.Errorf("Error #%d\n is not is-able on underlying error", i) 167 continue 168 } 169 } 170 }