CodeGeneratorFromStatementTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. //
  2. // Microsoft.CSharp.* Test Cases
  3. //
  4. // Authors:
  5. // Erik LeBel ([email protected])
  6. //
  7. // (c) 2003 Erik LeBel
  8. //
  9. using System;
  10. using System.CodeDom;
  11. using System.CodeDom.Compiler;
  12. using System.Globalization;
  13. using System.IO;
  14. using System.Text;
  15. using NUnit.Framework;
  16. namespace MonoTests.Microsoft.CSharp
  17. {
  18. /// <summary>
  19. /// Test ICodeGenerator's GenerateCodeFromStatement, along with a
  20. /// minimal set CodeDom components.
  21. /// </summary>
  22. [TestFixture]
  23. public class CodeGeneratorFromStatementTest: CodeGeneratorTestBase
  24. {
  25. private CodeStatement statement = null;
  26. [SetUp]
  27. public void Init ()
  28. {
  29. InitBase ();
  30. statement = new CodeStatement ();
  31. }
  32. protected override string Generate (CodeGeneratorOptions options)
  33. {
  34. StringWriter writer = new StringWriter ();
  35. writer.NewLine = NewLine;
  36. generator.GenerateCodeFromStatement (statement, writer, options);
  37. writer.Close ();
  38. return writer.ToString ();
  39. }
  40. [Test]
  41. [ExpectedException (typeof (ArgumentException))]
  42. public void DefaultStatementTest ()
  43. {
  44. Generate ();
  45. }
  46. [Test]
  47. [ExpectedException (typeof (NullReferenceException))]
  48. public void NullStatementTest ()
  49. {
  50. statement = null;
  51. Generate ();
  52. }
  53. [Test]
  54. public void CodeAssignStatementTest ()
  55. {
  56. CodeSnippetExpression cse1 = new CodeSnippetExpression("A");
  57. CodeSnippetExpression cse2 = new CodeSnippetExpression("B");
  58. CodeAssignStatement assignStatement = new CodeAssignStatement (cse1, cse2);
  59. statement = assignStatement;
  60. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  61. "A = B;{0}", NewLine), Generate (), "#1");
  62. assignStatement.Left = null;
  63. try {
  64. Generate ();
  65. Assert.Fail ("#2");
  66. } catch (ArgumentNullException) {
  67. }
  68. assignStatement.Left = cse1;
  69. Generate ();
  70. assignStatement.Right = null;
  71. try {
  72. Generate ();
  73. Assert.Fail ("#3");
  74. } catch (ArgumentNullException) {
  75. }
  76. assignStatement.Right = cse2;
  77. Generate ();
  78. }
  79. [Test]
  80. public void CodeAttachEventStatementTest ()
  81. {
  82. CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
  83. new CodeSnippetExpression ("A"), "class");
  84. CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
  85. CodeAttachEventStatement attachEventStatement = new CodeAttachEventStatement ();
  86. statement = attachEventStatement;
  87. try {
  88. Generate ();
  89. Assert.Fail ("#1");
  90. } catch (ArgumentNullException) {
  91. }
  92. attachEventStatement.Event = cere;
  93. try {
  94. Generate ();
  95. Assert.Fail ("#2");
  96. } catch (ArgumentNullException) {
  97. }
  98. attachEventStatement.Event = null;
  99. attachEventStatement.Listener = handler;
  100. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  101. " += EventHandler;{0}", NewLine), Generate (), "#3");
  102. attachEventStatement.Event = cere;
  103. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  104. "A.@class += EventHandler;{0}", NewLine), Generate (), "#4");
  105. attachEventStatement.Event = new CodeEventReferenceExpression (
  106. new CodeSnippetExpression ((string) null), "");
  107. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  108. ". += EventHandler;{0}", NewLine), Generate (), "#5");
  109. attachEventStatement.Listener = new CodeSnippetExpression ("");
  110. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  111. ". += ;{0}", NewLine), Generate (), "#6");
  112. }
  113. [Test]
  114. public void CodeCommentStatementTest ()
  115. {
  116. CodeCommentStatement commentStatement = new CodeCommentStatement ();
  117. CodeComment comment = new CodeComment ();
  118. commentStatement.Comment = comment;
  119. statement = commentStatement;
  120. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  121. "// \n", NewLine), Generate (), "#1");
  122. comment.Text = "a\nb";
  123. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  124. "// a\n//b\n", NewLine), Generate (), "#2");
  125. comment.Text = "a\r\nb";
  126. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  127. "// a\r\n//b{0}", NewLine), Generate (), "#3");
  128. comment.Text = "a\rb";
  129. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  130. "// a\r//b{0}", NewLine), Generate (), "#4");
  131. }
  132. [Test]
  133. public void CodeConditionStatementTest ()
  134. {
  135. CodeStatement[] trueStatements = new CodeStatement[] {
  136. new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
  137. new CodeExpressionStatement (new CodeSnippetExpression (";")),
  138. new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
  139. new CodeExpressionStatement (new CodeSnippetExpression ("")),
  140. new CodeSnippetStatement ("A"),
  141. new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) };
  142. CodeStatement[] falseStatements = new CodeStatement[] {
  143. new CodeExpressionStatement (new CodeSnippetExpression ("DoD()")),
  144. new CodeSnippetStatement ("B"),
  145. new CodeExpressionStatement (new CodeSnippetExpression (";")),
  146. new CodeExpressionStatement (new CodeSnippetExpression ("DoE()")),
  147. new CodeExpressionStatement (new CodeSnippetExpression ("")),
  148. new CodeExpressionStatement (new CodeSnippetExpression ("DoF()")) };
  149. CodeConditionStatement conditionStatement = new CodeConditionStatement ();
  150. statement = conditionStatement;
  151. try {
  152. Generate ();
  153. Assert.Fail ("#1");
  154. } catch (ArgumentNullException) {
  155. }
  156. conditionStatement.Condition = new CodeSnippetExpression ("");
  157. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  158. "if () {{{0}" +
  159. "}}{0}", NewLine), Generate (), "#2");
  160. conditionStatement.Condition = new CodeSnippetExpression ("true == false");
  161. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  162. "if (true == false) {{{0}" +
  163. "}}{0}", NewLine), Generate (), "#3");
  164. conditionStatement.TrueStatements.AddRange (trueStatements);
  165. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  166. "if (true == false) {{{0}" +
  167. " DoA();{0}" +
  168. " ;;{0}" +
  169. " DoB();{0}" +
  170. " ;{0}" +
  171. #if NET_2_0
  172. "A{0}" +
  173. #else
  174. " A{0}" +
  175. #endif
  176. " DoC();{0}" +
  177. "}}{0}", NewLine), Generate (), "#3");
  178. conditionStatement.FalseStatements.AddRange (falseStatements);
  179. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  180. "if (true == false) {{{0}" +
  181. " DoA();{0}" +
  182. " ;;{0}" +
  183. " DoB();{0}" +
  184. " ;{0}" +
  185. #if NET_2_0
  186. "A{0}" +
  187. #else
  188. " A{0}" +
  189. #endif
  190. " DoC();{0}" +
  191. "}}{0}" +
  192. "else {{{0}" +
  193. " DoD();{0}" +
  194. #if NET_2_0
  195. "B{0}" +
  196. #else
  197. " B{0}" +
  198. #endif
  199. " ;;{0}" +
  200. " DoE();{0}" +
  201. " ;{0}" +
  202. " DoF();{0}" +
  203. "}}{0}", NewLine), Generate (), "#4");
  204. options.ElseOnClosing = true;
  205. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  206. "if (true == false) {{{0}" +
  207. " DoA();{0}" +
  208. " ;;{0}" +
  209. " DoB();{0}" +
  210. " ;{0}" +
  211. #if NET_2_0
  212. "A{0}" +
  213. #else
  214. " A{0}" +
  215. #endif
  216. " DoC();{0}" +
  217. "}} else {{{0}" +
  218. " DoD();{0}" +
  219. #if NET_2_0
  220. "B{0}" +
  221. #else
  222. " B{0}" +
  223. #endif
  224. " ;;{0}" +
  225. " DoE();{0}" +
  226. " ;{0}" +
  227. " DoF();{0}" +
  228. "}}{0}", NewLine), Generate (), "#5");
  229. options.ElseOnClosing = false;
  230. conditionStatement.TrueStatements.Clear ();
  231. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  232. "if (true == false) {{{0}" +
  233. "}}{0}" +
  234. "else {{{0}" +
  235. " DoD();{0}" +
  236. #if NET_2_0
  237. "B{0}" +
  238. #else
  239. " B{0}" +
  240. #endif
  241. " ;;{0}" +
  242. " DoE();{0}" +
  243. " ;{0}" +
  244. " DoF();{0}" +
  245. "}}{0}", NewLine), Generate (), "#6");
  246. conditionStatement.TrueStatements.AddRange (trueStatements);
  247. conditionStatement.FalseStatements.Clear ();
  248. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  249. "if (true == false) {{{0}" +
  250. " DoA();{0}" +
  251. " ;;{0}" +
  252. " DoB();{0}" +
  253. " ;{0}" +
  254. #if NET_2_0
  255. "A{0}" +
  256. #else
  257. " A{0}" +
  258. #endif
  259. " DoC();{0}" +
  260. "}}{0}", NewLine), Generate (), "#7");
  261. }
  262. [Test]
  263. public void CodeExpressionStatementTest ()
  264. {
  265. CodeExpressionStatement ces = new CodeExpressionStatement ();
  266. statement = ces;
  267. try {
  268. Generate ();
  269. Assert.Fail ("#1");
  270. } catch (ArgumentNullException) {
  271. }
  272. ces.Expression = new CodeSnippetExpression ("something");
  273. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  274. "something;{0}", NewLine), Generate (), "#2");
  275. }
  276. [Test]
  277. public void CodeGotoStatementTest ()
  278. {
  279. statement = new CodeGotoStatement ("something");
  280. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  281. "goto something;{0}", NewLine), Generate ());
  282. }
  283. [Test]
  284. public void CodeIterationStatementTest ()
  285. {
  286. CodeIterationStatement cis = new CodeIterationStatement ();
  287. statement = cis;
  288. try {
  289. Generate ();
  290. Assert.Fail ("#1: null InitStatement should cause NRE");
  291. } catch (NullReferenceException) {
  292. }
  293. cis.InitStatement = new CodeVariableDeclarationStatement (typeof(int),
  294. "testInt", new CodePrimitiveExpression(1));
  295. try {
  296. Generate ();
  297. Assert.Fail ("#2: null TestExpression should cause ArgumentNullException");
  298. } catch (ArgumentNullException) {
  299. }
  300. cis.TestExpression = new CodeBinaryOperatorExpression (
  301. new CodeVariableReferenceExpression ("testInt"),
  302. CodeBinaryOperatorType.LessThan,
  303. new CodePrimitiveExpression (10));
  304. try {
  305. Generate ();
  306. Assert.Fail ("#3: null IncrementStatement should cause NRE");
  307. } catch (NullReferenceException) {
  308. }
  309. cis.IncrementStatement = new CodeAssignStatement (
  310. new CodeVariableReferenceExpression ("testInt"),
  311. new CodeBinaryOperatorExpression (
  312. new CodeVariableReferenceExpression ("testInt"),
  313. CodeBinaryOperatorType.Add,
  314. new CodePrimitiveExpression (1)));
  315. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  316. "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
  317. "}}{0}", NewLine), Generate (), "#4");
  318. cis.Statements.AddRange (new CodeStatement[] {
  319. new CodeExpressionStatement (new CodeSnippetExpression ("DoA()")),
  320. new CodeExpressionStatement (new CodeSnippetExpression (";")),
  321. new CodeExpressionStatement (new CodeSnippetExpression ("DoB()")),
  322. new CodeLabeledStatement ("test", new CodeSnippetStatement ("C")),
  323. new CodeExpressionStatement (new CodeSnippetExpression ("")),
  324. new CodeSnippetStatement ("A"),
  325. new CodeExpressionStatement (new CodeSnippetExpression ("DoC()")) });
  326. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  327. "for (int testInt = 1; (testInt < 10); testInt = (testInt + 1)) {{{0}" +
  328. " DoA();{0}" +
  329. " ;;{0}" +
  330. " DoB();{0}" +
  331. "test:{0}" +
  332. #if NET_2_0
  333. "C{0}" +
  334. #else
  335. " C{0}" +
  336. #endif
  337. " ;{0}" +
  338. #if NET_2_0
  339. "A{0}" +
  340. #else
  341. " A{0}" +
  342. #endif
  343. " DoC();{0}" +
  344. "}}{0}", NewLine), Generate (), "#5");
  345. }
  346. [Test]
  347. public void CodeLabeledStatementTest ()
  348. {
  349. CodeLabeledStatement cls = new CodeLabeledStatement ();
  350. statement = cls;
  351. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  352. ":{0}", NewLine), Generate (), "#1");
  353. cls.Label = "class";
  354. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  355. "class:{0}", NewLine), Generate (), "#2");
  356. cls.Statement = new CodeSnippetStatement ("A");
  357. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  358. "class:{0}" +
  359. #if NET_2_0
  360. "A{0}",
  361. #else
  362. " A{0}",
  363. #endif
  364. NewLine), Generate (), "#3");
  365. }
  366. [Test]
  367. public void CodeMethodReturnStatementTest ()
  368. {
  369. CodeMethodReturnStatement cmrs = new CodeMethodReturnStatement ();
  370. statement = cmrs;
  371. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  372. "return;{0}", NewLine), Generate (), "#1");
  373. cmrs.Expression = new CodePrimitiveExpression (1);
  374. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  375. "return 1;{0}", NewLine), Generate (), "#2");
  376. }
  377. [Test]
  378. public void CodeRemoveEventStatementTest ()
  379. {
  380. CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
  381. new CodeSnippetExpression ("A"), "class");
  382. CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
  383. CodeRemoveEventStatement cres = new CodeRemoveEventStatement ();
  384. statement = cres;
  385. try {
  386. Generate ();
  387. Assert.Fail ("#1");
  388. } catch (ArgumentNullException) {
  389. }
  390. cres.Event = cere;
  391. try {
  392. Generate ();
  393. Assert.Fail ("#2");
  394. } catch (ArgumentNullException) {
  395. }
  396. cres.Event = null;
  397. cres.Listener = handler;
  398. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  399. " -= EventHandler;{0}", NewLine), Generate (), "#3");
  400. cres.Event = cere;
  401. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  402. "A.@class -= EventHandler;{0}", NewLine), Generate (), "#4");
  403. cres.Event = new CodeEventReferenceExpression (
  404. new CodeSnippetExpression ((string) null), "");
  405. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  406. ". -= EventHandler;{0}", NewLine), Generate (), "#5");
  407. cres.Listener = new CodeSnippetExpression ("");
  408. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  409. ". -= ;{0}", NewLine), Generate (), "#6");
  410. }
  411. [Test]
  412. public void CodeSnippetStatementTest ()
  413. {
  414. CodeSnippetStatement css = new CodeSnippetStatement ();
  415. statement = css;
  416. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  417. "{0}", NewLine), Generate (), "#1");
  418. css.Value = "class";
  419. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  420. "class{0}", NewLine), Generate (), "#2");
  421. css.Value = null;
  422. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  423. "{0}", NewLine), Generate (), "#3");
  424. }
  425. [Test]
  426. [ExpectedException (typeof (ArgumentException))]
  427. public void CodeStatement ()
  428. {
  429. CodeStatement cs = new CodeStatement ();
  430. statement = cs;
  431. Generate ();
  432. }
  433. [Test]
  434. public void CodeThrowExceptionStatementTest ()
  435. {
  436. CodeThrowExceptionStatement ctes = new CodeThrowExceptionStatement ();
  437. statement = ctes;
  438. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  439. "throw;{0}", NewLine), Generate (), "#1");
  440. ctes.ToThrow = new CodeSnippetExpression ("whatever");
  441. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  442. "throw whatever;{0}", NewLine), Generate (), "#2");
  443. }
  444. [Test]
  445. public void CodeTryCatchFinallyStatementTest ()
  446. {
  447. CodeStatement cs = new CodeGotoStatement ("exit");
  448. CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
  449. CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
  450. CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
  451. CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
  452. statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
  453. new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
  454. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  455. "try {{{0}" +
  456. " goto exit;{0}" +
  457. "}}{0}" +
  458. "catch (System.ArgumentException ex1) {{{0}" +
  459. "}}{0}" +
  460. "catch (System.ApplicationException ) {{{0}" +
  461. "}}{0}" +
  462. "finally {{{0}" +
  463. #if NET_2_0
  464. "A{0}" +
  465. "B{0}" +
  466. #else
  467. " A{0}" +
  468. " B{0}" +
  469. #endif
  470. "}}{0}", NewLine), Generate (), "#1");
  471. options.ElseOnClosing = true;
  472. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  473. "try {{{0}" +
  474. " goto exit;{0}" +
  475. "}} catch (System.ArgumentException ex1) {{{0}" +
  476. "}} catch (System.ApplicationException ) {{{0}" +
  477. "}} finally {{{0}" +
  478. #if NET_2_0
  479. "A{0}" +
  480. "B{0}" +
  481. #else
  482. " A{0}" +
  483. " B{0}" +
  484. #endif
  485. "}}{0}", NewLine), Generate (), "#2");
  486. statement = new CodeTryCatchFinallyStatement ();
  487. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  488. "try {{{0}" +
  489. "}}{0}", NewLine), Generate (), "#3");
  490. options.ElseOnClosing = false;
  491. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  492. "try {{{0}" +
  493. "}}{0}", NewLine), Generate (), "#4");
  494. }
  495. [Test]
  496. public void CodeVariableDeclarationStatementTest ()
  497. {
  498. CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ();
  499. statement = cvds;
  500. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  501. "void ;{0}", NewLine), Generate (), "#1");
  502. cvds.Name = "class";
  503. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  504. "void @class;{0}", NewLine), Generate (), "#2");
  505. cvds.Name = "A";
  506. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  507. "void A;{0}", NewLine), Generate (), "#3");
  508. cvds.Type = new CodeTypeReference (typeof (int));
  509. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  510. "int A;{0}", NewLine), Generate (), "#4");
  511. cvds.InitExpression = new CodePrimitiveExpression (25);
  512. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  513. "int A = 25;{0}", NewLine), Generate (), "#5");
  514. cvds.Name = null;
  515. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  516. "int = 25;{0}", NewLine), Generate (), "#5");
  517. }
  518. }
  519. }