2
0

CodeGeneratorFromStatementTest.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. //
  2. // Microsoft.VisualBasic.* Test Cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (c) 2005 Gert Driesen
  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.VisualBasic
  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. "AddHandler , EventHandler{0}", NewLine), Generate (), "#3");
  102. attachEventStatement.Event = cere;
  103. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  104. "AddHandler 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. "AddHandler ., EventHandler{0}", NewLine), Generate (), "#5");
  109. attachEventStatement.Listener = new CodeSnippetExpression ("");
  110. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  111. "AddHandler ., {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. "'{0}", NewLine), Generate (), "#1");
  122. comment.Text = "a\nb";
  123. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  124. "'a\n'b{0}", 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 Then{0}" +
  159. "End If{0}", NewLine), Generate (), "#2");
  160. conditionStatement.Condition = new CodeSnippetExpression ("true == false");
  161. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  162. "If true == false Then{0}" +
  163. "End If{0}", NewLine), Generate (), "#3");
  164. conditionStatement.TrueStatements.AddRange (trueStatements);
  165. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  166. "If true == false Then{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. "End If{0}", NewLine), Generate (), "#3");
  178. conditionStatement.FalseStatements.AddRange (falseStatements);
  179. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  180. "If true == false Then{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. "Else{0}" +
  192. " DoD(){0}" +
  193. #if NET_2_0
  194. "B{0}" +
  195. #else
  196. " B{0}" +
  197. #endif
  198. " ;{0}" +
  199. " DoE(){0}" +
  200. " {0}" +
  201. " DoF(){0}" +
  202. "End If{0}", NewLine), Generate (), "#4");
  203. options.ElseOnClosing = true;
  204. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  205. "If true == false Then{0}" +
  206. " DoA(){0}" +
  207. " ;{0}" +
  208. " DoB(){0}" +
  209. " {0}" +
  210. #if NET_2_0
  211. "A{0}" +
  212. #else
  213. " A{0}" +
  214. #endif
  215. " DoC(){0}" +
  216. "Else{0}" +
  217. " DoD(){0}" +
  218. #if NET_2_0
  219. "B{0}" +
  220. #else
  221. " B{0}" +
  222. #endif
  223. " ;{0}" +
  224. " DoE(){0}" +
  225. " {0}" +
  226. " DoF(){0}" +
  227. "End If{0}", NewLine), Generate (), "#5");
  228. options.ElseOnClosing = false;
  229. conditionStatement.TrueStatements.Clear ();
  230. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  231. "If true == false Then{0}" +
  232. "Else{0}" +
  233. " DoD(){0}" +
  234. #if NET_2_0
  235. "B{0}" +
  236. #else
  237. " B{0}" +
  238. #endif
  239. " ;{0}" +
  240. " DoE(){0}" +
  241. " {0}" +
  242. " DoF(){0}" +
  243. "End If{0}", NewLine), Generate (), "#6");
  244. conditionStatement.TrueStatements.AddRange (trueStatements);
  245. conditionStatement.FalseStatements.Clear ();
  246. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  247. "If true == false Then{0}" +
  248. " DoA(){0}" +
  249. " ;{0}" +
  250. " DoB(){0}" +
  251. " {0}" +
  252. #if NET_2_0
  253. "A{0}" +
  254. #else
  255. " A{0}" +
  256. #endif
  257. " DoC(){0}" +
  258. "End If{0}", NewLine), Generate (), "#7");
  259. }
  260. [Test]
  261. public void CodeExpressionStatementTest ()
  262. {
  263. CodeExpressionStatement ces = new CodeExpressionStatement ();
  264. statement = ces;
  265. try {
  266. Generate ();
  267. Assert.Fail ("#1");
  268. } catch (ArgumentNullException) {
  269. }
  270. ces.Expression = new CodeSnippetExpression ("something");
  271. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  272. "something{0}", NewLine), Generate (), "#2");
  273. }
  274. [Test]
  275. public void CodeGotoStatementTest ()
  276. {
  277. statement = new CodeGotoStatement ("something");
  278. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  279. "goto something{0}", NewLine), Generate ());
  280. }
  281. [Test]
  282. public void CodeIterationStatementTest ()
  283. {
  284. CodeIterationStatement cis = new CodeIterationStatement ();
  285. statement = cis;
  286. try {
  287. Generate ();
  288. Assert.Fail ("#1: null InitStatement should cause NRE");
  289. } catch (NullReferenceException) {
  290. }
  291. cis.InitStatement = new CodeVariableDeclarationStatement (typeof (int),
  292. "testInt", new CodePrimitiveExpression (1));
  293. try {
  294. Generate ();
  295. Assert.Fail ("#2: null TestExpression should cause ArgumentNullException");
  296. } catch (ArgumentNullException) {
  297. }
  298. cis.TestExpression = new CodeBinaryOperatorExpression (
  299. new CodeVariableReferenceExpression ("testInt"),
  300. CodeBinaryOperatorType.LessThan,
  301. new CodePrimitiveExpression (10));
  302. try {
  303. Generate ();
  304. Assert.Fail ("#3: null IncrementStatement should cause NRE");
  305. } catch (NullReferenceException) {
  306. }
  307. cis.IncrementStatement = new CodeAssignStatement (
  308. new CodeVariableReferenceExpression ("testInt"),
  309. new CodeBinaryOperatorExpression (
  310. new CodeVariableReferenceExpression ("testInt"),
  311. CodeBinaryOperatorType.Add,
  312. new CodePrimitiveExpression (1)));
  313. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  314. "Dim testInt As Integer = 1{0}" +
  315. "Do While (testInt < 10){0}" +
  316. " testInt = (testInt + 1){0}" +
  317. "Loop{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. "Dim testInt As Integer = 1{0}" +
  328. "Do While (testInt < 10){0}" +
  329. " DoA(){0}" +
  330. " ;{0}" +
  331. " DoB(){0}" +
  332. "test:{0}" +
  333. #if NET_2_0
  334. "C{0}" +
  335. #else
  336. " C{0}" +
  337. #endif
  338. " {0}" +
  339. #if NET_2_0
  340. "A{0}" +
  341. #else
  342. " A{0}" +
  343. #endif
  344. " DoC(){0}" +
  345. " testInt = (testInt + 1){0}" +
  346. "Loop{0}", NewLine), Generate (), "#5");
  347. }
  348. [Test]
  349. public void CodeLabeledStatementTest ()
  350. {
  351. CodeLabeledStatement cls = new CodeLabeledStatement ();
  352. statement = cls;
  353. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  354. ":{0}", NewLine), Generate (), "#1");
  355. cls.Label = "class";
  356. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  357. "class:{0}", NewLine), Generate (), "#2");
  358. cls.Statement = new CodeSnippetStatement ("A");
  359. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  360. "class:{0}" +
  361. #if NET_2_0
  362. "A{0}",
  363. #else
  364. " A{0}",
  365. #endif
  366. NewLine), Generate (), "#3");
  367. }
  368. [Test]
  369. public void CodeMethodReturnStatementTest ()
  370. {
  371. CodeMethodReturnStatement cmrs = new CodeMethodReturnStatement ();
  372. statement = cmrs;
  373. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  374. "Return{0}", NewLine), Generate (), "#1");
  375. cmrs.Expression = new CodePrimitiveExpression (1);
  376. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  377. "Return 1{0}", NewLine), Generate (), "#2");
  378. }
  379. [Test]
  380. public void CodeRemoveEventStatementTest ()
  381. {
  382. CodeEventReferenceExpression cere = new CodeEventReferenceExpression (
  383. new CodeSnippetExpression ("A"), "class");
  384. CodeSnippetExpression handler = new CodeSnippetExpression ("EventHandler");
  385. CodeRemoveEventStatement cres = new CodeRemoveEventStatement ();
  386. statement = cres;
  387. try {
  388. Generate ();
  389. Assert.Fail ("#1");
  390. } catch (ArgumentNullException) {
  391. }
  392. cres.Event = cere;
  393. try {
  394. Generate ();
  395. Assert.Fail ("#2");
  396. } catch (ArgumentNullException) {
  397. }
  398. cres.Event = null;
  399. cres.Listener = handler;
  400. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  401. "RemoveHandler , EventHandler{0}", NewLine), Generate (), "#3");
  402. cres.Event = cere;
  403. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  404. "RemoveHandler A.[class], EventHandler{0}", NewLine), Generate (), "#4");
  405. cres.Event = new CodeEventReferenceExpression (
  406. new CodeSnippetExpression ((string) null), "");
  407. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  408. "RemoveHandler ., EventHandler{0}", NewLine), Generate (), "#5");
  409. cres.Listener = new CodeSnippetExpression ("");
  410. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  411. "RemoveHandler ., {0}", NewLine), Generate (), "#6");
  412. }
  413. [Test]
  414. public void CodeSnippetStatementTest ()
  415. {
  416. CodeSnippetStatement css = new CodeSnippetStatement ();
  417. statement = css;
  418. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  419. "{0}", NewLine), Generate (), "#1");
  420. css.Value = "class";
  421. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  422. "class{0}", NewLine), Generate (), "#2");
  423. css.Value = null;
  424. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  425. "{0}", NewLine), Generate (), "#3");
  426. }
  427. [Test]
  428. [ExpectedException (typeof (ArgumentException))]
  429. public void CodeStatement ()
  430. {
  431. CodeStatement cs = new CodeStatement ();
  432. statement = cs;
  433. Generate ();
  434. }
  435. [Test]
  436. public void CodeThrowExceptionStatementTest ()
  437. {
  438. CodeThrowExceptionStatement ctet = new CodeThrowExceptionStatement ();
  439. statement = ctet;
  440. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  441. "Throw{0}", NewLine), Generate (), "#1");
  442. ctet.ToThrow = new CodeSnippetExpression ("whatever");
  443. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  444. "Throw whatever{0}", NewLine), Generate (), "#2");
  445. }
  446. [Test]
  447. public void CodeTryCatchFinallyStatementTest ()
  448. {
  449. CodeStatement cs = new CodeGotoStatement ("exit");
  450. CodeCatchClause ccc1 = new CodeCatchClause ("ex1", new CodeTypeReference ("System.ArgumentException"));
  451. CodeCatchClause ccc2 = new CodeCatchClause (null, new CodeTypeReference ("System.ApplicationException"));
  452. CodeSnippetStatement fin1 = new CodeSnippetStatement ("A");
  453. CodeSnippetStatement fin2 = new CodeSnippetStatement ("B");
  454. statement = new CodeTryCatchFinallyStatement (new CodeStatement[] { cs },
  455. new CodeCatchClause[] { ccc1, ccc2 }, new CodeStatement[] { fin1, fin2 });
  456. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  457. "Try {0}" +
  458. " goto exit{0}" +
  459. "Catch ex1 As System.ArgumentException{0}" +
  460. #if NET_2_0
  461. "Catch __exception As System.ApplicationException{0}" +
  462. #else
  463. "Catch As System.ApplicationException{0}" +
  464. #endif
  465. "Finally{0}" +
  466. #if NET_2_0
  467. "A{0}" +
  468. "B{0}" +
  469. #else
  470. " A{0}" +
  471. " B{0}" +
  472. #endif
  473. "End Try{0}", NewLine), Generate (), "#1");
  474. options.ElseOnClosing = true;
  475. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  476. "Try {0}" +
  477. " goto exit{0}" +
  478. "Catch ex1 As System.ArgumentException{0}" +
  479. #if NET_2_0
  480. "Catch __exception As System.ApplicationException{0}" +
  481. #else
  482. "Catch As System.ApplicationException{0}" +
  483. #endif
  484. "Finally{0}" +
  485. #if NET_2_0
  486. "A{0}" +
  487. "B{0}" +
  488. #else
  489. " A{0}" +
  490. " B{0}" +
  491. #endif
  492. "End Try{0}", NewLine), Generate (), "#2");
  493. statement = new CodeTryCatchFinallyStatement ();
  494. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  495. "Try {0}" +
  496. "End Try{0}", NewLine), Generate (), "#3");
  497. options.ElseOnClosing = false;
  498. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  499. "Try {0}" +
  500. "End Try{0}", NewLine), Generate (), "#4");
  501. }
  502. [Test]
  503. public void CodeVariableDeclarationStatementTest ()
  504. {
  505. CodeVariableDeclarationStatement cvds = new CodeVariableDeclarationStatement ();
  506. statement = cvds;
  507. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  508. #if NET_2_0
  509. "Dim __exception As System.Void{0}",
  510. #else
  511. "Dim As System.Void{0}",
  512. #endif
  513. NewLine), Generate (), "#1");
  514. cvds.Name = "class";
  515. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  516. "Dim [class] As System.Void{0}", NewLine), Generate (), "#2");
  517. cvds.Name = "A";
  518. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  519. "Dim A As System.Void{0}", NewLine), Generate (), "#3");
  520. cvds.Type = new CodeTypeReference (typeof (int));
  521. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  522. "Dim A As Integer{0}", NewLine), Generate (), "#4");
  523. cvds.InitExpression = new CodePrimitiveExpression (25);
  524. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  525. "Dim A As Integer = 25{0}", NewLine), Generate (), "#5");
  526. cvds.Name = null;
  527. Assert.AreEqual (string.Format (CultureInfo.InvariantCulture,
  528. #if NET_2_0
  529. "Dim __exception As Integer = 25{0}",
  530. #else
  531. "Dim As Integer = 25{0}",
  532. #endif
  533. NewLine), Generate (), "#6");
  534. }
  535. }
  536. }