views:

232

answers:

3

I have a custom collection type of data. This data is sorted by three properties in their order, e.g. take the following example:

class Data
{
  public int PropertyA() { get; set; }
  public int PropertyB() { get; set; }
  public int PropertyC() { get; set; }
}

The collection must maintain the order of A, B, C, e.g.:

[A, B, C]
[1, 2, 5]
[1, 3, 3]
[1, 3, 4]
[1, 4, 1]
[2, 1, 2]
[3, 3, 1]
[3, 4, 2]

I'd like to write some tests to ensure that this order is maintained in the collection through the usual suspect Add and Remove operations. I'm using Gallio and MbUnit 3, and I think there must be an easy way to do this with their attributes, I just don't get it right now. Any ideas?

A: 

In MbUnit v2, you could use CollectionOrderFixture.. can't find it it MbUnit v3 though

Mauricio Scheffer
Unfortunately it seems this is gone in MbUnit v3. Thanks for the help, I'll reward you with the right answer!
__grover
I would implement this in v3 using the new ContractVerifiers (http://code.google.com/p/mb-unit/source/browse/trunk/v3/src/MbUnit/Samples/MbUnit.Samples/ContractVerifiers/Collection/SampleCollection.Test.cs)
Mauricio Scheffer
Try asking in the dev group (http://groups.google.com/group/gallio-dev) maybe there is a replacement in v3.
Mauricio Scheffer
+1  A: 

Yann Trevin has been working on a "CollectionContract" for MbUnit v3. I don't think it can handle ordered collections right now but I'm sure he'd be interested in adding that capability given an appropriate comparison delegate to describe the ordering invariant.

You'll find a example of this in the "SampleCollectionTest" fixture of the MbUnit.Samples project in MbUnit v3.0.6.

I recommend that you post your idea to the mbunitdev mailing list where he'll see it: http://groups.google.com/group/mbunitdev

Thanks, I'll post my request there later. Maybe the group has a better idea too.
__grover
A: 

MbUnit v3 has a new useful Assert.Sorted method. You just need to pass the enumeration instance to evaluate. If the enumerated objects implements IEquatable, then everything is automatic.

[Test]
public void MySimpleTest
{
   var array = new int[] { 1, 5, 9, 12, 26 };
   Assert.Sorted(array);
}

Otherwise, you still have the possibility to specify a custom comparison criterion (with the new handy structural equality comparer, for example).

[Test]
public void MyComplexTest
{
   var array = new Foo[] { new Foo(123), new Foo(456), new Foo(789) };
   Assert.Sorted(array, new StructuralEqualityComparer<Foo>
   {
      { x => x.Value }
   });
}

Have a look at the Gallio/MbUnit API doc reference for more details.

Yann Trevin