Files
SwiftShell/SwiftShellTests/FileHandle_Tests.swift
T
Kare Morstol b33bfb9a5f Move "temporaryDirectory" to new file Files.swift and rename it "tempdirectory".
Because it could be useful in scripts too.
2014-11-25 20:38:32 +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 = tempdirectory.URLByAppendingPathComponent("testOpenForWritingFileWhichDoesNotExist.txt").path!
let file = open(forWriting: path)
file.writeln( "line 1")
file.closeStream()
XCTAssertEqual( open(path).read(), "line 1\n" )
}
func testOpenForOverWritingFileWhichDoesNotExist () {
let path = tempdirectory.URLByAppendingPathComponent("testOpenForOverWritingFileWhichDoesNotExist.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 = tempdirectory.URLByAppendingPathComponent("testOpenForWritingExistingFile_AppendsFile.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 = tempdirectory.URLByAppendingPathComponent("testOpenForOverWritingExistingFile.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" )
}
}