StatusTests.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPCCore
  17. import XCTest
  18. final class StatusTests: XCTestCase {
  19. private static let statusCodeRawValue: [(Status.Code, Int)] = [
  20. (.ok, 0),
  21. (.cancelled, 1),
  22. (.unknown, 2),
  23. (.invalidArgument, 3),
  24. (.deadlineExceeded, 4),
  25. (.notFound, 5),
  26. (.alreadyExists, 6),
  27. (.permissionDenied, 7),
  28. (.resourceExhausted, 8),
  29. (.failedPrecondition, 9),
  30. (.aborted, 10),
  31. (.outOfRange, 11),
  32. (.unimplemented, 12),
  33. (.internalError, 13),
  34. (.unavailable, 14),
  35. (.dataLoss, 15),
  36. (.unauthenticated, 16),
  37. ]
  38. func testCustomStringConvertible() {
  39. XCTAssertDescription(Status(code: .ok, message: ""), #"ok: """#)
  40. XCTAssertDescription(Status(code: .dataLoss, message: "message"), #"dataLoss: "message""#)
  41. XCTAssertDescription(Status(code: .unknown, message: "message"), #"unknown: "message""#)
  42. XCTAssertDescription(Status(code: .aborted, message: "message"), #"aborted: "message""#)
  43. }
  44. func testStatusCodeRawValues() {
  45. for (code, expected) in Self.statusCodeRawValue {
  46. XCTAssertEqual(code.rawValue, expected, "\(code) had unexpected raw value")
  47. }
  48. }
  49. func testStatusCodeFromErrorCode() throws {
  50. XCTAssertEqual(Status.Code(RPCError.Code.cancelled), .cancelled)
  51. XCTAssertEqual(Status.Code(RPCError.Code.unknown), .unknown)
  52. XCTAssertEqual(Status.Code(RPCError.Code.invalidArgument), .invalidArgument)
  53. XCTAssertEqual(Status.Code(RPCError.Code.deadlineExceeded), .deadlineExceeded)
  54. XCTAssertEqual(Status.Code(RPCError.Code.notFound), .notFound)
  55. XCTAssertEqual(Status.Code(RPCError.Code.alreadyExists), .alreadyExists)
  56. XCTAssertEqual(Status.Code(RPCError.Code.permissionDenied), .permissionDenied)
  57. XCTAssertEqual(Status.Code(RPCError.Code.resourceExhausted), .resourceExhausted)
  58. XCTAssertEqual(Status.Code(RPCError.Code.failedPrecondition), .failedPrecondition)
  59. XCTAssertEqual(Status.Code(RPCError.Code.aborted), .aborted)
  60. XCTAssertEqual(Status.Code(RPCError.Code.outOfRange), .outOfRange)
  61. XCTAssertEqual(Status.Code(RPCError.Code.unimplemented), .unimplemented)
  62. XCTAssertEqual(Status.Code(RPCError.Code.internalError), .internalError)
  63. XCTAssertEqual(Status.Code(RPCError.Code.unavailable), .unavailable)
  64. XCTAssertEqual(Status.Code(RPCError.Code.dataLoss), .dataLoss)
  65. XCTAssertEqual(Status.Code(RPCError.Code.unauthenticated), .unauthenticated)
  66. }
  67. func testStatusCodeFromValidRawValue() {
  68. for (expected, rawValue) in Self.statusCodeRawValue {
  69. XCTAssertEqual(
  70. Status.Code(rawValue: rawValue),
  71. expected,
  72. "\(rawValue) didn't convert to expected code \(expected)"
  73. )
  74. }
  75. }
  76. func testStatusCodeFromInvalidRawValue() {
  77. // Internally represented as a `UInt8`; try all other values.
  78. for rawValue in UInt8(17) ... UInt8.max {
  79. XCTAssertNil(Status.Code(rawValue: Int(rawValue)))
  80. }
  81. // API accepts `Int` so try invalid `Int` values too.
  82. XCTAssertNil(Status.Code(rawValue: -1))
  83. XCTAssertNil(Status.Code(rawValue: 1000))
  84. XCTAssertNil(Status.Code(rawValue: .max))
  85. }
  86. func testEquatableConformance() {
  87. XCTAssertEqual(Status(code: .ok, message: ""), Status(code: .ok, message: ""))
  88. XCTAssertEqual(Status(code: .ok, message: "message"), Status(code: .ok, message: "message"))
  89. XCTAssertNotEqual(
  90. Status(code: .ok, message: ""),
  91. Status(code: .ok, message: "message")
  92. )
  93. XCTAssertNotEqual(
  94. Status(code: .ok, message: "message"),
  95. Status(code: .internalError, message: "message")
  96. )
  97. XCTAssertNotEqual(
  98. Status(code: .ok, message: "message"),
  99. Status(code: .ok, message: "different message")
  100. )
  101. }
  102. func testFitsInExistentialContainer() {
  103. XCTAssertLessThanOrEqual(MemoryLayout<Status>.size, 24)
  104. }
  105. }