/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeParameter2Tests.cs

https://github.com/ajadex/SharpDevelop · C# · 156 lines · 111 code · 28 blank · 17 comment · 0 complexity · fdb0b18a5eed83ef135376d5b1c2a580 MD5 · raw file

  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.Linq;
  20. using ICSharpCode.NRefactory.TypeSystem;
  21. using ICSharpCode.PackageManagement.EnvDTE;
  22. using ICSharpCode.SharpDevelop.Dom;
  23. using NUnit.Framework;
  24. using PackageManagement.Tests.Helpers;
  25. namespace PackageManagement.Tests.EnvDTE
  26. {
  27. [TestFixture]
  28. public class CodeParameter2Tests : CodeModelTestBase
  29. {
  30. CodeParameter2 parameter;
  31. void CreateParameter(string code)
  32. {
  33. AddCodeFile("class.cs", code);
  34. IMethod method = assemblyModel
  35. .TopLevelTypeDefinitions
  36. .First()
  37. .Members
  38. .First()
  39. .Resolve() as IMethod;
  40. IParameter member = method.Parameters.First();
  41. parameter = new CodeParameter2(codeModelContext, member);
  42. }
  43. [Test]
  44. public void ParameterKind_NormalParameter_ReturnsNone()
  45. {
  46. CreateParameter(
  47. "public class MyClass {\r\n" +
  48. " public void MyMethod(int parameter) {}\r\n" +
  49. "}");
  50. global::EnvDTE.vsCMParameterKind kind = parameter.ParameterKind;
  51. Assert.AreEqual(global::EnvDTE.vsCMParameterKind.vsCMParameterKindNone, kind);
  52. }
  53. [Test]
  54. public void ParameterKind_OptionalParameter_ReturnsOptional()
  55. {
  56. CreateParameter(
  57. "public class MyClass {\r\n" +
  58. " public void MyMethod(int parameter = 0) {}\r\n" +
  59. "}");
  60. global::EnvDTE.vsCMParameterKind kind = parameter.ParameterKind;
  61. Assert.AreEqual(global::EnvDTE.vsCMParameterKind.vsCMParameterKindOptional, kind);
  62. }
  63. [Test]
  64. public void ParameterKind_OutParameter_ReturnsOut()
  65. {
  66. CreateParameter(
  67. "public class MyClass {\r\n" +
  68. " public void MyMethod(out int parameter) { parameter = 2; }\r\n" +
  69. "}");
  70. global::EnvDTE.vsCMParameterKind kind = parameter.ParameterKind;
  71. Assert.AreEqual(global::EnvDTE.vsCMParameterKind.vsCMParameterKindOut, kind);
  72. }
  73. [Test]
  74. public void ParameterKind_RefParameter_ReturnsRef()
  75. {
  76. CreateParameter(
  77. "public class MyClass {\r\n" +
  78. " public void MyMethod(ref int parameter) {}\r\n" +
  79. "}");
  80. global::EnvDTE.vsCMParameterKind kind = parameter.ParameterKind;
  81. Assert.AreEqual(global::EnvDTE.vsCMParameterKind.vsCMParameterKindRef, kind);
  82. }
  83. [Test]
  84. public void ParameterKind_ParamArrayParameter_ReturnsParamArray()
  85. {
  86. CreateParameter(
  87. "public class MyClass {\r\n" +
  88. " public void MyMethod(params int[] parameters) {}\r\n" +
  89. "}");
  90. global::EnvDTE.vsCMParameterKind kind = parameter.ParameterKind;
  91. Assert.AreEqual(global::EnvDTE.vsCMParameterKind.vsCMParameterKindParamArray, kind);
  92. }
  93. [Test]
  94. [Ignore("Not supported by NRefactory. Maps to VB.NET's ByVal. For C# vsCMParameterKindNone is returned.")]
  95. public void ParameterKind_InParameter_ReturnsIn()
  96. {
  97. CreateParameter(
  98. "public class MyClass {\r\n" +
  99. " public void MyMethod(int parameter) {}\r\n" +
  100. "}");
  101. global::EnvDTE.vsCMParameterKind kind = parameter.ParameterKind;
  102. Assert.AreEqual(global::EnvDTE.vsCMParameterKind.vsCMParameterKindIn, kind);
  103. }
  104. [Test]
  105. public void Kind_Parameter_ReturnsParameter()
  106. {
  107. CreateParameter(
  108. "public class MyClass {\r\n" +
  109. " public void MyMethod(int parameter) {}\r\n" +
  110. "}");
  111. global::EnvDTE.vsCMElement kind = parameter.Kind;
  112. Assert.AreEqual(global::EnvDTE.vsCMElement.vsCMElementParameter, kind);
  113. }
  114. [Test]
  115. public void Attributes_ParameterHasOneAttribute_ReturnsOneAttribute()
  116. {
  117. CreateParameter(
  118. "using System;\r\n" +
  119. "public class MyClass {\r\n" +
  120. " public void MyMethod([Obsolete] int parameter) {}\r\n" +
  121. "}");
  122. global::EnvDTE.CodeElements attributes = parameter.Attributes;
  123. CodeAttribute2 attribute = parameter.Attributes.FirstCodeAttribute2OrDefault();
  124. Assert.AreEqual(1, attributes.Count);
  125. Assert.AreEqual("System.ObsoleteAttribute", attribute.FullName);
  126. }
  127. }
  128. }