Skip to content

Instantly share code, notes, and snippets.

@UglyBlueCat
Last active June 2, 2017 15:43
Show Gist options
  • Save UglyBlueCat/cf73e4a3311f54e5531a66c22c86f471 to your computer and use it in GitHub Desktop.
Save UglyBlueCat/cf73e4a3311f54e5531a66c22c86f471 to your computer and use it in GitHub Desktop.
Swift File Handling in TDD
class FileHandler {
enum FileHandlerError: Error {
case findPathError
case appendFolderError(_ : String)
case writeError(_ : String)
}
var fileURL : URL!
init() {}
init(fileName : String) throws {
do {
try createFileURL(fileName)
} catch {
throw error
}
}
/**
Creates a URL for a file and assigns it to the global variable fileURL
- parameters:
- fileName: The name of the file
- throws: An error describing what went wrong
*/
private func createFileURL(_ fileName : String) throws {
let folder = "AuctionAlert"
guard let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first else {
throw FileHandlerError.findPathError
}
guard let writePath = NSURL(fileURLWithPath: path).appendingPathComponent(folder) else {
throw FileHandlerError.appendFolderError(folder)
}
do {
try FileManager.default.createDirectory(atPath: writePath.path, withIntermediateDirectories: true)
} catch {
throw error
}
fileURL = writePath.appendingPathComponent(fileName)
}
/**
Stores a Data object in the file at the URL created by init
- parameters:
- data: The Data object to store
- throws: An error describing what went wrong
*/
func write(_ data : Data) throws {
do {
try data.write(to: fileURL)
} catch {
throw FileHandlerError.writeError(error.localizedDescription)
}
}
/**
Reads data from the file at the URL created by init
- returns: The Data object read from the file
- throws: An error describing what went wrong
*/
func read() throws -> Data {
var readData : Data
do {
readData = try Data(contentsOf: fileURL)
} catch {
throw error
}
return readData
}
/**
Deletes the file at the URL created by init
- throws: An error describing what went wrong
*/
func delete(completion : (() throws -> Void)? = nil) throws {
do {
try FileManager.default.removeItem(at: fileURL)
} catch {
throw error
}
do {
try completion?()
} catch {
throw error
}
}
}
class FileHandlerTests: XCTestCase {
var testFile : FileHandler = FileHandler()
var testData : Data = Data()
var readData : Data = Data()
override func setUp() {
super.setUp()
let testDict : Dictionary<String, Any> = ["array":[1,2,3], "boolean":true, "number":123, "object":["a":"b","c":"d","e":"f"], "string":"Hello World"]
do {
try testFile = FileHandler(fileName: "testFile")
} catch {
XCTFail("Failed file URL creation: \(error.localizedDescription)")
}
do {
testData = try JSONSerialization.data(withJSONObject: testDict)
} catch {
XCTFail("Failed to convert testDict to JSON data: \(error.localizedDescription)")
}
}
override func tearDown() {
super.tearDown()
do {
try testFile.delete()
} catch {
XCTFail("Failed to delete file: \(error.localizedDescription)")
}
}
func testWriteAndRead() {
do {
try testFile.write(testData)
} catch {
XCTFail("Failed to write to file: \(error.localizedDescription)")
}
do {
readData = try testFile.read()
} catch {
XCTFail("Failed to read from file: \(error.localizedDescription)")
}
XCTAssertEqual(testData, readData)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment