using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using NUnit.Framework;

namespace m3uTool.Tests
{
	[TestFixture]
	public class ProcessStreamWrapperTests : ProgressCallbackTestBase
	{
		private class StdinToStdoutProcess : ProcessStreamWrapper
		{
			/// <summary>
			/// Gets the process executable filename, such as "LAME.EXE", "FAAC.EXE", etc.
			/// </summary>
			/// <value>The process executable filename.</value>
			protected override string ProcessExecutableFilename
			{
				get { return "StdinToStdout.exe"; }
			}

			public StdinToStdoutProcess(ProcessArguments encOpt, Stream outputStream)
				: base(encOpt, outputStream)
			{
			}
		}
		
		[Test]
		public void ProcessStreamWrapperFunctionalityTest()
		{
			string inputString = File.ReadAllText("testText.txt");
			string writtenString = ""; // string that was written to the process

			Stream inputStream = new MemoryStream();

			StreamWriter sw = new StreamWriter(inputStream, Encoding.Default);
			{
				int writeSize = 127;
				for (int i = 0; i < inputString.Length; i += writeSize)
				{
					// select a substring of characters from the input string
					string substring = inputString.Substring(i, (i + writeSize < inputString.Length) ? writeSize : inputString.Length - i);
					sw.Write(substring.ToCharArray(), 0, substring.Length);
					writtenString += substring;
				}
			}
			inputStream.Seek(0, SeekOrigin.Begin);

			Stream outputStream = new MemoryStream();
			using (StdinToStdoutProcess s = new StdinToStdoutProcess(new ProcessArguments(), outputStream))
			{
				s.ProgressCallback = this;
				s.ProcessInput(inputStream);
			}
						
			outputStream.Seek(0, SeekOrigin.Begin);
			string outputString;
			using (StreamReader sr = new StreamReader(outputStream, Encoding.Default))
			{
				outputString = sr.ReadToEnd();
			}
			Assert.AreEqual(writtenString, outputString);
		}
	}
}
