Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftPM 5.3 support with resource bundle and watchOS support #294

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ DerivedData

#CocoaPods
Pods

#swiftpm
.swiftpm
*.xcodeproj
25 changes: 20 additions & 5 deletions DateToolsSwift/DateTools/Date+Bundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,25 @@
import Foundation

public extension Bundle {

class func dateToolsBundle() -> Bundle {
let assetPath = Bundle(for: Constants.self).resourcePath!
return Bundle(path: NSString(string: assetPath).appendingPathComponent("DateTools.bundle"))!
}

class func dateToolsBundle() -> Bundle {
let containerBundle: Bundle

#if SWIFT_PACKAGE
containerBundle = Bundle.module
#else
containerBundle = Bundle(for: Constants.self)
#endif

guard
let dateToolsBundleURL = containerBundle.url(forResource: "DateTools", withExtension: "bundle"),
let dateToolsBundle = Bundle(url: dateToolsBundleURL)
else {
assertionFailure("Make sure you have included DateTools.bundle in your app.")
return containerBundle
}

return dateToolsBundle
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class DateComparatorsTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class DateComponentsTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class DateFormatTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class DateDateToolsTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class DateManipulationsTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class DateTimeAgoTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class IntegerTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,29 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift


class TimeAgoTests : XCTestCase {

var formatter: DateFormatter?
var date0: Date?
var date1: Date?
let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy MM dd HH:mm:ss.SSS"
return formatter
}()
var date0 = Date()
var date1 = Date()

override func setUp() {
super.up = nil
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
self.formatter = DateFormatter()
self.formatter?.dateFormat = "yyyy MM dd HH:mm:ss.SSS"
self.date0 = self.formatter?.date(from: "2014 11 05 18:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")

do {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 05 18:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
} catch {
XCTFail("Failed to format dates from string.")
}
}

override func tearDown() {
Expand All @@ -31,112 +38,126 @@ class TimeAgoTests : XCTestCase {
}

func testBasicLongTimeAgo() {
var now: String = self.date0.timeAgoSinceDate(self.date0)
XCTAssert(now && now.length > 0, "'Now' is nil or empty.")
var ago: String = self.date1.timeAgoSinceDate(self.date0)
XCTAssert(ago && ago.length > 0, "Ago is nil or empty.")
let now: String = self.date0.timeAgo(since: self.date0)
XCTAssertFalse(now.isEmpty, "'Now' is empty.")
let ago: String = self.date1.timeAgo(since: self.date0)
XCTAssertFalse(ago.isEmpty, "Ago is empty.")
}

func testLongTimeAgo2Days() {
self.date0 = self.formatter?.date(from: "2014 11 05 18:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.timeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("2 days ago"))
func testLongTimeAgo2Days() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 05 18:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.timeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("2 days ago"))
}

func testLongTimeAgo1DayAndHalf() {
self.date0 = self.formatter?.date(from: "2014 11 06 9:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.timeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("Yesterday"))
func testLongTimeAgo1DayAndHalf() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 06 9:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.timeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("Yesterday"))
}

func testLongTimeAgoExactlyYesterday() {
self.date0 = self.formatter?.date(from: "2014 11 06 18:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.timeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("Yesterday"))
func testLongTimeAgoExactlyYesterday() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 06 18:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.timeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("Yesterday"))
}

func testLongTimeAgoLessThan24hoursButYesterday() {
self.date0 = self.formatter?.date(from: "2014 11 06 20:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.timeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("22 hours ago"))
func testLongTimeAgoLessThan24hoursButYesterday() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 00:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 22:15:12.000"))
let ago: String = self.date0.timeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("22 hours ago"))
}

func testLongTimeAgoLessThan24hoursSameDay() {
self.date0 = self.formatter?.date(from: "2014 11 07 10:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.timeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("8 hours ago"))
func testLongTimeAgoLessThan24hoursSameDay() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 10:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.timeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("8 hours ago"))
}

func testLongTimeAgoBetween24And48Hours() {
self.date0 = self.formatter?.date(from: "2014 11 07 10:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 08 18:15:12.000")
var ago: String = self.date0.timeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("Yesterday"))
func testLongTimeAgoBetween24And48Hours() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 10:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 08 18:15:12.000"))
let ago: String = self.date0.timeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("Yesterday"))
}

func testBasicShortTimeAgo() {
var now: String = self.date0.shortTimeAgoSinceDate(self.date0)
XCTAssert(now && now.length > 0, "'Now' is nil or empty.")
var ago: String = self.date1.shortTimeAgoSinceDate(self.date0)
XCTAssert(ago && ago.length > 0, "Ago is nil or empty.")
let now: String = self.date0.shortTimeAgo(since: self.date0)
XCTAssertFalse(now.isEmpty, "'Now' is empty.")
let ago: String = self.date1.shortTimeAgo(since: self.date0)
XCTAssertFalse(ago.isEmpty, "Ago is empty.")
}

func testShortTimeAgo2Days() {
self.date0 = self.formatter?.date(from: "2014 11 05 18:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.shortTimeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("2d"))
func testShortTimeAgo2Days() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 05 18:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.shortTimeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("2d"))
}

func testShortTimeAgo1DayAndHalf() {
self.date0 = self.formatter?.date(from: "2014 11 06 9:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.shortTimeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("1d"))
func testShortTimeAgo1DayAndHalf() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 06 9:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.shortTimeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("1d"))
}

func testShortTimeAgoExactlyYesterday() {
self.date0 = self.formatter?.date(from: "2014 11 06 18:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.shortTimeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("1d"))
func testShortTimeAgoExactlyYesterday() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 06 18:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.shortTimeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("1d"))
}

func testShortTimeAgoLessThan24hoursButYesterday() {
self.date0 = self.formatter?.date(from: "2014 11 06 20:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.shortTimeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("22h"))
func testShortTimeAgoLessThan24hoursButYesterday() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 00:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 22:15:12.000"))
let ago: String = self.date0.shortTimeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("22h"))
}

func testShortTimeAgoLessThan24hoursSameDay() {
self.date0 = self.formatter?.date(from: "2014 11 07 10:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 07 18:15:12.000")
var ago: String = self.date0.shortTimeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("8h"))
func testShortTimeAgoLessThan24hoursSameDay() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 10:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 18:15:12.000"))
let ago: String = self.date0.shortTimeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("8h"))
}

func testShortTimeAgoBetween24And48Hours() {
self.date0 = self.formatter?.date(from: "2014 11 07 10:15:12.000")
self.date1 = self.formatter?.date(from: "2014 11 08 18:15:12.000")
var ago: String = self.date0.shortTimeAgoSinceDate(self.date1)
XCTAssertEqualObjects(ago, DateToolsLocalizedStrings("1d"))
func testShortTimeAgoBetween24And48Hours() throws {
self.date0 = try XCTUnwrap(self.formatter.date(from: "2014 11 07 10:15:12.000"))
self.date1 = try XCTUnwrap(self.formatter.date(from: "2014 11 08 18:15:12.000"))
let ago: String = self.date0.shortTimeAgo(since: self.date1)
XCTAssertEqual(ago, try DateToolsLocalizedStrings("1d"))
}

func testLongTimeAgoLocalizationsAccessible() {
var en_local: String = "Yesterday"
var ja_local: String = "昨日"
var key: String = en_local
var path: String = NSBundlemainBundle.bundlePath.stringByAppendingPathComponent("DateTools.bundle/ja.lproj")
var bundle: Bundle = Bundle(path: path)!
var ja_result: String = NSLocalizedStringFromTableInBundle(key, "DateTools", bundle, nil)
XCTAssertEqualObjects(ja_local, ja_result, "Could not access localizations.")
func testLongTimeAgoLocalizationsAccessible() throws {
let en_local = "Yesterday"
let ja_local = "昨日"
let key = en_local
let ja_result = try DateToolsLocalizedStrings(key, for: .japanese)
XCTAssertEqual(ja_local, ja_result, "Could not access localizations.")
}

}

extension XCTestCase {
enum LanguageIdentifier: String {
case english = "en"
case japanese = "ja"
}
func DateToolsLocalizedStrings(_ key: String, for language: LanguageIdentifier = .english) throws -> String {
let dateToolsBundlePath = try XCTUnwrap(Bundle.module.path(forResource: "DateTools", ofType: "bundle"))
let dateToolsBundle = try XCTUnwrap(Bundle(path: dateToolsBundlePath))
let path = try XCTUnwrap(dateToolsBundle.path(forResource: language.rawValue, ofType: "lproj"))
let languageBundle = try XCTUnwrap(Bundle(path: path))
return NSLocalizedString(key,
tableName: "DateTools",
bundle: languageBundle,
value: "",
comment: "")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class TimeChunkTests: XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift


class TimePeriodChainTests : XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class TimePeriodCollectionTests : XCTestCase {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift


class TimePeriodGroupTests : XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

import XCTest
@testable import DateToolsTests
@testable import DateToolsSwift

class TimePeriodTests : XCTestCase {

Expand Down
Loading