Created
April 19, 2013 20:49
-
-
Save philiplaureano/5423127 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Nemerle; | |
using Nemerle.Collections; | |
using Nemerle.Text; | |
using Nemerle.Utility; | |
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Tests | |
{ | |
public abstract class StreamDecorator : Stream | |
{ | |
private _stream : Stream; | |
protected this(actualStream : Stream) | |
{ | |
_stream = actualStream; | |
} | |
public override Flush() : void | |
{ | |
_stream.Flush(); | |
} | |
public override Seek(offset : long, origin : SeekOrigin) : long | |
{ | |
_stream.Seek(offset, origin); | |
} | |
public override SetLength(value : long) : void | |
{ | |
_stream.SetLength(value); | |
} | |
public override Read(buffer : array[byte], offset : int, count : int) : int | |
{ | |
_stream.Read(buffer, offset, count); | |
} | |
public override CanRead : bool | |
{ | |
get { _stream.CanRead; } | |
} | |
public override CanSeek : bool | |
{ | |
get { _stream.CanSeek; } | |
} | |
public override CanWrite : bool | |
{ | |
get { _stream.CanWrite; } | |
} | |
public override Length : long | |
{ | |
get { _stream.Length; } | |
} | |
public override Position : long | |
{ | |
get { _stream.Position; } | |
set { _stream.Position = value; } | |
} | |
public override Write(buffer : array[byte], offset : int, count : int) : void | |
{ | |
_stream.Write(buffer, offset, count); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Nemerle; | |
using Nemerle.Collections; | |
using Nemerle.Text; | |
using Nemerle.Utility; | |
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Tests | |
{ | |
/// <summary> | |
/// A stream that fails the test if the bytes being written don't match the ones | |
/// given in the original stream | |
/// </summary> | |
public class TracerStream : StreamDecorator | |
{ | |
private _reader : BinaryReader; | |
private _expectedStream : Stream; | |
private _outputStream : Stream; | |
public this(expectedStream : Stream, outputStream : Stream) | |
{ | |
base(outputStream); | |
_expectedStream = expectedStream; | |
_reader = BinaryReader(expectedStream); | |
_outputStream = outputStream; | |
} | |
public override Write(buffer : array[byte], offset : int, count : int) : void | |
{ | |
// Seek the matching point in the expected stream | |
def bufferData = buffer.ToStream(); | |
def bufferReader = BinaryReader(bufferData); | |
_ = bufferData.Seek(offset, SeekOrigin.Begin); | |
def actualBytes = bufferReader.ReadBytes(count); | |
def startPosition = _outputStream.Position; | |
base.Write(buffer, offset, count); | |
def endPosition = _outputStream.Position :> uint; | |
when(ShouldCompare(buffer, startPosition :> uint, endPosition)) | |
{ | |
// Determine the expected bytes | |
def reader = BinaryReader(_expectedStream); | |
def length = endPosition - startPosition; | |
when(length > 0) | |
{ | |
_ = _expectedStream.Seek(startPosition, SeekOrigin.Begin); | |
def expectedBytes = reader.ReadBytes(length :> int); | |
expectedBytes.ShouldMatch(actualBytes); | |
} | |
} | |
} | |
protected virtual ShouldCompare(buffer : array[byte], startPosition : uint, endPosition : uint) : bool | |
{ | |
true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment