Click here to Skip to main content
15,885,864 members
Articles / Programming Languages / C#

CodeDom Assistant

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
21 Sep 20074 min read 137.7K   6.6K   82  
Generating CodeDom Code By Parsing C# or VB
// <file>
//     <copyright see="prj:///doc/copyright.txt"/>
//     <license see="prj:///doc/license.txt"/>
//     <owner name="Andrea Paatz" email="andrea@icsharpcode.net"/>
//     <version>$Revision: 2517 $</version>
// </file>

using System;
using System.Reflection;
using NUnit.Framework;
using ICSharpCode.NRefactory.Ast;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Visitors;

namespace ICSharpCode.NRefactory.Tests
{
	[TestFixture]
	public class StructuralTest
	{
		[Test]
		public void TestToStringMethods()
		{
			Type[] allTypes = typeof(INode).Assembly.GetTypes();
			
			foreach (Type type in allTypes) {
				if (type.IsClass && !type.IsAbstract && !type.IsNested && type.GetInterface(typeof(INode).FullName) != null) {
					MethodInfo methodInfo = type.GetMethod("ToString", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
					Assert.IsNotNull(methodInfo, "ToString() not found in " + type.FullName);
				}
			}
		}
		
		[Test]
		public void TestUnitTests()
		{
			Type[] allTypes = typeof(StructuralTest).Assembly.GetTypes();
			
			foreach (Type type in allTypes) {
				if (type.GetCustomAttributes(typeof(TestFixtureAttribute), true).Length > 0) {
					foreach (MethodInfo m in type.GetMethods()) {
						if (m.IsPublic && m.ReturnType == typeof(void) && m.GetParameters().Length == 0) {
							if (m.GetCustomAttributes(typeof(TestAttribute), true).Length == 0) {
								Assert.Fail(type.Name + "." + m.Name + " should have the [Test] attribute!");
							}
						}
					}
				}
			}
		}
		
//		[Test]
//		public void TestAcceptVisitorMethods()
//		{
//			Type[] allTypes = typeof(AbstractNode).Assembly.GetTypes();
//
//			foreach (Type type in allTypes) {
//				if (type.IsClass && !type.IsAbstract && type.GetInterface(typeof(INode).FullName) != null) {
//					MethodInfo methodInfo = type.GetMethod("AcceptVisitor", BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public);
//					Assertion.AssertNotNull("AcceptVisitor() not found in " + type.FullName, methodInfo);
//				}
//			}
//		}
		
		[Test]
		public void TestIAstVisitor()
		{
			Type[] allTypes = typeof(AbstractNode).Assembly.GetTypes();
			Type visitor = typeof(IAstVisitor);
			
			foreach (Type type in allTypes) {
				if (type.IsClass && !type.IsAbstract && !type.IsNested && type.GetInterface(typeof(INode).FullName) != null && !type.Name.StartsWith("Null")) {
					MethodInfo methodInfo = visitor.GetMethod("Visit" + type.Name, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.ExactBinding, null, new Type[] {type, typeof(object)}, null);
					Assert.IsNotNull(methodInfo, "Visit with parameter " + type.FullName + " not found");
					Assert.AreEqual(2, methodInfo.GetParameters().Length);
					ParameterInfo first = methodInfo.GetParameters()[0];
					Assert.AreEqual(Char.ToLower(first.ParameterType.Name[0]) + first.ParameterType.Name.Substring(1), first.Name);
					
					ParameterInfo second = methodInfo.GetParameters()[1];
					Assert.AreEqual(typeof(System.Object), second.ParameterType);
					Assert.AreEqual("data", second.Name);
				}
			}
		}
		
		[Test]
		public void TestAbstractASTVisitorVisitor()
		{
			Type[] allTypes = typeof(AbstractNode).Assembly.GetTypes();
			Type visitor = typeof(AbstractAstVisitor);
			
			foreach (Type type in allTypes) {
				if (type.IsClass && !type.IsAbstract && !type.IsNested && type.GetInterface(typeof(INode).FullName) != null && !type.Name.StartsWith("Null")) {
					MethodInfo methodInfo = visitor.GetMethod("Visit" + type.Name, BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.ExactBinding, null, new Type[] {type, typeof(object)}, null);
					Assert.IsNotNull(methodInfo, "Visit with parameter " + type.FullName + " not found");
					
					Assert.AreEqual(2, methodInfo.GetParameters().Length);
					ParameterInfo first = methodInfo.GetParameters()[0];
					Assert.AreEqual(Char.ToLower(first.ParameterType.Name[0]) + first.ParameterType.Name.Substring(1), first.Name);
					
					ParameterInfo second = methodInfo.GetParameters()[1];
					Assert.AreEqual(typeof(System.Object), second.ParameterType);
					Assert.AreEqual("data", second.Name);
				}
			}
		}
	}
}

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
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions