user4143172
user4143172

Reputation:

How do you test an enum flag combination?

Let’s say I have an enum flag:

[Flags]
public enum ColorType
{
    None = 0,
    Red = 1 << 0,
    White = 1<<1,
    Yellow = 1 << 2,
    Blue = 1 << 3,
    All = Red | White | Yellow | Blue
}

I have the below function, which parameter is a combination of flag, such as DoSomething( ColorType.Blue | ColorType.Yellow ).

public void DoSomethingr(ColorType theColorTypes)
{
        if (theColorTypes.HasFlag(All)) Foo1();
        if (theColorTypes.HasFlag(White) && theColorTypes.HasFlag(Red) )  Foo2();
        if (!theColorTypes.HasFlag(Blue)) Foo3();
        . . . 
}

Is there an easy way to test all of possible flag bitwise combination?

[Test] 
public void Test1(ColorType.Red | ColorType.Yellow | ColorType.White) 

[Test]
public void Test1(ColorType.Red | ColorType.Yellow | ColorType.white | ColorType.Blue) 

Thanks

Upvotes: 2

Views: 1887

Answers (2)

Silvermind
Silvermind

Reputation: 5944

Just my two cents and this could probably be improved to accept 'other' value types as well, but as an alternative when you like extension methods:

public static class EnumExtensions
{
    public static bool HasFlags<TEnum>(this TEnum @enum,
                                       TEnum flag,
                                       params TEnum[] flags)
        where TEnum : struct
    {
        var type = typeof(TEnum);
        if (!type.IsEnum)
            throw new ArgumentException("@enum is not an Enum");

        var hasFlagsMethod = type.GetMethod("HasFlag");
        var hasFlag = new Func<TEnum, bool>(e =>
        {
            return (bool)hasFlagsMethod.Invoke(@enum, new object[] { e });
        });

        // test the first flag argument
        if (!hasFlag(flag))
            return false;

        // test the params flags argument
        foreach (var flagValue in flags)
        {
            if (!hasFlag(flagValue))
                return false;
        }
        return true;
    }
}


[Flags]
public enum ColorType
{
    None = 0,
    Red = 1 << 0,
    White = 1 << 1,
    Yellow = 1 << 2,
    Blue = 1 << 3,
    All = Red | White | Yellow | Blue
}

Call it like this:

class Program
{
    static void Main(string[] args)
    {
        var color = ColorType.Red;
        Console.WriteLine(color.HasFlags(ColorType.Red)); // true;
        Console.WriteLine(color.HasFlags(ColorType.Red, ColorType.Blue)); // false;

        color = ColorType.All;
        Console.WriteLine(color.HasFlags(ColorType.Red, ColorType.Blue)); // true;

        Console.ReadLine();
    }
}

Upvotes: 0

Patrick Quirk
Patrick Quirk

Reputation: 23747

Loop over all the possible values and put it in a TestCaseSource to generate a different test for each enumeration value:

public IEnumerable<ColorType> TestCaseSource 
{ 
    get
    {
        int start = (int)ColorType.None;
        int count = (int)ColorType.All - start + 1;
        return Enumerable.Range(start, count).Select(i => (ColorType)i); 
    } 
}

[TestCaseSource("TestCaseSource")]
public void Test1(ColorType colorType)
{
    // whatever your test is
}

Upvotes: 1

Related Questions