Files
SwiftShell/SwiftShellTests/FileHandle_Tests.swift
T
Kare Morstol a9dbdcf629 Update to Swift 2.0 .
Wow, lots of changes in Swift 2.0! And all of them great, except for renaming println to print. What's up with that?
This is just to get it to compile, we will be trying and throwing errors in no time.
2015-06-28 00:52:40 +02:00

71 lines
1.8 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.
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 + "/testOpenForWritingFileWhichDoesNotExist.txt"
let file = open(forWriting: path)
file.writeln( "line 1")
file.closeStream()
XCTAssertEqual( open(path).read(), "line 1\n" )
}
func testOpenForOverWritingFileWhichDoesNotExist () {
let path = tempdirectory + "/testOpenForOverWritingFileWhichDoesNotExist.txt"
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 + "/testOpenForWritingExistingFile_AppendsFile.txt"
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 + "/testOpenForOverWritingExistingFile.txt"
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" )
}
}