| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- //
- // Microsoft.CSharp.* Test Cases
- //
- // Authors:
- // Erik LeBel ([email protected])
- //
- // (c) 2003 Erik LeBel
- //
- using System;
- using System.CodeDom;
- using System.CodeDom.Compiler;
- using System.Globalization;
- using System.IO;
- using System.Text;
- using NUnit.Framework;
- namespace MonoTests.Microsoft.CSharp
- {
- /// <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,
- " += EventHandler;{0}", NewLine), Generate (), "#3");
- attachEventStatement.Event = cere;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "A.@class += EventHandler;{0}", NewLine), Generate (), "#4");
- attachEventStatement.Event = new CodeEventReferenceExpression (
- new CodeSnippetExpression ((string) null), "");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- ". += EventHandler;{0}", NewLine), Generate (), "#5");
- attachEventStatement.Listener = new CodeSnippetExpression ("");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- ". += ;{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,
- "// \n", NewLine), Generate (), "#1");
- comment.Text = "a\nb";
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "// a\n//b\n", 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()")) };
- CodeStatement[] falseStatements = new CodeStatement[] {
- new CodeExpressionStatement (new CodeSnippetExpression ("DoD()")),
- new CodeSnippetStatement ("B"),
- new CodeExpressionStatement (new CodeSnippetExpression (";")),
- new CodeExpressionStatement (new CodeSnippetExpression ("DoE()")),
- new CodeExpressionStatement (new CodeSnippetExpression ("")),
- new CodeExpressionStatement (new CodeSnippetExpression ("DoF()")) };
- CodeConditionStatement conditionStatement = new CodeConditionStatement ();
- statement = conditionStatement;
- try {
- Generate ();
- Assert.Fail ("#1");
- } catch (ArgumentNullException) {
- }
- conditionStatement.Condition = new CodeSnippetExpression ("");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if () {{{0}" +
- "}}{0}", NewLine), Generate (), "#2");
- conditionStatement.Condition = new CodeSnippetExpression ("true == false");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if (true == false) {{{0}" +
- "}}{0}", NewLine), Generate (), "#3");
- conditionStatement.TrueStatements.AddRange (trueStatements);
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if (true == false) {{{0}" +
- " DoA();{0}" +
- " ;;{0}" +
- " DoB();{0}" +
- " ;{0}" +
- #if NET_2_0
- "A{0}" +
- #else
- " A{0}" +
- #endif
- " DoC();{0}" +
- "}}{0}", NewLine), Generate (), "#3");
- conditionStatement.FalseStatements.AddRange (falseStatements);
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if (true == false) {{{0}" +
- " DoA();{0}" +
- " ;;{0}" +
- " DoB();{0}" +
- " ;{0}" +
- #if NET_2_0
- "A{0}" +
- #else
- " A{0}" +
- #endif
- " DoC();{0}" +
- "}}{0}" +
- "else {{{0}" +
- " DoD();{0}" +
- #if NET_2_0
- "B{0}" +
- #else
- " B{0}" +
- #endif
- " ;;{0}" +
- " DoE();{0}" +
- " ;{0}" +
- " DoF();{0}" +
- "}}{0}", NewLine), Generate (), "#4");
- options.ElseOnClosing = true;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if (true == false) {{{0}" +
- " DoA();{0}" +
- " ;;{0}" +
- " DoB();{0}" +
- " ;{0}" +
- #if NET_2_0
- "A{0}" +
- #else
- " A{0}" +
- #endif
- " DoC();{0}" +
- "}} else {{{0}" +
- " DoD();{0}" +
- #if NET_2_0
- "B{0}" +
- #else
- " B{0}" +
- #endif
- " ;;{0}" +
- " DoE();{0}" +
- " ;{0}" +
- " DoF();{0}" +
- "}}{0}", NewLine), Generate (), "#5");
- options.ElseOnClosing = false;
- conditionStatement.TrueStatements.Clear ();
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if (true == false) {{{0}" +
- "}}{0}" +
- "else {{{0}" +
- " DoD();{0}" +
- #if NET_2_0
- "B{0}" +
- #else
- " B{0}" +
- #endif
- " ;;{0}" +
- " DoE();{0}" +
- " ;{0}" +
- " DoF();{0}" +
- "}}{0}", NewLine), Generate (), "#6");
- conditionStatement.TrueStatements.AddRange (trueStatements);
- conditionStatement.FalseStatements.Clear ();
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "if (true == false) {{{0}" +
- " DoA();{0}" +
- " ;;{0}" +
- " DoB();{0}" +
- " ;{0}" +
- #if NET_2_0
- "A{0}" +
- #else
- " A{0}" +
- #endif
- " DoC();{0}" +
- "}}{0}", NewLine), Generate (), "#7");
- }
- [Test]
- public void CodeExpressionStatementTest ()
- {
- CodeExpressionStatement ces = new CodeExpressionStatement ();
- statement = ces;
- try {
- Generate ();
- Assert.Fail ("#1");
- } catch (ArgumentNullException) {
- }
- ces.Expression = new CodeSnippetExpression ("something");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "something;{0}", NewLine), Generate (), "#2");
- }
- [Test]
- public void CodeGotoStatementTest ()
- {
- statement = new CodeGotoStatement ("something");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "goto something;{0}", NewLine), Generate ());
- }
- [Test]
- public void CodeIterationStatementTest ()
- {
- CodeIterationStatement cis = new CodeIterationStatement ();
- statement = cis;
- try {
- Generate ();
- Assert.Fail ("#1: null InitStatement should cause NRE");
- } catch (NullReferenceException) {
- }
- cis.InitStatement = new CodeVariableDeclarationStatement (typeof(int),
- "testInt", new CodePrimitiveExpression(1));
- try {
- Generate ();
- Assert.Fail ("#2: null TestExpression should cause ArgumentNullException");
- } catch (ArgumentNullException) {
- }
- cis.TestExpression = new CodeBinaryOperatorExpression (
- new CodeVariableReferenceExpression ("testInt"),
- CodeBinaryOperatorType.LessThan,
- new CodePrimitiveExpression (10));
- try {
- Generate ();
- Assert.Fail ("#3: null IncrementStatement should cause NRE");
- } catch (NullReferenceException) {
- }
- cis.IncrementStatement = new CodeAssignStatement (
- new CodeVariableReferenceExpression ("testInt"),
- new CodeBinaryOperatorExpression (
- new CodeVariableReferenceExpression ("testInt"),
- CodeBinaryOperatorType.Add,
- new CodePrimitiveExpression (1)));
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
- "}}{0}", NewLine), Generate (), "#4");
- cis.Statements.AddRange (new CodeStatement[] {
- new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
- new CodeExpressionStatement (new CodeSnippetExpression (";")),
- new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
- new CodeLabeledStatement ("test", new CodeSnippetStatement ("C")),
- new CodeExpressionStatement (new CodeSnippetExpression ("")),
- new CodeSnippetStatement ("A"),
- new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) });
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
- " DoA();{0}" +
- " ;;{0}" +
- " DoB();{0}" +
- "test:{0}" +
- #if NET_2_0
- "C{0}" +
- #else
- " C{0}" +
- #endif
- " ;{0}" +
- #if NET_2_0
- "A{0}" +
- #else
- " A{0}" +
- #endif
- " DoC();{0}" +
- "}}{0}", NewLine), Generate (), "#5");
- }
- [Test]
- public void CodeLabeledStatementTest ()
- {
- CodeLabeledStatement cls = new CodeLabeledStatement ();
- statement = cls;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- ":{0}", NewLine), Generate (), "#1");
- cls.Label = "class";
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "class:{0}", NewLine), Generate (), "#2");
- cls.Statement = new CodeSnippetStatement ("A");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "class:{0}" +
- #if NET_2_0
- "A{0}",
- #else
- " A{0}",
- #endif
- NewLine), Generate (), "#3");
- }
- [Test]
- public void CodeMethodReturnStatementTest ()
- {
- CodeMethodReturnStatement cmrs = new CodeMethodReturnStatement ();
- statement = cmrs;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "return;{0}", NewLine), Generate (), "#1");
- cmrs.Expression = new CodePrimitiveExpression (1);
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "return 1;{0}", NewLine), Generate (), "#2");
- }
- [Test]
- public void CodeRemoveEventStatementTest ()
- {
- CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
- new CodeSnippetExpression ("A"), "class");
- CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
- CodeRemoveEventStatement cres = new CodeRemoveEventStatement ();
- statement = cres;
- try {
- Generate ();
- Assert.Fail ("#1");
- } catch (ArgumentNullException) {
- }
- cres.Event = cere;
- try {
- Generate ();
- Assert.Fail ("#2");
- } catch (ArgumentNullException) {
- }
- cres.Event = null;
- cres.Listener = handler;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- " -= EventHandler;{0}", NewLine), Generate (), "#3");
- cres.Event = cere;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "A.@class -= EventHandler;{0}", NewLine), Generate (), "#4");
- cres.Event = new CodeEventReferenceExpression (
- new CodeSnippetExpression ((string) null), "");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- ". -= EventHandler;{0}", NewLine), Generate (), "#5");
- cres.Listener = new CodeSnippetExpression ("");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- ". -= ;{0}", NewLine), Generate (), "#6");
- }
- [Test]
- public void CodeSnippetStatementTest ()
- {
- CodeSnippetStatement css = new CodeSnippetStatement ();
- statement = css;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "{0}", NewLine), Generate (), "#1");
- css.Value = "class";
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "class{0}", NewLine), Generate (), "#2");
- css.Value = null;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "{0}", NewLine), Generate (), "#3");
- }
- [Test]
- [ExpectedException (typeof (ArgumentException))]
- public void CodeStatement ()
- {
- CodeStatement cs = new CodeStatement ();
- statement = cs;
- Generate ();
- }
- [Test]
- public void CodeThrowExceptionStatementTest ()
- {
- CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement ();
- statement = ctes;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "throw;{0}", NewLine), Generate (), "#1");
- ctes.ToThrow = new CodeSnippetExpression ("whatever");
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "throw whatever;{0}", NewLine), Generate (), "#2");
- }
- [Test]
- public void CodeTryCatchFinallyStatementTest ()
- {
- CodeStatement cs = new CodeGotoStatement ("exit");
- CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
- CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
- CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
- CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
- statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
- new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "try {{{0}" +
- " goto exit;{0}" +
- "}}{0}" +
- "catch (System.ArgumentException ex1) {{{0}" +
- "}}{0}" +
- "catch (System.ApplicationException ) {{{0}" +
- "}}{0}" +
- "finally {{{0}" +
- #if NET_2_0
- "A{0}" +
- "B{0}" +
- #else
- " A{0}" +
- " B{0}" +
- #endif
- "}}{0}", NewLine), Generate (), "#1");
- options.ElseOnClosing = true;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "try {{{0}" +
- " goto exit;{0}" +
- "}} catch (System.ArgumentException ex1) {{{0}" +
- "}} catch (System.ApplicationException ) {{{0}" +
- "}} finally {{{0}" +
- #if NET_2_0
- "A{0}" +
- "B{0}" +
- #else
- " A{0}" +
- " B{0}" +
- #endif
- "}}{0}", NewLine), Generate (), "#2");
- statement = new CodeTryCatchFinallyStatement ();
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "try {{{0}" +
- "}}{0}", NewLine), Generate (), "#3");
- options.ElseOnClosing = false;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "try {{{0}" +
- "}}{0}", NewLine), Generate (), "#4");
- }
- [Test]
- public void CodeVariableDeclarationStatementTest ()
- {
- CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ();
- statement = cvds;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "void ;{0}", NewLine), Generate (), "#1");
- cvds.Name = "class";
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "void @class;{0}", NewLine), Generate (), "#2");
- cvds.Name = "A";
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "void A;{0}", NewLine), Generate (), "#3");
- cvds.Type = new CodeTypeReference (typeof (int));
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "int A;{0}", NewLine), Generate (), "#4");
- cvds.InitExpression = new CodePrimitiveExpression (25);
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "int A = 25;{0}", NewLine), Generate (), "#5");
- cvds.Name = null;
- Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
- "int = 25;{0}", NewLine), Generate (), "#5");
- }
- }
- }
|