using Perforce.P4; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; namespace p4api.net.unit.test { /// ///This is a test class for StringListTest and is intended ///to contain all StringListTest Unit Tests /// [TestClass()] public class StringEnumTest { private TestContext testContextInstance; /// ///Gets or sets the test context which provides ///information about and functionality for the current test run. /// public TestContext TestContext { get { return testContextInstance; } set { testContextInstance = value; } } #region Additional test attributes // //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion enum myEnum { Alpha, beta, GAMMA, DeltaEpsilon }; /// ///A test for StringEnum Constructor /// [TestMethod()] public void StringEnumConstructorTest() { Perforce.P4.StringEnum target = new StringEnum(myEnum.Alpha); Assert.AreEqual(myEnum.Alpha, (myEnum) target); } /// ///A test for implicit cast from StringEnmum ==> T /// [TestMethod()] public void ImplicitOperatiorTest1() { StringEnum target = new StringEnum(myEnum.Alpha); myEnum actual = target; Assert.AreEqual(myEnum.Alpha, actual); } /// ///A test for implicit cast from T ==> StringEnmum /// [TestMethod()] public void ImplicitOperatiorTest2() { StringEnum target = myEnum.Alpha; StringEnum expected = new StringEnum(myEnum.Alpha); Assert.AreEqual(expected, target); } /// ///A test for implicit cast from string ==> StringEnmum /// [TestMethod()] public void ImplicitOperatiorTest3() { StringEnum target = "Alpha"; StringEnum expected = new StringEnum(myEnum.Alpha); Assert.AreEqual(expected, target); } /// ///A test for equality and inequality operators /// [TestMethod()] public void EqualityOperatiorTest() { StringEnum target = new StringEnum(myEnum.Alpha); ; StringEnum expected = new StringEnum(myEnum.Alpha); StringEnum notExpected = new StringEnum(myEnum.beta); myEnum expected2 = myEnum.Alpha; myEnum notExpected2 = myEnum.beta; Assert.IsTrue(expected == target); Assert.IsTrue(target == expected); Assert.IsTrue(notExpected != target); Assert.IsTrue(target != notExpected); Assert.IsTrue(expected2 == target); Assert.IsTrue(target == expected); Assert.IsTrue(notExpected2 != target); Assert.IsTrue(target != notExpected2); } /// ///A test for ToString /// [TestMethod()] public void ToStringTest() { StringEnum target = new StringEnum(myEnum.Alpha); ; string expected = "Alpha"; string actual = target.ToString(); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.beta); ; expected = "beta"; actual = target.ToString(); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.GAMMA); ; expected = "GAMMA"; actual = target.ToString(); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.DeltaEpsilon); expected = "DeltaEpsilon"; actual = target.ToString(); Assert.AreEqual(expected, actual); } /// ///A test for ToString(CASE) /// [TestMethod()] public void ToStringTest2() { // lower StringEnum target = new StringEnum(myEnum.Alpha); ; string expected = "Alpha".ToLower(); string actual = target.ToString(StringEnumCase.Lower); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.beta); ; expected = "beta".ToLower(); actual = target.ToString(StringEnumCase.Lower); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.GAMMA); ; expected = "GAMMA".ToLower(); actual = target.ToString(StringEnumCase.Lower); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.DeltaEpsilon); expected = "DeltaEpsilon".ToLower(); actual = target.ToString(StringEnumCase.Lower); Assert.AreEqual(expected, actual); //upper target = new StringEnum(myEnum.Alpha); ; expected = "Alpha".ToUpper(); actual = target.ToString(StringEnumCase.Upper); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.beta); ; expected = "beta".ToUpper(); actual = target.ToString(StringEnumCase.Upper); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.GAMMA); ; expected = "GAMMA".ToUpper(); actual = target.ToString(StringEnumCase.Upper); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.DeltaEpsilon); expected = "DeltaEpsilon".ToUpper(); actual = target.ToString(StringEnumCase.Upper); Assert.AreEqual(expected, actual); //keep case target = new StringEnum(myEnum.Alpha); ; expected = "Alpha"; actual = target.ToString(StringEnumCase.None); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.beta); ; expected = "beta"; actual = target.ToString(StringEnumCase.None); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.GAMMA); ; expected = "GAMMA"; actual = target.ToString(StringEnumCase.None); Assert.AreEqual(expected, actual); target = new StringEnum(myEnum.DeltaEpsilon); expected = "DeltaEpsilon"; actual = target.ToString(StringEnumCase.None); Assert.AreEqual(expected, actual); } /// ///A test for TryParse /// [TestMethod()] public void TryParseTest() { StringEnum target = null; StringEnum.TryParse("Alpha", ref target); myEnum expected = myEnum.Alpha; myEnum actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("beta", ref target); expected = myEnum.beta; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("GAMMA", ref target); expected = myEnum.GAMMA; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("DeltaEpsilon", ref target); expected = myEnum.DeltaEpsilon; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); // this parse should not work and leave the target as null target = null; StringEnum.TryParse("badvaluestring", ref target); Assert.AreEqual(null, target); } /// ///A test for TryParse with IgnoreCase /// [TestMethod()] public void TryParseTest2() { StringEnum target = null; StringEnum.TryParse("Alpha", true, ref target); myEnum expected = myEnum.Alpha; myEnum actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("ALPhA", true, ref target); expected = myEnum.Alpha; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("beta", true, ref target); expected = myEnum.beta; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("BETa", true, ref target); expected = myEnum.beta; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("GAMMA", true, ref target); expected = myEnum.GAMMA; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("gammA", true, ref target); expected = myEnum.GAMMA; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); target = null; StringEnum.TryParse("DeltaEpsilon", true, ref target); expected = myEnum.DeltaEpsilon; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); StringEnum.TryParse("DELTAepsilon", true, ref target); expected = myEnum.DeltaEpsilon; actual = target; // cast to myEnum Assert.AreEqual(expected, actual); // this parse should not work and leave the target as null target = null; StringEnum.TryParse("badvaluestring", true, ref target); Assert.AreEqual(null, target); } } }