| 1 | // Copyright The Notary Project Authors. |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package plugin |
| 15 | |
| 16 | import ( |
| 17 | "testing" |
| 18 | ) |
| 19 | |
| 20 | func TestNewError(t *testing.T) { |
| 21 | msg := "someMSg" |
| 22 | err := NewError(ErrorCodeValidation, msg) |
| 23 | if err != nil { |
| 24 | if ErrorCodeValidation != err.ErrCode { |
| 25 | t.Errorf("NewError, expected errorCode '%s' but found'%s'", ErrorCodeValidation, err.ErrCode) |
| 26 | } |
| 27 | |
| 28 | if msg != err.Message { |
| 29 | t.Errorf("NewError, expected message'%s' but found '%s'", msg, err.Message) |
| 30 | } |
| 31 | |
| 32 | if err.Metadata != nil { |
| 33 | t.Errorf("NewError, expected metadata to be nil but found '%s'", err.Metadata) |
| 34 | } |
| 35 | |
| 36 | expError := "{\"errorCode\":\"VALIDATION_ERROR\",\"errorMessage\":\"someMSg\"}" |
| 37 | if expError != err.Error() { |
| 38 | t.Errorf("NewError#Error, expected error to be '%s' but found '%s'", expError, err.Error()) |
| 39 | |
| 40 | } |
| 41 | |
| 42 | } else { |
| 43 | t.Error("NewError didn't return an error") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestErrorCodes(t *testing.T) { |
| 48 | testCases := []struct { |
| 49 | err *Error |
| 50 | errCode ErrorCode |
| 51 | }{ |
| 52 | {err: NewValidationError(""), errCode: ErrorCodeValidation}, |
| 53 | {err: NewValidationErrorf("%s", ""), errCode: ErrorCodeValidation}, |
| 54 | {err: NewUnsupportedError(""), errCode: ErrorCodeValidation}, |
| 55 | {err: NewGenericError(""), errCode: ErrorCodeGeneric}, |
| 56 | {err: NewGenericErrorf("%s", ""), errCode: ErrorCodeGeneric}, |
| 57 | {err: NewJSONParsingError(""), errCode: ErrorCodeValidation}, |
| 58 | {err: NewUnsupportedContractVersionError(""), errCode: ErrorCodeUnsupportedContractVersion}, |
| 59 | } |
| 60 | for _, test := range testCases { |
| 61 | if test.errCode != test.err.ErrCode { |
| 62 | t.Errorf("Expected errorCode %s but found %s", test.errCode, test.err.ErrCode) |
| 63 | } |
| 64 | } |
| 65 | } |