2

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

2 Answers2

1

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
}
Patrick Quirk
  • 23,334
  • 2
  • 57
  • 88
  • Where All is not defined, we can do that with var all = Enum.GetValues(typeof(ColorType)).Cast().Aggregate((x, y) => x | y); – Pramod B R Sep 18 '20 at 17:01
0

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();
    }
}
Silvermind
  • 5,791
  • 2
  • 24
  • 44