PageRenderTime 33ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/src/Main/ICSharpCode.SharpDevelop.Dom/Tests/ICSharpCode.SharpDevelop.Dom.Tests/NRefactoryAstConverterTests.cs

http://github.com/icsharpcode/SharpDevelop
C# | 336 lines | 278 code | 40 blank | 18 comment | 1 complexity | f5f9ca0212ff512f67900bf0a7962aa7 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, CPL-1.0, LGPL-2.1
  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Linq;
  21. using System.IO;
  22. using ICSharpCode.NRefactory;
  23. using ICSharpCode.SharpDevelop.Dom.NRefactoryResolver;
  24. using NUnit.Framework;
  25. namespace ICSharpCode.SharpDevelop.Dom.Tests
  26. {
  27. [TestFixture]
  28. public class NRefactoryAstConverterTests
  29. {
  30. ICompilationUnit Parse(string code, SupportedLanguage language, params IProjectContent[] references)
  31. {
  32. DefaultProjectContent pc = new DefaultProjectContent();
  33. foreach (var reference in references) {
  34. pc.AddReferencedContent(reference);
  35. }
  36. NRefactoryASTConvertVisitor visitor = new NRefactoryASTConvertVisitor(pc, language);
  37. using (IParser p = ParserFactory.CreateParser(language, new StringReader(code))) {
  38. p.ParseMethodBodies = false;
  39. p.Parse();
  40. visitor.Specials = p.Lexer.SpecialTracker.CurrentSpecials;
  41. visitor.VisitCompilationUnit(p.CompilationUnit, null);
  42. }
  43. return visitor.Cu;
  44. }
  45. ICompilationUnit Parse(string code)
  46. {
  47. return Parse(code, SupportedLanguage.CSharp);
  48. }
  49. string SurroundWithSummaryTags(string text)
  50. {
  51. return " <summary>\r\n " + text + "\r\n </summary>\r\n";
  52. }
  53. [Test]
  54. public void FindDocumentationComment()
  55. {
  56. ICompilationUnit cu = Parse(@"
  57. using System;
  58. namespace X
  59. {
  60. /// <summary>
  61. /// This is the comment
  62. /// </summary>
  63. public class A
  64. {
  65. }
  66. }
  67. ");
  68. Assert.AreEqual(SurroundWithSummaryTags("This is the comment"), cu.Classes[0].Documentation);
  69. }
  70. [Test]
  71. public void FindDocumentationCommentAboveAttribute()
  72. {
  73. ICompilationUnit cu = Parse(@"
  74. using System;
  75. namespace X
  76. {
  77. /// <summary>
  78. /// This is the comment
  79. /// </summary>
  80. [SomeAttribute]
  81. public class A
  82. {
  83. }
  84. }
  85. ");
  86. Assert.AreEqual(SurroundWithSummaryTags("This is the comment"), cu.Classes[0].Documentation);
  87. }
  88. [Test]
  89. public void FindDocumentationCommentAboveAttribute2()
  90. {
  91. ICompilationUnit cu = Parse(@"
  92. using System;
  93. namespace X
  94. {
  95. /// <summary>
  96. /// This is the comment
  97. /// </summary>
  98. [SomeAttribute] // a comment on the attribute
  99. public class A
  100. {
  101. }
  102. }
  103. ");
  104. Assert.AreEqual(SurroundWithSummaryTags("This is the comment"), cu.Classes[0].Documentation);
  105. }
  106. [Test]
  107. public void FindDocumentationCommentAboveAttributeInRegion()
  108. {
  109. ICompilationUnit cu = Parse(@"
  110. using System;
  111. namespace X
  112. {
  113. /// <summary>
  114. /// This is the comment
  115. /// </summary>
  116. #region R
  117. [SomeAttribute]
  118. #endregion
  119. public class A
  120. {
  121. }
  122. }
  123. ");
  124. Assert.AreEqual(SurroundWithSummaryTags("This is the comment"), cu.Classes[0].Documentation);
  125. }
  126. [Test]
  127. public void GenericMethodWithConstraintsTest()
  128. {
  129. // Test that constaints can reference other type parameters.
  130. ICompilationUnit cu = Parse(@"
  131. using System;
  132. class X {
  133. public static A Method<A, B>(A p1, B p2) where A : IComparable<B> where B : IComparable<A> { }
  134. }
  135. ");
  136. IMethod method = cu.Classes[0].Methods[0];
  137. Assert.AreEqual(2, method.TypeParameters.Count);
  138. ITypeParameter a = method.TypeParameters[0];
  139. ITypeParameter b = method.TypeParameters[1];
  140. Assert.AreSame(a, method.ReturnType.CastToGenericReturnType().TypeParameter);
  141. Assert.AreSame(a, method.Parameters[0].ReturnType.CastToGenericReturnType().TypeParameter);
  142. Assert.AreSame(b, method.Parameters[1].ReturnType.CastToGenericReturnType().TypeParameter);
  143. Assert.AreEqual(1, a.Constraints.Count);
  144. ConstructedReturnType crt = a.Constraints[0].CastToConstructedReturnType();
  145. Assert.AreEqual("IComparable", crt.Name);
  146. Assert.AreSame(b, crt.TypeArguments[0].CastToGenericReturnType().TypeParameter);
  147. Assert.AreEqual(1, b.Constraints.Count);
  148. crt = b.Constraints[0].CastToConstructedReturnType();
  149. Assert.AreEqual("IComparable", crt.Name);
  150. Assert.AreSame(a, crt.TypeArguments[0].CastToGenericReturnType().TypeParameter);
  151. }
  152. [Test]
  153. public void StaticClassTest()
  154. {
  155. ICompilationUnit cu = Parse(@"
  156. using System;
  157. static class X {}
  158. ");
  159. IClass c = cu.Classes[0];
  160. Assert.IsTrue(c.IsAbstract, "class should be abstract");
  161. Assert.IsTrue(c.IsSealed, "class should be sealed");
  162. Assert.IsTrue(c.IsStatic, "class should be static");
  163. }
  164. [Test]
  165. public void IndexerDefaultNameTest()
  166. {
  167. ICompilationUnit cu = Parse(@"
  168. class X {
  169. public int this[int index] {
  170. get { return 0; }
  171. }
  172. }
  173. ");
  174. IProperty p = cu.Classes[0].Properties[0];
  175. Assert.IsTrue(p.IsIndexer, "IsIndexer must be true");
  176. Assert.AreEqual("Item", p.Name);
  177. Assert.AreEqual(1, p.Parameters.Count);
  178. }
  179. [Test]
  180. public void IndexerNonDefaultNameTest()
  181. {
  182. ICompilationUnit cu = Parse(@"
  183. using System.Runtime.CompilerServices;
  184. class X {
  185. [IndexerName(""Foo"")]
  186. public int this[int index] {
  187. get { return 0; }
  188. }
  189. }
  190. ", SupportedLanguage.CSharp, SharedProjectContentRegistryForTests.Instance.Mscorlib);
  191. IProperty p = cu.Classes[0].Properties[0];
  192. Assert.IsTrue(p.IsIndexer, "IsIndexer must be true");
  193. Assert.AreEqual("Foo", p.Name);
  194. Assert.AreEqual(1, p.Parameters.Count);
  195. }
  196. [Test]
  197. public void GenericInnerClassTest()
  198. {
  199. ICompilationUnit cu = Parse(@"
  200. using System;
  201. class Outer<T1> where T1 : IDisposable {
  202. class Inner<T2> where T2 : T1 {
  203. public static void Method<T3>(T1 p1, T2 p2, T3 p3) where T3 : T2 { }
  204. }
  205. }
  206. ");
  207. IClass outer = cu.Classes[0];
  208. Assert.AreEqual(1, outer.TypeParameters.Count);
  209. IClass inner = outer.InnerClasses[0];
  210. Assert.AreEqual(2, inner.TypeParameters.Count);
  211. Assert.AreEqual("T1", inner.TypeParameters[0].Name);
  212. Assert.AreSame(inner, inner.TypeParameters[0].Class);
  213. Assert.AreEqual("T2", inner.TypeParameters[1].Name);
  214. Assert.AreSame(inner.TypeParameters[0], inner.TypeParameters[1].Constraints[0].CastToGenericReturnType().TypeParameter);
  215. Assert.AreEqual("IDisposable", inner.TypeParameters[0].Constraints[0].Name);
  216. IMethod method = inner.Methods.Single(m => m.Name == "Method");
  217. Assert.AreEqual(1, method.TypeParameters.Count);
  218. Assert.AreSame(inner.TypeParameters[0], method.Parameters[0].ReturnType.CastToGenericReturnType().TypeParameter);
  219. Assert.AreSame(inner.TypeParameters[1], method.Parameters[1].ReturnType.CastToGenericReturnType().TypeParameter);
  220. Assert.AreSame(method.TypeParameters[0], method.Parameters[2].ReturnType.CastToGenericReturnType().TypeParameter);
  221. Assert.AreSame(inner.TypeParameters[1], method.TypeParameters[0].Constraints[0].CastToGenericReturnType().TypeParameter);
  222. }
  223. [Test]
  224. public void DefaultConstructorTest()
  225. {
  226. ICompilationUnit cu = Parse("class X { }", SupportedLanguage.CSharp, SharedProjectContentRegistryForTests.Instance.Mscorlib);
  227. Assert.AreEqual(0, cu.Classes[0].Methods.Count);
  228. IMethod ctor = cu.Classes[0].DefaultReturnType.GetMethods().Single(m => m.IsConstructor);
  229. Assert.AreEqual(ModifierEnum.Public | ModifierEnum.Synthetic, ctor.Modifiers);
  230. Assert.AreEqual(0, ctor.Parameters.Count);
  231. }
  232. [Test]
  233. public void DefaultConstructorOnAbstractClassTest()
  234. {
  235. ICompilationUnit cu = Parse("abstract class X { }", SupportedLanguage.CSharp, SharedProjectContentRegistryForTests.Instance.Mscorlib);
  236. Assert.AreEqual(0, cu.Classes[0].Methods.Count);
  237. IMethod ctor = cu.Classes[0].DefaultReturnType.GetMethods().Single(m => m.IsConstructor);
  238. Assert.AreEqual(ModifierEnum.Protected | ModifierEnum.Synthetic, ctor.Modifiers);
  239. Assert.AreEqual(0, ctor.Parameters.Count);
  240. }
  241. [Test]
  242. public void NoDefaultConstructorWithExplicitConstructorTest()
  243. {
  244. ICompilationUnit cu = Parse("class X { private X(int a) {} }", SupportedLanguage.CSharp, SharedProjectContentRegistryForTests.Instance.Mscorlib);
  245. Assert.AreEqual(1, cu.Classes[0].Methods.Count);
  246. IMethod ctor = cu.Classes[0].DefaultReturnType.GetMethods().Single(m => m.IsConstructor);
  247. Assert.AreEqual(ModifierEnum.Private, ctor.Modifiers);
  248. Assert.AreEqual(1, ctor.Parameters.Count);
  249. }
  250. [Test]
  251. public void DefaultConstructorWithExplicitConstructorOnStructTest()
  252. {
  253. ICompilationUnit cu = Parse("struct X { private X(int a) {} }", SupportedLanguage.CSharp, SharedProjectContentRegistryForTests.Instance.Mscorlib);
  254. Assert.AreEqual(1, cu.Classes[0].Methods.Count);
  255. List<IMethod> ctors = cu.Classes[0].DefaultReturnType.GetMethods().FindAll(m => m.IsConstructor);
  256. Assert.AreEqual(2, ctors.Count);
  257. Assert.AreEqual(ModifierEnum.Private, ctors[0].Modifiers);
  258. Assert.AreEqual(1, ctors[0].Parameters.Count);
  259. Assert.AreEqual(ModifierEnum.Public | ModifierEnum.Synthetic, ctors[1].Modifiers);
  260. Assert.AreEqual(0, ctors[1].Parameters.Count);
  261. }
  262. [Test]
  263. public void NoDefaultConstructorOnStaticClassTest()
  264. {
  265. ICompilationUnit cu = Parse("static class X { }", SupportedLanguage.CSharp);
  266. Assert.AreEqual(0, cu.Classes[0].Methods.Count);
  267. Assert.IsFalse(cu.Classes[0].DefaultReturnType.GetMethods().Any(m => m.IsConstructor));
  268. }
  269. [Test]
  270. public void VBNetIsExtensionMethodTest()
  271. {
  272. string code = @"Imports System.Runtime.CompilerServices
  273. Module StringExtensions
  274. <Extension> _
  275. Sub Print(s As String)
  276. End Sub
  277. End Module";
  278. ICompilationUnit cu = Parse(code, SupportedLanguage.VBNet, SharedProjectContentRegistryForTests.Instance.Mscorlib,
  279. SharedProjectContentRegistryForTests.Instance.GetProjectContentForReference("System.Core", typeof(System.Linq.Enumerable).Module.FullyQualifiedName));
  280. Assert.Greater(cu.Classes.Count, 0);
  281. Assert.AreEqual("StringExtensions", cu.Classes[0].Name);
  282. Assert.AreEqual(ClassType.Module, cu.Classes[0].ClassType);
  283. Assert.Greater(cu.Classes[0].Methods.Count, 0);
  284. Assert.IsTrue(cu.Classes[0].Methods[0].IsExtensionMethod);
  285. Assert.AreEqual("Print", cu.Classes[0].Methods[0].Name);
  286. }
  287. }
  288. }