Skip to content

Instantly share code, notes, and snippets.

@Shamar
Last active December 28, 2015 03:19
Show Gist options
  • Select an option

  • Save Shamar/7433830 to your computer and use it in GitHub Desktop.

Select an option

Save Shamar/7433830 to your computer and use it in GitHub Desktop.

Revisions

  1. Shamar revised this gist Nov 12, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions CounterExample.cs
    Original file line number Diff line number Diff line change
    @@ -13,8 +13,8 @@ public interface IFluentCounter
    public interface IFluentCounter<T> : IFluentCounter
    where T : IFluentCounter<T>
    {
    T Increase();
    T Decrease();
    new T Increase();
    new T Decrease();
    }

    public class NaturalCounter : IFluentCounter<NaturalCounter>
  2. Shamar revised this gist Nov 12, 2013. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions CounterExample.cs
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,9 @@ namespace FluentSample
    {
    public interface IFluentCounter
    {
    IFluentCounter Increase();
    IFluentCounter Decrease();

    int Value { get; }
    }
    public interface IFluentCounter<T> : IFluentCounter
    @@ -46,6 +49,17 @@ public int Value
    get { return _value; }
    }


    IFluentCounter IFluentCounter.Increase()
    {
    return Increase();
    }

    IFluentCounter IFluentCounter.Decrease()
    {
    return Decrease();
    }

    #endregion
    }

    @@ -83,6 +97,16 @@ public int Value
    get { return _inner.Value; }
    }

    IFluentCounter IFluentCounter.Increase()
    {
    return Increase();
    }

    IFluentCounter IFluentCounter.Decrease()
    {
    return Decrease();
    }

    #endregion
    }

  3. Shamar created this gist Nov 12, 2013.
    141 changes: 141 additions & 0 deletions CounterExample.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,141 @@
    using System;
    using NUnit.Framework;

    namespace FluentSample
    {
    public interface IFluentCounter
    {
    int Value { get; }
    }
    public interface IFluentCounter<T> : IFluentCounter
    where T : IFluentCounter<T>
    {
    T Increase();
    T Decrease();
    }

    public class NaturalCounter : IFluentCounter<NaturalCounter>
    {
    private readonly Int32 _value;

    public NaturalCounter(Int32 value)
    {
    _value = value;
    }

    #region IFluentCounter<NaturalCounter> Members

    public NaturalCounter Increase()
    {
    return new NaturalCounter(_value + 1);
    }

    public NaturalCounter Decrease()
    {
    if (_value > 0)
    return new NaturalCounter(_value - 1);
    return new NaturalCounter(0);
    }

    #endregion

    #region IFluentCounter Members

    public int Value
    {
    get { return _value; }
    }

    #endregion
    }

    public class EchoingCounter<T> : IFluentCounter<EchoingCounter<T>>
    where T : IFluentCounter<T>
    {
    private readonly T _inner;
    public EchoingCounter(T inner)
    {
    _inner = inner;
    }

    #region IFluentCounter<EchoingCounter<T>> Members

    public EchoingCounter<T> Increase()
    {
    Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

    return new EchoingCounter<T>(_inner.Increase());
    }

    public EchoingCounter<T> Decrease()
    {
    Console.WriteLine(System.Reflection.MethodBase.GetCurrentMethod().Name);

    return new EchoingCounter<T>(_inner.Decrease());
    }

    #endregion

    #region IFluentCounter Members

    public int Value
    {
    get { return _inner.Value; }
    }

    #endregion
    }

    public abstract class FluentCounterImplementationsTesterBase<T>
    where T : IFluentCounter<T>
    {
    protected abstract T StartWith(Int32 value);

    [Test]
    public void Increase_twiceFromZero_produceANewInstanceWithValue2()
    {
    // arrange:
    T toTest = StartWith(0);

    // act:
    T result = toTest.Increase().Increase();

    // assert:
    Assert.AreNotSame(toTest, result);
    Assert.AreEqual(2, result.Value);
    }

    [Test]
    public void Decrease_twiceFromFour_produceANewInstanceWithValue2()
    {
    // arrange:
    T toTest = StartWith(4);

    // act:
    T result = toTest.Decrease().Decrease();

    // assert:
    Assert.AreNotSame(toTest, result);
    Assert.AreEqual(2, result.Value);
    }
    }

    [TestFixture]
    public class NaturalCounterTester : FluentCounterImplementationsTesterBase<NaturalCounter>
    {
    protected override NaturalCounter StartWith(Int32 value)
    {
    return new NaturalCounter(value);
    }
    }

    [TestFixture]
    public class EchoingCounterTester : FluentCounterImplementationsTesterBase<EchoingCounter<NaturalCounter>>
    {
    protected override EchoingCounter<NaturalCounter> StartWith(Int32 value)
    {
    return new EchoingCounter<NaturalCounter>(new NaturalCounter(value));
    }
    }

    }