CodeGeneratorCas.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. //
  2. // CodeGeneratorCas.cs
  3. // - CAS unit tests for System.CodeDom.Compiler.CodeGenerator
  4. //
  5. // Author:
  6. // Sebastien Pouliot <[email protected]>
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using NUnit.Framework;
  30. using System;
  31. using System.CodeDom;
  32. using System.CodeDom.Compiler;
  33. using System.IO;
  34. using System.Reflection;
  35. using System.Security;
  36. using System.Security.Permissions;
  37. namespace MonoCasTests.System.CodeDom.Compiler {
  38. class CodeGeneratorTest: CodeGenerator {
  39. public CodeGeneratorTest ()
  40. {
  41. }
  42. protected override string CreateEscapedIdentifier (string value)
  43. {
  44. return String.Empty;
  45. }
  46. protected override string CreateValidIdentifier (string value)
  47. {
  48. return String.Empty;
  49. }
  50. protected override void GenerateArgumentReferenceExpression (CodeArgumentReferenceExpression e)
  51. {
  52. }
  53. protected override void GenerateArrayCreateExpression (CodeArrayCreateExpression e)
  54. {
  55. }
  56. protected override void GenerateArrayIndexerExpression (CodeArrayIndexerExpression e)
  57. {
  58. }
  59. protected override void GenerateAssignStatement (CodeAssignStatement e)
  60. {
  61. }
  62. protected override void GenerateAttachEventStatement (CodeAttachEventStatement e)
  63. {
  64. }
  65. protected override void GenerateAttributeDeclarationsEnd (CodeAttributeDeclarationCollection attributes)
  66. {
  67. }
  68. protected override void GenerateAttributeDeclarationsStart (CodeAttributeDeclarationCollection attributes)
  69. {
  70. }
  71. protected override void GenerateBaseReferenceExpression (CodeBaseReferenceExpression e)
  72. {
  73. }
  74. protected override void GenerateCastExpression (CodeCastExpression e)
  75. {
  76. }
  77. protected override void GenerateComment (CodeComment e)
  78. {
  79. }
  80. protected override void GenerateConditionStatement (CodeConditionStatement e)
  81. {
  82. }
  83. protected override void GenerateConstructor (CodeConstructor e, CodeTypeDeclaration c)
  84. {
  85. }
  86. protected override void GenerateDelegateCreateExpression (CodeDelegateCreateExpression e)
  87. {
  88. }
  89. protected override void GenerateDelegateInvokeExpression (CodeDelegateInvokeExpression e)
  90. {
  91. }
  92. protected override void GenerateEntryPointMethod (CodeEntryPointMethod e, CodeTypeDeclaration c)
  93. {
  94. }
  95. protected override void GenerateEvent (CodeMemberEvent e, CodeTypeDeclaration c)
  96. {
  97. }
  98. protected override void GenerateEventReferenceExpression (CodeEventReferenceExpression e)
  99. {
  100. }
  101. protected override void GenerateExpressionStatement (CodeExpressionStatement e)
  102. {
  103. }
  104. protected override void GenerateField (CodeMemberField e)
  105. {
  106. }
  107. protected override void GenerateFieldReferenceExpression (CodeFieldReferenceExpression e)
  108. {
  109. }
  110. protected override void GenerateGotoStatement (CodeGotoStatement e)
  111. {
  112. }
  113. protected override void GenerateIndexerExpression (CodeIndexerExpression e)
  114. {
  115. }
  116. protected override void GenerateIterationStatement (CodeIterationStatement e)
  117. {
  118. }
  119. protected override void GenerateLabeledStatement (CodeLabeledStatement e)
  120. {
  121. }
  122. protected override void GenerateLinePragmaEnd (CodeLinePragma e)
  123. {
  124. }
  125. protected override void GenerateLinePragmaStart (CodeLinePragma e)
  126. {
  127. }
  128. protected override void GenerateMethod (CodeMemberMethod e, CodeTypeDeclaration c)
  129. {
  130. }
  131. protected override void GenerateMethodInvokeExpression (CodeMethodInvokeExpression e)
  132. {
  133. }
  134. protected override void GenerateMethodReferenceExpression (CodeMethodReferenceExpression e)
  135. {
  136. }
  137. protected override void GenerateMethodReturnStatement (CodeMethodReturnStatement e)
  138. {
  139. }
  140. protected override void GenerateNamespaceEnd (CodeNamespace e)
  141. {
  142. }
  143. protected override void GenerateNamespaceImport (CodeNamespaceImport e)
  144. {
  145. }
  146. protected override void GenerateNamespaceStart (CodeNamespace e)
  147. {
  148. }
  149. protected override void GenerateObjectCreateExpression (CodeObjectCreateExpression e)
  150. {
  151. }
  152. protected override void GenerateProperty (CodeMemberProperty e, CodeTypeDeclaration c)
  153. {
  154. }
  155. protected override void GeneratePropertyReferenceExpression (CodePropertyReferenceExpression e)
  156. {
  157. }
  158. protected override void GeneratePropertySetValueReferenceExpression (CodePropertySetValueReferenceExpression e)
  159. {
  160. }
  161. protected override void GenerateRemoveEventStatement (CodeRemoveEventStatement e)
  162. {
  163. }
  164. protected override void GenerateSnippetExpression (CodeSnippetExpression e)
  165. {
  166. }
  167. protected override void GenerateSnippetMember (CodeSnippetTypeMember e)
  168. {
  169. }
  170. protected override void GenerateThisReferenceExpression (CodeThisReferenceExpression e)
  171. {
  172. }
  173. protected override void GenerateThrowExceptionStatement (CodeThrowExceptionStatement e)
  174. {
  175. }
  176. protected override void GenerateTryCatchFinallyStatement (CodeTryCatchFinallyStatement e)
  177. {
  178. }
  179. protected override void GenerateTypeConstructor (CodeTypeConstructor e)
  180. {
  181. }
  182. protected override void GenerateTypeEnd (CodeTypeDeclaration e)
  183. {
  184. }
  185. protected override void GenerateTypeStart (CodeTypeDeclaration e)
  186. {
  187. }
  188. protected override void GenerateVariableDeclarationStatement (CodeVariableDeclarationStatement e)
  189. {
  190. }
  191. protected override void GenerateVariableReferenceExpression (CodeVariableReferenceExpression e)
  192. {
  193. }
  194. protected override string GetTypeOutput (CodeTypeReference value)
  195. {
  196. return String.Empty;
  197. }
  198. protected override bool IsValidIdentifier (string value)
  199. {
  200. return true;
  201. }
  202. protected override string NullToken {
  203. get {
  204. return String.Empty;
  205. }
  206. }
  207. protected override void OutputType (CodeTypeReference typeRef)
  208. {
  209. }
  210. protected override string QuoteSnippetString (string value)
  211. {
  212. return String.Empty;
  213. }
  214. protected override bool Supports (GeneratorSupport support)
  215. {
  216. return true;
  217. }
  218. public void TestProtectedProperties ()
  219. {
  220. #if NET_2_0
  221. Assert.IsNull (CurrentClass, "CurrentClass");
  222. #endif
  223. Assert.IsNull (CurrentMember, "CurrentMember");
  224. Assert.AreEqual ("<% unknown %>", CurrentMemberName, "CurrentMemberName");
  225. Assert.AreEqual ("<% unknown %>", CurrentTypeName, "CurrentTypeName");
  226. try {
  227. Assert.AreEqual (0, Indent, "Indent");
  228. }
  229. catch (NullReferenceException) {
  230. }
  231. try {
  232. Indent = Int32.MinValue;
  233. }
  234. catch (NullReferenceException) {
  235. }
  236. Assert.IsFalse (IsCurrentClass, "IsCurrentClass");
  237. Assert.IsFalse (IsCurrentDelegate, "IsCurrentDelegate");
  238. Assert.IsFalse (IsCurrentEnum, "IsCurrentEnum");
  239. Assert.IsFalse (IsCurrentInterface, "IsCurrentInterface");
  240. Assert.IsFalse (IsCurrentStruct, "IsCurrentStruct");
  241. Assert.AreEqual (String.Empty, NullToken, "NullToken");
  242. Assert.IsNull (Options, "Options");
  243. Assert.IsNull (Output, "Output");
  244. }
  245. public void TestProtectedMethods ()
  246. {
  247. try {
  248. ContinueOnNewLine (String.Empty);
  249. }
  250. catch (NullReferenceException) {
  251. }
  252. try {
  253. GenerateBinaryOperatorExpression (null);
  254. }
  255. catch (NullReferenceException) {
  256. }
  257. try {
  258. GenerateCommentStatement (null);
  259. }
  260. catch (NullReferenceException) {
  261. }
  262. try {
  263. GenerateCommentStatements (null);
  264. }
  265. catch (NullReferenceException) {
  266. }
  267. try {
  268. GenerateCompileUnit (null);
  269. }
  270. catch (NullReferenceException) {
  271. }
  272. try {
  273. GenerateCompileUnitEnd (null);
  274. }
  275. catch (NullReferenceException) {
  276. }
  277. try {
  278. GenerateCompileUnitStart (null);
  279. }
  280. catch (NullReferenceException) {
  281. }
  282. try {
  283. GenerateDecimalValue (0);
  284. }
  285. catch (NullReferenceException) {
  286. }
  287. #if NET_2_0
  288. try {
  289. GenerateDefaultValueExpression (null);
  290. }
  291. catch (NotImplementedException) {
  292. // both mono & ms
  293. }
  294. #endif
  295. try {
  296. GenerateDirectionExpression (null);
  297. }
  298. catch (NullReferenceException) {
  299. }
  300. #if NET_2_0
  301. GenerateDirectives (null);
  302. #endif
  303. try {
  304. GenerateDoubleValue (Double.MaxValue);
  305. }
  306. catch (NullReferenceException) {
  307. }
  308. try {
  309. GenerateExpression (null);
  310. }
  311. catch (ArgumentNullException) {
  312. }
  313. try {
  314. GenerateNamespace (null);
  315. }
  316. catch (NullReferenceException) {
  317. }
  318. try {
  319. GenerateNamespaceImports (null);
  320. }
  321. catch (NullReferenceException) {
  322. }
  323. try {
  324. GenerateNamespaces (null);
  325. }
  326. catch (NullReferenceException) {
  327. }
  328. try {
  329. GenerateParameterDeclarationExpression (null);
  330. }
  331. catch (NullReferenceException) {
  332. }
  333. try {
  334. GeneratePrimitiveExpression (null);
  335. }
  336. catch (NullReferenceException) {
  337. }
  338. try {
  339. GenerateSingleFloatValue (Single.MinValue);
  340. }
  341. catch (NullReferenceException) {
  342. }
  343. try {
  344. GenerateSnippetCompileUnit (null);
  345. }
  346. catch (NullReferenceException) {
  347. }
  348. try {
  349. GenerateSnippetStatement (null);
  350. }
  351. catch (NullReferenceException) {
  352. }
  353. try {
  354. GenerateStatement (null);
  355. }
  356. catch (NullReferenceException) {
  357. }
  358. try {
  359. GenerateStatements (null);
  360. }
  361. catch (NullReferenceException) {
  362. }
  363. try {
  364. GenerateTypeOfExpression (null);
  365. }
  366. catch (NullReferenceException) {
  367. }
  368. try {
  369. GenerateTypeReferenceExpression (null);
  370. }
  371. catch (NullReferenceException) {
  372. }
  373. try {
  374. GenerateTypes (null);
  375. }
  376. catch (NullReferenceException) {
  377. }
  378. try {
  379. OutputAttributeArgument (null);
  380. }
  381. catch (NullReferenceException) {
  382. }
  383. try {
  384. OutputAttributeDeclarations (null);
  385. }
  386. catch (NullReferenceException) {
  387. }
  388. try {
  389. OutputDirection (FieldDirection.In);
  390. }
  391. catch (NullReferenceException) {
  392. }
  393. try {
  394. OutputExpressionList (null);
  395. }
  396. catch (NullReferenceException) {
  397. }
  398. try {
  399. OutputExpressionList (null, true);
  400. }
  401. catch (NullReferenceException) {
  402. }
  403. try {
  404. OutputFieldScopeModifier (MemberAttributes.Abstract);
  405. }
  406. catch (NullReferenceException) {
  407. }
  408. try {
  409. OutputIdentifier (null);
  410. }
  411. catch (NullReferenceException) {
  412. }
  413. try {
  414. OutputMemberAccessModifier (MemberAttributes.Abstract);
  415. }
  416. catch (NullReferenceException) {
  417. }
  418. try {
  419. OutputMemberScopeModifier (MemberAttributes.Abstract);
  420. }
  421. catch (NullReferenceException) {
  422. }
  423. try {
  424. OutputOperator (CodeBinaryOperatorType.Add);
  425. }
  426. catch (NullReferenceException) {
  427. }
  428. try {
  429. OutputParameters (null);
  430. }
  431. catch (NullReferenceException) {
  432. }
  433. try {
  434. OutputTypeAttributes (TypeAttributes.Abstract, false, false);
  435. }
  436. catch (NullReferenceException) {
  437. }
  438. try {
  439. OutputTypeNamePair (null, null);
  440. }
  441. catch (NullReferenceException) {
  442. }
  443. try {
  444. Supports (GeneratorSupport.ArraysOfArrays);
  445. }
  446. catch (NullReferenceException) {
  447. }
  448. try {
  449. ValidateIdentifier (null);
  450. }
  451. catch (NullReferenceException) {
  452. }
  453. }
  454. }
  455. [TestFixture]
  456. [Category ("CAS")]
  457. public class CodeGeneratorCas {
  458. private StringWriter writer;
  459. [TestFixtureSetUp]
  460. public void FixtureSetUp ()
  461. {
  462. // at full trust
  463. writer = new StringWriter ();
  464. }
  465. [SetUp]
  466. public void SetUp ()
  467. {
  468. if (!SecurityManager.SecurityEnabled)
  469. Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
  470. }
  471. #if NET_2_0
  472. [Test]
  473. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  474. public void Public ()
  475. {
  476. CodeGeneratorTest cg = new CodeGeneratorTest ();
  477. try {
  478. cg.GenerateCodeFromMember (new CodeTypeMember (), writer, null);
  479. }
  480. catch (NotImplementedException) {
  481. // mono
  482. }
  483. }
  484. #endif
  485. [Test]
  486. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  487. public void Protected ()
  488. {
  489. CodeGeneratorTest cg = new CodeGeneratorTest ();
  490. // test protected (but not abstract) stuff from within the class itself
  491. cg.TestProtectedProperties ();
  492. cg.TestProtectedMethods ();
  493. }
  494. [Test]
  495. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  496. public void StaticMethods ()
  497. {
  498. Assert.IsFalse (CodeGenerator.IsValidLanguageIndependentIdentifier ("@"), "IsValidLanguageIndependentIdentifier");
  499. try {
  500. CodeGenerator.ValidateIdentifiers (new CodeCompileUnit ());
  501. }
  502. catch (NotImplementedException) {
  503. // mono
  504. }
  505. }
  506. [Test]
  507. [PermissionSet (SecurityAction.Deny, Unrestricted = true)]
  508. public void LinkDemand_Deny_Unrestricted ()
  509. {
  510. ConstructorInfo ci = typeof (CodeGeneratorTest).GetConstructor (new Type[0]);
  511. Assert.IsNotNull (ci, "default .ctor()");
  512. Assert.IsNotNull (ci.Invoke (null), "invoke");
  513. }
  514. }
  515. }