PageRenderTime 30ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 1ms

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