Skip to content

Instantly share code, notes, and snippets.

@MultiColourPixel
Last active May 27, 2022 08:35
Show Gist options
  • Select an option

  • Save MultiColourPixel/a5dce5dadb284ad7f2b3320937c3a085 to your computer and use it in GitHub Desktop.

Select an option

Save MultiColourPixel/a5dce5dadb284ad7f2b3320937c3a085 to your computer and use it in GitHub Desktop.
import XCTest
extension XCTestCase {
private class TestBundle { }
func deserializedObject<T: Decodable>(resourceName: String) throws -> T {
let bundle = Bundle(for: TestBundle.self)
let url = try XCTUnwrap(bundle.url(forResource: resourceName, withExtension: "json"))
let data = try Data(contentsOf: url)
return try JSONDecoder().decode(T.self, from: data)
}
func loadJSON(resourceName: String) throws -> Any {
let bundle = Bundle(for: TestBundle.self)
let url = try XCTUnwrap(bundle.url(forResource: resourceName, withExtension: "json"))
let data = try Data(contentsOf: url)
return try JSONSerialization.jsonObject(with: data)
}
func loadData(resourceName: String) throws -> Data {
let bundle = Bundle(for: TestBundle.self)
let url = try XCTUnwrap(bundle.url(forResource: resourceName, withExtension: "json"))
return try Data(contentsOf: url)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment