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/grpc/grpcutil" |
10 "github.com/smartystreets/goconvey/convey" | 11 "github.com/smartystreets/goconvey/convey" |
11 "google.golang.org/grpc" | 12 "google.golang.org/grpc" |
12 "google.golang.org/grpc/codes" | 13 "google.golang.org/grpc/codes" |
13 ) | 14 ) |
14 | 15 |
15 // ShouldHaveRPCCode is a goconvey assertion, asserting that the supplied | 16 // ShouldHaveRPCCode is a goconvey assertion, asserting that the supplied |
16 // "actual" value has a gRPC code value and, optionally, errors like a supplied | 17 // "actual" value has a gRPC code value and, optionally, errors like a supplied |
17 // message string. | 18 // message string. |
18 // | 19 // |
19 // If no "expected" arguments are supplied, ShouldHaveRPCCode will assert that | 20 // If no "expected" arguments are supplied, ShouldHaveRPCCode will assert that |
(...skipping 28 matching lines...) Expand all Loading... |
48 return fmt.Sprintf("The code must be a codes.Code, not a
%T", expected[0]) | 49 return fmt.Sprintf("The code must be a codes.Code, not a
%T", expected[0]) |
49 } | 50 } |
50 | 51 |
51 case 0: | 52 case 0: |
52 ecode = codes.OK | 53 ecode = codes.OK |
53 | 54 |
54 default: | 55 default: |
55 return "Expected argument must have the form: [codes.Code[string
]]" | 56 return "Expected argument must have the form: [codes.Code[string
]]" |
56 } | 57 } |
57 | 58 |
58 » if acode := grpc.Code(aerr); acode != ecode { | 59 » if acode := grpcutil.Code(aerr); acode != ecode { |
59 return fmt.Sprintf("expected gRPC code %q (%d), not %q (%d), typ
e %T: %v", | 60 return fmt.Sprintf("expected gRPC code %q (%d), not %q (%d), typ
e %T: %v", |
60 ecode, ecode, acode, acode, actual, actual) | 61 ecode, ecode, acode, acode, actual, actual) |
61 } | 62 } |
62 | 63 |
63 if errLike != "" { | 64 if errLike != "" { |
64 return convey.ShouldContainSubstring(grpc.ErrorDesc(aerr), errLi
ke) | 65 return convey.ShouldContainSubstring(grpc.ErrorDesc(aerr), errLi
ke) |
65 } | 66 } |
66 return "" | 67 return "" |
67 } | 68 } |
68 | 69 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 func ShouldBeRPCAborted(actual interface{}, expected ...interface{}) string { | 158 func ShouldBeRPCAborted(actual interface{}, expected ...interface{}) string { |
158 return ShouldHaveRPCCode(actual, prepend(codes.Aborted, expected)...) | 159 return ShouldHaveRPCCode(actual, prepend(codes.Aborted, expected)...) |
159 } | 160 } |
160 | 161 |
161 func prepend(c codes.Code, exp []interface{}) []interface{} { | 162 func prepend(c codes.Code, exp []interface{}) []interface{} { |
162 args := make([]interface{}, len(exp)+1) | 163 args := make([]interface{}, len(exp)+1) |
163 args[0] = c | 164 args[0] = c |
164 copy(args[1:], exp) | 165 copy(args[1:], exp) |
165 return args | 166 return args |
166 } | 167 } |
OLD | NEW |