public void TestConversion() { string[] names = Enum.GetNames(typeof(ColorsWithFlag)); Assert.AreEqual("Red, Green, Blue, Yellow", String.Join(", ", names)); ColorsWithFlag color = (ColorsWithFlag)Enum.Parse(typeof(ColorsWithFlag), "Green"); //Values converted to Object will never be the same. Consider using AreEqual(). Assert.AreNotSame(ColorsWithFlag.Green, color); Assert.AreEqual(ColorsWithFlag.Green, color); ColorsWithFlag color2 = (ColorsWithFlag)Enum.Parse(typeof(ColorsWithFlag), "green", true); Assert.AreEqual(ColorsWithFlag.Green, color2); }
public void TestFlag() { //With Flag ColorsWithFlag colorsWithFlag = (ColorsWithFlag)3; Assert.AreEqual("Red, Green", colorsWithFlag.ToString()); //Without Flag Colors colors = (Colors)3; Assert.AreEqual("3", colors.ToString()); //Personally, I think below form is better presented than above ColorsWithFlag colorsWithFlagCombined = ColorsWithFlag.Red | ColorsWithFlag.Green; Assert.AreEqual(colorsWithFlagCombined, colorsWithFlag); Colors colorsCombined = Colors.Red | Colors.Green; Assert.AreEqual((Colors)3, colorsCombined); Assert.AreEqual(ColorsWithFlag.Red, colorsWithFlagCombined & ColorsWithFlag.Red); }