Skip to content

Commit

Permalink
Tests: added tests for ClockType (#3029)
Browse files Browse the repository at this point in the history
  • Loading branch information
NachoSoto authored Aug 17, 2023
1 parent 75c0d03 commit 3245077
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Sources/Misc/DateAndTime/Clock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ extension ClockType {
}
}

func durationSince(_ date: Date) -> TimeInterval {
if #available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *) {
return date.distance(to: self.now)
} else {
return date.timeIntervalSince(self.now)
}
}

}
57 changes: 57 additions & 0 deletions Tests/UnitTests/Misc/ClockTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// ClockTests.swift
//
// Created by Nacho Soto on 8/16/23.

import Nimble
@testable import RevenueCat
import XCTest

class ClockTests: TestCase {

private var startDate: Date!
private var startTime: DispatchTime!
private var clock: TestClock!

override func setUp() {
super.setUp()

self.startDate = Date()
self.startTime = .now()

self.clock = .init(now: self.startDate, currentTime: self.startTime)
}

func testDurationSinceDispatchTimeWithNoTime() {
expect(self.clock.durationSince(self.startTime)).to(beCloseTo(0))
}

func testDurationSinceDispatchTime() {
self.clock.advance(by: .seconds(5))
expect(self.clock.durationSince(self.startTime)).to(beCloseTo(5))

self.clock.advance(by: .seconds(-10))
expect(self.clock.durationSince(self.startTime)).to(beCloseTo(-5))
}

func testDurationSinceDateWithNoTime() {
expect(self.clock.durationSince(self.startDate)).to(beCloseTo(0))
}

func testDurationSinceDate() {
self.clock.advance(by: .seconds(5))
expect(self.clock.durationSince(self.startDate)).to(beCloseTo(5))

self.clock.advance(by: .seconds(-10))
expect(self.clock.durationSince(self.startDate)).to(beCloseTo(-5))
}

}

0 comments on commit 3245077

Please sign in to comment.