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

« back to all changes in this revision

Viewing changes to external/mono-tools/gendarme/rules/Gendarme.Rules.Maintainability/Test/AvoidAlwaysNullFieldTest.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 tests for AvoidAlwaysNullFieldRule
 
3
//
 
4
// Authors:
 
5
//      Jesse Jones <jesjones@mindspring.com>
 
6
//      Sebastien Pouliot <sebastien@ximian.com>
 
7
//
 
8
// Copyright (C) 2008 Jesse Jones
 
9
// Copyright (C) 2011 Novell, Inc (http://www.novell.com)
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining
 
12
// a copy of this software and associated documentation files (the
 
13
// "Software"), to deal in the Software without restriction, including
 
14
// without limitation the rights to use, copy, modify, merge, publish,
 
15
// distribute, sublicense, and/or sell copies of the Software, and to
 
16
// permit persons to whom the Software is furnished to do so, subject to
 
17
// the following conditions:
 
18
// 
 
19
// The above copyright notice and this permission notice shall be
 
20
// included in all copies or substantial portions of the Software.
 
21
// 
 
22
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
23
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
24
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
25
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
26
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
27
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
28
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
29
//
 
30
 
 
31
using System;
 
32
using System.Collections.Generic;
 
33
using System.Linq;
 
34
using System.Threading;
 
35
 
 
36
using Gendarme.Rules.Maintainability;
 
37
 
 
38
using NUnit.Framework;
 
39
using Test.Rules.Definitions;
 
40
using Test.Rules.Fixtures;
 
41
 
 
42
namespace Test.Rules.Maintainability {
 
43
 
 
44
        [TestFixture]
 
45
        public sealed class AvoidAlwaysNullFieldTest : TypeRuleTestFixture<AvoidAlwaysNullFieldRule> {
 
46
 
 
47
                private sealed class Good1 {
 
48
                        private string name = "hey";    // initialized
 
49
                        
 
50
                        public void Write ()
 
51
                        {
 
52
                                Console.WriteLine (name);
 
53
                        }
 
54
                }
 
55
 
 
56
                private class Good2 {
 
57
                        protected string name;  // not private
 
58
                        
 
59
                        public void Write ()
 
60
                        {
 
61
                                if (name != null)
 
62
                                        Console.WriteLine (name);
 
63
                        }
 
64
                }
 
65
 
 
66
                private sealed class Good3 {
 
67
                        private string name;    // not used (AvoidUnusedPrivateField will handle this case)
 
68
                        
 
69
                        public void Write ()
 
70
                        {
 
71
                                Console.WriteLine ("hey");
 
72
                        }
 
73
                }
 
74
 
 
75
                private sealed class Good4 {
 
76
                        private IntPtr zero;    // not a reference type
 
77
                        
 
78
                        public void Write ()
 
79
                        {
 
80
                                Console.WriteLine (zero);
 
81
                        }
 
82
                }
 
83
 
 
84
                private sealed class Good5 {
 
85
                        private static string name = "hey";     // initialized
 
86
                        
 
87
                        public void Write ()
 
88
                        {
 
89
                                Console.WriteLine (name);
 
90
                        }
 
91
                }
 
92
 
 
93
                private sealed class Good6 {
 
94
                        private string name;
 
95
                        
 
96
                        public Good6 ()
 
97
                        {
 
98
                                IndirectSet (ref name); // address taken
 
99
                        }
 
100
                        
 
101
                        public void Write ()
 
102
                        {
 
103
                                Console.WriteLine (name);
 
104
                        }
 
105
                        
 
106
                        public void IndirectSet (ref string n)
 
107
                        {
 
108
                                n = "hey";
 
109
                        }
 
110
                }
 
111
 
 
112
                private class Bad1 {
 
113
                        private string name;    
 
114
                        
 
115
                        public void Write ()
 
116
                        {
 
117
                                if (name != null)
 
118
                                        Console.WriteLine (name);
 
119
                        }
 
120
                }
 
121
 
 
122
                private class Bad2 {
 
123
                        private string name;    
 
124
                        
 
125
                        public Bad2 ()
 
126
                        {
 
127
                                name = null;
 
128
                        }
 
129
                        
 
130
                        public void Write ()
 
131
                        {
 
132
                                if (name != null)
 
133
                                        Console.WriteLine (name);
 
134
                        }
 
135
                }
 
136
 
 
137
                private class Bad3 {
 
138
                        private static string name;     
 
139
                        
 
140
                        public Bad3 ()
 
141
                        {
 
142
                                name = null;
 
143
                        }
 
144
                        
 
145
                        public void Write ()
 
146
                        {
 
147
                                if (name != null)
 
148
                                        Console.WriteLine (name);
 
149
                        }
 
150
                }
 
151
 
 
152
                private class Bad4 {
 
153
                        private string name1;   
 
154
                        private string name2;   
 
155
                        
 
156
                        public Bad4 ()
 
157
                        {
 
158
                                name1 = null;   // explictly set to null, but not used
 
159
                                name2 = "hey";
 
160
                        }
 
161
                        
 
162
                        public void Write ()
 
163
                        {
 
164
                                Console.WriteLine (name2);
 
165
                        }
 
166
                }
 
167
 
 
168
                private class TernaryIf {
 
169
                        private string name;
 
170
 
 
171
                        public TernaryIf (bool flag)
 
172
                        {
 
173
                                name = flag ? "hmm" : null;     // we don't handle this properly
 
174
                        }
 
175
                        
 
176
                        public void Write ()
 
177
                        {
 
178
                                if (name != null)
 
179
                                        Console.WriteLine (name);
 
180
                        }
 
181
                }
 
182
 
 
183
                [Test]
 
184
                public void Simple ()
 
185
                {
 
186
                        AssertRuleDoesNotApply (SimpleTypes.Class);     // no fields
 
187
                        AssertRuleDoesNotApply (SimpleTypes.Enum);
 
188
                        AssertRuleDoesNotApply (SimpleTypes.Interface);
 
189
                        AssertRuleSuccess (SimpleTypes.Structure);      // a few public fields
 
190
                }
 
191
 
 
192
                [Test]
 
193
                public void Cases ()
 
194
                {
 
195
                        AssertRuleSuccess<Good1> ();    
 
196
                        AssertRuleSuccess<Good2> ();    
 
197
                        AssertRuleSuccess<Good3> ();    
 
198
                        AssertRuleSuccess<Good4> ();    
 
199
                        AssertRuleSuccess<Good5> ();    
 
200
                        AssertRuleSuccess<Good6> ();    
 
201
 
 
202
                        AssertRuleFailure<Bad1> (1);
 
203
                        AssertRuleFailure<Bad2> (1);
 
204
                        AssertRuleFailure<Bad3> (1);
 
205
                        AssertRuleFailure<Bad4> (1);
 
206
                }
 
207
 
 
208
                class Bug667692a {
 
209
 
 
210
                        private string [] bar = null;
 
211
 
 
212
                        public void SetBar (IEnumerable<string> boo)
 
213
                        {
 
214
                                bar = boo == null ? null : boo.ToArray ();
 
215
                        }
 
216
                }
 
217
 
 
218
                class Bug667692b {
 
219
 
 
220
                        private string [] bar = null;
 
221
 
 
222
                        public void SetBar (IEnumerable<string> boo)
 
223
                        {
 
224
                                bar = boo != null ? boo.ToArray () : null;
 
225
                        }
 
226
                }
 
227
 
 
228
                [Test]
 
229
                public void MultipleAssignment ()
 
230
                {
 
231
                        AssertRuleSuccess<TernaryIf> ();
 
232
                        AssertRuleSuccess<Bug667692a> ();
 
233
                        AssertRuleSuccess<Bug667692b> ();
 
234
                }
 
235
 
 
236
                // this will create an anonymous method
 
237
                class AnonymousDelegatesAllFields {
 
238
 
 
239
                        string file;
 
240
 
 
241
                        void Parse ()
 
242
                        {
 
243
                                ThreadPool.QueueUserWorkItem (delegate {
 
244
                                        file = ""; 
 
245
                                });
 
246
                        }
 
247
 
 
248
                        void Show ()
 
249
                        {
 
250
                                Console.WriteLine (file);
 
251
                        }
 
252
                }
 
253
 
 
254
                // this will create a nested type with the anonymous method
 
255
                class AnonymousDelegatesFieldsAndLocals {
 
256
 
 
257
                        string file;
 
258
 
 
259
                        void Parse ()
 
260
                        {
 
261
                                string local;
 
262
 
 
263
                                ThreadPool.QueueUserWorkItem (delegate {
 
264
                                        file = "";
 
265
                                        local = file;
 
266
                                });
 
267
                        }
 
268
 
 
269
                        void Show ()
 
270
                        {
 
271
                                Console.WriteLine (file);
 
272
                        }
 
273
                }
 
274
 
 
275
                [Test]
 
276
                public void AnonymousDelegates ()
 
277
                {
 
278
                        AssertRuleSuccess<AnonymousDelegatesAllFields> ();
 
279
                        AssertRuleSuccess<AnonymousDelegatesFieldsAndLocals> ();
 
280
                }
 
281
        }
 
282
}