context: //
  // Microsoft.VisualBasic.* Test Cases
  //
  // Authors:
  // 	Gert Driesen (drieseng@users.sourceforge.net)
  //
  // (c) 2005 Gert Driesen
  //
  using System;
  using System.CodeDom;
  using System.CodeDom.Compiler;
  using System.Globalization;
  using System.IO;
  using System.Text;
  
  using NUnit.Framework;
  
  namespace MonoTests.Microsoft.VisualBasic
  {
  	/// <summary>
  	/// Test ICodeGenerator's GenerateCodeFromStatement, along with a 
  	/// minimal set CodeDom components.
  	/// </summary>
  	[TestFixture]
  	public class CodeGeneratorFromStatementTest: CodeGeneratorTestBase
  	{
  		private CodeStatement statement = null;
  
  		[SetUp]
  		public void Init ()
  		{
  			InitBase ();
  			statement = new CodeStatement ();
  		}
  		
  		protected override string Generate (CodeGeneratorOptions options)
  		{
  			StringWriter writer = new StringWriter ();
  			writer.NewLine = NewLine;
  
  			generator.GenerateCodeFromStatement (statement, writer, options);
  			writer.Close ();
  			return writer.ToString ();
  		}
  		
  		[Test]
  		[ExpectedException (typeof (ArgumentException))]
  		public void DefaultStatementTest ()
  		{
  			Generate ();
  		}
  
  		[Test]
  		[ExpectedException (typeof (NullReferenceException))]
  		public void NullStatementTest ()
  		{
  			statement = null;
  			Generate ();
  		}
  
  		[Test]
  		public void CodeAssignStatementTest ()
  		{
  			CodeSnippetExpression cse1 = new CodeSnippetExpression ("A");
  			CodeSnippetExpression cse2 = new CodeSnippetExpression ("B");
  
  			CodeAssignStatement assignStatement = new CodeAssignStatement (cse1, cse2);
  			statement = assignStatement;
  
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"A = B{0}", NewLine), Generate (), "#1");
  
  			assignStatement.Left = null;
  			try {
  				Generate ();
  				Assert.Fail ("#2");
  			} catch (ArgumentNullException) {
  			}
  
  			assignStatement.Left = cse1;
  			Generate ();
  
  			assignStatement.Right = null;
  			try {
  				Generate ();
  				Assert.Fail ("#3");
  			} catch (ArgumentNullException) {
  			}
  
  			assignStatement.Right = cse2;
  			Generate ();
  		}
  
  		[Test]
  		public void CodeAttachEventStatementTest ()
  		{
  			CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
  				new CodeSnippetExpression ("A"), "class");
  			CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
  
  			CodeAttachEventStatement attachEventStatement = new CodeAttachEventStatement ();
  			statement = attachEventStatement;
  
  			try {
  				Generate ();
  				Assert.Fail ("#1");
  			} catch (ArgumentNullException) {
  			}
  
  			attachEventStatement.Event = cere;
  			try {
  				Generate ();
  				Assert.Fail ("#2");
  			} catch (ArgumentNullException) {
  			}
  
  			attachEventStatement.Event = null;
  			attachEventStatement.Listener = handler;
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler , EventHandler{0}", NewLine), Generate (), "#3");
  
  			attachEventStatement.Event = cere;
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler A.[class], EventHandler{0}", NewLine), Generate (), "#4");
  
  			attachEventStatement.Event = new CodeEventReferenceExpression (
  				new CodeSnippetExpression ((string) null), "");
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler ., EventHandler{0}", NewLine), Generate (), "#5");
  
  			attachEventStatement.Listener = new CodeSnippetExpression ("");
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler ., {0}", NewLine), Generate (), "#6");
  		}
  		
  		[Test]
  		public void CodeAttachEventStatementKeywordTest ()
  		{
  			CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
  				new CodeSnippetExpression ("Set"), "Event");
  			CodeSnippetExpression handler = new CodeSnippetExpression ("Handles");
  
  			CodeAttachEventStatement attachEventStatement = new CodeAttachEventStatement ();
  			statement = attachEventStatement;
  
  			try {
  				Generate ();
  				Assert.Fail ("#1");
  			} catch (ArgumentNullException) {
  			}
  
  			attachEventStatement.Event = cere;
  			try {
  				Generate ();
  				Assert.Fail ("#2");
  			} catch (ArgumentNullException) {
  			}
  
  			attachEventStatement.Event = null;
  			attachEventStatement.Listener = handler;
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler , Handles{0}", NewLine), Generate (), "#3");
  
  			attachEventStatement.Event = cere;
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler Set.[Event], Handles{0}", NewLine), Generate (), "#4");
  
  			attachEventStatement.Event = new CodeEventReferenceExpression (
  				new CodeSnippetExpression ((string) null), "");
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler ., Handles{0}", NewLine), Generate (), "#5");
  
  			attachEventStatement.Listener = new CodeSnippetExpression ("");
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"AddHandler ., {0}", NewLine), Generate (), "#6");
  		}
  		[Test]
  		public void CodeCommentStatementTest ()
  		{
  			CodeCommentStatement commentStatement = new CodeCommentStatement ();
  			CodeComment comment = new CodeComment ();
  			commentStatement.Comment = comment;
  			statement = commentStatement;
  
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"'{0}", NewLine), Generate (), "#1");
  
  			comment.Text = "a\nb";
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"'a\n'b{0}", NewLine), Generate (), "#2");
  
  			comment.Text = "a\r\nb";
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"'a\r\n'b{0}", NewLine), Generate (), "#3");
  
  			comment.Text = "a\rb";
  			Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  				"'a\r'b{0}", NewLine), Generate (), "#4");
  		}
  
  		[Test]
  		public void CodeConditionStatementTest ()
  		{
  			CodeStatement[] trueStatements = new CodeStatement[] {
  				new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
  				new CodeExpressionStatement (new CodeSnippetExpression (";")),
  				new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
  				new CodeExpressionStatement (new CodeSnippetExpression ("")),
  				new CodeSnippetStatement ("A"),
  				new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) };
  
  
gt: 			CodeStatement[] falseStatements = new CodeStatement[] {