<commit_before>//
// Stream_Iteration_Tests.swift
// SwiftShell
//
// Created by <NAME> on 18/07/14.
// Copyright (c) 2014 NotTooBad Software. All rights reserved.
//

import XCTest
import SwiftShell

class Stream_Iteration_Tests: XCTestCase {
	
	func stream(text: String) -> NSFileHandle {
		let pipe = NSPipe()
		let input = pipe.fileHandleForWriting
		input.write(text)
		input.closeFile()
		return pipe.fileHandleForReading
	}
	
	func stream ( array: [String]) -> ReadableStreamType {
		class ArrayStream: ReadableStreamType {
			var generator: IndexingGenerator<[ String]>
			
			init(array: Array <String >) {
				generator = array.generate() 
			}
			
			func readSome() -> String? {
				return generator.next()
			}
			
			func read() -> String {
				XCTAssert(false, "not implemented")
				return "" 
			}
			
			func lines() -> SequenceOf <String >{
				return split(delimiter: "\n")(stream: self)
			}
		}
		
		return ArrayStream (array: array)
	}
	
	
	func testIterateOverFileHandle() {
		var filehandletest = ""
		
		for line in stream("line 1\nline 2\n").lines() {
			filehandletest += line + "\n"
		}
		XCTAssert(filehandletest == "line 1\nline 2\n")
		
		XCTAssert(["line 1","line 2"] == Array(stream("line 1\nline 2").lines()))
		XCTAssert(["line 1"] == Array(stream("line 1\n").lines()))
		XCTAssert(["line 1"] == Array(stream("line 1").lines()))
		XCTAssert(["line 1","", "line 3"] == Array(stream("line 1\n\nline 3").lines()))
		XCTAssert(["","line 2", "line 3"] == Array(stream("\nline 2\nline 3").lines()))
		XCTAssert(["","", "line 3"] == Array(stream("\n\nline 3").lines()))
	}
	
	func testIterateOverStreamInPieces () {
		XCTAssert(["line 1","line 2"] == Array(stream(["line"," 1\nline 2"]).lines()))
		XCTAssert(["line 1"] == Array(stream(["line 1","\n"]).lines()))<commit_msg>stream(text: String) -> NSFileHandle uses writeabilityHandler<commit_after>//
// Stream_Iteration_Tests.swift
// SwiftShell
//
// Created by <NAME> on 18/07/14.
// Copyright (c) 2014 NotTooBad Software. All rights reserved.
//

import XCTest
import SwiftShell

class Stream_Iteration_Tests: XCTestCase {
	
	func stream(text: String) -> NSFileHandle {
		let pipe = NSPipe()
		
		let input: NSFileHandle = pipe.fileHandleForWriting
		input.writeabilityHandler = {  filehandle in 
			filehandle.write(text) 
			// input.closeFile() 
			input.writeabilityHandler = nil
		} 

		return pipe.fileHandleForReading
	}
	
	func stream ( array: [String]) -> ReadableStreamType {
		class ArrayStream: ReadableStreamType {
			var generator: IndexingGenerator<[ String]>
			
			init(array: Array <String >) {
				generator = array.generate() 
			}
			
			func readSome() -> String? {
				return generator.next()
			}
			
			func read() -> String {
				XCTAssert(false, "not implemented")
				return "" 
			}
			
			func lines() -> SequenceOf <String >{
				return split(delimiter: "\n")(stream: self)
			}
		}
		
		return ArrayStream (array: array)
	}
	
	
	func testIterateOverFileHandle() {
		var filehandletest = ""
		
		for line in stream("line 1\nline 2\n").lines() {
			filehandletest += line + "\n"
		}
		XCTAssert(filehandletest == "line 1\nline 2\n")
		
		XCTAssertEqual(["line 1","line 2"] , Array(stream("line 1\nline 2").lines()))
		XCTAssert(["line 1"] == Array(stream("line 1\n").lines()))
		XCTAssert(["line 1"] == Array(stream("line 1").lines()))
		XCTAssert(["line 1","", "line 3"] == Array(stream("line 1\n\nline 3").lines()))
		XCTAssert(["","line 2", "line 3"] == Array(stream("\nline 2\nline 3").lines()))
		XCTAssert(["","", "line 3"] == Array(stream("\n\nline 3").lines()))
	}
	
	func testIterateOverStreamInPieces () {
		XCTAssert(["line 1","line 2"] == Array(stream(["line"," 1\nline 2"]).lines()))
		XCTAssert(["line 1"] == Array(stream(["line 1","\n"]).lines()))
{'fraction_non_alphanumeric': 0.18034957627118645, 'fraction_numerical': 0.020127118644067795, 'mean_word_length': 3.6977611940298507, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5440872', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1267, 'n_words': 289}