github.com/davecgh/go-xdr@v0.0.0-20161123171359-e6a2ba005892/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 "errors" 21 "testing" 22 23 . "github.com/davecgh/go-xdr/xdr2" 24 ) 25 26 // TestErrorCodeStringer tests the stringized output for the ErrorCode type. 27 func TestErrorCodeStringer(t *testing.T) { 28 tests := []struct { 29 in ErrorCode 30 want string 31 }{ 32 {ErrBadArguments, "ErrBadArguments"}, 33 {ErrUnsupportedType, "ErrUnsupportedType"}, 34 {ErrBadEnumValue, "ErrBadEnumValue"}, 35 {ErrNotSettable, "ErrNotSettable"}, 36 {ErrOverflow, "ErrOverflow"}, 37 {ErrNilInterface, "ErrNilInterface"}, 38 {ErrIO, "ErrIO"}, 39 {ErrParseTime, "ErrParseTime"}, 40 {0xffff, "Unknown ErrorCode (65535)"}, 41 } 42 43 for i, test := range tests { 44 result := test.in.String() 45 if result != test.want { 46 t.Errorf("String #%d\n got: %s want: %s", i, result, 47 test.want) 48 continue 49 } 50 } 51 } 52 53 // TestUnmarshalError tests the error output for the UnmarshalError type. 54 func TestUnmarshalError(t *testing.T) { 55 tests := []struct { 56 in UnmarshalError 57 want string 58 }{ 59 { 60 UnmarshalError{ 61 ErrorCode: ErrIO, 62 Func: "test", 63 Description: "EOF while decoding 5 bytes", 64 Value: "testval", 65 }, 66 "xdr:test: EOF while decoding 5 bytes - read: 'testval'", 67 }, 68 { 69 UnmarshalError{ 70 ErrorCode: ErrBadEnumValue, 71 Func: "test", 72 Description: "invalid enum", 73 Value: "testenum", 74 }, 75 "xdr:test: invalid enum - read: 'testenum'", 76 }, 77 { 78 UnmarshalError{ 79 ErrorCode: ErrNilInterface, 80 Func: "test", 81 Description: "can't unmarshal to nil interface", 82 Value: nil, 83 }, 84 "xdr:test: can't unmarshal to nil interface", 85 }, 86 } 87 88 for i, test := range tests { 89 result := test.in.Error() 90 if result != test.want { 91 t.Errorf("Error #%d\n got: %s want: %s", i, result, 92 test.want) 93 continue 94 } 95 } 96 } 97 98 // TestMarshalError tests the error output for the MarshalError type. 99 func TestMarshalError(t *testing.T) { 100 tests := []struct { 101 in MarshalError 102 want string 103 }{ 104 { 105 MarshalError{ 106 ErrorCode: ErrIO, 107 Func: "test", 108 Description: "EOF while encoding 5 bytes", 109 Value: []byte{0x01, 0x02}, 110 }, 111 "xdr:test: EOF while encoding 5 bytes - wrote: '[1 2]'", 112 }, 113 { 114 MarshalError{ 115 ErrorCode: ErrBadEnumValue, 116 Func: "test", 117 Description: "invalid enum", 118 Value: "testenum", 119 }, 120 "xdr:test: invalid enum - value: 'testenum'", 121 }, 122 { 123 MarshalError{ 124 ErrorCode: ErrNilInterface, 125 Func: "test", 126 Description: "can't marshal to nil interface", 127 Value: nil, 128 }, 129 "xdr:test: can't marshal to nil interface", 130 }, 131 } 132 133 for i, test := range tests { 134 result := test.in.Error() 135 if result != test.want { 136 t.Errorf("Error #%d\n got: %s want: %s", i, result, 137 test.want) 138 continue 139 } 140 } 141 } 142 143 // TestIOErr ensures the IsIO function behaves as expected given different error 144 // types. 145 func TestIOErr(t *testing.T) { 146 tests := []struct { 147 in error 148 want bool 149 }{ 150 { 151 &MarshalError{ 152 ErrorCode: ErrIO, 153 Func: "test", 154 Description: "EOF while encoding 5 bytes", 155 Value: []byte{0x01, 0x02}, 156 }, 157 true, 158 }, 159 { 160 &MarshalError{ 161 ErrorCode: ErrUnsupportedType, 162 Func: "test", 163 Description: "ErrUnsupportedType", 164 Value: []byte{}, 165 }, 166 false, 167 }, 168 { 169 &UnmarshalError{ 170 ErrorCode: ErrIO, 171 Func: "test", 172 Description: "EOF while decoding 5 bytes", 173 Value: []byte{0x01, 0x02}, 174 }, 175 true, 176 }, 177 { 178 &UnmarshalError{ 179 ErrorCode: ErrUnsupportedType, 180 Func: "test", 181 Description: "ErrUnsupportedType", 182 Value: []byte{}, 183 }, 184 false, 185 }, 186 { 187 errors.New("boom"), 188 false, 189 }, 190 } 191 192 for i, test := range tests { 193 result := IsIO(test.in) 194 if result != test.want { 195 t.Errorf("Error #%d\n got: %v want: %v", i, result, 196 test.want) 197 continue 198 } 199 } 200 }