Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
Get your own ranked flair here
posts - 88, comments - 54, trackbacks - 0

µUnit - when just a little is just enough

 

I am always a bit annoyed with myself and others when nunit is employed to perform simple code exercise.

I find this perfect for quickies and one-offs as well as for exercising code in examples. Just drop the file in and be done with it.

 

 

µUnit (uUnit.cs)

// <copyright file="uUnit.cs" company="Sky Sanders">
// 
// This source is a Public Domain Dedication. 
// 
// http://salientqc.codeplex.com
// 
// Attribution is appreciated.
// 
// </copyright> 

using System;

namespace Salient.QualityControl
{

    /// <summary>
    /// uUnit
    /// A micro test runner.
    /// 
    /// For those times when just a little is just enough.
    /// 
    /// Simply wraps a void method in a try/catch and writes pass/fail to console.
    /// Find nested a simple assertion class.
    /// 
    /// If you need more, should probably break out the references to nunit.framework.
    ///
    /// </summary>
    public static class uUnit
    {
        #region Delegates

        public delegate void TestDelegate();

        #endregion

        public static void Run(TestDelegate test)
        {
            string name = test.Method.Name;
            try
            {
                test.Invoke();
                Console.WriteLine(string.Format("{0} : PASSED", name));
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("{0} : FAILED : {1}", name, ex.Message));
            }
        }

        #region Nested type: Assert

        /// <summary>
        /// simple assertions. 
        /// If you need more should probably break out the ol' NUnit.
        /// </summary>
        public static class Assert
        {
            public static void AreEqual(object expected, object actual)
            {
                AreEqual(expected, actual, "");
            }

            public static void AreEqual(object expected, object actual, string message)
            {
                if (!expected.Equals(actual))
                {
                    throw new Exception(string.Format("{2}: expected {0} but got {1}", expected, actual, message));
                }
            }

            public static void IsNull(object target)
            {
                IsNull(target, "");
            }

            public static void IsNull(object target, string message)
            {
                if (target != null)
                {
                    throw new Exception(string.Format("{0}: Expected null but got {1}", message, target));
                }
            }

            public static void IsNotNull(object target)
            {
                IsNotNull(target, "");
            }

            public static void IsNotNull(object target, string message)
            {
                if (target == null)
                {
                    throw new Exception(string.Format("{0}: got null", message));
                }
            }

            public static void IsFalse(bool expression)
            {
                IsFalse(expression, "");
            }

            public static void IsFalse(bool expression, string message)
            {
                AreEqual(false, expression, message);
            }

            public static void IsTrue(bool expression)
            {
                IsTrue(expression, "");
            }

            public static void IsTrue(bool expression, string message)
            {
                AreEqual(true, expression, message);
            }
        }

        #endregion
    }
}

 

Usage:

using System;
using Salient.QualityControl;

namespace uUnit_GettingStarted
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            Console.WriteLine("Passsing tests\r\n");

            uUnit.Run(TestTrueShouldPass);
            uUnit.Run(TestFalseShouldPass);
            uUnit.Run(TestAreEqualShouldPass);
            uUnit.Run(TestIsNullShouldPass);
            uUnit.Run(TestIsNotNullShouldPass);

            Console.WriteLine("\r\nFailing tests\r\n");

            uUnit.Run(TestTrueShouldFail);
            uUnit.Run(TestFalseShouldFail);
            uUnit.Run(TestAreEqualShouldFail);
            uUnit.Run(TestIsNullShouldFail);
            uUnit.Run(TestIsNotNullShouldFail);


            Console.WriteLine("\r\nPress any key");
            Console.ReadKey();
        }


        public static void TestIsNotNullShouldPass()
        {
            uUnit.Assert.IsNotNull(new object());
        }

        public static void TestIsNotNullShouldFail()
        {
            uUnit.Assert.IsNotNull(null);
        }

        public static void TestIsNullShouldPass()
        {
            uUnit.Assert.IsNull(null);
        }

        public static void TestIsNullShouldFail()
        {
            uUnit.Assert.IsNull(new object());
        }

        public static void TestAreEqualShouldPass()
        {
            uUnit.Assert.AreEqual("a", "a");
        }

        public static void TestAreEqualShouldFail()
        {
            uUnit.Assert.AreEqual("a", "B");
        }

        public static void TestTrueShouldPass()
        {
            uUnit.Assert.IsTrue(true);
        }

        public static void TestFalseShouldPass()
        {
            uUnit.Assert.IsFalse(false);
        }


        public static void TestTrueShouldFail()
        {
            uUnit.Assert.IsTrue(false);
        }

        public static void TestFalseShouldFail()
        {
            uUnit.Assert.IsFalse(true);
        }
    }
}

Technorati tags: ,

Print | posted on Sunday, January 24, 2010 5:41 PM |

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 4 and 4 and type the answer here:

Powered by: