PageRenderTime 45ms CodeModel.GetById 40ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Manos.Tests/Nunit-Extensions/Nunit_Extension_Methods.cs

http://github.com/jacksonh/manos
C# | 100 lines | 58 code | 16 blank | 26 comment | 16 complexity | 6493ff739358cf8fe71dde2545969316 MD5 | raw file
  1//
  2// Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3//
  4// Permission is hereby granted, free of charge, to any person obtaining
  5// a copy of this software and associated documentation files (the
  6// "Software"), to deal in the Software without restriction, including
  7// without limitation the rights to use, copy, modify, merge, publish,
  8// distribute, sublicense, and/or sell copies of the Software, and to
  9// permit persons to whom the Software is furnished to do so, subject to
 10// the following conditions:
 11// 
 12// The above copyright notice and this permission notice shall be
 13// included in all copies or substantial portions of the Software.
 14// 
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 16// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 17// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 18// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 19// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 20// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 21// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 22//
 23//
 24
 25using System;
 26
 27using NUnit.Framework;
 28//
 29// Lets you use some of Nunits modern assert methods on monodevelops archaic nunit 
 30//
 31
 32namespace Manos.ShouldExt
 33{
 34	public delegate void TestSnippet ();
 35	
 36	public class Should {
 37	
 38		public static void Throw<T> (TestSnippet snippet) where T : Exception
 39		{
 40			try {
 41				snippet ();
 42			} catch (Exception e) {
 43				if (e.GetType () == typeof (T))
 44					return;
 45				throw new Exception (String.Format ("Invalid exception type. Expected '{0}' got '{1}'", typeof (T), e.GetType ()));
 46			}
 47		
 48			throw new Exception ("No exception thrown.");
 49		}
 50		
 51		public static void Throw<T> (TestSnippet snippet, string message) where T : Exception
 52		{
 53			try {
 54				snippet ();
 55			} catch (Exception e) {
 56				if (e.GetType () == typeof (T))
 57					return;
 58				throw new Exception (String.Format ("{0}: Invalid exception type. Expected '{1}' got '{2}'", message, typeof (T), e.GetType ()));
 59			}
 60		
 61			throw new Exception (String.Format ("{0}: No exception thrown.", message));
 62		}
 63			
 64		public static void NotBeNull (object o)
 65		{
 66			if (o == null)
 67				throw new Exception ("Object is null.");
 68		}
 69		
 70		public static void NotBeNull (object o, string message)
 71		{
 72			if (o == null)
 73				throw new Exception (String.Format ("{0}: should not be null.", message));
 74		}
 75		
 76		public static void BeInstanceOf<T> (object o)
 77		{
 78			if (o == null)
 79				throw new Exception ("Item is null.");
 80			
 81			if (o.GetType () != typeof (T))
 82				throw new Exception (String.Format ("Expected '{0}' got '{1}'", typeof (T), o.GetType ()));
 83		}
 84		
 85		public static void BeInstanceOf<T> (object o, string message)
 86		{
 87			if (o == null)
 88				throw new Exception (String.Format ("{0}: Item is null.", message));
 89			
 90			if (o.GetType () != typeof (T))
 91				throw new Exception (String.Format ("{0}: Expected '{1}' got '{2}'", message, typeof (T), o.GetType ()));
 92		}
 93		
 94		public static void NotThrow (TestSnippet snippet)
 95		{
 96			snippet ();
 97		}
 98	}
 99}
100