mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-13 11:03:35 -06:00
38 lines
1.7 KiB
Swift
38 lines
1.7 KiB
Swift
import Foundation
|
|
|
|
/// A type that wraps fixture data for use in mocking responses during tests.
|
|
///
|
|
public enum TestDataHelpers {
|
|
/// The default bundle to try loading files from.
|
|
public static var defaultBundle: Bundle?
|
|
|
|
/// Loads the data from the provided file.
|
|
public static func loadFromBundle(resource: String, extension: String, bundle: Bundle? = nil) -> Data {
|
|
let resolvedBundle = bundle ?? defaultBundle
|
|
guard let resolvedBundle else {
|
|
fatalError("Default test data bundle from not set properly in the test case.")
|
|
}
|
|
guard let url = resolvedBundle.url(forResource: resource, withExtension: `extension`) else {
|
|
// swiftlint:disable:next line_length
|
|
fatalError("Unable to locate file \(resource).\(`extension`) in the bundle \(resolvedBundle.bundleURL.lastPathComponent).")
|
|
}
|
|
do {
|
|
return try Data(contentsOf: url)
|
|
} catch {
|
|
// swiftlint:disable:next line_length
|
|
fatalError("Unable to load data from \(resource).\(`extension`) in the bundle \(resolvedBundle.bundleURL.lastPathComponent). Error: \(error)")
|
|
}
|
|
}
|
|
|
|
/// Convenience function for loading data from a JSON file.
|
|
public static func loadFromJsonBundle(resource: String, bundle: Bundle? = nil) -> Data {
|
|
loadFromBundle(resource: resource, extension: "json", bundle: bundle)
|
|
}
|
|
|
|
/// Convenience function for loading a JSON file into a UTF-8 string.
|
|
public static func loadUTFStringFromJsonBundle(resource: String, bundle: Bundle? = nil) -> String? {
|
|
let data = loadFromJsonBundle(resource: resource, bundle: bundle)
|
|
return String(data: data, encoding: .utf8)
|
|
}
|
|
}
|