Skip to content

Commit ac58f54

Browse files
committed
Make loading test JSON files throwing error
1 parent d51d3f7 commit ac58f54

File tree

2 files changed

+10
-18
lines changed

2 files changed

+10
-18
lines changed

WordPress/WordPressTest/NSManagedObject+Fixture.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ extension NSManagedObject {
88
/// - filename: The name of the JSON file to be loaded
99
/// - context: The managed object context to use
1010
/// - Returns: A new instance with property values of the given JSON file.
11-
static func fixture(fromFile fileName: String, insertInto context: NSManagedObjectContext) -> Self {
12-
let jsonObject = JSONObject.loadFile(named: fileName)
11+
static func fixture(fromFile fileName: String, insertInto context: NSManagedObjectContext) throws -> Self {
12+
let jsonObject = try JSONObject.loadFile(named: fileName)
1313
let model = Self.init(context: context)
1414
for (key, value) in jsonObject {
1515
model.setValue(value, forKey: key)

WordPress/WordPressTest/TestUtilities/JSONObject.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Foundation
2+
import XCTest
23

34
typealias JSONObject = Dictionary<String, AnyObject>
45

@@ -8,8 +9,8 @@ extension JSONObject {
89
///
910
/// - Parameter fileName: The full name of the json file to load.
1011
/// - Returns: A dictionary representing the contents of the json file.
11-
static func loadFile(named fileName: String) -> JSONObject {
12-
return loadFile(
12+
static func loadFile(named fileName: String) throws -> JSONObject {
13+
return try loadFile(
1314
(fileName as NSString).deletingPathExtension,
1415
type: (fileName as NSString).pathExtension
1516
)
@@ -21,20 +22,11 @@ extension JSONObject {
2122
/// - name: The name of the file
2223
/// - type: The extension of the file
2324
/// - Returns: A dictionary representing the contents of the JSON file.
24-
static func loadFile(_ name: String, type: String) -> JSONObject {
25-
guard let url = Bundle(for: BundlerFinder.self).url(forResource: name, withExtension: type) else {
26-
fatalError("File not found in the test bundle: \(name).\(type)")
27-
}
28-
guard let data = try? Data(contentsOf: url) else {
29-
fatalError("Can't read content of file: \(name).\(type)")
30-
}
31-
guard let parseResult = try? JSONSerialization.jsonObject(with: data, options: [.mutableContainers, .mutableLeaves]) else {
32-
fatalError("Can't parse file as JSON: \(name).\(type)")
33-
}
34-
guard let result = parseResult as? JSONObject else {
35-
fatalError("File content isn't a JSON object: \(name).\(type)")
36-
}
37-
return result
25+
static func loadFile(_ name: String, type: String) throws -> JSONObject {
26+
let url = try XCTUnwrap(Bundle(for: BundlerFinder.self).url(forResource: name, withExtension: type))
27+
let data = try Data(contentsOf: url)
28+
let parseResult = try JSONSerialization.jsonObject(with: data, options: [.mutableContainers, .mutableLeaves])
29+
return try XCTUnwrap(parseResult as? JSONObject)
3830
}
3931

4032
}

0 commit comments

Comments
 (0)