Improve tests

This commit is contained in:
Quentin Jin 2019-04-04 14:54:46 +08:00
parent d4f25b6031
commit 3358f38ee9
18 changed files with 355 additions and 186 deletions

View File

@ -21,6 +21,10 @@
/* End PBXAggregateTarget section */
/* Begin PBXBuildFile section */
629FAA6F2255CC2700ED5D67 /* IntervalTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 629FAA6E2255CC2700ED5D67 /* IntervalTests.swift */; };
629FAA712255D14800ED5D67 /* WeekdayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 629FAA702255D14800ED5D67 /* WeekdayTests.swift */; };
629FAA732255D1CA00ED5D67 /* PeriodTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 629FAA722255D1CA00ED5D67 /* PeriodTests.swift */; };
629FAA752255D6B100ED5D67 /* MonthdayTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 629FAA742255D6B100ED5D67 /* MonthdayTests.swift */; };
OBJ_49 /* Atomic.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_9 /* Atomic.swift */; };
OBJ_50 /* Bag.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_10 /* Bag.swift */; };
OBJ_51 /* DeinitObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_11 /* DeinitObserver.swift */; };
@ -38,7 +42,7 @@
OBJ_70 /* Package.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_6 /* Package.swift */; };
OBJ_81 /* AtomicTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_26 /* AtomicTests.swift */; };
OBJ_82 /* BagTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_27 /* BagTests.swift */; };
OBJ_83 /* DateTimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_28 /* DateTimeTests.swift */; };
OBJ_83 /* TimeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_28 /* TimeTests.swift */; };
OBJ_84 /* DeinitObserverTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_29 /* DeinitObserverTests.swift */; };
OBJ_85 /* ExtensionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_30 /* ExtensionsTests.swift */; };
OBJ_86 /* Helpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = OBJ_31 /* Helpers.swift */; };
@ -67,6 +71,10 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
629FAA6E2255CC2700ED5D67 /* IntervalTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IntervalTests.swift; sourceTree = "<group>"; };
629FAA702255D14800ED5D67 /* WeekdayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WeekdayTests.swift; sourceTree = "<group>"; };
629FAA722255D1CA00ED5D67 /* PeriodTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PeriodTests.swift; sourceTree = "<group>"; };
629FAA742255D6B100ED5D67 /* MonthdayTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MonthdayTests.swift; sourceTree = "<group>"; };
OBJ_10 /* Bag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bag.swift; sourceTree = "<group>"; };
OBJ_11 /* DeinitObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeinitObserver.swift; sourceTree = "<group>"; };
OBJ_13 /* Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
@ -82,7 +90,7 @@
OBJ_23 /* Weekday.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Weekday.swift; sourceTree = "<group>"; };
OBJ_26 /* AtomicTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AtomicTests.swift; sourceTree = "<group>"; };
OBJ_27 /* BagTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BagTests.swift; sourceTree = "<group>"; };
OBJ_28 /* DateTimeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DateTimeTests.swift; sourceTree = "<group>"; };
OBJ_28 /* TimeTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TimeTests.swift; sourceTree = "<group>"; };
OBJ_29 /* DeinitObserverTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeinitObserverTests.swift; sourceTree = "<group>"; };
OBJ_30 /* ExtensionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ExtensionsTests.swift; sourceTree = "<group>"; };
OBJ_31 /* Helpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Helpers.swift; sourceTree = "<group>"; };
@ -133,13 +141,17 @@
children = (
OBJ_26 /* AtomicTests.swift */,
OBJ_27 /* BagTests.swift */,
OBJ_28 /* DateTimeTests.swift */,
OBJ_29 /* DeinitObserverTests.swift */,
OBJ_30 /* ExtensionsTests.swift */,
OBJ_31 /* Helpers.swift */,
629FAA6E2255CC2700ED5D67 /* IntervalTests.swift */,
629FAA742255D6B100ED5D67 /* MonthdayTests.swift */,
629FAA722255D1CA00ED5D67 /* PeriodTests.swift */,
OBJ_32 /* PlanTests.swift */,
OBJ_33 /* TaskCenterTests.swift */,
OBJ_34 /* TaskTests.swift */,
OBJ_28 /* TimeTests.swift */,
629FAA702255D14800ED5D67 /* WeekdayTests.swift */,
OBJ_35 /* XCTestManifests.swift */,
);
name = ScheduleTests;
@ -316,12 +328,16 @@
files = (
OBJ_81 /* AtomicTests.swift in Sources */,
OBJ_82 /* BagTests.swift in Sources */,
OBJ_83 /* DateTimeTests.swift in Sources */,
OBJ_83 /* TimeTests.swift in Sources */,
OBJ_84 /* DeinitObserverTests.swift in Sources */,
OBJ_85 /* ExtensionsTests.swift in Sources */,
OBJ_86 /* Helpers.swift in Sources */,
629FAA712255D14800ED5D67 /* WeekdayTests.swift in Sources */,
629FAA6F2255CC2700ED5D67 /* IntervalTests.swift in Sources */,
OBJ_87 /* PlanTests.swift in Sources */,
629FAA732255D1CA00ED5D67 /* PeriodTests.swift in Sources */,
OBJ_88 /* TaskCenterTests.swift in Sources */,
629FAA752255D6B100ED5D67 /* MonthdayTests.swift in Sources */,
OBJ_89 /* TaskTests.swift in Sources */,
OBJ_90 /* XCTestManifests.swift in Sources */,
);

View File

@ -26,10 +26,12 @@
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
codeCoverageEnabled = "YES"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
<TestableReference
skipped = "NO">
skipped = "NO"
parallelizable = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Schedule::ScheduleTests"

View File

@ -19,12 +19,17 @@ extension Int {
}
}
extension Locale {
static let posix = Locale(identifier: "en_US_POSIX")
}
extension Calendar {
/// The gregorian calendar with `en_US_POSIX` locale.
static let gregorian: Calendar = {
var cal = Calendar(identifier: .gregorian)
cal.locale = Locale(identifier: "en_US_POSIX")
cal.locale = Locale.posix
return cal
}()
}

View File

@ -12,7 +12,13 @@ public struct Interval {
}
}
// MARK: - Describing
extension Interval: Hashable {
/// Returns a boolean value indicating whether two intervals are equal.
public static func == (lhs: Interval, rhs: Interval) -> Bool {
return lhs.nanoseconds == rhs.nanoseconds
}
}
extension Interval {
@ -36,21 +42,13 @@ extension Interval {
}
}
extension Interval: Hashable {
/// Returns a boolean value indicating whether two intervals are equal.
public static func == (lhs: Interval, rhs: Interval) -> Bool {
return lhs.nanoseconds == rhs.nanoseconds
}
}
extension Interval: CustomStringConvertible {
/// A textual representation of this interval.
///
/// "Interval: 1000 nanoseconds"
public var description: String {
return "Interval: \(nanoseconds.clampedToInt()) nanoseconds"
return "Interval: \(nanoseconds.clampedToInt()) nanosecond(s)"
}
}
@ -159,11 +157,6 @@ extension Interval {
extension Interval {
/// Creates an interval from the given number of seconds.
public init(seconds: Double) {
self.init(nanoseconds: seconds * pow(10, 9))
}
/// The length of this interval in nanoseconds.
public func asNanoseconds() -> Double {
return nanoseconds
@ -251,7 +244,7 @@ extension IntervalConvertible {
/// Creates an interval from this amount of milliseconds.
public var milliseconds: Interval {
return nanoseconds * pow(10, 6)
return microseconds * pow(10, 3)
}
/// Alias for `second`.
@ -261,7 +254,7 @@ extension IntervalConvertible {
/// Creates an interval from this amount of seconds.
public var seconds: Interval {
return nanoseconds * pow(10, 9)
return milliseconds * pow(10, 3)
}
/// Alias for `minute`.

View File

@ -55,7 +55,7 @@ public enum Monthday {
extension Date {
/// Returns a Boolean value indicating whether this date is the monthday.
/// Returns a Boolean value indicating whether this date is the monthday in current time zone..
public func `is`(_ monthday: Monthday) -> Bool {
let components = monthday.asDateComponents()
@ -79,6 +79,7 @@ extension Monthday: CustomStringConvertible {
let ms = Calendar.gregorian.monthSymbols[m - 1]
let fmt = NumberFormatter()
fmt.locale = Locale.posix
fmt.numberStyle = .ordinal
let ds = fmt.string(from: NSNumber(value: d))!

View File

@ -65,9 +65,7 @@ public struct Period {
for (word, number) in Period.quantifiers.read({ $0 }) {
str = str.replacingOccurrences(of: word, with: "\(number)")
}
guard let regexp = try? NSRegularExpression(pattern: "( and |, )") else {
return nil
}
let regexp = try! NSRegularExpression(pattern: "( and |, )")
str = regexp.stringByReplacingMatches(in: str, range: NSRange(location: 0, length: str.count), withTemplate: "$")
var period = 0.year
@ -109,11 +107,14 @@ public struct Period {
}
/// Returns a new period by adding an interval to the period.
///
/// The return value will be tidied to `day` aotumatically.
public func adding(_ interval: Interval) -> Period {
return Period(
years: years, months: months, days: days,
hours: hours, minutes: minutes, seconds: seconds,
nanoseconds: nanoseconds.clampedAdding(interval.nanoseconds.clampedToInt()))
.tidied(to: .day)
}
/// Returns a new period by adding the right period to the left period.
@ -124,6 +125,8 @@ public struct Period {
}
/// Returns a new period by adding an interval to the period.
///
/// The return value will be tidied to `day` aotumatically.
public static func + (lhs: Period, rhs: Interval) -> Period {
return lhs.adding(rhs)
}

View File

@ -48,7 +48,7 @@ public struct Time {
public init?(_ string: String) {
let pattern = "^(\\d{1,2})(:(\\d{1,2})(:(\\d{1,2})(.(\\d{1,3}))?)?)?( (am|AM|pm|PM))?$"
guard let regexp = try? NSRegularExpression(pattern: pattern, options: []) else { return nil }
let regexp = try! NSRegularExpression(pattern: pattern, options: [])
guard let matches = regexp.matches(in: string, options: [], range: NSRange(location: 0, length: string.count)).first else { return nil }
var hasAM = false

View File

@ -17,7 +17,7 @@ public enum Weekday: Int {
extension Date {
/// Returns a Boolean value indicating whether this date is the weekday.
/// Returns a Boolean value indicating whether this date is the weekday in current time zone.
public func `is`(_ weekday: Weekday) -> Bool {
return Calendar.gregorian.component(.weekday, from: self) == weekday.rawValue
}

View File

@ -12,7 +12,7 @@ final class AtomicTests: XCTestCase {
func testReadVoid() {
let i = Atomic<Int>(1)
var val = 0
i.read { val = $0 }
i.readVoid { val = $0 }
XCTAssertEqual(val, 1)
}
@ -28,7 +28,7 @@ final class AtomicTests: XCTestCase {
func testWriteVoid() {
let i = Atomic<Int>(1)
var val = 0
i.write {
i.writeVoid {
$0 += 1
val = $0
}

View File

@ -47,9 +47,11 @@ final class BagTests: XCTestCase {
let fn1 = bag.removeValue(for: k1)
XCTAssertNotNil(fn1)
XCTAssertNil(bag.value(for: k1))
let fn2 = bag.removeValue(for: k2)
XCTAssertNotNil(fn2)
XCTAssertNil(bag.removeValue(for: k2))
guard let _fn1 = fn1, let _fn2 = fn2 else { return }

View File

@ -1,148 +0,0 @@
import XCTest
@testable import Schedule
final class DateTimeTests: XCTestCase {
func testInterval() {
XCTAssertTrue((-1).second.isNegative)
XCTAssertEqual(1.1.second.abs, 1.1.second)
XCTAssertEqual(1.second.negated, (-1).second)
XCTAssertEqual(7.day.hashValue, 1.week.hashValue)
XCTAssertEqual(7.day, 1.week)
XCTAssertEqual((-2).seconds.compare(1.second), .orderedAscending)
XCTAssertTrue(1.1.second > 1.0.second)
XCTAssertTrue(3.days < 1.week)
XCTAssertTrue(4.day >= 4.days)
XCTAssertTrue(-2.seconds < 1.seconds)
XCTAssertTrue(1.1.second.isLonger(than: 1.0.second))
XCTAssertTrue(3.days.isShorter(than: 1.week))
XCTAssertEqual(1.second * 60, 1.minute)
XCTAssertEqual(59.minutes + 60.seconds, 1.hour)
XCTAssertEqual(1.week - 24.hours, 6.days)
var i0 = 1.day
i0 += 1.day
XCTAssertEqual(i0, 2.days)
XCTAssertEqual(-(1.second), (-1).second)
let i1 = Interval(seconds: 24 * 60 * 60)
XCTAssertEqual(1.nanosecond * i1.asNanoseconds(), 1.day)
XCTAssertEqual(2.microsecond * i1.asMicroseconds(), 2.days)
XCTAssertEqual(3.millisecond * i1.asMilliseconds(), 3.days)
XCTAssertEqual(4.second * i1.asSeconds(), 4.days)
XCTAssertEqual(5.1.minute * i1.asMinutes(), 5.1.days)
XCTAssertEqual(6.2.hour * i1.asHours(), 6.2.days)
XCTAssertEqual(7.3.day * i1.asDays(), 7.3.days)
XCTAssertEqual(1.week * i1.asWeeks(), 1.days)
let date0 = Date()
let date1 = date0.addingTimeInterval(100)
XCTAssertEqual(date0.interval(since: date1), date0.timeIntervalSince(date1).seconds)
XCTAssertEqual(date0.adding(1.seconds), date0.addingTimeInterval(1))
XCTAssertEqual(date0 + 1.seconds, date0.addingTimeInterval(1))
}
func testMonthday() {
let d = Date(year: 2019, month: 1, day: 1)
XCTAssertTrue(d.is(.january(1)))
XCTAssertEqual(Monthday.january(1).asDateComponents().month, 1)
XCTAssertEqual(Monthday.february(1).asDateComponents().month, 2)
XCTAssertEqual(Monthday.march(1).asDateComponents().month, 3)
XCTAssertEqual(Monthday.april(1).asDateComponents().month, 4)
XCTAssertEqual(Monthday.may(1).asDateComponents().month, 5)
XCTAssertEqual(Monthday.june(1).asDateComponents().month, 6)
XCTAssertEqual(Monthday.july(1).asDateComponents().month, 7)
XCTAssertEqual(Monthday.august(1).asDateComponents().month, 8)
XCTAssertEqual(Monthday.september(1).asDateComponents().month, 9)
XCTAssertEqual(Monthday.october(1).asDateComponents().month, 10)
XCTAssertEqual(Monthday.november(1).asDateComponents().month, 11)
XCTAssertEqual(Monthday.december(1).asDateComponents().month, 12)
}
func testPeriod() {
let p0 = (1.year + 2.years + 1.month + 2.months + 3.days).tidied(to: .day)
XCTAssertEqual(p0.years, 3)
XCTAssertEqual(p0.months, 3)
XCTAssertEqual(p0.days, 3)
let p1 = Period("one second")?.tidied(to: .second)
XCTAssertNotNil(p1)
XCTAssertEqual(p1!.seconds, 1)
let p2 = Period("two hours and ten minutes")?.tidied(to: .day)
XCTAssertNotNil(p2)
XCTAssertEqual(p2!.hours, 2)
XCTAssertEqual(p2!.minutes, 10)
let p3 = Period("1 year, 2 months and 3 days")?.tidied(to: .day)
XCTAssertNotNil(p3)
XCTAssertEqual(p3!.years, 1)
XCTAssertEqual(p3!.months, 2)
XCTAssertEqual(p3!.days, 3)
Period.registerQuantifier("many", for: 100 * 1000)
let p4 = Period("many days")
XCTAssertEqual(p4!.days, 100 * 1000)
let date = Date(year: 1989, month: 6, day: 4) + 1.year
let year = date.dateComponents.year
XCTAssertEqual(year, 1990)
let p5 = Period(hours: 25).tidied(to: .day)
XCTAssertEqual(p5.days, 1)
}
func testTime() {
let t0 = Time(hour: -1, minute: -2, second: -3, nanosecond: -4)
XCTAssertNil(t0)
let t1 = Time("11:12:13.456")
XCTAssertNotNil(t1)
XCTAssertEqual(t1?.hour, 11)
XCTAssertEqual(t1?.minute, 12)
XCTAssertEqual(t1?.second, 13)
if let i = t1?.nanosecond.nanoseconds {
XCTAssertTrue(i.isAlmostEqual(to: (0.456.second.nanoseconds).nanoseconds, leeway: 0.001.seconds))
}
let components = t1?.asDateComponents()
XCTAssertEqual(components?.hour, 11)
XCTAssertEqual(components?.minute, 12)
XCTAssertEqual(components?.second, 13)
if let i = components?.nanosecond?.nanoseconds {
XCTAssertTrue(i.isAlmostEqual(to: (0.456.second.nanoseconds).nanoseconds, leeway: 0.001.seconds))
}
let t2 = Time("11 pm")
XCTAssertNotNil(t2)
XCTAssertEqual(t2?.hour, 23)
let t3 = Time("12 am")
XCTAssertNotNil(t3)
XCTAssertEqual(t3?.hour, 0)
let t4 = Time("schedule")
XCTAssertNil(t4)
XCTAssertEqual(Time(hour: 1)!.intervalSinceStartOfDay, 1.hour)
}
func testWeekday() {
// Be careful the time zone problem.
let d = Date(year: 2019, month: 1, day: 1)
XCTAssertTrue(d.is(.tuesday))
XCTAssertEqual(Weekday.monday.asDateComponents().weekday!, 2)
}
static var allTests = [
("testInterval", testInterval),
("testMonthday", testMonthday),
("testPeriod", testPeriod),
("testTime", testTime),
("testWeekday", testWeekday)
]
}

View File

@ -64,7 +64,7 @@ extension Plan {
extension DispatchQueue {
func async(after interval: Interval, execute body: @escaping () -> Void) {
asyncAfter(wallDeadline: .now() + interval.asSeconds(), execute: body)
asyncAfter(deadline: .now() + interval.asSeconds(), execute: body)
}
static func `is`(_ queue: DispatchQueue) -> Bool {

View File

@ -0,0 +1,110 @@
//
// IntervalTests.swift
// ScheduleTests
//
// Created by Quentin MED on 2019/4/4.
//
import XCTest
@testable import Schedule
final class IntervalTests: XCTestCase {
private let leeway = 0.01.second
func testEquatable() {
XCTAssertEqual(1.second, 1.second)
XCTAssertEqual(1.week, 7.days)
}
func testIsNegative() {
XCTAssertFalse(1.second.isNegative)
XCTAssertTrue((-1).second.isNegative)
}
func testAbs() {
XCTAssertEqual(1.second, (-1).second.abs)
}
func testNegated() {
XCTAssertEqual(1.second.negated, (-1).second)
XCTAssertEqual(1.second.negated.negated, 1.second)
}
func testCompare() {
XCTAssertEqual((-1).second.compare(1.second), ComparisonResult.orderedAscending)
XCTAssertEqual(8.days.compare(1.week), ComparisonResult.orderedDescending)
XCTAssertTrue(23.hours < 1.day)
XCTAssertTrue(25.hours > 1.day)
}
func testLongerShorter() {
XCTAssertTrue((-25).hour.isLonger(than: 1.day))
XCTAssertTrue(1.week.isShorter(than: 8.days))
}
func testMultiplying() {
XCTAssertEqual(7.days * 2, 2.week)
}
func testAdding() {
XCTAssertEqual(6.days + 1.day, 1.week)
XCTAssertEqual(1.1.weeks, 1.week + 0.1.weeks)
}
func testOperators() {
XCTAssertEqual(1.week - 6.days, 1.day)
var i = 6.days
i += 1.day
XCTAssertEqual(i, 1.week)
XCTAssertEqual(-(7.days), (-1).week)
}
func testAs() {
XCTAssertEqual(1.millisecond.asNanoseconds(), 1.microsecond.asNanoseconds() * pow(10, 3))
XCTAssertEqual(1.second.asNanoseconds(), pow(10, 9))
XCTAssertEqual(1.second.asMicroseconds(), pow(10, 6))
XCTAssertEqual(1.second.asMilliseconds(), pow(10, 3))
XCTAssertEqual(1.minute.asSeconds(), 60)
XCTAssertEqual(1.hour.asMinutes(), 60)
XCTAssertEqual(1.day.asHours(), 24)
XCTAssertEqual(1.week.asDays(), 7)
XCTAssertEqual(7.days.asWeeks(), 1)
}
func testDate() {
let date0 = Date()
let date1 = date0.addingTimeInterval(100)
XCTAssertTrue(date1.intervalSinceNow.isAlmostEqual(to: 100.seconds, leeway: leeway))
XCTAssertEqual(date0.interval(since: date1), date0.timeIntervalSince(date1).seconds)
XCTAssertEqual(date0.adding(1.seconds), date0.addingTimeInterval(1))
XCTAssertEqual(date0 + 1.seconds, date0.addingTimeInterval(1))
}
func testDescription() {
XCTAssertEqual(1.nanosecond.debugDescription, "Interval: 1 nanosecond(s)")
}
static var allTests = [
("testEquatable", testEquatable),
("testIsNegative", testIsNegative),
("testAbs", testAbs),
("testNegated", testNegated),
("testCompare", testCompare),
("testLongerShorter", testLongerShorter),
("testMultiplying", testMultiplying),
("testAdding", testAdding),
("testOperators", testOperators),
("testAs", testAs),
("testDate", testDate),
]
}

View File

@ -0,0 +1,28 @@
import XCTest
@testable import Schedule
final class MonthdayTests: XCTestCase {
func testIs() {
// ! Be careful the time zone problem.
let d = Date(year: 2019, month: 1, day: 1)
XCTAssertTrue(d.is(.january(1)))
}
func testAsDateComponents() {
let comps = Monthday.april(1).asDateComponents()
XCTAssertEqual(comps.month, 4)
XCTAssertEqual(comps.day, 1)
}
func testDescription() {
let md = Monthday.april(1)
XCTAssertEqual(md.debugDescription, "Monthday: April 1st")
}
static var allTests = [
("testIs", testIs),
("testAsDateComponents", testAsDateComponents),
("testDescription", testDescription)
]
}

View File

@ -0,0 +1,76 @@
import XCTest
@testable import Schedule
final class PeriodTests: XCTestCase {
func testPeriod() {
let period = (1.year + 2.years + 1.month + 2.months + 3.days)
XCTAssertEqual(period.years, 3)
XCTAssertEqual(period.months, 3)
XCTAssertEqual(period.days, 3)
}
func testInitWithString() {
let p1 = Period("one second")
XCTAssertNotNil(p1)
XCTAssertEqual(p1!.seconds, 1)
let p2 = Period("two hours and ten minutes")
XCTAssertNotNil(p2)
XCTAssertEqual(p2!.hours, 2)
XCTAssertEqual(p2!.minutes, 10)
let p3 = Period("1 year, 2 months and 3 days")
XCTAssertNotNil(p3)
XCTAssertEqual(p3!.years, 1)
XCTAssertEqual(p3!.months, 2)
XCTAssertEqual(p3!.days, 3)
Period.registerQuantifier("many", for: 100 * 1000)
let p4 = Period("many days")
XCTAssertEqual(p4!.days, 100 * 1000)
}
func testAdd() {
XCTAssertEqual(1.month.adding(1.month).months, 2)
XCTAssertEqual(Period(days: 1).adding(1.day).days, 2)
}
func testTidy() {
let period = 1.month.adding(25.hour).tidied(to: .day)
XCTAssertEqual(period.days, 1)
}
func testAsDateComponents() {
let period = Period(years: 1, months: 2, days: 3, hours: 4, minutes: 5, seconds: 6, nanoseconds: 7)
let comps = period.asDateComponents()
XCTAssertEqual(comps.year, 1)
XCTAssertEqual(comps.month, 2)
XCTAssertEqual(comps.day, 3)
XCTAssertEqual(comps.hour, 4)
XCTAssertEqual(comps.minute, 5)
XCTAssertEqual(comps.second, 6)
XCTAssertEqual(comps.nanosecond, 7)
}
func testDate() {
let d = Date(year: 1989, month: 6, day: 4) + 1.year
let year = d.dateComponents.year
XCTAssertEqual(year, 1990)
}
func testDescription() {
let period = Period(years: 1, nanoseconds: 1)
XCTAssertEqual(period.debugDescription, "Period: 1 year(s) 1 nanosecond(s)")
}
static var allTests = [
("testPeriod", testPeriod),
("testInitWithString", testInitWithString),
("testAdd", testAdd),
("testTidy", testTidy),
("testAsDateComponents", testAsDateComponents),
("testDate", testDate),
("testDescription", testDescription)
]
}

View File

@ -0,0 +1,52 @@
import XCTest
@testable import Schedule
final class TimeTests: XCTestCase {
func testTime() {
let t1 = Time("11:12:13.456")
XCTAssertNotNil(t1)
XCTAssertEqual(t1?.hour, 11)
XCTAssertEqual(t1?.minute, 12)
XCTAssertEqual(t1?.second, 13)
if let i = t1?.nanosecond.nanoseconds {
XCTAssertTrue(i.isAlmostEqual(to: 0.456.second, leeway: 0.001.seconds))
}
let t2 = Time("11 pm")
XCTAssertNotNil(t2)
XCTAssertEqual(t2?.hour, 23)
let t3 = Time("12 am")
XCTAssertNotNil(t3)
XCTAssertEqual(t3?.hour, 0)
let t4 = Time("schedule")
XCTAssertNil(t4)
}
func testIntervalSinceStartOfDay() {
XCTAssertEqual(Time(hour: 1)!.intervalSinceStartOfDay, 1.hour)
}
func testAsDateComponents() {
let time = Time(hour: 11, minute: 12, second: 13, nanosecond: 456)
let components = time?.asDateComponents()
XCTAssertEqual(components?.hour, 11)
XCTAssertEqual(components?.minute, 12)
XCTAssertEqual(components?.second, 13)
XCTAssertEqual(components?.nanosecond, 456)
}
func testDescription() {
let time = Time("11:12:13.456")
XCTAssertEqual(time!.debugDescription, "Time: 11:12:13.456")
}
static var allTests = [
("testTime", testTime),
("testIntervalSinceStartOfDay", testIntervalSinceStartOfDay),
("testAsDateComponents", testAsDateComponents),
("testDescription", testDescription)
]
}

View File

@ -0,0 +1,26 @@
import XCTest
@testable import Schedule
final class WeekdayTests: XCTestCase {
func testIs() {
// ! Be careful the time zone problem.
let d = Date(year: 2019, month: 1, day: 1)
XCTAssertTrue(d.is(.tuesday))
}
func testAsDateComponents() {
XCTAssertEqual(Weekday.monday.asDateComponents().weekday!, 2)
}
func testDescription() {
let wd = Weekday.tuesday
XCTAssertEqual(wd.debugDescription, "Weekday: Tuesday")
}
static var allTests = [
("testIs", testIs),
("testAsDateComponents", testAsDateComponents),
("testDescription", testDescription)
]
}

View File

@ -3,14 +3,17 @@ import XCTest
#if os(Linux)
public func allTests() -> [XCTestCaseEntry] {
return [
testCase(DateTimeTests.allTests),
testCase(AtomicTests.allTests),
testCase(BagTests.allTests),
testCase(ExtensionsTests.allTests),
testCase(IntervalTests.allTests),
testCase(MonthdayTests.allTests),
testCase(PeriodTests.allTests),
testCase(PlanTests.allTests),
testCase(TaskCenterTests.allTests),
testCase(TaskTests.allTests),
testCase(AtomicTests.allTests),
testCase(BagTests.allTests),
testCase(CalendarTests.allTests),
testCase(ExtensionsTests.allTests)
testCase(TimeTests.allTests),
testCase(WeekdayTests.allTests)
]
}
#endif