//
//  Tests_iOS.swift
//  Tests iOS
//
//  Created by Trey Tartt on 1/10/22.
//

import XCTest

// Local copy — UI test target cannot @testable import Reflect
private extension Date {
    static func dates(from fromDate: Date, toDate: Date, includingToDate: Bool = false) -> [Date] {
        var dates: [Date] = []
        var date = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: fromDate)!
        let toDate = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: toDate)!

        if includingToDate {
            while date <= toDate {
                dates.append(date)
                guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
                date = newDate
            }
        } else {
            while date < toDate {
                dates.append(date)
                guard let newDate = Calendar.current.date(byAdding: .day, value: 1, to: date) else { break }
                date = newDate
            }
        }

        return dates
    }
}

class Tests_iOS: XCTestCase {
    override func setUpWithError() throws {
        continueAfterFailure = false
    }

    override func tearDownWithError() throws {
    }

    func testDatesBetween() {
        let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
        let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: today)!
        let tenDaysAgo = Calendar.current.date(byAdding: .day, value: -10, to: today)!

        let dates = Date.dates(from: Calendar.current.date(byAdding: .day, value: -10, to: Date())!, toDate: Date())

        XCTAssertTrue(dates.last == yesterday)
        XCTAssertTrue(dates.first == tenDaysAgo)
    }

    func testDatesIncluding() {
        let today = Calendar.current.date(bySettingHour: 0, minute: 0, second: 0, of: Date())!
        let tenDaysAgo = Calendar.current.date(byAdding: .day, value: -10, to: today)!

        let dates = Date.dates(from: Calendar.current.date(byAdding: .day, value: -10, to: Date())!, toDate: Date(), includingToDate: true)

        XCTAssertTrue(dates.last == today)
        XCTAssertTrue(dates.first == tenDaysAgo)
    }
}
