I've understood the decorator pattern.
Here is my little example for extending StreamReder class by password, when it reads.
using System;
using System.IO;
namespace Decorator
{
public class LoginStream
{
private StreamReader _streamReader;
private string _password;
public LoginStream (StreamReader streamReader, string password){
_streamReader = streamReader;
_password = password;
}
public string Read (string password){
if (_password == password) {
return _streamReader.ReadToEnd ();
}
throw new Exception ("Ivalid Password");
}
}
}
And the tests: using System;
using System.IO;
using Decorator;
using NUnit.Framework;
namespace DecoratorTests
{
[TestFixture()]
public class Test
{
[Test()]
public void LoginStream_Read_return_string_when_has_a_valid_password ()
{
var stream = new StreamReader("/home/luis/tmp/Prueba/Prueba.sln");
var password = "password";
var loginStream = new LoginStream(stream, password);
var result = loginStream.Read(password);
Assert.IsNotEmpty (result);
}
[Test()]
[ExpectedException(typeof(Exception))]
public void LoginStraeam_Read_return_exception_when_has_an_invalid_password ()
{
var stream = new StreamReader("path");
var password = "password";
var loginStream = new LoginStream(stream, password);
loginStream.Read("invalidPassword");
Assert.Fail ("Exception expected");
}
}
}