From 61f52a04f7a06c7c8767b8d57405c34ac490fe28 Mon Sep 17 00:00:00 2001 From: Dmitry Date: Sun, 23 Apr 2023 18:27:44 +0300 Subject: [PATCH] fix Date.between(_ from: _ to:) --- Sources/Fakery/Faker.swift | 4 ++-- Sources/Fakery/Generators/Date.swift | 10 +++++++++- Sources/Fakery/Generators/Generator.swift | 2 +- Tests/Fakery/Generators/DateSpec.swift | 2 +- Tests/Fakery/Generators/NumberSpec.swift | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/Sources/Fakery/Faker.swift b/Sources/Fakery/Faker.swift index 58e912b..17c63a6 100644 --- a/Sources/Fakery/Faker.swift +++ b/Sources/Fakery/Faker.swift @@ -50,9 +50,9 @@ public final class Faker { name = Name(parser: parser) phoneNumber = PhoneNumber(parser: parser) team = Team(parser: parser) - number = Number() + number = Number(parser: parser) bank = Bank(parser: parser) - date = Date() + date = Date(parser: parser) hobbit = Hobbit(parser: parser) car = Car(parser: parser) programmingLanguage = ProgrammingLanguage(parser: parser) diff --git a/Sources/Fakery/Generators/Date.swift b/Sources/Fakery/Generators/Date.swift index 07d940c..3c9029f 100644 --- a/Sources/Fakery/Generators/Date.swift +++ b/Sources/Fakery/Generators/Date.swift @@ -2,6 +2,14 @@ import Foundation extension Faker { public final class Date: Generator { + + private let number: Number + + public required init(parser: Parser) { + self.number = Number(parser: parser) + super.init(parser: parser) + } + public func backward(days: Int) -> Foundation.Date { return todayAddingDays(-days) } @@ -13,7 +21,7 @@ extension Faker { public func between(_ from: Foundation.Date, _ to: Foundation.Date) -> Foundation.Date { let fromInSeconds = from.timeIntervalSince1970 let toInSeconds = to.timeIntervalSince1970 - let targetInSeconds = Number().randomDouble(min: fromInSeconds, max: toInSeconds) + let targetInSeconds = number.randomDouble(min: fromInSeconds, max: toInSeconds) return Foundation.Date(timeIntervalSince1970: targetInSeconds) } diff --git a/Sources/Fakery/Generators/Generator.swift b/Sources/Fakery/Generators/Generator.swift index 1d681ec..1efdbbd 100644 --- a/Sources/Fakery/Generators/Generator.swift +++ b/Sources/Fakery/Generators/Generator.swift @@ -12,7 +12,7 @@ extension Faker { let parser: Parser let dateFormatter: DateFormatter - public required init(parser: Parser = Parser()) { + public required init(parser: Parser) { self.parser = parser dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyyy-MM-dd" diff --git a/Tests/Fakery/Generators/DateSpec.swift b/Tests/Fakery/Generators/DateSpec.swift index 461ce29..44ce6a5 100644 --- a/Tests/Fakery/Generators/DateSpec.swift +++ b/Tests/Fakery/Generators/DateSpec.swift @@ -9,7 +9,7 @@ final class DateSpec: QuickSpec { var date: Faker.Date! beforeEach { - date = Faker.Date() + date = Faker.Date(parser: Parser()) } describe("#between") { diff --git a/Tests/Fakery/Generators/NumberSpec.swift b/Tests/Fakery/Generators/NumberSpec.swift index c3392e5..d7283dc 100644 --- a/Tests/Fakery/Generators/NumberSpec.swift +++ b/Tests/Fakery/Generators/NumberSpec.swift @@ -8,7 +8,7 @@ final class NumberSpec: QuickSpec { var number: Faker.Number! beforeEach { - number = Faker.Number() + number = Faker.Number(parser: Parser()) } it("creates contionusly increasing ids") {