11import Foundation
2+ import XCTest
23
34typealias 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