|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestError_Error(t *testing.T) { |
| 9 | + t.Parallel() |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + err *Error |
| 13 | + want string |
| 14 | + }{ |
| 15 | + { |
| 16 | + name: "error with cause", |
| 17 | + err: &Error{ |
| 18 | + Type: ErrInvalidArgument, |
| 19 | + Message: "test message", |
| 20 | + Cause: errors.New("underlying error"), |
| 21 | + }, |
| 22 | + want: "invalid_argument: test message: underlying error", |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "error without cause", |
| 26 | + err: &Error{ |
| 27 | + Type: ErrContainerRuntime, |
| 28 | + Message: "test message", |
| 29 | + Cause: nil, |
| 30 | + }, |
| 31 | + want: "container_runtime: test message", |
| 32 | + }, |
| 33 | + } |
| 34 | + |
| 35 | + for _, tt := range tests { |
| 36 | + t.Run(tt.name, func(t *testing.T) { |
| 37 | + t.Parallel() |
| 38 | + got := tt.err.Error() |
| 39 | + if got != tt.want { |
| 40 | + t.Errorf("Error.Error() = %v, want %v", got, tt.want) |
| 41 | + } |
| 42 | + }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestError_Unwrap(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + cause := errors.New("underlying error") |
| 49 | + err := &Error{ |
| 50 | + Type: ErrInternal, |
| 51 | + Message: "test message", |
| 52 | + Cause: cause, |
| 53 | + } |
| 54 | + |
| 55 | + if got := err.Unwrap(); got != cause { |
| 56 | + t.Errorf("Error.Unwrap() = %v, want %v", got, cause) |
| 57 | + } |
| 58 | + |
| 59 | + errNoCause := &Error{ |
| 60 | + Type: ErrInternal, |
| 61 | + Message: "test message", |
| 62 | + Cause: nil, |
| 63 | + } |
| 64 | + |
| 65 | + if got := errNoCause.Unwrap(); got != nil { |
| 66 | + t.Errorf("Error.Unwrap() = %v, want nil", got) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestNewError(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + cause := errors.New("underlying error") |
| 73 | + err := NewError(ErrInvalidArgument, "test message", cause) |
| 74 | + |
| 75 | + if err.Type != ErrInvalidArgument { |
| 76 | + t.Errorf("NewError().Type = %v, want %v", err.Type, ErrInvalidArgument) |
| 77 | + } |
| 78 | + if err.Message != "test message" { |
| 79 | + t.Errorf("NewError().Message = %v, want %v", err.Message, "test message") |
| 80 | + } |
| 81 | + if err.Cause != cause { |
| 82 | + t.Errorf("NewError().Cause = %v, want %v", err.Cause, cause) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func TestNewErrorConstructors(t *testing.T) { |
| 87 | + t.Parallel() |
| 88 | + cause := errors.New("cause") |
| 89 | + |
| 90 | + tests := []struct { |
| 91 | + name string |
| 92 | + constructor func(string, error) *Error |
| 93 | + wantType string |
| 94 | + }{ |
| 95 | + { |
| 96 | + name: "NewInvalidArgumentError", |
| 97 | + constructor: NewInvalidArgumentError, |
| 98 | + wantType: ErrInvalidArgument, |
| 99 | + }, |
| 100 | + { |
| 101 | + name: "NewContainerRuntimeError", |
| 102 | + constructor: NewContainerRuntimeError, |
| 103 | + wantType: ErrContainerRuntime, |
| 104 | + }, |
| 105 | + { |
| 106 | + name: "NewContainerNotFoundError", |
| 107 | + constructor: NewContainerNotFoundError, |
| 108 | + wantType: ErrContainerNotFound, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "NewContainerAlreadyExistsError", |
| 112 | + constructor: NewContainerAlreadyExistsError, |
| 113 | + wantType: ErrContainerAlreadyExists, |
| 114 | + }, |
| 115 | + { |
| 116 | + name: "NewContainerNotRunningError", |
| 117 | + constructor: NewContainerNotRunningError, |
| 118 | + wantType: ErrContainerNotRunning, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: "NewContainerAlreadyRunningError", |
| 122 | + constructor: NewContainerAlreadyRunningError, |
| 123 | + wantType: ErrContainerAlreadyRunning, |
| 124 | + }, |
| 125 | + { |
| 126 | + name: "NewRunConfigNotFoundError", |
| 127 | + constructor: NewRunConfigNotFoundError, |
| 128 | + wantType: ErrRunConfigNotFound, |
| 129 | + }, |
| 130 | + { |
| 131 | + name: "NewGroupAlreadyExistsError", |
| 132 | + constructor: NewGroupAlreadyExistsError, |
| 133 | + wantType: ErrGroupAlreadyExists, |
| 134 | + }, |
| 135 | + { |
| 136 | + name: "NewGroupNotFoundError", |
| 137 | + constructor: NewGroupNotFoundError, |
| 138 | + wantType: ErrGroupNotFound, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "NewTransportError", |
| 142 | + constructor: NewTransportError, |
| 143 | + wantType: ErrTransport, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "NewPermissionsError", |
| 147 | + constructor: NewPermissionsError, |
| 148 | + wantType: ErrPermissions, |
| 149 | + }, |
| 150 | + { |
| 151 | + name: "NewInternalError", |
| 152 | + constructor: NewInternalError, |
| 153 | + wantType: ErrInternal, |
| 154 | + }, |
| 155 | + } |
| 156 | + |
| 157 | + for _, tt := range tests { |
| 158 | + t.Run(tt.name, func(t *testing.T) { |
| 159 | + t.Parallel() |
| 160 | + err := tt.constructor("test message", cause) |
| 161 | + if err.Type != tt.wantType { |
| 162 | + t.Errorf("%s().Type = %v, want %v", tt.name, err.Type, tt.wantType) |
| 163 | + } |
| 164 | + if err.Message != "test message" { |
| 165 | + t.Errorf("%s().Message = %v, want %v", tt.name, err.Message, "test message") |
| 166 | + } |
| 167 | + if err.Cause != cause { |
| 168 | + t.Errorf("%s().Cause = %v, want %v", tt.name, err.Cause, cause) |
| 169 | + } |
| 170 | + }) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestErrorTypeCheckers(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + tests := []struct { |
| 177 | + name string |
| 178 | + err error |
| 179 | + checker func(error) bool |
| 180 | + want bool |
| 181 | + }{ |
| 182 | + { |
| 183 | + name: "IsInvalidArgument with matching error", |
| 184 | + err: NewInvalidArgumentError("test", nil), |
| 185 | + checker: IsInvalidArgument, |
| 186 | + want: true, |
| 187 | + }, |
| 188 | + { |
| 189 | + name: "IsInvalidArgument with non-matching error", |
| 190 | + err: NewContainerRuntimeError("test", nil), |
| 191 | + checker: IsInvalidArgument, |
| 192 | + want: false, |
| 193 | + }, |
| 194 | + { |
| 195 | + name: "IsInvalidArgument with non-Error type", |
| 196 | + err: errors.New("regular error"), |
| 197 | + checker: IsInvalidArgument, |
| 198 | + want: false, |
| 199 | + }, |
| 200 | + { |
| 201 | + name: "IsContainerRuntime with matching error", |
| 202 | + err: NewContainerRuntimeError("test", nil), |
| 203 | + checker: IsContainerRuntime, |
| 204 | + want: true, |
| 205 | + }, |
| 206 | + { |
| 207 | + name: "IsContainerNotFound with matching error", |
| 208 | + err: NewContainerNotFoundError("test", nil), |
| 209 | + checker: IsContainerNotFound, |
| 210 | + want: true, |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "IsContainerAlreadyExists with matching error", |
| 214 | + err: NewContainerAlreadyExistsError("test", nil), |
| 215 | + checker: IsContainerAlreadyExists, |
| 216 | + want: true, |
| 217 | + }, |
| 218 | + { |
| 219 | + name: "IsContainerNotRunning with matching error", |
| 220 | + err: NewContainerNotRunningError("test", nil), |
| 221 | + checker: IsContainerNotRunning, |
| 222 | + want: true, |
| 223 | + }, |
| 224 | + { |
| 225 | + name: "IsContainerAlreadyRunning with matching error", |
| 226 | + err: NewContainerAlreadyRunningError("test", nil), |
| 227 | + checker: IsContainerAlreadyRunning, |
| 228 | + want: true, |
| 229 | + }, |
| 230 | + { |
| 231 | + name: "IsRunConfigNotFound with matching error", |
| 232 | + err: NewRunConfigNotFoundError("test", nil), |
| 233 | + checker: IsRunConfigNotFound, |
| 234 | + want: true, |
| 235 | + }, |
| 236 | + { |
| 237 | + name: "IsGroupAlreadyExists with matching error", |
| 238 | + err: NewGroupAlreadyExistsError("test", nil), |
| 239 | + checker: IsGroupAlreadyExists, |
| 240 | + want: true, |
| 241 | + }, |
| 242 | + { |
| 243 | + name: "IsGroupNotFound with matching error", |
| 244 | + err: NewGroupNotFoundError("test", nil), |
| 245 | + checker: IsGroupNotFound, |
| 246 | + want: true, |
| 247 | + }, |
| 248 | + { |
| 249 | + name: "IsTransport with matching error", |
| 250 | + err: NewTransportError("test", nil), |
| 251 | + checker: IsTransport, |
| 252 | + want: true, |
| 253 | + }, |
| 254 | + { |
| 255 | + name: "IsPermissions with matching error", |
| 256 | + err: NewPermissionsError("test", nil), |
| 257 | + checker: IsPermissions, |
| 258 | + want: true, |
| 259 | + }, |
| 260 | + { |
| 261 | + name: "IsInternal with matching error", |
| 262 | + err: NewInternalError("test", nil), |
| 263 | + checker: IsInternal, |
| 264 | + want: true, |
| 265 | + }, |
| 266 | + { |
| 267 | + name: "IsInternal with nil error", |
| 268 | + err: nil, |
| 269 | + checker: IsInternal, |
| 270 | + want: false, |
| 271 | + }, |
| 272 | + } |
| 273 | + |
| 274 | + for _, tt := range tests { |
| 275 | + t.Run(tt.name, func(t *testing.T) { |
| 276 | + t.Parallel() |
| 277 | + got := tt.checker(tt.err) |
| 278 | + if got != tt.want { |
| 279 | + t.Errorf("%s() = %v, want %v", tt.name, got, tt.want) |
| 280 | + } |
| 281 | + }) |
| 282 | + } |
| 283 | +} |
0 commit comments