1
1
import Foundation
2
+ import XCTest
2
3
3
4
typealias JSONObject = Dictionary < String , AnyObject >
4
5
@@ -8,8 +9,8 @@ extension JSONObject {
8
9
///
9
10
/// - Parameter fileName: The full name of the json file to load.
10
11
/// - 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 (
13
14
( fileName as NSString ) . deletingPathExtension,
14
15
type: ( fileName as NSString ) . pathExtension
15
16
)
@@ -21,20 +22,11 @@ extension JSONObject {
21
22
/// - name: The name of the file
22
23
/// - type: The extension of the file
23
24
/// - 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 )
38
30
}
39
31
40
32
}
0 commit comments