Scott Baker | 20481aa | 2019-06-20 11:00:54 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package error |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "github.com/stretchr/testify/assert" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | SetPrefix("cordctl") |
| 27 | } |
| 28 | |
| 29 | func TestGenericError(t *testing.T) { |
| 30 | var err error |
| 31 | |
| 32 | err = fmt.Errorf("Some error") |
| 33 | |
| 34 | // Type conversion from `error` to ChecksumMismatchError should fail |
| 35 | _, ok := err.(ChecksumMismatchError) |
| 36 | assert.False(t, ok) |
| 37 | |
| 38 | // Type conversion from `error` to CordCtlError should fail |
| 39 | _, ok = err.(CordCtlError) |
| 40 | assert.False(t, ok) |
| 41 | } |
| 42 | |
| 43 | func TestChecksumMismatchError(t *testing.T) { |
| 44 | var err error |
| 45 | |
| 46 | err = WithStackTrace(&ChecksumMismatchError{Actual: "123", Expected: "456"}) |
| 47 | |
| 48 | //assert.Equal(t, err.(*ChecksumMismatchError).Stack(), "foo") |
| 49 | |
| 50 | // Check that the Error() function returns the right text |
| 51 | assert.Equal(t, err.Error(), "cordctl: checksum mismatch (actual=456, expected=123)") |
| 52 | |
| 53 | // Type conversion from `error` to ChecksumMismatchError should succeed |
| 54 | _, ok := err.(*ChecksumMismatchError) |
| 55 | assert.True(t, ok) |
| 56 | |
| 57 | // Type switch is another way of doing the same |
| 58 | switch err.(type) { |
| 59 | case *ChecksumMismatchError: |
| 60 | // do nothing |
| 61 | case CordCtlError: |
| 62 | assert.Fail(t, "Should have used the ChecksumMismatchError case instead") |
| 63 | default: |
| 64 | assert.Fail(t, "Wrong part of switch statement was called") |
| 65 | } |
| 66 | |
| 67 | // Type conversion from `error` to CordCtlError should succeed |
| 68 | cce, ok := err.(CordCtlError) |
| 69 | assert.True(t, ok) |
| 70 | |
| 71 | // ShouldDumpStack() returned from a ChecksumMismatchError should be false |
| 72 | assert.False(t, cce.ShouldDumpStack()) |
| 73 | } |
| 74 | |
| 75 | func TestUnknownModelTypeError(t *testing.T) { |
| 76 | var err error |
| 77 | |
| 78 | err = WithStackTrace(&UnknownModelTypeError{Name: "foo"}) |
| 79 | |
| 80 | _ = err |
| 81 | } |