Example #1
0
		public void TestParse2 ()
		{
			try {
				String name = "huh?";
				Enum.Parse (null, name, true);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.IsNotNull (ex.ParamName, "#A5");
				Assert.AreEqual ("enumType", ex.ParamName, "#A6");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				Enum.Parse (x.GetType (), null, true);
				Assert.Fail ("#B1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.IsNotNull (ex.ParamName, "#B5");
				Assert.AreEqual ("value", ex.ParamName, "#B6");
			}

			try {
				String bad = "huh?";
				Enum.Parse (bad.GetType (), bad, true);
				Assert.Fail ("#C1");
			} catch (ArgumentException ex) {
				// Type provided must be an Enum
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
				Assert.IsNull (ex.InnerException, "#D3");
				Assert.IsNotNull (ex.Message, "#D4");
				Assert.IsNotNull (ex.ParamName, "#D5");
				Assert.AreEqual ("enumType", ex.ParamName, "#D6");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				String bad = "";
				Enum.Parse (x.GetType (), bad, true);
				Assert.Fail ("#E1");
			} catch (ArgumentException ex) {
				// Must specify valid information for parsing
				// in the string
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
				Assert.IsNull (ex.InnerException, "#E3");
				Assert.IsNotNull (ex.Message, "#E4");
				Assert.IsNull (ex.ParamName, "#E5");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				String bad = " ";
				Enum.Parse (x.GetType (), bad, true);
				Assert.Fail ("#F1");
			} catch (ArgumentException ex) {
				// Must specify valid information for parsing
				// in the string
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
				Assert.IsNull (ex.InnerException, "#F3");
				Assert.IsNotNull (ex.Message, "#F4");
				Assert.IsFalse (ex.Message.IndexOf ("' '") != -1, "#F5");
				Assert.IsNull (ex.ParamName, "#F6");
			}

			try {
				String bad = "huh?";
				TestingEnum x = TestingEnum.Test;
				Enum.Parse (x.GetType (), bad, true);
				Assert.Fail ("#G1");
			} catch (ArgumentException ex) {
				// Requested value 'huh?' was not found
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#G2");
				Assert.IsNull (ex.InnerException, "#G3");
				Assert.IsNotNull (ex.Message, "#G4");
				Assert.IsTrue (ex.Message.IndexOf ("'huh?'") != -1, "#G5");
				Assert.IsNull (ex.ParamName, "#G6");
			}

			try {
				String bad = "test";
				TestingEnum x = TestingEnum.Test;
				Enum.Parse (x.GetType (), bad, false);
				Assert.Fail ("#H1");
			} catch (ArgumentException ex) {
				// Requested value 'test' was not found
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#H2");
				Assert.IsNull (ex.InnerException, "#H3");
				Assert.IsNotNull (ex.Message, "#H4");
				Assert.IsTrue (ex.Message.IndexOf ("'test'") != -1, "#H5");
				Assert.IsNull (ex.ParamName, "#H6");
			}

			TestingEnum t1 = new TestingEnum ();
			Assert.AreEqual (TestingEnum.This, Enum.Parse (t1.GetType (), "this", true), "#I1");
			Assert.AreEqual (TestingEnum.Is, Enum.Parse (t1.GetType (), "is", true), "#I2");
			Assert.AreEqual (TestingEnum.A, Enum.Parse (t1.GetType (), "a", true), "#I3");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "test", true), "#I4");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "    \n\ntest\t", true), "#I5");

			Assert.AreEqual (TestingEnum.Is, Enum.Parse (t1.GetType (), "This,is", true), "#J1");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "This,test", true), "#J2");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "This,is,A", true), "#J3");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "   \n\tThis \t\n,    is,a \n", true), "#J4");
		}
Example #2
0
		public void TestIsDefined ()
		{
			try {
				Enum.IsDefined (null, 1);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.IsNotNull (ex.ParamName, "#A5");
				Assert.AreEqual ("enumType", ex.ParamName, "#A6");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				Enum.IsDefined (x.GetType (), null);
				Assert.Fail ("#B1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.IsNotNull (ex.ParamName, "#B5");
				Assert.AreEqual ("value", ex.ParamName, "#B6");
			}

			try {
				String bad = "huh?";
				int i = 4;
				Enum.IsDefined (bad.GetType (), i);
				Assert.Fail ("#C1");
			} catch (ArgumentException ex) {
				// Type provided must be an Enum
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
				Assert.IsNull (ex.InnerException, "#C3");
				Assert.IsNotNull (ex.Message, "#C4");
				Assert.IsNotNull (ex.ParamName, "#C5");
				Assert.AreEqual ("enumType", ex.ParamName, "#C6");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				short i = 4;
				Enum.IsDefined (x.GetType (), i);
				Assert.Fail ("#D1");
			} catch (ArgumentException ex) {
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
				Assert.IsNull (ex.InnerException, "#D3");
				Assert.IsNotNull (ex.Message, "#D4");
				Assert.IsNull (ex.ParamName, "#D5");
			}

			Enum t1 = new TestingEnum ();
			int valCount = Enum.GetValues (t1.GetType ()).Length;
			for (int i = 0; i < valCount; i++)
				Assert.IsTrue (Enum.IsDefined (t1.GetType (), i), "#F1:" + i);
			Assert.IsFalse (Enum.IsDefined (t1.GetType (), valCount), "#F2");
			Assert.IsFalse (Enum.IsDefined (typeof (TestingEnum), "huh?"), "#F3");
		}
Example #3
0
		public void TestParse1 ()
		{
			try {
				String name = "huh?";
				Enum.Parse (null, name);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.IsNotNull (ex.ParamName, "#A5");
				Assert.AreEqual ("enumType", ex.ParamName, "#A6");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				Enum.Parse (x.GetType (), null);
				Assert.Fail ("#B1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.IsNotNull (ex.ParamName, "#B5");
				Assert.AreEqual ("value", ex.ParamName, "#B6");
			}

			try {
				String bad = "huh?";
				Enum.Parse (bad.GetType (), bad);
				Assert.Fail ("#C1");
			} catch (ArgumentException ex) {
				// Type provided must be an Enum
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#C2");
				Assert.IsNull (ex.InnerException, "#C3");
				Assert.IsNotNull (ex.Message, "#C4");
				Assert.IsNotNull (ex.ParamName, "#C5");
				Assert.AreEqual ("enumType", ex.ParamName, "#C6");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				String bad = "";
				Enum.Parse (x.GetType (), bad);
				Assert.Fail ("#D1");
			} catch (ArgumentException ex) {
				// Must specify valid information for parsing
				// in the string
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
				Assert.IsNull (ex.InnerException, "#D3");
				Assert.IsNotNull (ex.Message, "#D4");
				Assert.IsNull (ex.ParamName, "#D5");
			}

			try {
				TestingEnum x = TestingEnum.Test;
				String bad = " ";
				Enum.Parse (x.GetType (), bad);
				Assert.Fail ("#E1");
			} catch (ArgumentException ex) {
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#E2");
				Assert.IsNull (ex.InnerException, "#E3");
				Assert.IsNotNull (ex.Message, "#E4");
				Assert.IsNull (ex.ParamName, "#E5");
			}

			try {
				String bad = "huh?";
				TestingEnum x = TestingEnum.Test;
				Enum.Parse (x.GetType (), bad);
				Assert.Fail ("#F1");
			} catch (ArgumentException ex) {
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#F2");
				Assert.IsNull (ex.InnerException, "#F3");
				Assert.IsNotNull (ex.Message, "#F4");
				Assert.IsNull (ex.ParamName, "#F5");
			}

			TestingEnum t1 = new TestingEnum ();
			Assert.AreEqual (TestingEnum.This, Enum.Parse (t1.GetType (), "This"), "#G1");
			Assert.AreEqual (TestingEnum.Is, Enum.Parse (t1.GetType (), "Is"), "#G2");
			Assert.AreEqual (TestingEnum.A, Enum.Parse (t1.GetType (), "A"), "#G3");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "Test"), "#G4");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "    \n\nTest\t"), "#G5");
			Assert.AreEqual (TestingEnum.Is, Enum.Parse (t1.GetType (), "This,Is"), "#G6");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "This,Test"), "#G7");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "This,Is,A"), "#G8");
			Assert.AreEqual (TestingEnum.Test, Enum.Parse (t1.GetType (), "   \n\tThis \t\n,    Is,A \n"), "#G9");
		}
Example #4
0
		public void TestGetUnderlyingType ()
		{
			try {
				Enum.GetUnderlyingType (null);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.IsNotNull (ex.ParamName, "#A5");
				Assert.AreEqual ("enumType", ex.ParamName, "#A6");
			}

			try {
				String bad = "huh?";
				Enum.GetUnderlyingType (bad.GetType ());
				Assert.Fail ("#B1");
			} catch (ArgumentException ex) {
				// Type provided must be an Enum
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.IsNotNull (ex.ParamName, "#B5");
				Assert.AreEqual ("enumType", ex.ParamName, "#B6");
			}

			short sh = 5;
			int i = 5;
			Enum t1 = new TestingEnum ();
			Enum t2 = new TestShortEnum ();
			Assert.AreEqual (i.GetType (), Enum.GetUnderlyingType (t1.GetType ()), "#C1");
			Assert.AreEqual (sh.GetType (), Enum.GetUnderlyingType (t2.GetType ()), "#C2");
		}
Example #5
0
		public void TestGetValues ()
		{
			try {
				Enum.GetValues (null);
				Assert.Fail ("#A1");
			} catch (ArgumentNullException ex) {
				Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#A2");
				Assert.IsNull (ex.InnerException, "#A3");
				Assert.IsNotNull (ex.Message, "#A4");
				Assert.IsNotNull (ex.ParamName, "#A5");
				Assert.AreEqual ("enumType", ex.ParamName, "#A6");
			}

			try {
				String bad = "huh?";
				Enum.GetValues (bad.GetType ());
				Assert.Fail ("#B1");
			} catch (ArgumentException ex) {
				// Type provided must be an Enum
				Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#B2");
				Assert.IsNull (ex.InnerException, "#B3");
				Assert.IsNotNull (ex.Message, "#B4");
				Assert.IsNotNull (ex.ParamName, "#B5");
				Assert.AreEqual ("enumType", ex.ParamName, "#B6");
			}

			Enum t1 = new TestingEnum ();
			Array a1 = Enum.GetValues (t1.GetType ());
			for (int i = 0; i < a1.Length; i++)
				Assert.AreEqual ((TestingEnum) i, a1.GetValue (i), "#C1");

			Enum t2 = new TestShortEnum ();
			Array a2 = Enum.GetValues (t2.GetType ());
			for (short i = 0; i < a1.Length; i++)
				Assert.AreEqual ((TestShortEnum) i, a2.GetValue (i), "#C2");
		}
	public void TestToObject() {
		{
			bool errorThrown = false;
			try {
				Enum.ToObject(null, 1);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null type not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = false;
			try {
				Enum.ToObject("huh?".GetType(), 1);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("null type not caught.", 
			       errorThrown);
		}
		{
			TestingEnum t1 = new TestingEnum();
			AssertEquals("Should get object",
				     TestingEnum.This,
				     Enum.ToObject(t1.GetType(), 0));
		}
		// TODO - should probably test all the different underlying types
	}
	public void TestParse2() {
		{
			bool errorThrown = true;
			try {
				String name = "huh?";
				Enum.Parse(null, name, true);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null first arg not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = true;
			try {
				TestingEnum x = TestingEnum.Test;
				Enum.Parse(x.GetType(), null, true);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null second arg not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = true;
			try {
				String bad = "huh?";
				Enum.Parse(bad.GetType(), bad, true);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("non-enum type not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = true;
			try {
				TestingEnum x = TestingEnum.Test;
				String bad = "";
				Enum.Parse(x.GetType(), bad, true);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("empty string not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = true;
			try {
				TestingEnum x = TestingEnum.Test;
				String bad = " ";
				Enum.Parse(x.GetType(), bad, true);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("space-only string not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = true;
			try {
				String bad = "huh?";
				TestingEnum x = TestingEnum.Test;
				Enum.Parse(x.GetType(), bad, true);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("not-in-enum error not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = true;
			try {
				String bad = "test";
				TestingEnum x = TestingEnum.Test;
				Enum.Parse(x.GetType(), bad, false);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("not-in-enum error not caught.", 
			       errorThrown);
		}
		{
			TestingEnum t1 = new TestingEnum();
			AssertEquals("parse first enum",
				     TestingEnum.This, 
				     Enum.Parse(t1.GetType(), "this", true));
			AssertEquals("parse second enum",
				     TestingEnum.Is, 
				     Enum.Parse(t1.GetType(), "is", true));
			AssertEquals("parse third enum",
				     TestingEnum.A, 
				     Enum.Parse(t1.GetType(), "a", true));
			AssertEquals("parse last enum",
				     TestingEnum.Test, 
				     Enum.Parse(t1.GetType(), "test", true));

			AssertEquals("parse last enum with whitespace",
				     TestingEnum.Test, 
				     Enum.Parse(t1.GetType(), "    \n\ntest\t", true));

			AssertEquals("parse bitwise-or enum",
				     TestingEnum.Is, 
				     Enum.Parse(t1.GetType(), "This,is", true));
			AssertEquals("parse bitwise-or enum",
				     TestingEnum.Test, 
				     Enum.Parse(t1.GetType(), "This,test", true));
			AssertEquals("parse bitwise-or enum",
				     TestingEnum.Test, 
				     Enum.Parse(t1.GetType(), "This,is,A", true));

			AssertEquals("parse bitwise-or enum with whitespace",
				     TestingEnum.Test, 
					 Enum.Parse(t1.GetType(), "   \n\tThis \t\n,    is,a \n",
						        true));
		}
	}
	public void TestIsDefined() {
		{
			bool errorThrown = false;
			try {
				Enum.IsDefined(null, 1);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null first arg not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = false;
			try {
				TestingEnum x = TestingEnum.Test;
				Enum.IsDefined(x.GetType(), null);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null second arg not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = false;
			try {
				String bad = "huh?";
				int i = 4;
				Enum.IsDefined(bad.GetType(), i);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("non-enum type not caught.", 
			       errorThrown);
		}

		try {
			TestingEnum x = TestingEnum.Test;
			short i = 4;
			Enum.IsDefined(x.GetType(), i);
			Fail("wrong underlying type not caught.");
		} catch (ArgumentException) {
		} catch (Exception e) {
			Fail("wrong Exception thrown ("+e.ToString()+")for underlying type not caught.");
		}

		// spec says yes, MS impl says no.
		//{
		//bool errorThrown = false;
		//try {
		//String bad = "huh?";
		//TestingEnum x = TestingEnum.Test;
		//Enum.IsDefined(x.GetType(), bad);
		//} catch (ExecutionEngineException) {
		//errorThrown = true;
		//}
		//Assert("non-enum object not caught.", 
		//errorThrown);
		//}
		{
			Enum t1 = new TestingEnum();
			int i = 0;
			for (i = 0; 
			     i < Enum.GetValues(t1.GetType()).Length; i++) {
				Assert("should have value for i=" + i,
				       Enum.IsDefined(t1.GetType(), i));
			}
			Assert("Shouldn't have value",
			       !Enum.IsDefined(t1.GetType(), i));
		}
	}
	public void TestGetValues() {
		{
			bool errorThrown = false;
			try {
				Enum.GetValues(null);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null type not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = false;
			try {
				String bad = "huh?";
				Enum.GetValues(bad.GetType());
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("non-enum type not caught.", 
			       errorThrown);
		}
		{
			Enum t1 = new TestingEnum();
			Array a1 = Enum.GetValues(t1.GetType());
			for (int i= 0; i < a1.Length; i++) {
				AssertEquals("wrong enum value",
					     (TestingEnum)i,
					     a1.GetValue(i));
			}
		}
		{
			Enum t1 = new TestShortEnum();
			Array a1 = Enum.GetValues(t1.GetType());
			for (short i= 0; i < a1.Length; i++) {
				AssertEquals("wrong short enum value",
					     (TestShortEnum)i,
					     a1.GetValue(i));
			}
		}
	}
Example #10
0
	public void TestGetUnderlyingType() {
		{
			bool errorThrown = false;
			try {
				Enum.GetUnderlyingType(null);
			} catch (ArgumentNullException) {
				errorThrown = true;
			}
			Assert("null type not caught.", 
			       errorThrown);
		}
		{
			bool errorThrown = false;
			try {
				String bad = "huh?";
				Enum.GetUnderlyingType(bad.GetType());
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("non-enum type not caught.", 
			       errorThrown);
		}
		{
			short sh = 5;
			int i = 5;
			Enum t1 = new TestingEnum();
			Enum t2 = new TestShortEnum();
			AssertEquals("Wrong default underlying type",
				     i.GetType(), 
				     Enum.GetUnderlyingType(t1.GetType()));
			AssertEquals("Not short underlying type",
				     sh.GetType(), 
				     Enum.GetUnderlyingType(t2.GetType()));
		}
	}