2
// Unit tests for AvoidAlwaysNullFieldRule
5
// Jesse Jones <jesjones@mindspring.com>
6
// Sebastien Pouliot <sebastien@ximian.com>
8
// Copyright (C) 2008 Jesse Jones
9
// Copyright (C) 2011 Novell, Inc (http://www.novell.com)
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:
19
// The above copyright notice and this permission notice shall be
20
// included in all copies or substantial portions of the Software.
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.
32
using System.Collections.Generic;
34
using System.Threading;
36
using Gendarme.Rules.Maintainability;
38
using NUnit.Framework;
39
using Test.Rules.Definitions;
40
using Test.Rules.Fixtures;
42
namespace Test.Rules.Maintainability {
45
public sealed class AvoidAlwaysNullFieldTest : TypeRuleTestFixture<AvoidAlwaysNullFieldRule> {
47
private sealed class Good1 {
48
private string name = "hey"; // initialized
52
Console.WriteLine (name);
57
protected string name; // not private
62
Console.WriteLine (name);
66
private sealed class Good3 {
67
private string name; // not used (AvoidUnusedPrivateField will handle this case)
71
Console.WriteLine ("hey");
75
private sealed class Good4 {
76
private IntPtr zero; // not a reference type
80
Console.WriteLine (zero);
84
private sealed class Good5 {
85
private static string name = "hey"; // initialized
89
Console.WriteLine (name);
93
private sealed class Good6 {
98
IndirectSet (ref name); // address taken
103
Console.WriteLine (name);
106
public void IndirectSet (ref string n)
118
Console.WriteLine (name);
133
Console.WriteLine (name);
138
private static string name;
148
Console.WriteLine (name);
153
private string name1;
154
private string name2;
158
name1 = null; // explictly set to null, but not used
164
Console.WriteLine (name2);
168
private class TernaryIf {
171
public TernaryIf (bool flag)
173
name = flag ? "hmm" : null; // we don't handle this properly
179
Console.WriteLine (name);
184
public void Simple ()
186
AssertRuleDoesNotApply (SimpleTypes.Class); // no fields
187
AssertRuleDoesNotApply (SimpleTypes.Enum);
188
AssertRuleDoesNotApply (SimpleTypes.Interface);
189
AssertRuleSuccess (SimpleTypes.Structure); // a few public fields
195
AssertRuleSuccess<Good1> ();
196
AssertRuleSuccess<Good2> ();
197
AssertRuleSuccess<Good3> ();
198
AssertRuleSuccess<Good4> ();
199
AssertRuleSuccess<Good5> ();
200
AssertRuleSuccess<Good6> ();
202
AssertRuleFailure<Bad1> (1);
203
AssertRuleFailure<Bad2> (1);
204
AssertRuleFailure<Bad3> (1);
205
AssertRuleFailure<Bad4> (1);
210
private string [] bar = null;
212
public void SetBar (IEnumerable<string> boo)
214
bar = boo == null ? null : boo.ToArray ();
220
private string [] bar = null;
222
public void SetBar (IEnumerable<string> boo)
224
bar = boo != null ? boo.ToArray () : null;
229
public void MultipleAssignment ()
231
AssertRuleSuccess<TernaryIf> ();
232
AssertRuleSuccess<Bug667692a> ();
233
AssertRuleSuccess<Bug667692b> ();
236
// this will create an anonymous method
237
class AnonymousDelegatesAllFields {
243
ThreadPool.QueueUserWorkItem (delegate {
250
Console.WriteLine (file);
254
// this will create a nested type with the anonymous method
255
class AnonymousDelegatesFieldsAndLocals {
263
ThreadPool.QueueUserWorkItem (delegate {
271
Console.WriteLine (file);
276
public void AnonymousDelegates ()
278
AssertRuleSuccess<AnonymousDelegatesAllFields> ();
279
AssertRuleSuccess<AnonymousDelegatesFieldsAndLocals> ();