Skip to content

Commit

Permalink
Fix DateFormattingTests
Browse files Browse the repository at this point in the history
  • Loading branch information
Pietro Caselani committed Dec 17, 2019
1 parent 4705706 commit 8a735c2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
28 changes: 28 additions & 0 deletions CouchTrackerCore/Core/DateFormatting.swift
@@ -0,0 +1,28 @@
import Foundation

public enum DateFormatting {
public static func shortString(
localeProvider: () -> Locale = { DefaultBundleProvider.instance.currentLanguage.asLocale },
timeZoneProvider: () -> TimeZone = { NSTimeZone.system },
date: Date
) -> String {
let dateFormatter = DateFormatter()
dateFormatter.locale = localeProvider()
dateFormatter.dateFormat = CouchTrackerCoreStrings.shortDateFormat()
dateFormatter.timeZone = timeZoneProvider()
return dateFormatter.string(from: date)
}
}

extension Date {
public func shortString(
localeProvider: () -> Locale = { DefaultBundleProvider.instance.currentLanguage.asLocale },
timeZoneProvider: () -> TimeZone = { NSTimeZone.system }
) -> String {
return DateFormatting.shortString(
localeProvider: localeProvider,
timeZoneProvider: timeZoneProvider,
date: self
)
}
}
11 changes: 0 additions & 11 deletions CouchTrackerCore/Extensions/Date+Presentable.swift

This file was deleted.

2 changes: 1 addition & 1 deletion CouchTrackerCore/Localization/SupportedLanguages.swift
Expand Up @@ -4,7 +4,7 @@ public enum SupportedLanguages: String, Hashable, CaseIterable {
case englishUS = "en_US"
case portugueseBR = "pt_BR"

var asLocale: Locale {
public var asLocale: Locale {
return Locale(identifier: rawValue)
}
}
@@ -1,9 +1,11 @@
@testable import CouchTrackerCore
import Foundation
import XCTest

final class DatePresentableTests: XCTestCase {
final class DateFormattingTests: XCTestCase {
private var userDefaults: UserDefaults!
private var oldLanguage: SupportedLanguages!
private let timeZone = { TimeZone(identifier: "America/Sao_Paulo")! }

override func setUp() {
super.setUp()
Expand All @@ -24,7 +26,7 @@ final class DatePresentableTests: XCTestCase {

let date = Date(timeIntervalSince1970: 1_557_187_200) // 2019-05-07 00:00:00 +0000
let expectedString = "06 de maio"
XCTAssertEqual(date.shortString(), expectedString)
XCTAssertEqual(date.shortString(timeZoneProvider: timeZone), expectedString)
}

func testDatePresentable_inEnglishUS() {
Expand All @@ -34,6 +36,6 @@ final class DatePresentableTests: XCTestCase {

let date = Date(timeIntervalSince1970: 1_557_187_200) // 2019-05-07 00:00:00 +0000
let expectedString = "May 06"
XCTAssertEqual(date.shortString(), expectedString)
XCTAssertEqual(date.shortString(timeZoneProvider: timeZone), expectedString)
}
}

0 comments on commit 8a735c2

Please sign in to comment.