Click here to Skip to main content
15,891,136 members
Articles / Web Development / HTML

An XML Compiler

Rate me:
Please Sign up or sign in to vote.
4.59/5 (25 votes)
13 Sep 2005CPOL9 min read 96.2K   1.3K   72  
Convert your XML object graph to code using CodeDom
/*
(c) 2005, Marc Clifton
All Rights Reserved
 
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer. 

Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution. 

Neither the name of the Marc Clifton, "Advanced Unit Test", "AUT", nor the names
of its contributors may be used to endorse or promote products derived from this
software without specific prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Diagnostics;

using Vts.KALib;

namespace Vts.UTCore
{
	#region TestUnitAttribute

//	[Serializable]
	public class TestUnitAttribute// : MarshalByRefObject
	{
		#region Fields

		protected ClassItem ci;
		protected MethodItem mi;
		protected object attr;

		private long ticks;
		private double secs;
		private string time;
		private double minSecs;
		private string minTime;
		private double maxSecs;
		private string maxTime;
		protected string result;
		protected string className;

		#endregion

		#region Properties

		public string Result
		{
			get {return result;}
			set {result=value;}
		}

		public string ClassName
		{
			get {return className;}
			set {className=value;}
		}

		public ClassItem TestClass
		{
			get {return ci;}
			set {ci=value;}
		}

		public MethodItem TestMethod
		{
			get {return mi;}
		}

		public string Time
		{
			get {return time;}
			set {time=value;}
		}

		public string MaxTime
		{
			get { return maxTime; }
			set { maxTime=value; }
		}

		public string MinTime
		{
			get { return minTime; }
			set { minTime=value; }
		}

		public double dMaxTime
		{
			get { return maxSecs; }
			set 
			{ 
				maxSecs=value;
				if (maxSecs<10.0)
				{
				maxTime=String.Format("{0:0.000} ms", maxSecs*1000.0);
				}
				else
				{
				maxTime=String.Format("{0:0.000} s", maxSecs);
				}
			}
		}
      
		public double dMinTime
		{
			get { return minSecs; }
			set 
			{ 
				minSecs=value;
				if (minSecs<10.0)
				{
				minTime=String.Format("{0:0.000} ms", minSecs*1000.0);
				}
				else
				{
				minTime=String.Format("{0:0.000} s", minSecs);
				}
			}
		}
	      
		public double dTime
		{
			get {return secs;}
			set
			{
				secs=value;
				if (secs<10.0)
				{
			time=String.Format("{0:0.000} ms", secs*1000.0);
			}
				else
				{
				time=String.Format("{0:0.000} s", secs);
				}
			}
		}
		
//		public string UniqueKey 
//		{
//			get { return string.Format("{0}.{1}", TestClass.FullName, TestMethod); }
//		}

		#endregion

		public TestUnitAttribute()
		{
		}

		#region Methods

		public virtual void SelfRegister(TestFixture tf) {}

		public void Initialize(ClassItem ci, MethodItem mi, object attr)
		{
			this.ci=ci;
			this.mi=mi;
			this.attr=attr;
		}

		public object CreateClass()
		{
			object obj=ci.Create();
			return obj;
		}

		public virtual void Invoke(object classInstance)
		{
			// make sure we time exceptions as well.
			try
			{
				mi.Invoke(classInstance);
			}
			catch(Exception e)
			{
				throw(e);
			}
			finally
			{
				ticks=mi.ExecutionTime;
				secs=(double)ticks/HiResTimer.TicksPerSecond;
            if (secs>100.0)
            {
               time=String.Format("{0:0.0} s", secs);
            }
            else if (secs>1.0)
            {
               time=String.Format("{0:0.000} s", secs);
            }
            else if (secs>0.100)
            {
               time=String.Format("{0:0} ms", secs*1000.0);
            }
            else 
            {
               time=String.Format("{0:0.000} ms", secs*1000.0);
            }
         }
		}

		#endregion
	}

	#endregion

	#region Derived Attribute classes

	public class NullTestUnitAttribute : TestUnitAttribute
	{
		public override void Invoke(object classInstance)
		{
		}

		public override void SelfRegister(TestFixture tf)
		{
		}

	}

	public class TestFixtureAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			tf.AddTestFixtureAttribute(this);
		}
	}

	public class ProcessTestAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			tf.IsProcessTest=true;
		}
	}

	public class TestFixtureSetUpAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			tf.AddFixtureSetUpAttribute(this);
		}
	}

	public class TestFixtureTearDownAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			tf.AddFixtureTearDownAttribute(this);
		}
	}

	public class SequenceAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			UnitTest.SequenceAttribute sa=attr as UnitTest.SequenceAttribute;
			mi.Sequence=sa.Order;
		}
	}

	public class RequiresAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			UnitTest.RequiresAttribute ra=attr as UnitTest.RequiresAttribute;
			mi.AddRequires(ra.PriorTestMethod);
		}
	}

	public class CodePathRangeAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			UnitTest.CodePathRangeAttribute cpra=attr as UnitTest.CodePathRangeAttribute;
			tf.AddCodePathRangeAttribute(this);
			tf.StartCodePath=cpra.Start;
			tf.StopCodePath=cpra.Stop;
		}
	}

	public class CodePathsAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			UnitTest.CodePathsAttribute cpa=attr as UnitTest.CodePathsAttribute;
			tf.AddCodePathsAttribute(this);
			tf.CodePathList=cpa.Paths;
		}
	}

	public class ShouldExecuteCodePathAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			UnitTest.ShouldExecuteCodePathAttribute secpa=attr as UnitTest.ShouldExecuteCodePathAttribute;
			mi.AddCodePath(secpa.PathNum);
		}
	}

	public class TestAttribute : TestUnitAttribute
	{
		public enum TestState
		{
			Untested=0,
			NotRun,
			Pass,
			Ignore,
			Fail,
		}

		TestState state;
		bool skipTest;

		public TestState State
		{
			get {return state;}
			set {state=value;}
		}

		public bool SkipTest 
		{
			get {return skipTest;}
			set {skipTest=value;}
		}

		public bool IgnoreTest
		{
			get {return mi.Ignore;}
		}

		public int CodePath
		{
			get {return mi.CodePath;}
		}

		public string IgnoreReason
		{
			get {return mi.IgnoreReason;}
		}

		public UnitTest.ExpectedExceptionAttribute ExpectedException
		{
			get	{ return (mi.ExpectedException==null) ? null : mi.ExpectedException; }
		}

		public TestAttribute()
		{
			state=TestState.Untested;
			result="";
		}

		public override void SelfRegister(TestFixture tf)
		{
			tf.AddTestAttribute(this);
		}
	}

	public class SetUpAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			tf.AddSetUpAttribute(this);
		}
	}

	public class TearDownAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			tf.AddTearDownAttribute(this);
		}
	}

	public class ExpectedExceptionAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			mi.ExpectedException=attr as UnitTest.ExpectedExceptionAttribute;
		}
	}

	public class ReverseProcessExpectedExceptionAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			mi.ReverseProcessExpectedException.Add((attr as UnitTest.ReverseProcessExpectedExceptionAttribute).ExceptionType, attr);
		}
	}

	public class IgnoreAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
		   if (mi==null)
		   {
            ci.Ignore=true;
            ci.IgnoreReason=(attr as UnitTest.IgnoreAttribute).Reason;
            tf.IgnoreFixture=true;
            tf.IgnoreReason=(attr as UnitTest.IgnoreAttribute).Reason;
         }
		   else
		   {
			   mi.Ignore=true;
			   mi.IgnoreReason=(attr as UnitTest.IgnoreAttribute).Reason;
			}
		}
	}

	public class MinOperationsPerSecondAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			mi.MinOPS=(attr as UnitTest.MinOperationsPerSecondAttribute).MinOPS;
		}
	}

	public class RepeatAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			mi.RepeatCount=(attr as UnitTest.RepeatAttribute).RepeatCount;
			mi.RepeatDelay=(attr as UnitTest.RepeatAttribute).RepeatDelay;
		}
	}

	public class MaxKMemoryAttribute : TestUnitAttribute
	{
		public override void SelfRegister(TestFixture tf)
		{
			mi.MaxK=(attr as UnitTest.MaxKMemoryAttribute).MaxK;
		}
	}

	#endregion
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Interacx
United States United States
Blog: https://marcclifton.wordpress.com/
Home Page: http://www.marcclifton.com
Research: http://www.higherorderprogramming.com/
GitHub: https://github.com/cliftonm

All my life I have been passionate about architecture / software design, as this is the cornerstone to a maintainable and extensible application. As such, I have enjoyed exploring some crazy ideas and discovering that they are not so crazy after all. I also love writing about my ideas and seeing the community response. As a consultant, I've enjoyed working in a wide range of industries such as aerospace, boatyard management, remote sensing, emergency services / data management, and casino operations. I've done a variety of pro-bono work non-profit organizations related to nature conservancy, drug recovery and women's health.

Comments and Discussions