Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AvoidLongParameterListsTest.cs « Test « Gendarme.Rules.Smells « rules « gendarme - github.com/mono/mono-tools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58afd4b24c9e70a67c10fa15505aacd4a22bba58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//
// Unit Test for AvoidLongParameterLists Rule.
//
// Authors:
//      Néstor Salceda <nestor.salceda@gmail.com>
//
//      (C) 2007 Néstor Salceda
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Reflection;
using System.Runtime.InteropServices;

using Mono.Cecil;
using NUnit.Framework;
using Gendarme.Framework;
using Gendarme.Rules.Smells;

namespace Test.Rules.Smells {
	[TestFixture]
	public class AvoidLongParameterListsTest {
		private IMethodRule rule;
		private AssemblyDefinition assembly;
		private MethodDefinition method;
		private TypeDefinition type;
		private TestRunner runner;

		[TestFixtureSetUp]
		public void FixtureSetUp () 
		{
			string unit = Assembly.GetExecutingAssembly ().Location;
			assembly = AssemblyFactory.GetAssembly (unit);
			type = assembly.MainModule.Types["Test.Rules.Smells.AvoidLongParameterListsTest"];
			rule = new AvoidLongParameterListsRule ();
			runner = new TestRunner (rule);
		}

		private MethodDefinition GetMethodForTest (string methodName, Type[] parameterTypes) 
		{
			if (parameterTypes == Type.EmptyTypes) {
				if (type.Methods.GetMethod (methodName).Length == 1)
					return type.Methods.GetMethod (methodName)[0];
			}
			return type.Methods.GetMethod (methodName, parameterTypes);
		}

		public void MethodWithoutParameters () 
		{
		}

		public void MethodWithoutLongParameterList (int x, char c, object obj, bool j, string f) 
		{
		}

		public void MethodWithLongParameterList (int x, char c, object obj, bool j, string f, float z, double u, short s, int v, string[] array)
		{
		}

		public void OverloadedMethod () 
		{
		}

		public void OverloadedMethod (int x) 
		{
		}

		public void OverloadedMethod (int x, char c, object obj, bool j, string f, float z, double u, short s, int v, string[] array) 
		{
		}

		public void OtherOverloaded (int x, char c, object obj, bool j, string f, float z, double u)
		{
		}

		public void OtherOverloaded (int x, char c, object obj, bool j, string f, float z, double u, short s)
		{
		}

		// copy-paste from Mono's System.Drawing, types changed to avoid referencing assembly
		[DllImport ("gdiplus.dll")]
		static internal extern int GdipCreateLineBrushFromRectI (object rect, int color1, int color2, int linearGradientMode, int wrapMode, IntPtr brush);

		[Test]
		public void MethodWithoutParametersTest () 
		{
			method = GetMethodForTest ("MethodWithoutParameters", Type.EmptyTypes);
			Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method));
		}

		[Test]
		public void MethodwithLongParameterListTest () 
		{
			method = GetMethodForTest ("MethodWithLongParameterList", Type.EmptyTypes);
			Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method));
			Assert.AreEqual (1, runner.Defects.Count);
		}

		[Test]
		public void MethodWithoutLongParameterList () 
		{
			method = GetMethodForTest ("MethodWithoutLongParameterList", Type.EmptyTypes);
			Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method));
		}

		[Test]
		public void OverloadedMethodTest () 
		{
			method = GetMethodForTest ("OverloadedMethod", Type.EmptyTypes);
			Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method));
		}

		[Test]
		public void OverloadedMethodWithParametersTest () 
		{
			method = GetMethodForTest ("OverloadedMethod", new Type[] {typeof (int)});
			Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method));
		}

		[Test]
		public void LongOverloadedMethodWithLittleOverloadTest () 
		{
			method = GetMethodForTest ("OverloadedMethod", new Type[] {typeof (int), typeof (char), typeof (object), typeof (bool), typeof (string), typeof (float), typeof (double), typeof (short), typeof (int), typeof (string[])});
			Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method));
		}

		[Test]
		public void LongOverloadedMethodWithoutLittleOverloadTest () 
		{
			method = GetMethodForTest ("OtherOverloaded", new Type[] {typeof (int), typeof (char), typeof (object), typeof (bool), typeof (string), typeof (float), typeof (double)});
			Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method));
			Assert.AreEqual (1, runner.Defects.Count);
		}

		[Test]
		public void OtherLongOverloadedMethodWithoutLittleOverloadTest () 
		{
			method = GetMethodForTest ("OtherOverloaded", new Type[] {typeof (int), typeof (char), typeof (object), typeof (bool), typeof (string), typeof (float), typeof (double), typeof (short)});
			Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method));
			Assert.AreEqual (1, runner.Defects.Count);
		}

		[Test]
		public void IgnoreExternalMethods ()
		{
			method = GetMethodForTest ("GdipCreateLineBrushFromRectI", new Type[] {typeof (object), typeof (int), typeof (int), typeof (int), typeof (int), typeof (IntPtr) });
			Assert.AreEqual (RuleResult.DoesNotApply ,runner.CheckMethod (method));
		}
	}
}