LocalDateTests.swift 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import XCTest
  15. @testable import FirebaseDataConnect
  16. import Foundation
  17. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  18. final class LocalDateTests: XCTestCase {
  19. override func setUpWithError() throws {}
  20. override func tearDownWithError() throws {}
  21. func testEqualityWithDifferentCreationMethods() throws {
  22. let ldString = try LocalDate(localDateString: "2024-05-14")
  23. let ldComponents = try LocalDate(year: 2024, month: 5, day: 14)
  24. XCTAssertEqual(ldString, ldComponents)
  25. }
  26. func testEqualitySameDayInstances() throws {
  27. let calendar = Calendar(identifier: .gregorian)
  28. let dc = DateComponents(calendar: calendar, year: 2024, month: 6, day: 1, hour: 6, minute: 5)
  29. let date = calendar.date(from: dc)!
  30. let ld1 = LocalDate(date: date)
  31. let date2 = date.addingTimeInterval(72.0) // add 60 seconds. Should be same day
  32. let ld2 = LocalDate(date: date2)
  33. XCTAssertEqual(ld1, ld2)
  34. }
  35. func testLessThan() throws {
  36. let ldLower = try LocalDate(localDateString: "2023-12-29")
  37. let ldHigher = try LocalDate(localDateString: "2024-02-01")
  38. XCTAssertTrue(ldLower < ldHigher)
  39. }
  40. func testInvalidLessThan() throws {
  41. let ldLower = try LocalDate(localDateString: "2023-12-29")
  42. let ldHigher = try LocalDate(localDateString: "2024-02-01")
  43. XCTAssertFalse(ldLower > ldHigher)
  44. }
  45. func testInvalidDateComponents() throws {
  46. XCTAssertThrowsError(try LocalDate(year: 2024, month: 13, day: 45))
  47. }
  48. func testEncodingDecodingJSON() throws {
  49. let ld = try LocalDate(year: 2024, month: 05, day: 14)
  50. let jsonEncoder = JSONEncoder()
  51. let jsonData = try jsonEncoder.encode(ld)
  52. let jsonDecoder = JSONDecoder()
  53. let decodedLocalDate = try jsonDecoder.decode(LocalDate.self, from: jsonData)
  54. XCTAssertEqual(ld, decodedLocalDate)
  55. }
  56. }