github.com/stellar/go-xdr@v0.0.0-20231122183749-b53fb00bcac2/xdr2/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 "testing" 21 22 . "github.com/stellar/go-xdr/xdr2" 23 ) 24 25 // TestErrorCodeStringer tests the stringized output for the ErrorCode type. 26 func TestErrorCodeStringer(t *testing.T) { 27 tests := []struct { 28 in ErrorCode 29 want string 30 }{ 31 {ErrBadArguments, "ErrBadArguments"}, 32 {ErrUnsupportedType, "ErrUnsupportedType"}, 33 {ErrBadEnumValue, "ErrBadEnumValue"}, 34 {ErrNotSettable, "ErrNotSettable"}, 35 {ErrOverflow, "ErrOverflow"}, 36 {ErrNilInterface, "ErrNilInterface"}, 37 {ErrIO, "ErrIO"}, 38 {ErrParseTime, "ErrParseTime"}, 39 {0xffff, "Unknown ErrorCode (65535)"}, 40 } 41 42 for i, test := range tests { 43 result := test.in.String() 44 if result != test.want { 45 t.Errorf("String #%d\n got: %s want: %s", i, result, 46 test.want) 47 continue 48 } 49 } 50 } 51 52 // TestUnmarshalError tests the error output for the UnmarshalError type. 53 func TestUnmarshalError(t *testing.T) { 54 tests := []struct { 55 in UnmarshalError 56 want string 57 }{ 58 { 59 UnmarshalError{ 60 ErrorCode: ErrIO, 61 Func: "test", 62 Description: "EOF while decoding 5 bytes", 63 Value: "testval", 64 }, 65 "xdr:test: EOF while decoding 5 bytes - read: 'testval'", 66 }, 67 { 68 UnmarshalError{ 69 ErrorCode: ErrBadEnumValue, 70 Func: "test", 71 Description: "invalid enum", 72 Value: "testenum", 73 }, 74 "xdr:test: invalid enum - read: 'testenum'", 75 }, 76 { 77 UnmarshalError{ 78 ErrorCode: ErrNilInterface, 79 Func: "test", 80 Description: "can't unmarshal to nil interface", 81 Value: nil, 82 }, 83 "xdr:test: can't unmarshal to nil interface", 84 }, 85 } 86 87 for i, test := range tests { 88 result := test.in.Error() 89 if result != test.want { 90 t.Errorf("Error #%d\n got: %s want: %s", i, result, 91 test.want) 92 continue 93 } 94 } 95 } 96 97 // TestMarshalError tests the error output for the MarshalError type. 98 func TestMarshalError(t *testing.T) { 99 tests := []struct { 100 in MarshalError 101 want string 102 }{ 103 { 104 MarshalError{ 105 ErrorCode: ErrIO, 106 Func: "test", 107 Description: "EOF while encoding 5 bytes", 108 Value: []byte{0x01, 0x02}, 109 }, 110 "xdr:test: EOF while encoding 5 bytes - wrote: '[1 2]'", 111 }, 112 { 113 MarshalError{ 114 ErrorCode: ErrBadEnumValue, 115 Func: "test", 116 Description: "invalid enum", 117 Value: "testenum", 118 }, 119 "xdr:test: invalid enum - value: 'testenum'", 120 }, 121 { 122 MarshalError{ 123 ErrorCode: ErrNilInterface, 124 Func: "test", 125 Description: "can't marshal to nil interface", 126 Value: nil, 127 }, 128 "xdr:test: can't marshal to nil interface", 129 }, 130 } 131 132 for i, test := range tests { 133 result := test.in.Error() 134 if result != test.want { 135 t.Errorf("Error #%d\n got: %s want: %s", i, result, 136 test.want) 137 continue 138 } 139 } 140 }