|
| 1 | +/* |
| 2 | +Copyright SecureKey Technologies Inc. All Rights Reserved. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: Apache-2.0 |
| 5 | +*/ |
| 6 | + |
| 7 | +package multi |
| 8 | + |
| 9 | +import ( |
| 10 | + "fmt" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestErrorString(t *testing.T) { |
| 17 | + testErr := fmt.Errorf("test") |
| 18 | + var errs Errors |
| 19 | + |
| 20 | + assert.Equal(t, "", errs.Error()) |
| 21 | + |
| 22 | + errs = append(errs, testErr) |
| 23 | + assert.Equal(t, testErr.Error(), errs.Error()) |
| 24 | + |
| 25 | + errs = append(errs, testErr) |
| 26 | + assert.Equal(t, "Multiple errors occurred: \ntest\ntest", errs.Error()) |
| 27 | +} |
| 28 | + |
| 29 | +func TestAppend(t *testing.T) { |
| 30 | + testErr := fmt.Errorf("test") |
| 31 | + testErr2 := fmt.Errorf("test2") |
| 32 | + |
| 33 | + m := Append(nil, nil) |
| 34 | + assert.Nil(t, m) |
| 35 | + |
| 36 | + m = Append(nil, testErr) |
| 37 | + assert.Equal(t, testErr, m) |
| 38 | + |
| 39 | + m = Append(testErr, testErr2) |
| 40 | + m1, ok := m.(Errors) |
| 41 | + assert.True(t, ok) |
| 42 | + assert.Equal(t, testErr, m1[0]) |
| 43 | + assert.Equal(t, testErr2, m1[1]) |
| 44 | + |
| 45 | + m = Append(Errors{testErr}, testErr2) |
| 46 | + m1, ok = m.(Errors) |
| 47 | + assert.True(t, ok) |
| 48 | + assert.Equal(t, testErr, m1[0]) |
| 49 | + assert.Equal(t, testErr2, m1[1]) |
| 50 | +} |
| 51 | + |
| 52 | +func TestToError(t *testing.T) { |
| 53 | + testErr := fmt.Errorf("test") |
| 54 | + var errs Errors |
| 55 | + |
| 56 | + assert.Equal(t, nil, errs.ToError()) |
| 57 | + |
| 58 | + errs = append(errs, testErr) |
| 59 | + assert.Equal(t, testErr, errs.ToError()) |
| 60 | + |
| 61 | + errs = append(errs, testErr) |
| 62 | + assert.Equal(t, errs, errs.ToError()) |
| 63 | +} |
0 commit comments