Example #1
0
        public static void Throws(Type exceptionType, AssertThrowsDelegate code, string message)
        {
            if (code == null)
            {
                Assert.Fail("No code provided for the test.");
            }

            Exception exception = null;

            try {
                code();
            } catch (Exception ex) {
                exception = ex;
            }

            if (exceptionType == null)
            {
                if (exception == null)
                {
                    Assert.Fail("{0}{1}Expected: any exception thrown{1}But was: no exception thrown{1}",
                                message, Environment.NewLine);
                }
                return;
            }

            if (exception == null || exception.GetType() != exceptionType)
            {
                Assert.Fail("{0}{1}Expected: {2}{1}But was: {3}{1}{4}{1}",
                            message,
                            Environment.NewLine,
                            exceptionType,
                            exception == null ? "no exception" : exception.GetType().ToString(),
                            exception == null ? "no exception" : exception.ToString());
            }
        }
Example #2
0
        public static void Throws(Type exceptionType, AssertThrowsDelegate code, string message)
        {
            if (code == null)
            {
                throw new ArgumentNullException("code");
            }
            if (exceptionType == null)
            {
                throw new ArgumentNullException("exceptionType");
            }

            try {
                code();
            } catch (Exception ex) {
                if (ex.GetType() != exceptionType)
                {
                    Assert.Fail(message + " (got exception of type '" + ex.GetType() + "')");
                }

                // good!
                return;
            }

            Assert.Fail(message);
        }
Example #3
0
 public static void TrapInconclusiveException <E>(AssertThrowsDelegate code) where E : Exception
 {
     try
     {
         code();
     }
     catch (Exception e)
     {
         if (e.GetType() == typeof(E))
         {
             Assert.Inconclusive(e.ToString());
         }
         else
         {
             throw;
         }
     }
 }
Example #4
0
        public static void AssertThrows <E>(AssertThrowsDelegate code) where E : Exception
        {
            bool exceptionNotThrown = false;

            try
            {
                code();
                exceptionNotThrown = true;
            }
            catch (Exception e)
            {
                Assert.AreEqual <string>(typeof(E).FullName, e.GetType().FullName);
                //pass
            }
            if (exceptionNotThrown)
            {
                Assert.Fail("No exception was thrown");
            }
        }
Example #5
0
 public static void Throws(AssertThrowsDelegate code, string message)
 {
     Throws(null, code, message);
 }
Example #6
0
 public static void Throws <ET> (AssertThrowsDelegate code, string message)
 {
     Throws(typeof(ET), code, message);
 }
Example #7
0
 public static void Throws(Type exceptionType, AssertThrowsDelegate code)
 {
     Throws(exceptionType, code, String.Empty);
 }
Example #8
0
 public static void Throws <EX> (AssertThrowsDelegate code)
 {
     Throws(typeof(EX), code);
 }