Error+EquatableTests.swift 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright 2022 Google LLC
  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. @testable import FirebaseSessions
  16. import XCTest
  17. /// This file and extension exist for ease of testing. Without this, you cannot use
  18. /// XCTAssertEqual on errors, which means you need to do switches whenever you
  19. /// want to assert on errors, which is clunky.
  20. ///
  21. /// This class exists for unit testing purposes only. The SDK should use switch internally
  22. /// when handling errors, because equating errors is prone to issues (eg. we're just comparing
  23. /// the types, but not the values).
  24. extension FirebaseSessionsError: Swift.Equatable {
  25. public static func == (lhs: FirebaseSessions.FirebaseSessionsError,
  26. rhs: FirebaseSessions.FirebaseSessionsError) -> Bool {
  27. return String(reflecting: lhs) == String(reflecting: rhs)
  28. }
  29. }
  30. enum FakeError: Error {
  31. case Fake
  32. }
  33. final class ErrorEquatableTests: XCTestCase {
  34. func test_equalErrorTypes_areEqual() throws {
  35. let fakeError = FakeError.Fake
  36. let errs: [FirebaseSessionsError] = [
  37. .DataCollectionError,
  38. .SessionSamplingError,
  39. .SessionInstallationsError(fakeError),
  40. .DisabledViaSettingsError,
  41. .DataTransportError(fakeError),
  42. ]
  43. let errs2: [FirebaseSessionsError] = [
  44. .DataCollectionError,
  45. .SessionSamplingError,
  46. .SessionInstallationsError(fakeError),
  47. .DisabledViaSettingsError,
  48. .DataTransportError(fakeError),
  49. ]
  50. for (i, err) in errs.enumerated() {
  51. XCTAssertEqual(err, errs2[i])
  52. }
  53. }
  54. func test_unequalErrorTypes_areNotEqual() throws {
  55. let fakeError = FakeError.Fake
  56. let errs: [FirebaseSessionsError] = [
  57. .DataCollectionError,
  58. .SessionSamplingError,
  59. .SessionInstallationsError(fakeError),
  60. .DisabledViaSettingsError,
  61. .DataTransportError(fakeError),
  62. ]
  63. // errs2 is off by one from errs so none of the elements match
  64. let errsOffByOne: [FirebaseSessionsError] = [
  65. .DataTransportError(fakeError),
  66. .DataCollectionError,
  67. .SessionSamplingError,
  68. .SessionInstallationsError(fakeError),
  69. .DisabledViaSettingsError,
  70. ]
  71. for (i, err) in errs.enumerated() {
  72. XCTAssertNotEqual(err, errsOffByOne[i])
  73. }
  74. }
  75. }