Click here to Skip to main content
15,879,326 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.2K   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="Daniel Grunwald" email="daniel@danielgrunwald.de"/>
//     <version>$Revision: 2637 $</version>
// </file>

using System;
using System.IO;
using NUnit.Framework;
using ICSharpCode.NRefactory.Parser;
using ICSharpCode.NRefactory.Parser.CSharp;
using ICSharpCode.NRefactory.PrettyPrinter;

namespace ICSharpCode.NRefactory.Tests.Lexer.CSharp
{
	[TestFixture]
	public sealed class CustomLexerTests
	{
		ILexer GenerateLexer(StringReader sr)
		{
			return ParserFactory.CreateLexer(SupportedLanguage.CSharp, sr);
		}
		
		[Test]
		public void TestEmptyBlock()
		{
			ILexer lexer = GenerateLexer(new StringReader("{}+"));
			Assert.AreEqual(Tokens.OpenCurlyBrace, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.CloseCurlyBrace, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.Plus, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.EOF, lexer.NextToken().kind);
		}
		
		void CheckIdentifier(string text, string actualIdentifier)
		{
			ILexer lexer = GenerateLexer(new StringReader(text));
			Token t = lexer.NextToken();
			Assert.AreEqual(Tokens.Identifier, t.kind);
			Assert.AreEqual(actualIdentifier, t.val);
			t = lexer.NextToken();
			Assert.AreEqual(Tokens.EOF, t.kind);
			Assert.AreEqual("", lexer.Errors.ErrorOutput);
		}
		
		[Test]
		public void TestIdentifier()
		{
			CheckIdentifier("a_Bc05", "a_Bc05");
		}
		
		[Test]
		public void TestIdentifierStartingWithUnderscore()
		{
			CheckIdentifier("_Bc05", "_Bc05");
		}
		
		[Test]
		public void TestIdentifierStartingWithEscapeSequence()
		{
			CheckIdentifier(@"\u006cexer", "lexer");
		}
		
		[Test]
		public void TestIdentifierContainingEscapeSequence()
		{
			CheckIdentifier(@"l\U00000065xer", "lexer");
		}
		
		[Test]
		public void TestKeyWordAsIdentifier()
		{
			CheckIdentifier("@int", "int");
		}
		
		[Test]
		public void TestKeywordWithEscapeSequenceIsIdentifier()
		{
			CheckIdentifier(@"i\u006et", "int");
		}
		
		[Test]
		public void TestKeyWordAsIdentifierStartingWithUnderscore()
		{
			CheckIdentifier("@_int", "_int");
		}
		
		[Test]
		public void TestSkippedEmptyBlock()
		{
			ILexer lexer = GenerateLexer(new StringReader("{}+"));
			Assert.AreEqual(Tokens.OpenCurlyBrace, lexer.NextToken().kind);
			lexer.NextToken();
			lexer.SkipCurrentBlock(Tokens.CloseCurlyBrace);
			Assert.AreEqual(Tokens.CloseCurlyBrace, lexer.LookAhead.kind);
			Assert.AreEqual(Tokens.Plus, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.EOF, lexer.NextToken().kind);
		}
		
		[Test]
		public void TestSkippedNonEmptyBlock()
		{
			ILexer lexer = GenerateLexer(new StringReader("{ TestMethod('}'); /* }}} */ while(1) {break;} }+"));
			Assert.AreEqual(Tokens.OpenCurlyBrace, lexer.NextToken().kind);
			lexer.NextToken();
			lexer.SkipCurrentBlock(Tokens.CloseCurlyBrace);
			Assert.AreEqual(Tokens.CloseCurlyBrace, lexer.LookAhead.kind);
			Assert.AreEqual(Tokens.Plus, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.EOF, lexer.NextToken().kind);
		}
		
		[Test]
		public void TestSkippedNonEmptyBlockWithPeek()
		{
			ILexer lexer = GenerateLexer(new StringReader("{ TestMethod(\"}\"); // }}}\n" +
			                                              "while(1) {break;} }+"));
			Assert.AreEqual(Tokens.OpenCurlyBrace, lexer.NextToken().kind);
			lexer.NextToken();
			lexer.StartPeek();
			lexer.Peek();
			lexer.Peek();
			lexer.Peek();
			lexer.SkipCurrentBlock(Tokens.CloseCurlyBrace);
			Assert.AreEqual(Tokens.CloseCurlyBrace, lexer.LookAhead.kind);
			Assert.AreEqual(Tokens.Plus, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.EOF, lexer.NextToken().kind);
		}
		
		[Test]
		public void TestSkippedEmptyBlockWithPeek()
		{
			ILexer lexer = GenerateLexer(new StringReader("{}+"));
			Assert.AreEqual(Tokens.OpenCurlyBrace, lexer.NextToken().kind);
			lexer.NextToken();
			lexer.StartPeek();
			lexer.Peek();
			lexer.Peek();
			lexer.Peek();
			lexer.SkipCurrentBlock(Tokens.CloseCurlyBrace);
			Assert.AreEqual(Tokens.CloseCurlyBrace, lexer.LookAhead.kind);
			Assert.AreEqual(Tokens.Plus, lexer.NextToken().kind);
			Assert.AreEqual(Tokens.EOF, lexer.NextToken().kind);
		}
	}
}

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