@@ -54,54 +54,56 @@ class TestStream : XCTestCase {
54
54
func test_InputStreamWithUrl( ) {
55
55
let message : NSString = " Hello, playground "
56
56
let messageData : Data = message. data ( using: String . Encoding. utf8. rawValue) !
57
+ guard let testFile = createTestFile ( " testFile_in.txt " , _contents: messageData) else {
58
+ XCTFail ( " Unable to create temp file " )
59
+ return
60
+ }
61
+
57
62
//Initialiser with url
58
- if let testFile = createTestFile ( " testFile_in.txt " , _contents: messageData) {
59
- let url = URL ( fileURLWithPath: testFile)
60
- let urlStream : InputStream = InputStream ( url: url) !
61
- XCTAssertEqual ( Stream . Status. notOpen, urlStream. streamStatus)
62
- urlStream. open ( )
63
- XCTAssertEqual ( Stream . Status. open, urlStream. streamStatus)
64
- var buffer = [ UInt8] ( repeating: 0 , count: 20 )
65
- if urlStream. hasBytesAvailable {
66
- let result : Int = urlStream. read ( & buffer, maxLength: buffer. count)
67
- urlStream. close ( )
68
- XCTAssertEqual ( Stream . Status. closed, urlStream. streamStatus)
69
- XCTAssertEqual ( messageData. count, result)
70
- if ( result > 0 ) {
71
- let output = NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue)
72
- XCTAssertEqual ( message, output!)
73
- }
63
+ let url = URL ( fileURLWithPath: testFile)
64
+ let urlStream : InputStream = InputStream ( url: url) !
65
+ XCTAssertEqual ( Stream . Status. notOpen, urlStream. streamStatus)
66
+ urlStream. open ( )
67
+ XCTAssertEqual ( Stream . Status. open, urlStream. streamStatus)
68
+ var buffer = [ UInt8] ( repeating: 0 , count: 20 )
69
+ if urlStream. hasBytesAvailable {
70
+ let result : Int = urlStream. read ( & buffer, maxLength: buffer. count)
71
+ urlStream. close ( )
72
+ XCTAssertEqual ( Stream . Status. closed, urlStream. streamStatus)
73
+ XCTAssertEqual ( messageData. count, result)
74
+ if ( result > 0 ) {
75
+ let output = NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue)
76
+ XCTAssertEqual ( message, output!)
74
77
}
75
- removeTestFile ( testFile)
76
- } else {
77
- XCTFail ( " Unable to create temp file " )
78
78
}
79
+ removeTestFile ( testFile)
79
80
}
80
81
81
82
func test_InputStreamWithFile( ) {
82
83
let message : NSString = " Hello, playground "
83
84
let messageData : Data = message. data ( using: String . Encoding. utf8. rawValue) !
85
+ guard let testFile = createTestFile ( " testFile_in.txt " , _contents: messageData) else {
86
+ XCTFail ( " Unable to create temp file " )
87
+ return
88
+ }
89
+
84
90
//Initialiser with file
85
- if let testFile = createTestFile ( " testFile_in.txt " , _contents: messageData) {
86
- let fileStream : InputStream = InputStream ( fileAtPath: testFile) !
87
- XCTAssertEqual ( Stream . Status. notOpen, fileStream. streamStatus)
88
- fileStream. open ( )
89
- XCTAssertEqual ( Stream . Status. open, fileStream. streamStatus)
90
- var buffer = [ UInt8] ( repeating: 0 , count: 20 )
91
- if fileStream. hasBytesAvailable {
92
- let result : Int = fileStream. read ( & buffer, maxLength: buffer. count)
93
- fileStream. close ( )
94
- XCTAssertEqual ( Stream . Status. closed, fileStream. streamStatus)
95
- XCTAssertEqual ( messageData. count, result)
96
- if ( result > 0 ) {
97
- let output = NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue)
98
- XCTAssertEqual ( message, output!)
99
- }
91
+ let fileStream : InputStream = InputStream ( fileAtPath: testFile) !
92
+ XCTAssertEqual ( Stream . Status. notOpen, fileStream. streamStatus)
93
+ fileStream. open ( )
94
+ XCTAssertEqual ( Stream . Status. open, fileStream. streamStatus)
95
+ var buffer = [ UInt8] ( repeating: 0 , count: 20 )
96
+ if fileStream. hasBytesAvailable {
97
+ let result : Int = fileStream. read ( & buffer, maxLength: buffer. count)
98
+ fileStream. close ( )
99
+ XCTAssertEqual ( Stream . Status. closed, fileStream. streamStatus)
100
+ XCTAssertEqual ( messageData. count, result)
101
+ if ( result > 0 ) {
102
+ let output = NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue)
103
+ XCTAssertEqual ( message, output!)
100
104
}
101
- removeTestFile ( testFile)
102
- } else {
103
- XCTFail ( " Unable to create temp file " )
104
105
}
106
+ removeTestFile ( testFile)
105
107
}
106
108
107
109
func test_InputStreamHasBytesAvailable( ) {
@@ -123,21 +125,22 @@ class TestStream : XCTestCase {
123
125
}
124
126
125
127
func test_outputStreamCreationToFile( ) {
126
- if let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) ) {
127
- let outputStream = OutputStream ( toFileAtPath: filePath, append: true )
128
- XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
129
- var myString = " Hello world! "
130
- let encodedData = [ UInt8] ( myString. utf8)
131
- outputStream? . open ( )
132
- XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
133
- let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
134
- outputStream? . close ( )
135
- XCTAssertEqual ( myString. count, result)
136
- XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
137
- removeTestFile ( filePath)
138
- } else {
128
+ guard let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) ) else {
139
129
XCTFail ( " Unable to create temp file " ) ;
130
+ return
140
131
}
132
+
133
+ let outputStream = OutputStream ( toFileAtPath: filePath, append: true )
134
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
135
+ var myString = " Hello world! "
136
+ let encodedData = [ UInt8] ( myString. utf8)
137
+ outputStream? . open ( )
138
+ XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
139
+ let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
140
+ outputStream? . close ( )
141
+ XCTAssertEqual ( myString. count, result)
142
+ XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
143
+ removeTestFile ( filePath)
141
144
}
142
145
143
146
func test_outputStreamCreationToBuffer( ) {
@@ -156,21 +159,22 @@ class TestStream : XCTestCase {
156
159
}
157
160
158
161
func test_outputStreamCreationWithUrl( ) {
159
- if let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) ) {
160
- let outputStream = OutputStream ( url: URL ( fileURLWithPath: filePath) , append: true )
161
- XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
162
- var myString = " Hello world! "
163
- let encodedData = [ UInt8] ( myString. utf8)
164
- outputStream!. open ( )
165
- XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
166
- let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
167
- outputStream? . close ( )
168
- XCTAssertEqual ( myString. count, result)
169
- XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
170
- removeTestFile ( filePath)
171
- } else {
162
+ guard let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) ) else {
172
163
XCTFail ( " Unable to create temp file " ) ;
164
+ return
173
165
}
166
+
167
+ let outputStream = OutputStream ( url: URL ( fileURLWithPath: filePath) , append: true )
168
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
169
+ var myString = " Hello world! "
170
+ let encodedData = [ UInt8] ( myString. utf8)
171
+ outputStream!. open ( )
172
+ XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
173
+ let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
174
+ outputStream? . close ( )
175
+ XCTAssertEqual ( myString. count, result)
176
+ XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
177
+ removeTestFile ( filePath)
174
178
}
175
179
176
180
func test_outputStreamCreationToMemory( ) {
0 commit comments