From f60f6ae083d4ff2f5365bb36fda4d294c642ffbe Mon Sep 17 00:00:00 2001 From: Kare Morstol Date: Tue, 25 Nov 2014 20:45:38 +0100 Subject: [PATCH] Add script for testing reading and writing files. --- .../Scripts/readandwritefiles.swift | 32 +++++++++++++++++++ SwiftShellTests/Scripts/testall.bash | 1 + 2 files changed, 33 insertions(+) create mode 100755 SwiftShellTests/Scripts/readandwritefiles.swift diff --git a/SwiftShellTests/Scripts/readandwritefiles.swift b/SwiftShellTests/Scripts/readandwritefiles.swift new file mode 100755 index 0000000..1b1ec58 --- /dev/null +++ b/SwiftShellTests/Scripts/readandwritefiles.swift @@ -0,0 +1,32 @@ +#!/usr/bin/env swiftshell + +import SwiftShell + +let filepath = tempdirectory.URLByAppendingPathComponent("newfile.txt").path! + +// File doesn't exist. Create it. +let file1 = open(forWriting: filepath ) +file1.writeln("line 1") +file1.closeStream() + +// File does exist. Append it. +let file2 = open(forWriting: filepath) +file2.writeln("line 2") +file2.closeStream() + +// Test file. +let file3 = open(filepath) +if file3.read() != "line 1\nline 2\n" { + printErrorAndExit( "newfile.txt should contain 'line 1\\n line 2\\n'." ) +} + +// File exists. Overwrite it. +let file4 = open(forWriting: filepath, overwrite: true) +file4.write("file now contains only this") +file4.closeStream() + +// Test file. +let file5 = open(filepath) +if file5.read() != "file now contains only this" { + printErrorAndExit( "newfile.txt should contain 'file now contains only this'." ) +} diff --git a/SwiftShellTests/Scripts/testall.bash b/SwiftShellTests/Scripts/testall.bash index dd96bc8..bf7b015 100755 --- a/SwiftShellTests/Scripts/testall.bash +++ b/SwiftShellTests/Scripts/testall.bash @@ -14,3 +14,4 @@ ls | ./print_linenumbers.swift ./readfilelinebyline.swift ./stream_out.swift ./callswiftscriptfromswift.swift +./readandwritefiles.swift