~ubuntu-branches/ubuntu/saucy/monodevelop/saucy

« back to all changes in this revision

Viewing changes to external/mono-tools/gendarme/rules/Gendarme.Rules.Naming/Test/UseCorrectCasingTest.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (1.8.5) (1.5.8 sid)
  • Revision ID: package-import@ubuntu.com-20120527180820-f1ub6lhg0s50wci1
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Unit Test for UseCorrectCasingRule
 
3
//
 
4
// Authors:
 
5
//      Abramov Daniel <ex@vingrad.ru>
 
6
//
 
7
//  (C) 2007 Abramov Daniel
 
8
//
 
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:
 
16
// 
 
17
// The above copyright notice and this permission notice shall be
 
18
// included in all copies or substantial portions of the Software.
 
19
// 
 
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.
 
27
//
 
28
 
 
29
using System;
 
30
using System.Reflection;
 
31
 
 
32
using Gendarme.Rules.Naming;
 
33
using Mono.Cecil;
 
34
 
 
35
using NUnit.Framework;
 
36
using Test.Rules.Definitions;
 
37
using Test.Rules.Fixtures;
 
38
 
 
39
// no namespace
 
40
class Foo { }
 
41
 
 
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 { } }
 
48
 
 
49
namespace Test.Rules.Naming {
 
50
 
 
51
        [TestFixture]
 
52
        public class UseCorrectCasingAssemblyTest : AssemblyRuleTestFixture<UseCorrectCasingRule> {
 
53
 
 
54
                [Test]
 
55
                public void Namespaces ()
 
56
                {
 
57
                        // 1. Test.A
 
58
                        // 2. Test.Fa
 
59
                        // 3. Test.ASP
 
60
                        // 4. Test.Rules.ROCKS
 
61
                        // 5. Test.aSP
 
62
                        string unit = Assembly.GetExecutingAssembly ().Location;
 
63
                        AssertRuleFailure (AssemblyDefinition.ReadAssembly (unit), 5);
 
64
                }
 
65
        }
 
66
 
 
67
        [TestFixture]
 
68
        public class UseCorrectCasingTypeTest : TypeRuleTestFixture<UseCorrectCasingRule> {
 
69
 
 
70
                public class CorrectCasing {
 
71
                }
 
72
 
 
73
                public class incorrectCasing {
 
74
                }
 
75
 
 
76
                [Test]
 
77
                public void DoesNotApply ()
 
78
                {
 
79
                        AssertRuleDoesNotApply (SimpleTypes.GeneratedType);
 
80
                }
 
81
 
 
82
                [Test]
 
83
                public void Types ()
 
84
                {
 
85
                        AssertRuleSuccess<CorrectCasing> ();
 
86
                        AssertRuleFailure<incorrectCasing> (1);
 
87
                }
 
88
        }
 
89
 
 
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) { }
 
95
 
 
96
                public void IncorrectParameter (byte B) { }
 
97
                public void x () { }
 
98
                public void _X (short _S) { }
 
99
        }
 
100
 
 
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
 
109
                {
 
110
                        add { throw new NotImplementedException (); }
 
111
                        remove { throw new NotImplementedException (); }
 
112
                }
 
113
                public event EventHandler badEvent
 
114
                {
 
115
                        add { throw new NotImplementedException (); }
 
116
                        remove { throw new NotImplementedException (); }
 
117
                }
 
118
                public static int operator + (MoreComplexCasing a, int b) { return 0; } // ignore!
 
119
        }
 
120
 
 
121
        public class PrivateEventCasing {
 
122
                private event EventHandler good_private_event;
 
123
        }
 
124
 
 
125
        [TestFixture]
 
126
        public class UseCorrectCasingTest : MethodRuleTestFixture<UseCorrectCasingRule> {
 
127
 
 
128
                [Test]
 
129
                public void TestCorrectCasedMethod ()
 
130
                {
 
131
                        AssertRuleSuccess<CasingMethods> ("CorrectCasing");
 
132
                }
 
133
 
 
134
                [Test]
 
135
                public void TestIncorrectCasedMethod ()
 
136
                {
 
137
                        AssertRuleFailure<CasingMethods> ("incorrectCasing", 1);
 
138
                }
 
139
 
 
140
                [Test]
 
141
                public void TestCorrectCasedMethodWithIncorrectCasedParameters ()
 
142
                {
 
143
                        AssertRuleFailure<CasingMethods> ("CorrectCasingWithTwoIncorrectParameters", 2);
 
144
                }
 
145
 
 
146
                [Test]
 
147
                public void TestIncorrectCasedMethodWithIncorrectCasedParameters ()
 
148
                {
 
149
                        AssertRuleFailure<CasingMethods> ("incorrectCasingWithTwoIncorrectParameters", 3);
 
150
                }
 
151
 
 
152
                [Test]
 
153
                public void MoreCoverage ()
 
154
                {
 
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);
 
161
                }
 
162
 
 
163
                [Test]
 
164
                public void TestIgnoringCtor ()
 
165
                {
 
166
                        AssertRuleDoesNotApply<MoreComplexCasing> (".cctor");
 
167
                        AssertRuleDoesNotApply<MoreComplexCasing> (".ctor");
 
168
                }
 
169
 
 
170
                [Test]
 
171
                public void TestGoodProperty ()
 
172
                {
 
173
                        AssertRuleSuccess<MoreComplexCasing> ("get_GoodProperty");
 
174
                        AssertRuleSuccess<MoreComplexCasing> ("set_GoodProperty");
 
175
                }
 
176
 
 
177
                [Test]
 
178
                public void TestBadProperty ()
 
179
                {
 
180
                        AssertRuleFailure<MoreComplexCasing> ("get_badProperty", 1);
 
181
                        AssertRuleFailure<MoreComplexCasing> ("set_badProperty", 1);
 
182
                }
 
183
 
 
184
                [Test]
 
185
                public void TestGoodEventHandler ()
 
186
                {
 
187
                        AssertRuleSuccess<MoreComplexCasing> ("add_GoodEvent");
 
188
                        AssertRuleSuccess<MoreComplexCasing> ("remove_GoodEvent");
 
189
                }
 
190
 
 
191
                [Test]
 
192
                public void TestBadEventHandler ()
 
193
                {
 
194
                        AssertRuleFailure<MoreComplexCasing> ("add_badEvent", 1);
 
195
                        AssertRuleFailure<MoreComplexCasing> ("remove_badEvent", 1);
 
196
                }
 
197
 
 
198
                [Test]
 
199
                public void TestGoodPrivateEvent ()
 
200
                {
 
201
                        AssertRuleDoesNotApply<PrivateEventCasing> ("add_good_private_event");
 
202
                        AssertRuleDoesNotApply<PrivateEventCasing> ("remove_good_private_event");
 
203
                }
 
204
 
 
205
                [Test]
 
206
                public void TestPropertyLikeMethods ()
 
207
                {
 
208
                        AssertRuleFailure<MoreComplexCasing> ("get_AccessorLike", 1);
 
209
                        AssertRuleFailure<MoreComplexCasing> ("set_AccessorLike", 1);
 
210
                }
 
211
 
 
212
                [Test]
 
213
                public void TestIgnoringOperator ()
 
214
                {
 
215
                        AssertRuleSuccess<MoreComplexCasing> ("op_Addition");
 
216
                }
 
217
 
 
218
                public class AnonymousMethod {
 
219
                        private void MethodWithAnonymousMethod ()
 
220
                        {
 
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");
 
224
                        }
 
225
                }
 
226
 
 
227
                private AssemblyDefinition assembly;
 
228
 
 
229
                [TestFixtureSetUp]
 
230
                public void FixtureSetUp ()
 
231
                {
 
232
                        string unit = Assembly.GetExecutingAssembly ().Location;
 
233
                        assembly = AssemblyDefinition.ReadAssembly (unit);
 
234
                }
 
235
 
 
236
                [Test]
 
237
                public void TestAnonymousMethod ()
 
238
                {
 
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)
 
242
                        if (type == null)
 
243
                                type = assembly.MainModule.GetType ("Test.Rules.Naming.UseCorrectCasingTest/AnonymousMethod");
 
244
 
 
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)
 
250
                                        break;
 
251
                                default:
 
252
                                        AssertRuleDoesNotApply (method);
 
253
                                        break;
 
254
                                }
 
255
                        }
 
256
                }
 
257
        }
 
258
}