| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package assertions | 5 package assertions |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 | 9 |
| 10 "github.com/luci/luci-go/common/errors" |
| 11 |
| 10 "github.com/smartystreets/assertions" | 12 "github.com/smartystreets/assertions" |
| 11 ) | 13 ) |
| 12 | 14 |
| 13 // ShouldErrLike compares an `error` or `string` on the left side, to an `error` | 15 // ShouldErrLike compares an `error` or `string` on the left side, to an `error` |
| 14 // or `string` on the right side. | 16 // or `string` on the right side. |
| 15 // | 17 // |
| 16 // If the righthand side is omitted, this expects `actual` to be nil. | 18 // If the righthand side is omitted, this expects `actual` to be nil. |
| 17 // | 19 // |
| 18 // If a singular righthand side is provided, this expects the stringified | 20 // If a singular righthand side is provided, this expects the stringified |
| 19 // `actual` to contain the stringified `expected[0]` to be a substring of it. | 21 // `actual` to contain the stringified `expected[0]` to be a substring of it. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 f, ok := function.(func()) | 62 f, ok := function.(func()) |
| 61 if !ok { | 63 if !ok { |
| 62 return fmt.Sprintf("unknown argument type %T, expected `func()`"
, function) | 64 return fmt.Sprintf("unknown argument type %T, expected `func()`"
, function) |
| 63 } | 65 } |
| 64 defer func() { | 66 defer func() { |
| 65 ret = ShouldErrLike(recover(), expected...) | 67 ret = ShouldErrLike(recover(), expected...) |
| 66 }() | 68 }() |
| 67 f() | 69 f() |
| 68 return ShouldErrLike(nil, expected...) | 70 return ShouldErrLike(nil, expected...) |
| 69 } | 71 } |
| 72 |
| 73 // ShouldUnwrapTo asserts that an error, when unwrapped, equals another error. |
| 74 // |
| 75 // The actual field will be unwrapped using errors.Unwrap and then compared to |
| 76 // the error in expected. |
| 77 func ShouldUnwrapTo(actual interface{}, expected ...interface{}) string { |
| 78 act, ok := actual.(error) |
| 79 if !ok { |
| 80 return fmt.Sprintf("ShouldUnwrapTo requires an error actual type
, got %T", act) |
| 81 } |
| 82 |
| 83 if len(expected) != 1 { |
| 84 return fmt.Sprintf("ShouldUnwrapTo requires exactly one expected
value, got %d", len(expected)) |
| 85 } |
| 86 exp, ok := expected[0].(error) |
| 87 if !ok { |
| 88 return fmt.Sprintf("ShouldUnwrapTo requires an error expected ty
pe, got %T", expected[0]) |
| 89 } |
| 90 |
| 91 return assertions.ShouldEqual(errors.Unwrap(act), exp) |
| 92 } |
| OLD | NEW |