phuccaoca123 / mb-unit

Automatically exported from code.google.com/p/mb-unit
0 stars 0 forks source link

Contract-based testing #22

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Extract a general pattern for testing types based on partially declarative
contracts.  This builds on the notion of [TypeFixture] and other attributes
but extracts more common patterns in a reusable form.

(Note: This is all highly speculative.)

public class Contract<T>
{
    public T Instance;
}

[Contract]
public class CollectionContract<T> : Contract<ICollection<T>>
{
    [Invariant]
    public void CountAlwaysEqualsNumberOfItemsInEnumeration()
    {
        int enumerationCount;
        foreach (T item in Instance)
            enumerationCount += 1;

        Assert.AreEqual(enumerationCount, Instance.Count, "Collection count
should equal number of items in enumeration.");
    }

    // wrappers check pre/post conditions around an operation
    public void Add(T item)
    {
        int oldCount = Instance.Count;

        Instance.Add(item);

        Assert.AreEqual(oldCount + 1, Instance.Count, "Add should increase
collection count by 1.");
    }

    // specific tests
    [Test]
    public void AddThenRemove()
    {
        // etc...
    }
}

[Contract]
[PropertyContract("NonNullableProperty", Nullable=false)]
[PropertyContract("ComplexTypeProperty", Factory="SomeFactory")]
[PropertyContract("IgnoredProperty", Ignored=true)]
[PropertyContract("FancyProperty", Contract=typeof(SomePropertyContract))]
public class POCOContract<T> : Contract<T>
{
}

Original issue reported on code.google.com by jeff.br...@gmail.com on 27 Aug 2007 at 8:20

GoogleCodeExporter commented 8 years ago

Original comment by jeff.br...@gmail.com on 30 Oct 2007 at 2:24

GoogleCodeExporter commented 8 years ago

Original comment by jeff.br...@gmail.com on 17 Mar 2008 at 10:57

GoogleCodeExporter commented 8 years ago
Yann took care of this stuff.

Original comment by jeff.br...@gmail.com on 18 Sep 2008 at 8:20