Allow enums to be a ValueSource

16 views
Skip to first unread message

Juan Manuel Truppia

unread,
Feb 12, 2012, 11:32:37 AM2/12/12
to nunit-...@googlegroups.com
Hey, greetings to all

If I want to use all possible values of an enum, I have to create a helper method.

Suppose you have

public enum Month
{
    January,
    February,
  }

You could pass them as a Values attribute, but then you would have to list them all. What if you add new elements to the enum?
The solution is to add a helper method like this

public IEnumerable<Month> GetMonths()
{
    return Enum.GetValues(typeof(Month));
}

And then call it

[ValueSource("GetMonths")] Month exDestination,

This is a good solution, but what if you have lots of enums? You can't pass a parameterized method to ValueSource, that would solve the problem (you could just use Enum.GetValues inside the ValueSource)

It would be ideal to have some kind of override or a new attribute (maybe ValueSourceEnum?) that took the name of the enum and "expanded" it to all the possible values

Regards!

Juan Manuel

Charlie Poole

unread,
Feb 12, 2012, 12:05:57 PM2/12/12
to nunit-...@googlegroups.com
I'll probably add some syntax to do this in 3.0. See this feature request:
https://bugs.launchpad.net/nunit-3.0/+bug/629496

Charlie

> --
> You received this message because you are subscribed to the Google Groups
> "NUnit-Discuss" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/nunit-discuss/-/DUCVWXEH-boJ.
> 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.

Reply all
Reply to author
Forward
0 new messages