Should Generic Theories be Supported?

10 views
Skip to first unread message

Charlie Poole

unread,
Jul 21, 2010, 12:18:40 AM7/21/10
to nunit-...@googlegroups.com
Hi All,

We have a bug indicating that the following doesn't work...

[Datapoint]
public double[,] Array2X2 = new double[,] { { 1, 0 }, { 0, 1 } };

[Theory]
public void TestForArbitraryArray<T>(T[,] array)
{
...
}

That's understandable, because the Datapoints are required to exactly match the
argument type and double[,] is not an _exact_ match for T[,], even
though it would
a proper match when calling the method. The problem is that there can never be
an exact match for this argument, since Datapoints will always be of some
particular type rather than generic.

I'm inclined not to support this. I'm at a loss to think of a good use
case and it
seems to open up the possibility of a Theory that eats up any
datapoint whatsoever,
like this one...

[Theory]
public void TestForAnythingWhatsoevery<T>(T theThing)
{
...
}

Any thoughts on this?

Charlie

RJV

unread,
Jul 21, 2010, 3:13:09 AM7/21/10
to nunit-...@googlegroups.com
Hi Charlie,
 
What if the DataPoint are supplied by a method that returns a generic type?
 
[Datapoint]
private T thing=SomeClass.StaticMethodReturnsGenericType();
       [Theory]
       public void TestForAnythingWhatsoevery<T>(T theThing)
       {
            ...
       }
 
Same could be the use case for an array of generic type, too, don't you think? A method that returns an array of T to the datapoints attribute.

Regards,
 
Jv

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To post to this group, send email to nunit-...@googlegroups.com.
To unsubscribe from this group, send email to nunit-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nunit-discuss?hl=en.




--
Regards,

Ravichandran Jv
http://ravichandranjv.blogspot.com

Charlie Poole

unread,
Jul 21, 2010, 3:40:52 AM7/21/10
to nunit-...@googlegroups.com
This would make a bit more sense to me. Allowing methods to return datapoints is
an extension I'd like to make in the future. Right now, datapoints can
only be fields.

Charlie

RJV

unread,
Jul 21, 2010, 5:57:46 AM7/21/10
to nunit-...@googlegroups.com
Hi Charlie,
 
It does work now. I have used a method to generate dataoints for the Theory. 

[TestFixture]

public class GenomeSeries{

//private static string s = "AT<GC>,A<TA>";

private static GenomeSeries genomeObject = new GenomeSeries();

[Datapoints]

private string[] patternGeneration = genomeObject.GenerateData(s);

// GenerateData method returns the below strings

//private static string[] pattern = new string[4] { "ATGAT", "ATGAA", "ATCAT", "ATCAA" };

[Theory]

public void GenerateGenomeSequence(string x){

Assume.That(x == "ATGAT" || x == "ATGAA" || x == "ATCAT" || x == "ATCAA");

}}

// AT<GC>", "A<TA>

// Adenine, Guanine, Cytosine, Thianine,

Charlie Poole

unread,
Jul 21, 2010, 9:24:34 AM7/21/10
to nunit-...@googlegroups.com
Hi JV,

Yes, but the Datapoint is still a field, which is initialized by your
method. The
future extension I was writing about would allow you to write...

[Datapoints]
string[] GenerateGenomeData()
{
yield return "ATGAT";
yield return "ATGAA";
yield return "ATCAT";
yield return "ATCAA";
}

or perhaps something a little more sophisticated. :-)

Charlie

RJV

unread,
Jul 21, 2010, 11:04:30 PM7/21/10
to nunit-...@googlegroups.com
Hi Charlie,
 
Fyi, this unit test (or theory) received a "WOW, this is really interesting" from Naresh Jain in the Agile India 2010 conference where I used this in the "Programming with the stars" contest and the audience response was like a soccer stadium, they erupted! This must surely be the best feature of NUnit, all thanks to you (I guess?). I am certainly looking forward to the more "sophisticated" version of it!
 
Regards,
 
Jv

Charlie Poole

unread,
Jul 21, 2010, 11:10:28 PM7/21/10
to nunit-...@googlegroups.com
Yes, I find Theories to be the most interesting new direction in TDD. So far we
have only a small experimental implementation but the possibilities are great.

Charlie

Kelly Anderson

unread,
Jul 21, 2010, 1:54:59 PM7/21/10
to nunit-...@googlegroups.com
On Tue, Jul 20, 2010 at 10:18 PM, Charlie Poole <nuni...@gmail.com> wrote:
> Hi All,
>
> We have a bug indicating that the following doesn't work...
>
>        [Datapoint]
>        public double[,] Array2X2 = new double[,] { { 1, 0 }, { 0, 1 } };
>
>        [Theory]
>        public void TestForArbitraryArray<T>(T[,] array)
>        {
>            ...
>        }

I like the idea of a theory that can take any object. Seems like that
might be good for writing tests of things like conversion tostring,
streaming to disk and reading back from disk, equality and other base
level functionality that you might want in all of your objects.

I don't use generics enough to have a good feeling as to whether the
above would be directly useful, but if all your generics subclassed a
base class, then you could write a Theory for that base class... just
a thought.

-Kelly

Reply all
Reply to author
Forward
0 new messages