2
// Unit Test for UseCorrectCasingRule
5
// Abramov Daniel <ex@vingrad.ru>
7
// (C) 2007 Abramov Daniel
9
// Permission is hereby granted, free of charge, to any person obtaining
10
// a copy of this software and associated documentation files (the
11
// "Software"), to deal in the Software without restriction, including
12
// without limitation the rights to use, copy, modify, merge, publish,
13
// distribute, sublicense, and/or sell copies of the Software, and to
14
// permit persons to whom the Software is furnished to do so, subject to
15
// the following conditions:
17
// The above copyright notice and this permission notice shall be
18
// included in all copies or substantial portions of the Software.
20
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
using System.Reflection;
32
using Gendarme.Rules.Naming;
35
using NUnit.Framework;
36
using Test.Rules.Definitions;
37
using Test.Rules.Fixtures;
42
namespace Test.IO { class Foo {} }
43
namespace Test.Fa { class Foo {} }
44
namespace Test.ASP { class Foo {} class Bar {} }
45
namespace Test.A { class Foo {} }
46
namespace Test.Rules.ROCKS { class Foo { } }
47
namespace Test.aSP { class Zoo { } class Yar { } }
49
namespace Test.Rules.Naming {
52
public class UseCorrectCasingAssemblyTest : AssemblyRuleTestFixture<UseCorrectCasingRule> {
55
public void Namespaces ()
60
// 4. Test.Rules.ROCKS
62
string unit = Assembly.GetExecutingAssembly ().Location;
63
AssertRuleFailure (AssemblyDefinition.ReadAssembly (unit), 5);
68
public class UseCorrectCasingTypeTest : TypeRuleTestFixture<UseCorrectCasingRule> {
70
public class CorrectCasing {
73
public class incorrectCasing {
77
public void DoesNotApply ()
79
AssertRuleDoesNotApply (SimpleTypes.GeneratedType);
85
AssertRuleSuccess<CorrectCasing> ();
86
AssertRuleFailure<incorrectCasing> (1);
90
public class CasingMethods {
91
public void CorrectCasing (int foo, string bar) { }
92
public void incorrectCasing (int foo, string bar) { }
93
public void CorrectCasingWithTwoIncorrectParameters (int Bar, string Foo) { }
94
public void incorrectCasingWithTwoIncorrectParameters (int Bar, string Foo) { }
96
public void IncorrectParameter (byte B) { }
98
public void _X (short _S) { }
101
public class MoreComplexCasing {
102
static MoreComplexCasing () { } // .cctor, should be ignored
103
public MoreComplexCasing () { } // .ctor, should be ignored
104
public int GoodProperty { get { return 0; } set { } }
105
public int badProperty { get { return 0; } set { } }
106
public int get_AccessorLike () { return 0; } // should be catched
107
public void set_AccessorLike (int value) { } // should be catched
108
public event EventHandler GoodEvent
110
add { throw new NotImplementedException (); }
111
remove { throw new NotImplementedException (); }
113
public event EventHandler badEvent
115
add { throw new NotImplementedException (); }
116
remove { throw new NotImplementedException (); }
118
public static int operator + (MoreComplexCasing a, int b) { return 0; } // ignore!
121
public class PrivateEventCasing {
122
private event EventHandler good_private_event;
126
public class UseCorrectCasingTest : MethodRuleTestFixture<UseCorrectCasingRule> {
129
public void TestCorrectCasedMethod ()
131
AssertRuleSuccess<CasingMethods> ("CorrectCasing");
135
public void TestIncorrectCasedMethod ()
137
AssertRuleFailure<CasingMethods> ("incorrectCasing", 1);
141
public void TestCorrectCasedMethodWithIncorrectCasedParameters ()
143
AssertRuleFailure<CasingMethods> ("CorrectCasingWithTwoIncorrectParameters", 2);
147
public void TestIncorrectCasedMethodWithIncorrectCasedParameters ()
149
AssertRuleFailure<CasingMethods> ("incorrectCasingWithTwoIncorrectParameters", 3);
153
public void MoreCoverage ()
155
// parameter 'B' should be lower case to be CamelCase
156
AssertRuleFailure<CasingMethods> ("IncorrectParameter", 1);
157
// method name should be uppercase to be PascalCase
158
AssertRuleFailure<CasingMethods> ("x", 1);
159
// starts with an underscore for name and parameter, fails both Pascal and Camel checks
160
AssertRuleFailure<CasingMethods> ("_X", 2);
164
public void TestIgnoringCtor ()
166
AssertRuleDoesNotApply<MoreComplexCasing> (".cctor");
167
AssertRuleDoesNotApply<MoreComplexCasing> (".ctor");
171
public void TestGoodProperty ()
173
AssertRuleSuccess<MoreComplexCasing> ("get_GoodProperty");
174
AssertRuleSuccess<MoreComplexCasing> ("set_GoodProperty");
178
public void TestBadProperty ()
180
AssertRuleFailure<MoreComplexCasing> ("get_badProperty", 1);
181
AssertRuleFailure<MoreComplexCasing> ("set_badProperty", 1);
185
public void TestGoodEventHandler ()
187
AssertRuleSuccess<MoreComplexCasing> ("add_GoodEvent");
188
AssertRuleSuccess<MoreComplexCasing> ("remove_GoodEvent");
192
public void TestBadEventHandler ()
194
AssertRuleFailure<MoreComplexCasing> ("add_badEvent", 1);
195
AssertRuleFailure<MoreComplexCasing> ("remove_badEvent", 1);
199
public void TestGoodPrivateEvent ()
201
AssertRuleDoesNotApply<PrivateEventCasing> ("add_good_private_event");
202
AssertRuleDoesNotApply<PrivateEventCasing> ("remove_good_private_event");
206
public void TestPropertyLikeMethods ()
208
AssertRuleFailure<MoreComplexCasing> ("get_AccessorLike", 1);
209
AssertRuleFailure<MoreComplexCasing> ("set_AccessorLike", 1);
213
public void TestIgnoringOperator ()
215
AssertRuleSuccess<MoreComplexCasing> ("op_Addition");
218
public class AnonymousMethod {
219
private void MethodWithAnonymousMethod ()
221
string [] values = new string [] { "one", "two", "three" };
222
if (Array.Exists (values, delegate (string myString) { return myString.Length == 3; }))
223
Console.WriteLine ("Exists strings with length == 3");
227
private AssemblyDefinition assembly;
230
public void FixtureSetUp ()
232
string unit = Assembly.GetExecutingAssembly ().Location;
233
assembly = AssemblyDefinition.ReadAssembly (unit);
237
public void TestAnonymousMethod ()
239
// compiler generated code is compiler dependant, check for [g]mcs (inner type)
240
TypeDefinition type = assembly.MainModule.GetType ("Test.Rules.Naming.UseCorrectCasingTest/AnonymousMethod/<>c__CompilerGenerated0");
241
// otherwise try for csc (inside same class)
243
type = assembly.MainModule.GetType ("Test.Rules.Naming.UseCorrectCasingTest/AnonymousMethod");
245
Assert.IsNotNull (type, "type not found");
246
foreach (MethodDefinition method in type.Methods) {
247
switch (method.Name) {
248
case "MethodWithAnonymousMethod":
249
// this isn't part of the test (but included with CSC)
252
AssertRuleDoesNotApply (method);