StatusTests.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 testStatusCodeFromValidRawValue() {
  50. for (expected, rawValue) in Self.statusCodeRawValue {
  51. XCTAssertEqual(
  52. Status.Code(rawValue: rawValue),
  53. expected,
  54. "\(rawValue) didn't convert to expected code \(expected)"
  55. )
  56. }
  57. }
  58. func testStatusCodeFromInvalidRawValue() {
  59. // Internally represented as a `UInt8`; try all other values.
  60. for rawValue in UInt8(17) ... UInt8.max {
  61. XCTAssertNil(Status.Code(rawValue: Int(rawValue)))
  62. }
  63. // API accepts `Int` so try invalid `Int` values too.
  64. XCTAssertNil(Status.Code(rawValue: -1))
  65. XCTAssertNil(Status.Code(rawValue: 1000))
  66. XCTAssertNil(Status.Code(rawValue: .max))
  67. }
  68. func testEquatableConformance() {
  69. XCTAssertEqual(Status(code: .ok, message: ""), Status(code: .ok, message: ""))
  70. XCTAssertEqual(Status(code: .ok, message: "message"), Status(code: .ok, message: "message"))
  71. XCTAssertNotEqual(
  72. Status(code: .ok, message: ""),
  73. Status(code: .ok, message: "message")
  74. )
  75. XCTAssertNotEqual(
  76. Status(code: .ok, message: "message"),
  77. Status(code: .internalError, message: "message")
  78. )
  79. XCTAssertNotEqual(
  80. Status(code: .ok, message: "message"),
  81. Status(code: .ok, message: "different message")
  82. )
  83. }
  84. func testFitsInExistentialContainer() {
  85. XCTAssertLessThanOrEqual(MemoryLayout<Status>.size, 24)
  86. }
  87. }