|
13 | 13 | * See the License for the specific language governing permissions and |
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | | -import GRPCCore |
17 | | -import XCTest |
18 | | - |
19 | | -final class StatusTests: XCTestCase { |
20 | | - private static let statusCodeRawValue: [(Status.Code, Int)] = [ |
21 | | - (.ok, 0), |
22 | | - (.cancelled, 1), |
23 | | - (.unknown, 2), |
24 | | - (.invalidArgument, 3), |
25 | | - (.deadlineExceeded, 4), |
26 | | - (.notFound, 5), |
27 | | - (.alreadyExists, 6), |
28 | | - (.permissionDenied, 7), |
29 | | - (.resourceExhausted, 8), |
30 | | - (.failedPrecondition, 9), |
31 | | - (.aborted, 10), |
32 | | - (.outOfRange, 11), |
33 | | - (.unimplemented, 12), |
34 | | - (.internalError, 13), |
35 | | - (.unavailable, 14), |
36 | | - (.dataLoss, 15), |
37 | | - (.unauthenticated, 16), |
38 | | - ] |
39 | 16 |
|
40 | | - func testCustomStringConvertible() { |
41 | | - XCTAssertDescription(Status(code: .ok, message: ""), #"ok: """#) |
42 | | - XCTAssertDescription(Status(code: .dataLoss, message: "message"), #"dataLoss: "message""#) |
43 | | - XCTAssertDescription(Status(code: .unknown, message: "message"), #"unknown: "message""#) |
44 | | - XCTAssertDescription(Status(code: .aborted, message: "message"), #"aborted: "message""#) |
45 | | - } |
| 17 | +import GRPCCore |
| 18 | +import Testing |
46 | 19 |
|
47 | | - func testStatusCodeRawValues() { |
48 | | - for (code, expected) in Self.statusCodeRawValue { |
49 | | - XCTAssertEqual(code.rawValue, expected, "\(code) had unexpected raw value") |
| 20 | +@Suite("Status") |
| 21 | +struct StatusTests { |
| 22 | + @Suite("Code") |
| 23 | + struct Code { |
| 24 | + @Test("rawValue", arguments: zip(Status.Code.all, 0 ... 16)) |
| 25 | + func rawValueOfStatusCodes(code: Status.Code, expected: Int) { |
| 26 | + #expect(code.rawValue == expected) |
50 | 27 | } |
51 | | - } |
52 | | - |
53 | | - func testStatusCodeFromErrorCode() throws { |
54 | | - XCTAssertEqual(Status.Code(RPCError.Code.cancelled), .cancelled) |
55 | | - XCTAssertEqual(Status.Code(RPCError.Code.unknown), .unknown) |
56 | | - XCTAssertEqual(Status.Code(RPCError.Code.invalidArgument), .invalidArgument) |
57 | | - XCTAssertEqual(Status.Code(RPCError.Code.deadlineExceeded), .deadlineExceeded) |
58 | | - XCTAssertEqual(Status.Code(RPCError.Code.notFound), .notFound) |
59 | | - XCTAssertEqual(Status.Code(RPCError.Code.alreadyExists), .alreadyExists) |
60 | | - XCTAssertEqual(Status.Code(RPCError.Code.permissionDenied), .permissionDenied) |
61 | | - XCTAssertEqual(Status.Code(RPCError.Code.resourceExhausted), .resourceExhausted) |
62 | | - XCTAssertEqual(Status.Code(RPCError.Code.failedPrecondition), .failedPrecondition) |
63 | | - XCTAssertEqual(Status.Code(RPCError.Code.aborted), .aborted) |
64 | | - XCTAssertEqual(Status.Code(RPCError.Code.outOfRange), .outOfRange) |
65 | | - XCTAssertEqual(Status.Code(RPCError.Code.unimplemented), .unimplemented) |
66 | | - XCTAssertEqual(Status.Code(RPCError.Code.internalError), .internalError) |
67 | | - XCTAssertEqual(Status.Code(RPCError.Code.unavailable), .unavailable) |
68 | | - XCTAssertEqual(Status.Code(RPCError.Code.dataLoss), .dataLoss) |
69 | | - XCTAssertEqual(Status.Code(RPCError.Code.unauthenticated), .unauthenticated) |
70 | | - } |
71 | 28 |
|
72 | | - func testStatusCodeFromValidRawValue() { |
73 | | - for (expected, rawValue) in Self.statusCodeRawValue { |
74 | | - XCTAssertEqual( |
75 | | - Status.Code(rawValue: rawValue), |
76 | | - expected, |
77 | | - "\(rawValue) didn't convert to expected code \(expected)" |
| 29 | + @Test( |
| 30 | + "Initialize from RPCError.Code", |
| 31 | + arguments: zip( |
| 32 | + RPCError.Code.all, |
| 33 | + Status.Code.all.dropFirst() // Drop '.ok', there is no '.ok' error code. |
78 | 34 | ) |
| 35 | + ) |
| 36 | + func initFromRPCErrorCode(errorCode: RPCError.Code, expected: Status.Code) { |
| 37 | + #expect(Status.Code(errorCode) == expected) |
79 | 38 | } |
80 | | - } |
81 | 39 |
|
82 | | - func testStatusCodeFromInvalidRawValue() { |
83 | | - // Internally represented as a `UInt8`; try all other values. |
84 | | - for rawValue in UInt8(17) ... UInt8.max { |
85 | | - XCTAssertNil(Status.Code(rawValue: Int(rawValue))) |
| 40 | + @Test("Initialize from rawValue", arguments: zip(0 ... 16, Status.Code.all)) |
| 41 | + func initFromRawValue(rawValue: Int, expected: Status.Code) { |
| 42 | + #expect(Status.Code(rawValue: rawValue) == expected) |
86 | 43 | } |
87 | 44 |
|
88 | | - // API accepts `Int` so try invalid `Int` values too. |
89 | | - XCTAssertNil(Status.Code(rawValue: -1)) |
90 | | - XCTAssertNil(Status.Code(rawValue: 1000)) |
91 | | - XCTAssertNil(Status.Code(rawValue: .max)) |
| 45 | + @Test("Initialize from invalid rawValue", arguments: [-1, 17, 100, .max]) |
| 46 | + func initFromInvalidRawValue(rawValue: Int) { |
| 47 | + #expect(Status.Code(rawValue: rawValue) == nil) |
| 48 | + } |
92 | 49 | } |
93 | 50 |
|
94 | | - func testEquatableConformance() { |
95 | | - XCTAssertEqual(Status(code: .ok, message: ""), Status(code: .ok, message: "")) |
96 | | - XCTAssertEqual(Status(code: .ok, message: "message"), Status(code: .ok, message: "message")) |
97 | | - |
98 | | - XCTAssertNotEqual( |
99 | | - Status(code: .ok, message: ""), |
100 | | - Status(code: .ok, message: "message") |
101 | | - ) |
| 51 | + @Test("CustomStringConvertible conformance") |
| 52 | + func customStringConvertible() { |
| 53 | + #expect("\(Status(code: .ok, message: ""))" == #"ok: """#) |
| 54 | + #expect("\(Status(code: .dataLoss, message: "oh no"))" == #"dataLoss: "oh no""#) |
| 55 | + } |
102 | 56 |
|
103 | | - XCTAssertNotEqual( |
104 | | - Status(code: .ok, message: "message"), |
105 | | - Status(code: .internalError, message: "message") |
106 | | - ) |
| 57 | + @Test("Equatable conformance") |
| 58 | + func equatable() { |
| 59 | + let ok = Status(code: .ok, message: "") |
| 60 | + let okWithMessage = Status(code: .ok, message: "message") |
| 61 | + let internalError = Status(code: .internalError, message: "") |
107 | 62 |
|
108 | | - XCTAssertNotEqual( |
109 | | - Status(code: .ok, message: "message"), |
110 | | - Status(code: .ok, message: "different message") |
111 | | - ) |
| 63 | + #expect(ok == ok) |
| 64 | + #expect(ok != okWithMessage) |
| 65 | + #expect(ok != internalError) |
112 | 66 | } |
113 | 67 |
|
114 | | - func testFitsInExistentialContainer() { |
115 | | - XCTAssertLessThanOrEqual(MemoryLayout<Status>.size, 24) |
| 68 | + @Test("Fits in existential container") |
| 69 | + func fitsInExistentialContainer() { |
| 70 | + #expect(MemoryLayout<Status>.size <= 24) |
116 | 71 | } |
117 | 72 | } |
0 commit comments