From 19d6baa65d8a0f0f40502ccbfe00a69f6fd6c6b9 Mon Sep 17 00:00:00 2001 From: hectr Date: Sat, 13 Apr 2019 02:22:10 +0200 Subject: [PATCH 1/2] Implement method for accessing test resources --- Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift | 5 +++++ Tests/SwiftyChronoTests/JS/de/TestDE.swift | 2 +- Tests/SwiftyChronoTests/JS/en/TestEN.swift | 2 +- Tests/SwiftyChronoTests/JS/es/TestES.swift | 2 +- Tests/SwiftyChronoTests/JS/fr/TestFR.swift | 2 +- Tests/SwiftyChronoTests/JS/jp/TestJP.swift | 2 +- Tests/SwiftyChronoTests/JS/zh_hans/TestZHHans.swift | 2 +- Tests/SwiftyChronoTests/JS/zh_hant/testZHHant.swift | 2 +- 8 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift b/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift index 8f04cd5..69a5557 100644 --- a/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift +++ b/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift @@ -32,6 +32,11 @@ public class ChronoJSXCTestCase: XCTestCase, ChronoJSTestable { jsContext.evaluateScript(script) } + public func resourcePath(testCasePath: String = #file, name: String, type: String) -> String { + let bundle = Bundle(identifier: "io.quire.lib.SwiftyChrono")! + return bundle.path(forResource: name, ofType: type)! + } + override public func setUp() { super.setUp() diff --git a/Tests/SwiftyChronoTests/JS/de/TestDE.swift b/Tests/SwiftyChronoTests/JS/de/TestDE.swift index 87c1a0f..157653c 100644 --- a/Tests/SwiftyChronoTests/JS/de/TestDE.swift +++ b/Tests/SwiftyChronoTests/JS/de/TestDE.swift @@ -25,7 +25,7 @@ class TestDE: ChronoJSXCTestCase { Chrono.sixMinutesFixBefore1900 = true for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } diff --git a/Tests/SwiftyChronoTests/JS/en/TestEN.swift b/Tests/SwiftyChronoTests/JS/en/TestEN.swift index 8bc305e..0125221 100644 --- a/Tests/SwiftyChronoTests/JS/en/TestEN.swift +++ b/Tests/SwiftyChronoTests/JS/en/TestEN.swift @@ -33,7 +33,7 @@ class TestEN: ChronoJSXCTestCase { Chrono.preferredLanguage = .english for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } diff --git a/Tests/SwiftyChronoTests/JS/es/TestES.swift b/Tests/SwiftyChronoTests/JS/es/TestES.swift index 46a3c33..b09ce9a 100644 --- a/Tests/SwiftyChronoTests/JS/es/TestES.swift +++ b/Tests/SwiftyChronoTests/JS/es/TestES.swift @@ -24,7 +24,7 @@ class TestES: ChronoJSXCTestCase { Chrono.sixMinutesFixBefore1900 = true for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } diff --git a/Tests/SwiftyChronoTests/JS/fr/TestFR.swift b/Tests/SwiftyChronoTests/JS/fr/TestFR.swift index e76ba1a..0622ec8 100644 --- a/Tests/SwiftyChronoTests/JS/fr/TestFR.swift +++ b/Tests/SwiftyChronoTests/JS/fr/TestFR.swift @@ -25,7 +25,7 @@ class TestFR: ChronoJSXCTestCase { Chrono.sixMinutesFixBefore1900 = true for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } diff --git a/Tests/SwiftyChronoTests/JS/jp/TestJP.swift b/Tests/SwiftyChronoTests/JS/jp/TestJP.swift index c173255..2e26f23 100644 --- a/Tests/SwiftyChronoTests/JS/jp/TestJP.swift +++ b/Tests/SwiftyChronoTests/JS/jp/TestJP.swift @@ -19,7 +19,7 @@ class TestJP: ChronoJSXCTestCase { Chrono.sixMinutesFixBefore1900 = true for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } diff --git a/Tests/SwiftyChronoTests/JS/zh_hans/TestZHHans.swift b/Tests/SwiftyChronoTests/JS/zh_hans/TestZHHans.swift index bef248d..bf0fdc2 100644 --- a/Tests/SwiftyChronoTests/JS/zh_hans/TestZHHans.swift +++ b/Tests/SwiftyChronoTests/JS/zh_hans/TestZHHans.swift @@ -24,7 +24,7 @@ class TestZHHans: ChronoJSXCTestCase { Chrono.sixMinutesFixBefore1900 = true for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } diff --git a/Tests/SwiftyChronoTests/JS/zh_hant/testZHHant.swift b/Tests/SwiftyChronoTests/JS/zh_hant/testZHHant.swift index c5b6a6c..f69329d 100644 --- a/Tests/SwiftyChronoTests/JS/zh_hant/testZHHant.swift +++ b/Tests/SwiftyChronoTests/JS/zh_hant/testZHHant.swift @@ -24,7 +24,7 @@ class TestZHHant: ChronoJSXCTestCase { Chrono.sixMinutesFixBefore1900 = true for fileName in files { - let js = try! String(contentsOfFile: Bundle(identifier: "io.quire.lib.SwiftyChrono")!.path(forResource: fileName, ofType: "js")!) + let js = try! String(contentsOfFile: resourcePath(name: fileName, type: "js")) evalJS(js, fileName: fileName) } } From 2b5e9890b0bb563e8bfd82a3344223d814993f92 Mon Sep 17 00:00:00 2001 From: hectr Date: Sat, 13 Apr 2019 02:27:11 +0200 Subject: [PATCH 2/2] Fallback to test case folder when bundle does not exist This is kind of a hack, but a well known one, to be able to run the tests if you have regenerated SwiftyChrono.xcodeproj with SPM. --- Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift b/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift index 69a5557..f6efc25 100644 --- a/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift +++ b/Tests/SwiftyChronoTests/ChronoJSXCTestCase.swift @@ -33,8 +33,14 @@ public class ChronoJSXCTestCase: XCTestCase, ChronoJSTestable { } public func resourcePath(testCasePath: String = #file, name: String, type: String) -> String { - let bundle = Bundle(identifier: "io.quire.lib.SwiftyChrono")! - return bundle.path(forResource: name, ofType: type)! + if let bundle = Bundle(identifier: "io.quire.lib.SwiftyChrono"), let path = bundle.path(forResource: name, ofType: type) { + return path + } else { + // bundle does not exist (expect that if you regenerated SwiftyChrono.xcodeproj with SPM) + // assume resource is in the same folder than the test case source file + let folder = testCasePath.components(separatedBy: "/").dropLast().joined(separator: "/") + return "\(folder)/\(name).\(type)" + } } override public func setUp() {