Move "streams" function to the module.

Returns a writable stream and a readable stream. What you write to the 1st one can be read from the 2nd one.
Make sure to call closeStream() on the writable stream before you call read() on the readable one.
This commit is contained in:
Kare Morstol
2014-09-10 20:08:39 +02:00
parent 6bd78a11c5
commit 1d3d876f22
2 changed files with 13 additions and 8 deletions
+10
View File
@@ -91,6 +91,16 @@ public func stream <Seq:SequenceType where Seq.Generator.Element == String>(sequ
}
}
/**
Returns a writable stream and a readable stream. What you write to the 1st one can be read from the 2nd one.
Make sure to call closeStream() on the writable stream before you call read() on the readable one.
*/
public func streams () -> (WriteableStreamType, ReadableStreamType) {
let pipe = NSPipe()
return (pipe.fileHandleForWriting, pipe.fileHandleForReading)
}
/** Splits a stream into parts separated by "delimiter". */
struct StringStreamGenerator : GeneratorType {
private let stream: ReadableStreamType
+3 -8
View File
@@ -35,17 +35,12 @@ class Stream_Tests: XCTestCase {
XCTAssertEqual( stream(["item 1","item 2"]).read(), "item 1item 2")
}
func streams () -> (WriteableStreamType, ReadableStreamType) {
let pipe = NSPipe()
return (pipe.fileHandleForWriting, pipe.fileHandleForReading)
}
func testPrintStreamToStream () {
var (writable, readable) = streams()
stream("this goes in") |> writable
XCTAssertEqual(readable.readSome()!, "this goes in")
XCTAssertEqual( readable.readSome()!, "this goes in")
}
func testPrintStringToStream () {
@@ -53,7 +48,7 @@ class Stream_Tests: XCTestCase {
"this goes in" |> writable
XCTAssertEqual(readable.readSome()!, "this goes in")
XCTAssertEqual( readable.readSome()!, "this goes in")
}
func testCommandChainToStream () {
@@ -61,7 +56,7 @@ class Stream_Tests: XCTestCase {
SwiftShell.run("echo this is streamed") |> SwiftShell.run("wc -w") |> writable
XCTAssertEqual(readable.readSome()!.trim(), "3")
XCTAssertEqual( readable.readSome()!.trim(), "3")
}