CodeGeneratorFromExpressionTest.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Microsoft.VisualBasic.* Test Cases
  3. //
  4. // Authors:
  5. // Gert Driesen ([email protected])
  6. //
  7. // (c) 2005 Novell
  8. //
  9. using System;
  10. using System.IO;
  11. using System.Text;
  12. using System.CodeDom;
  13. using System.CodeDom.Compiler;
  14. using Microsoft.VisualBasic;
  15. using NUnit.Framework;
  16. namespace MonoTests.Microsoft.VisualBasic
  17. {
  18. [TestFixture]
  19. public class CodeGeneratorFromExpressionTest
  20. {
  21. VBCodeProvider provider;
  22. ICodeGenerator generator;
  23. CodeGeneratorOptions options;
  24. [SetUp]
  25. public void SetUp ()
  26. {
  27. provider = new VBCodeProvider ();
  28. generator = provider.CreateGenerator ();
  29. options = new CodeGeneratorOptions ();
  30. }
  31. [Test]
  32. [ExpectedException (typeof (ArgumentException))]
  33. public void DefaultExpressionTest ()
  34. {
  35. using (StringWriter sw = new StringWriter ()) {
  36. Generate(new CodeExpression (), sw);
  37. sw.Close ();
  38. }
  39. }
  40. [Test]
  41. [ExpectedException (typeof (ArgumentNullException))]
  42. public void NullExpressionTest ()
  43. {
  44. using (StringWriter sw = new StringWriter ()) {
  45. Generate (null, sw);
  46. }
  47. }
  48. [Test]
  49. public void TypeReferenceExpressionTest ()
  50. {
  51. StringBuilder sb = new StringBuilder();
  52. using (StringWriter sw = new StringWriter (sb)) {
  53. Assert.AreEqual ("Boolean", Generate (new CodeTypeReferenceExpression (typeof (bool)), sw), "#1");
  54. sb.Length = 0;
  55. Assert.AreEqual ("Byte", Generate (new CodeTypeReferenceExpression (typeof (byte)), sw), "#2");
  56. sb.Length = 0;
  57. Assert.AreEqual ("Char", Generate (new CodeTypeReferenceExpression (typeof (char)), sw), "#3");
  58. sb.Length = 0;
  59. Assert.AreEqual ("Date", Generate (new CodeTypeReferenceExpression (typeof (DateTime)), sw), "#4");
  60. sb.Length = 0;
  61. Assert.AreEqual ("Decimal", Generate (new CodeTypeReferenceExpression (typeof (decimal)), sw), "#5");
  62. sb.Length = 0;
  63. Assert.AreEqual ("Double", Generate (new CodeTypeReferenceExpression (typeof (double)), sw), "#6");
  64. sb.Length = 0;
  65. Assert.AreEqual ("Short", Generate (new CodeTypeReferenceExpression (typeof (short)), sw), "#7");
  66. sb.Length = 0;
  67. Assert.AreEqual ("Integer", Generate (new CodeTypeReferenceExpression (typeof (int)), sw), "#8");
  68. sb.Length = 0;
  69. Assert.AreEqual ("Long", Generate (new CodeTypeReferenceExpression (typeof (long)), sw), "#9");
  70. sb.Length = 0;
  71. Assert.AreEqual ("Single", Generate (new CodeTypeReferenceExpression (typeof (float)), sw), "#10");
  72. sb.Length = 0;
  73. Assert.AreEqual ("Object", Generate (new CodeTypeReferenceExpression (typeof (object)), sw), "#11");
  74. sb.Length = 0;
  75. Assert.AreEqual (typeof (void).FullName, Generate (new CodeTypeReferenceExpression (typeof (void)), sw), "#12");
  76. sb.Length = 0;
  77. Assert.AreEqual (typeof (void).FullName, Generate (new CodeTypeReferenceExpression ((string) null), sw), "#13");
  78. sb.Length = 0;
  79. Assert.AreEqual (typeof (void).FullName, Generate (new CodeTypeReferenceExpression (""), sw), "#14");
  80. sb.Length = 0;
  81. #if NET_2_0
  82. Assert.AreEqual ("SByte", Generate (new CodeTypeReferenceExpression (typeof (sbyte)), sw), "#15");
  83. sb.Length = 0;
  84. Assert.AreEqual ("UShort", Generate (new CodeTypeReferenceExpression (typeof (ushort)), sw), "#16");
  85. sb.Length = 0;
  86. Assert.AreEqual ("UInteger", Generate (new CodeTypeReferenceExpression (typeof (uint)), sw), "#17");
  87. sb.Length = 0;
  88. Assert.AreEqual ("ULong", Generate (new CodeTypeReferenceExpression (typeof (ulong)), sw), "#18");
  89. sb.Length = 0;
  90. #else
  91. Assert.AreEqual (typeof (sbyte).FullName, Generate (new CodeTypeReferenceExpression (typeof (sbyte)), sw), "#19");
  92. sb.Length = 0;
  93. Assert.AreEqual (typeof(ushort).FullName, Generate (new CodeTypeReferenceExpression (typeof (ushort)), sw), "#20");
  94. sb.Length = 0;
  95. Assert.AreEqual (typeof(uint).FullName, Generate (new CodeTypeReferenceExpression (typeof (uint)), sw), "#21");
  96. sb.Length = 0;
  97. Assert.AreEqual (typeof(ulong).FullName, Generate (new CodeTypeReferenceExpression (typeof (ulong)), sw), "#22");
  98. sb.Length = 0;
  99. #endif
  100. sw.Close ();
  101. }
  102. }
  103. [Test]
  104. public void ParameterDeclarationExpressionTest ()
  105. {
  106. CodeParameterDeclarationExpression cpde = null;
  107. StringBuilder sb = new StringBuilder();
  108. using (StringWriter sw = new StringWriter (sb)) {
  109. cpde = new CodeParameterDeclarationExpression ();
  110. #if NET_2_0
  111. Assert.AreEqual ("ByVal __exception As System.Void", Generate (cpde, sw), "#1");
  112. #else
  113. Assert.AreEqual ("ByVal As System.Void", Generate (cpde, sw), "#1");
  114. #endif
  115. sb.Length = 0;
  116. cpde = new CodeParameterDeclarationExpression ((string) null,
  117. (string) null);
  118. #if NET_2_0
  119. Assert.AreEqual ("ByVal __exception As System.Void", Generate (cpde, sw), "#2");
  120. #else
  121. Assert.AreEqual ("ByVal As System.Void", Generate (cpde, sw), "#2");
  122. #endif
  123. sb.Length = 0;
  124. cpde = new CodeParameterDeclarationExpression ("A", (string) null);
  125. #if NET_2_0
  126. Assert.AreEqual ("ByVal __exception As A", Generate (cpde, sw), "#3");
  127. #else
  128. Assert.AreEqual ("ByVal As A", Generate (cpde, sw), "#3");
  129. #endif
  130. sb.Length = 0;
  131. cpde = new CodeParameterDeclarationExpression ((string) null, "B");
  132. Assert.AreEqual ("ByVal B As System.Void", Generate (cpde, sw), "#4");
  133. sb.Length = 0;
  134. cpde = new CodeParameterDeclarationExpression ("A", "B");
  135. Assert.AreEqual ("ByVal B As A", Generate (cpde, sw), "#5");
  136. sb.Length = 0;
  137. cpde.Direction = FieldDirection.Out;
  138. Assert.AreEqual ("ByRef B As A", Generate (cpde, sw), "#6");
  139. sb.Length = 0;
  140. cpde.Direction = FieldDirection.Ref;
  141. Assert.AreEqual ("ByRef B As A", Generate (cpde, sw), "#7");
  142. sb.Length = 0;
  143. }
  144. }
  145. private string Generate (CodeExpression expression, StringWriter sw)
  146. {
  147. generator.GenerateCodeFromExpression (expression, sw, options);
  148. return sw.ToString ();
  149. }
  150. }
  151. }