Files
SwiftShell/SwiftShellTests/Stream_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

100 lines
2.5 KiB
Swift

//
// Stream_Tests.swift
// SwiftShell
//
// Created by Kåre Morstøl on 21/08/14.
// Copyright (c) 2014 NotTooBad Software. All rights reserved.
//
import SwiftShell
import XCTest
class Stream_Tests: XCTestCase {
func testStreamFromAString () {
XCTAssertEqual( "this is a string", stream("this is a string").read() )
XCTAssertEqual( "These are weird↔️🐻♨︎", stream("These are weird↔️🐻♨︎").read() )
}
func testCustomStream () {
let result = stream ({
var finished = false
return {
if !finished {
finished = true
return "this is it"
} else {
return nil
}
}
})
XCTAssertEqual( result.read(), "this is it" )
}
func testStreamFromArray () {
XCTAssertEqual( stream(["item 1","item 2"]).read(), "item 1item 2" )
}
func testPrintStreamToStream () {
let (writable, readable) = streams()
stream("this goes in") |>> writable
XCTAssertEqual( readable.readSome()!, "this goes in" )
}
func testPrintStreamToStreamInPieces () {
let (writable, readable) = streams()
stream(["this ", "goes", " in"]) |>> writable
writable.closeStream()
XCTAssertEqual( readable.read(), "this goes in" )
}
func testPrintStringToStream () {
let (writable, readable) = streams()
"this goes in" |>> writable
XCTAssertEqual( readable.readSome()!, "this goes in" )
}
func testCommandChainToStream () {
let (writable, readable) = streams()
SwiftShell.run("echo this is streamed") |> SwiftShell.run("wc -w") |>> writable
XCTAssertEqual( readable.readSome()!.trim(), "3" )
}
func testSequenceOfStreamsToStream () {
let (writable, readable) = streams()
// make sure the array isn't printed as a Printable.
AnySequence([stream("line 1"), stream("line 2"), stream("line 3")].generate()) |>> writable
XCTAssertEqual( readable.readSome()!.trim(), "line 1line 2line 3" )
}
func testChainWithSequenceOfStreamsPrintedToStream () {
let (writable, readable) = streams()
let dict = ["test":"line 1:line 2:line 3"]
dict["test"]! |> split(delimiter: ":")
|> map { line in SwiftShell.run("echo \(line)") }
|>> writable
XCTAssertEqual( readable.readSome()!.trim(), "line 1\nline 2\nline 3" )
}
func testChainWithSequenceOfStringsPrintedToStream () {
let (writable, readable) = streams()
var i = 1
stream("line 1\nline 2\nline 3").lines() |> map {line in "line \(i++): \(line)\n"} |>> writable
XCTAssertEqual( readable.readSome()!.trim(), "line 1: line 1\nline 2: line 2\nline 3: line 3" )
}
}