2019-01-06 19:26:08 +00:00
|
|
|
//
|
|
|
|
// Bundle+JSON.swift
|
|
|
|
// MasKitTests
|
|
|
|
//
|
|
|
|
// Created by Ben Chatelain on 1/5/19.
|
|
|
|
// Copyright © 2019 mas-cli. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2020-09-06 20:15:02 +00:00
|
|
|
extension Data {
|
|
|
|
/// Unsafe initializer for loading data from string paths.
|
|
|
|
/// - Parameter file: Relative path within the JSON folder
|
|
|
|
init(from fileName: String) {
|
|
|
|
let fileURL = Bundle.url(for: fileName)!
|
|
|
|
print("fileURL: \(fileURL)")
|
|
|
|
try! self.init(contentsOf: fileURL, options: .mappedIfSafe)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-06 19:26:08 +00:00
|
|
|
extension Bundle {
|
|
|
|
/// Locates a JSON response file from the test bundle.
|
|
|
|
///
|
|
|
|
/// - Parameter fileName: Name of file to locate.
|
|
|
|
/// - Returns: URL to file.
|
2020-09-06 20:15:02 +00:00
|
|
|
static func url(for fileName: String) -> URL? {
|
2021-03-22 05:46:17 +00:00
|
|
|
Bundle(for: NetworkSessionMock.self).url(for: fileName)
|
2019-01-06 19:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Builds a URL for a file in the JSON directory of the current bundle.
|
|
|
|
///
|
|
|
|
/// - Parameter fileName: Name of file to locate.
|
|
|
|
/// - Returns: URL to file.
|
2020-09-06 20:15:02 +00:00
|
|
|
func url(for fileName: String) -> URL? {
|
2021-03-22 05:25:18 +00:00
|
|
|
guard
|
|
|
|
let path = self.path(
|
|
|
|
forResource: fileName.fileNameWithoutExtension,
|
|
|
|
ofType: fileName.fileExtension,
|
2021-03-22 05:46:17 +00:00
|
|
|
inDirectory: "JSON"
|
|
|
|
)
|
2021-03-22 05:25:18 +00:00
|
|
|
else { fatalError("Unable to load file \(fileName)") }
|
2019-01-06 19:26:08 +00:00
|
|
|
|
|
|
|
return URL(fileURLWithPath: path)
|
|
|
|
}
|
|
|
|
}
|