Files
SwiftShell/SwiftShellTests/FileHandle_Tests.swift
T
Kare Morstol 90d10292c8 Add function "open(forWriting:, overwrite:)" for writing and appending to files.
Also added func "temporaryDirectory" to XCTestCase_Additions.
2014-11-25 01:15:36 +01:00

71 lines
1.9 KiB
Swift

//
// FileHandle_Tests.swift
// SwiftShell
//
// Created by Kåre Morstøl on 19/08/14.
// Copyright (c) 2014 NotTooBad Software. All rights reserved.
//
import SwiftShell
import XCTest
class FileHandle_Tests: XCTestCase {
func notestOpenForReadingFileWhichDoesNotExist () {
// prints error message and stops execution.
let file = open("file which does not exist")
}
func testReadFileLineByLine () {
let shorttextpath = pathForTestResource("shorttext", type: "txt")
var contents = ""
for line in open(shorttextpath).lines() {
contents.write(line)
}
XCTAssertFalse(contents.isEmpty, "could not read from file")
}
func testOpenForWritingFileWhichDoesNotExist () {
let path = self.temporaryDirectory().URLByAppendingPathComponent("non-existent.txt").path!
let file = open(forWriting: path)
file.writeln( "line 1")
file.closeStream()
XCTAssertEqual( open(path).read(), "line 1\n" )
}
func testOpenForOverWritingFileWhichDoesNotExist () {
let path = self.temporaryDirectory().URLByAppendingPathComponent("non-existent.txt").path!
let file = open(forWriting: path, overwrite: true)
file.writeln( "line 1")
file.closeStream()
XCTAssertEqual( open(path).read(), "line 1\n" )
}
func testOpenForWritingExistingFile_AppendsFile () {
let path = self.temporaryDirectory().URLByAppendingPathComponent("existent.txt").path!
SwiftShell.run("echo existing line > " + path)
let file = open(forWriting: path)
file.writeln( "new line")
file.closeStream()
XCTAssertEqual( open(path).read(), "existing line\nnew line\n" )
}
func testOpenForOverWritingExistingFile () {
let path = self.temporaryDirectory().URLByAppendingPathComponent("existent.txt").path!
SwiftShell.run("echo existing line > " + path)
let file = open(forWriting: path, overwrite: true)
file.writeln( "new line")
file.closeStream()
XCTAssertEqual( open(path).read(), "new line\n" )
}
}