multierror_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. Copyright 2020 Docker, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package multierror
  14. import (
  15. "errors"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. func TestSingleError(t *testing.T) {
  20. var err *Error
  21. err = Append(err, errors.New("error"))
  22. assert.Equal(t, 1, len(err.WrappedErrors()))
  23. }
  24. func TestGoError(t *testing.T) {
  25. var err error
  26. result := Append(err, errors.New("error"))
  27. assert.Equal(t, 1, len(result.WrappedErrors()))
  28. }
  29. func TestMultiError(t *testing.T) {
  30. var err *Error
  31. err = Append(err,
  32. errors.New("first"),
  33. errors.New("second"),
  34. )
  35. assert.Equal(t, 2, len(err.WrappedErrors()))
  36. assert.Equal(t, "Error: first\nError: second", err.Error())
  37. }
  38. func TestUnwrap(t *testing.T) {
  39. var err *Error
  40. assert.Equal(t, nil, errors.Unwrap(err))
  41. err = Append(err, errors.New("first"))
  42. e := errors.Unwrap(err)
  43. assert.Equal(t, "first", e.Error())
  44. }
  45. func TestErrorOrNil(t *testing.T) {
  46. var err *Error
  47. assert.Equal(t, nil, err.ErrorOrNil())
  48. err = Append(err, errors.New("error"))
  49. e := err.ErrorOrNil()
  50. assert.Equal(t, "error", e.Error())
  51. }