Click here to Skip to main content
15,898,134 members
Articles / Web Development / ASP.NET

MbUnit : Generative Unit Test Framework

Rate me:
Please Sign up or sign in to vote.
4.70/5 (38 votes)
15 Apr 20046 min read 222.1K   496   120  
A new highly flexible unit test framework with new fixtures
//------------------------------------------------------------------------------
// <autogenerated>
//     This code was generated by a tool.
//     Runtime Version: 1.1.4322.573
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// </autogenerated>
//------------------------------------------------------------------------------

namespace GUnit.Core.Invokers
{
	using System;
	using System.Reflection;
	using System.Xml;
	using System.Xml.Serialization;
	using System.IO;
	
	using QuickGraph.Concepts;
	using QuickGraph;
	using QuickGraph.Representations;
	using QuickGraph.Concepts.Providers;
	using QuickGraph.Providers;
	using QuickGraph.Algorithms;
	using QuickGraph.Algorithms.Search;
	using QuickGraph.Algorithms.Visitors;
	using QuickGraph.Exceptions;
	using QuickGraph.Serialization;
	using QuickGraph.Collections;
	using QuickGraph.Concepts.Collections;
	
	using System.Diagnostics;
	
	using GUnit.Core.Collections;
	using GUnit.Core.Framework;
	using GUnit.Core.Exceptions;
	using GUnit.Core.Runs;

		
	public class RunInvokerTree
	{
		private Type fixtureType;
		private AdjacencyGraph graph = null;
		private RunInvokerVertex root = null;
		
		public RunInvokerTree(Type fixtureType) 
		{
			if (fixtureType==null)
				throw new ArgumentNullException("fixtureType");
			this.fixtureType = fixtureType;

			this.graph = new AdjacencyGraph(
				new RunInvokerVertexProvider(),
				new EdgeProvider(),
				false
				);
			
			CreateRoot();
			Reflect();
		}
		
		
		public Type FixtureType
		{
			get
			{
				return this.fixtureType;
			}
		}
		
		public RunInvokerVertex Root
		{
			get
			{
				return this.root;
			}
		}
		
		public AdjacencyGraph Graph
		{
			get
			{
				return this.graph;
			}
		}
		
		public string Name
		{
			get
			{
				return this.fixtureType.Name;
			}
		}
		
		protected void CreateRoot()
		{
			// create vertex
			this.root = (RunInvokerVertex)this.graph.AddVertex();
		}

		protected void Reflect()
		{			
			// get TestFixturePatternAttribute 
			TestFixturePatternAttribute fix = 
				(TestFixturePatternAttribute)TypeHelper.GetFirstCustomAttribute(
				this.FixtureType,
				typeof(TestFixturePatternAttribute)
				);
			
			// get run
			TypeHelper.ShowMethodAttributes(this.FixtureType);
			
			IRun run = fix.GetRun();
			run.Reflect(this,this.Root,this.FixtureType);
		}

				
		public RunInvokerVertex AddChild(RunInvokerVertex parent, IRunInvoker child)
		{
			if (parent==null)
				throw new ArgumentNullException("parent");
			if (child==null)
				throw new ArgumentNullException("child");
			
			Debug.WriteLine(
			    String.Format("Add Child to {0}, {1}",parent.ToString(), child.ToString()));
				
			// create vertex
			RunInvokerVertex v = (RunInvokerVertex)this.graph.AddVertex();
			v.Invoker = child;
			
			// add edge
			this.graph.AddEdge(parent,v);
			
			Debug.WriteLine(
			    String.Format("Added Child {0}",v.ToString()));			
						
			return v;
		}
				
		public RunPipeCollection AllTestPipes()
		{
			ConstructorInfo ci = this.FixtureType.GetConstructor(Type.EmptyTypes);			
			if (ci==null)
				throw new ConstructorNotFoundException(this.FixtureType,Type.EmptyTypes);

			DepthFirstSearchAlgorithm dfs = new DepthFirstSearchAlgorithm(
				this.graph
				);
			
			// attach leaf recorder
			PredecessorRecorderVisitor pred = new PredecessorRecorderVisitor();
			dfs.RegisterPredecessorRecorderHandlers(pred);
			
			dfs.Compute(this.Root);
			
			RunPipeCollection pipes = 
			    new RunPipeCollection();
			
			foreach(EdgeCollection edges in pred.AllPaths())
			{
				RunPipe pipe = new RunPipe(ci.Invoke(null));
				
				foreach(IEdge e in edges)
				{
					pipe.Invokers.Add((RunInvokerVertex)e.Target);
				}
				
				pipes.Add(pipe);
			}
			return pipes;			
		}

		public IVertexEnumerable Leaves(RunInvokerVertex v)
		{
			return AlgoUtility.Leaves(this.graph,v);
		}
		
		public override string ToString()
		{
			return String.Format("Tree: {0} tests, {1} transtions",
			                     this.graph.VerticesCount,
			                     this.graph.EdgesCount
			                     );
		}
		
		public void ToXml(TextWriter writer)
		{
			if (writer==null)
				throw new ArgumentNullException("writer");
			
			GraphMLGraphSerializer ser = new GraphMLGraphSerializer("");			
			XmlTextWriter xmlWriter = new XmlTextWriter(writer);			
			xmlWriter.Formatting = Formatting.Indented;
			ser.Serialize(xmlWriter,this.graph);
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions