@@ -23,6 +23,11 @@ class TestNSStream : XCTestCase {
23
23
( " test_InputStreamWithFile " , test_InputStreamWithFile) ,
24
24
( " test_InputStreamHasBytesAvailable " , test_InputStreamHasBytesAvailable) ,
25
25
( " test_InputStreamInvalidPath " , test_InputStreamInvalidPath) ,
26
+ ( " test_outputStreamCreationToFile " , test_outputStreamCreationToFile) ,
27
+ ( " test_outputStreamCreationToBuffer " , test_outputStreamCreationToBuffer) ,
28
+ ( " test_outputStreamCreationWithUrl " , test_outputStreamCreationWithUrl) ,
29
+ ( " test_outputStreamHasSpaceAvailable " , test_outputStreamHasSpaceAvailable) ,
30
+ ( " test_ouputStreamWithInvalidPath " , test_ouputStreamWithInvalidPath) ,
26
31
]
27
32
}
28
33
@@ -118,19 +123,90 @@ class TestNSStream : XCTestCase {
118
123
XCTAssertEqual ( Stream . Status. error, fileStream. streamStatus)
119
124
}
120
125
121
- private func createTestFile( _ path: String , _contents: Data ) -> String ? {
126
+ func test_outputStreamCreationToFile( ) {
127
+ let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) !)
128
+ if filePath != nil {
129
+ let outputStream = NSOutputStream ( toFileAtPath: filePath!, append: true )
130
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
131
+ var myString = " Hello world! "
132
+ let encodedData = [ UInt8] ( myString. utf8)
133
+ outputStream? . open ( )
134
+ XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
135
+ let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
136
+ outputStream? . close ( )
137
+ XCTAssertEqual ( myString. characters. count, result)
138
+ XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
139
+ removeTestFile ( filePath!)
140
+ } else {
141
+ XCTFail ( " Unable to create temp file " ) ;
142
+ }
143
+ }
144
+
145
+ func test_outputStreamCreationToBuffer( ) {
146
+ var buffer = Array < UInt8 > ( repeating: 0 , count: 12 )
147
+ var myString = " Hello world! "
148
+ let encodedData = [ UInt8] ( myString. utf8)
149
+ let outputStream = NSOutputStream ( toBuffer: UnsafeMutablePointer < UInt8 > ( buffer) , capacity: 12 )
150
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream. streamStatus)
151
+ outputStream. open ( )
152
+ XCTAssertEqual ( Stream . Status. open, outputStream. streamStatus)
153
+ let result : Int ? = outputStream. write ( encodedData, maxLength: encodedData. count)
154
+ outputStream. close ( )
155
+ XCTAssertEqual ( Stream . Status. closed, outputStream. streamStatus)
156
+ XCTAssertEqual ( myString. characters. count, result)
157
+ XCTAssertEqual ( NSString ( bytes: & buffer, length: buffer. count, encoding: String . Encoding. utf8. rawValue) , myString. _bridgeToObject ( ) )
158
+ }
159
+
160
+ func test_outputStreamCreationWithUrl( ) {
161
+ let filePath = createTestFile ( " TestFileOut.txt " , _contents: Data ( capacity: 256 ) !)
162
+ if filePath != nil {
163
+ let outputStream = NSOutputStream ( url: URL ( fileURLWithPath: filePath!) , append: true )
164
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
165
+ var myString = " Hello world! "
166
+ let encodedData = [ UInt8] ( myString. utf8)
167
+ outputStream!. open ( )
168
+ XCTAssertEqual ( Stream . Status. open, outputStream!. streamStatus)
169
+ let result : Int ? = outputStream? . write ( encodedData, maxLength: encodedData. count)
170
+ outputStream? . close ( )
171
+ XCTAssertEqual ( myString. characters. count, result)
172
+ XCTAssertEqual ( Stream . Status. closed, outputStream!. streamStatus)
173
+ removeTestFile ( filePath!)
174
+ } else {
175
+ XCTFail ( " Unable to create temp file " ) ;
176
+ }
177
+ }
178
+
179
+ func test_outputStreamHasSpaceAvailable( ) {
180
+ let buffer = Array < UInt8 > ( repeating: 0 , count: 12 )
181
+ var myString = " Welcome To Hello world ! "
182
+ let encodedData = [ UInt8] ( myString. utf8)
183
+ let outputStream = NSOutputStream ( toBuffer: UnsafeMutablePointer < UInt8 > ( buffer) , capacity: 12 )
184
+ outputStream. open ( )
185
+ XCTAssertTrue ( outputStream. hasSpaceAvailable)
186
+ _ = outputStream. write ( encodedData, maxLength: encodedData. count)
187
+ XCTAssertFalse ( outputStream. hasSpaceAvailable)
188
+ }
189
+
190
+ func test_ouputStreamWithInvalidPath( ) {
191
+ let outputStream = NSOutputStream ( toFileAtPath: " http:///home/sdsfsdfd " , append: true )
192
+ XCTAssertEqual ( Stream . Status. notOpen, outputStream!. streamStatus)
193
+ outputStream? . open ( )
194
+ XCTAssertEqual ( Stream . Status. error, outputStream!. streamStatus)
195
+ }
196
+
197
+ private func createTestFile( _ path: String , _contents: Data ) -> String ? {
122
198
let tempDir = " /tmp/TestFoundation_Playground_ " + NSUUID( ) . UUIDString + " / "
123
199
do {
124
200
try FileManager . default ( ) . createDirectory ( atPath: tempDir, withIntermediateDirectories: false , attributes: nil )
125
- if FileManager . default ( ) . createFile ( atPath: tempDir + " / " + path, contents: _contents, attributes: nil ) {
126
- return tempDir + path
201
+ if FileManager . default ( ) . createFile ( atPath: tempDir + " / " + path, contents: _contents,
202
+ attributes: nil ) {
203
+ return tempDir + path
127
204
} else {
128
205
return nil
129
206
}
130
207
} catch _ {
131
208
return nil
132
209
}
133
-
134
210
}
135
211
136
212
private func removeTestFile( _ location: String ) {
@@ -142,4 +218,3 @@ class TestNSStream : XCTestCase {
142
218
}
143
219
}
144
220
145
-
0 commit comments