From 51f3fb71426c23aa99b923b79d06a3294d8d2629 Mon Sep 17 00:00:00 2001 From: Alexandr Goncharov Date: Wed, 10 Jun 2020 01:19:01 +0300 Subject: [PATCH] Resource loader helper --- Tests/ConfTests/Resource.swift | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Tests/ConfTests/Resource.swift diff --git a/Tests/ConfTests/Resource.swift b/Tests/ConfTests/Resource.swift new file mode 100644 index 0000000..0ca1914 --- /dev/null +++ b/Tests/ConfTests/Resource.swift @@ -0,0 +1,37 @@ +import Foundation + +struct Resource { + let name: String + let type: String + let url: URL + + init(name: String, type: String) { + self.name = name + self.type = type + url = Resource.resourceFolderURL.appendingPathComponent(name).appendingPathExtension(type) + } +} + +// MARK: - Content - +extension Resource { + func data() throws -> Data { try Data(contentsOf: url) } + func string() throws -> String { try String(contentsOf: url, encoding: .utf8) } +} + +// MARK: - Path helpers - +extension Resource { + // expected folder structure + // + // - + // - + // - + // - + // - + static let resourceFolderURL = testsFolderURL + .deletingLastPathComponent() + .appendingPathComponent(resourceFolder, isDirectory: true) + .standardized + static let testsFolderURL = sourceFileURL.deletingLastPathComponent() + private static let resourceFolder = "Resources" + private static let sourceFileURL = URL(fileURLWithPath: #file, isDirectory: false) +}