Author: spouliot
Date: 2007-10-07 19:26:01 -0400 (Sun, 07 Oct 2007)
New Revision: 87066

Added:
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ChangeLog
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/CloneMethodShouldNotReturnNullRule.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/EqualShouldHandleNullArgRule.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.mdp
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.xml.in
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Makefile.am
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/CloneMethodShouldNotReturnNullTest.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Makefile.am
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Test.Rules.BadPractice.mdp
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ToStringReturnsNullRule.cs
Log:
Integrate some GSoC 2007 work and update project files

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ChangeLog     
2007-10-07 23:12:20 UTC (rev 87065)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ChangeLog     
2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,10 @@
+2007-10-07  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * CloneMethodShouldNotReturnNullRule.cs: New rule (Nidhi Rawal, GSoC 
+       2007)
+       * EqualShouldHandleNullArgRule.cs: New rule (Nidhi Rawal, GSoC 2007)
+       * ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs: New rule
+       (Nidhi Rawal, GSoC 2007)
+       * ToStringReturnsNullRule.cs: New rule (Nidhi Rawal, GSoC 2007)
+       * Gendarme.Rules.BadPractice.mdp: Project file for MonoDevelop.
+       * Makefile.am: Build file.

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/CloneMethodShouldNotReturnNullRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/CloneMethodShouldNotReturnNullRule.cs
 2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/CloneMethodShouldNotReturnNullRule.cs
 2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,65 @@
+// 
+// Gendarme.Rules.BadPractice.CloneMethodShouldNotReturnNullRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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.Collections;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.BadPractice
+{
+       public class CloneMethodShouldNotReturnNullRule: ITypeRule
+       {
+               private bool ImplementsICloneable (TypeDefinition type)
+               {
+                       foreach (TypeReference iface in type.Interfaces) 
+                               if(iface.FullName == "System.ICloneable")
+                                       return true;
+                       return false;
+               }
+               
+               public MessageCollection CheckType (TypeDefinition type, Runner 
runner)
+               {
+                       MessageCollection messageCollection = new 
MessageCollection ();
+                       if (ImplementsICloneable (type))
+                               foreach (MethodDefinition method in 
type.Methods) 
+                                       if (method.Name == "Clone" && 
method.Parameters.Count == 0)
+                                               foreach (Instruction 
instruction in method.Body.Instructions) {
+                                                       Instruction prevInstr = 
instruction.Previous;
+                                                       if 
(instruction.OpCode.Name == "ret" && prevInstr.OpCode.Name == "ldnull") {
+                                                               Location 
location = new Location (type.FullName, type.Name, instruction.Offset);
+                                                               Message message 
= new Message ("The ICloneable.Clone () method returns null", location, 
MessageType.Error);
+                                                               
messageCollection.Add (message);
+                                                       }
+                                               }
+                       if (messageCollection.Count == 0)
+                                       return null;
+                       return messageCollection;
+               }
+       }
+}
\ No newline at end of file

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/EqualShouldHandleNullArgRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/EqualShouldHandleNullArgRule.cs
       2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/EqualShouldHandleNullArgRule.cs
       2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,66 @@
+// 
+// Gendarme.Rules.BadPractice.EqualShouldHandleNullArgRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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.Collections;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.BadPractice {
+       public class EqualShouldHandleNullArgRule: IMethodRule
+       {
+               public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
+               {
+                       MessageCollection messageCollection = new 
MessageCollection ();
+                       
+                       if (method.HasBody && method.Name == "Equals" && 
method.ReturnType.ReturnType.ToString () == "System.Boolean" && 
method.IsVirtual && !method.IsNewSlot)
+                               foreach (ParameterDefinition param in 
method.Parameters)
+                                       if (param.ParameterType.FullName == 
"System.Object")
+                                               if (!HandlesNullArg (method)) {
+                                               Location location = new 
Location (method.DeclaringType.FullName, method.Name, 0);
+                                               Message message = new Message 
("The overridden method Object.Equals (Object) does not return false if null 
value is found", location, MessageType.Error);
+                                               messageCollection.Add (message);
+                                       }
+                       
+                       if (messageCollection.Count == 0)
+                               return null;
+                       return messageCollection;
+               }
+               
+               public bool HandlesNullArg (MethodDefinition method)
+               {
+                       Instruction prevIns;
+                       foreach (Instruction ins in method.Body.Instructions) {
+                               prevIns = ins.Previous;
+                               if (ins.OpCode == OpCodes.Ret && prevIns.OpCode 
== OpCodes.Ldc_I4_0 && prevIns.Previous.OpCode == OpCodes.Brtrue)
+                                       return true;
+                       }
+                       return false;
+               }
+       }
+}
\ No newline at end of file

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.mdp
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.mdp
        2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.mdp
        2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,28 @@
+<Project name="Gendarme.Rules.BadPractice" fileversion="2.0" language="C#" 
clr-version="Net_1_1" ctype="DotNetProject">
+  <Configurations active="Debug">
+    <Configuration name="Debug" ctype="DotNetProjectConfiguration">
+      <Output directory="./bin/Debug" assembly="Gendarme.Rules.BadPractice" />
+      <Build debugmode="True" target="Library" />
+      <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" 
clr-version="Net_1_1" />
+      <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" 
unsafecodeallowed="False" generateoverflowchecks="True" 
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+    </Configuration>
+    <Configuration name="Release" ctype="DotNetProjectConfiguration">
+      <Output directory="./bin/Release" assembly="Gendarme.Rules.BadPractice" 
/>
+      <Build debugmode="False" target="Library" />
+      <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" 
clr-version="Net_1_1" />
+      <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" 
unsafecodeallowed="False" generateoverflowchecks="True" 
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+    </Configuration>
+  </Configurations>
+  <Contents>
+    <File name="./CloneMethodShouldNotReturnNullRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./EqualShouldHandleNullArgRule.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./Gendarme.Rules.BadPractice.xml.in" subtype="Code" 
buildaction="Nothing" />
+    <File name="./ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs" 
subtype="Code" buildaction="Compile" />
+    <File name="./ToStringReturnsNullRule.cs" subtype="Code" 
buildaction="Compile" />
+  </Contents>
+  <References>
+    <ProjectReference type="Gac" localcopy="True" refto="System, 
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+    <ProjectReference type="Assembly" localcopy="True" 
refto="../../../lib/Mono.Cecil.dll" />
+    <ProjectReference type="Project" localcopy="True" refto="framework" />
+  </References>
+</Project>
\ No newline at end of file

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.xml.in
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.xml.in
     2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Gendarme.Rules.BadPractice.xml.in
     2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,26 @@
+<rules>
+       <rule   Name="ImplementingEqualsButNotGetHashCodeAndViceVersaRule" 
+               
Type="Gendarme.Rules.BadPractice.ImplementingEqualsButNotGetHashCodeAndViceVersaRule,
 Gendarme.Rules.BadPractice, Version=0.0.3.1, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme#ImplementingEqualsButNotGetHashCodeAndViceVersaRule";
 >
+               <problem>Only one method of Object.Equals (Object) and 
Object.GetHashCode () is implemented in '{0}'.</problem>
+               <solution>Implement the other method also.</solution>
+       </rule>
+       <rule   Name="CloneMethodShouldNotReturnNullRule" 
+               
Type="Gendarme.Rules.BadPractice.CloneMethodShouldNotReturnNullRule, 
Gendarme.Rules.BadPractice, Version=0.0.3.1, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme#CloneMethodShouldNotReturnNullRule"; >
+               <problem>The ICloneable.Clone () method in '{0}' seems to 
return null in some circumstances.</problem>
+               <solution>Please return an appropriate object instead of 
returning null.</solution>
+       </rule>
+       <rule   Name="ToStringReturnsNullRule" 
+               Type="Gendarme.Rules.BadPractice.ToStringReturnsNullRule, 
Gendarme.Rules.BadPractice, Version=0.0.3.1, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme#ToStringReturnsNullRule"; >
+               <problem>The method '{0}' contains ToString () conversion that 
returns null.</problem>
+               <solution>Return an empty string or other appropriate string 
rather than returning null.</solution>
+       </rule>
+       <rule   Name="EqualShouldHandleNullArgRule" 
+               Type="Gendarme.Rules.BadPractice.EqualShouldHandleNullArgRule, 
Gendarme.Rules.BadPractice, Version=0.0.3.1, Culture=neutral, 
PublicKeyToken=null"
+               
Uri="http://www.mono-project.com/Gendarme#EqualShouldHandleNullArgRule"; >
+               <problem>The method '{0}' does not handle null argument as it 
should be.</problem>
+               <solution>Modify the method implementation to return false if 
null argument found.</solution>
+       </rule>
+</rules>

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs
        2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs
        2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,79 @@
+// 
+// 
Gendarme.Rules.BadPractice.ImplementingEqualsButNotGetHashCodeAndViceVersaRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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.Collections;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.BadPractice {
+
+       public class ImplementingEqualsButNotGetHashCodeAndViceVersaRule: 
ITypeRule
+       {
+               bool equals = false;
+               bool getHashCode = false;
+               
+               private void EqualsOrGetHashCode (TypeDefinition type)
+               {
+                       equals = false;
+                       getHashCode = false;
+                       
+                       foreach (MethodDefinition method in type.Methods) {
+                               if (method.Name == "Equals" && 
method.Parameters.Count == 1)
+                                       equals = true;
+                               else if (method.Name == "GetHashCode" && 
method.Parameters.Count == 0)
+                                       getHashCode = true;
+                       }
+               }
+               
+               public MessageCollection CheckType (TypeDefinition type, Runner 
runner)
+               {
+                       MessageCollection messageCollection = new 
MessageCollection ();
+                       EqualsOrGetHashCode (type);
+                       
+                       if (equals == true && getHashCode == false)
+                       {
+                               Location location = new Location 
(type.FullName, type.Name, 0);
+                               Message message = new Message ("Implements 
Object.Equals (Object) but does not implement Object.GetHashCode ()", location, 
MessageType.Error);
+                               messageCollection.Add (message);
+                       }
+                       else if (equals == false && getHashCode == true)
+                       {
+                               Location location = new Location 
(type.FullName, type.Name, 0);
+                               Message message = new Message ("Implements 
Object.GetHashCode () but does not implement Object.Equals (Object)", location, 
MessageType.Error);
+                               messageCollection.Add (message);
+                       }
+                       else
+                               return runner.RuleSuccess;
+                       
+                       if (messageCollection.Count == 0)
+                               return null;
+                       return messageCollection;
+               }
+       }
+}

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Makefile.am   
2007-10-07 23:12:20 UTC (rev 87065)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Makefile.am   
2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,49 @@
+SUBDIRS=Test
+
+badpractice_rulesdir=$(pkglibdir)
+badpractice_rules_SCRIPTS = Gendarme.Rules.BadPractice.dll
+EXTRA_DIST = $(badpractice_rules_sources) $(badpractice_rules_sources_in) 
Gendarme.Rules.BadPractice.xml \
+       Gendarme.Rules.BadPractice.mdp Gendarme.Rules.BadPractice.csproj
+CLEANFILES = Gendarme.Rules.BadPractice.dll Test.Rules.BadPractice.dll
+DISTCLEANFILES = Makefile.in Gendarme.Rules.BadPractice.xml TestResult.xml
+
+badpractice_rules_sources_in = ../../AssemblyInfo.cs.in
+badpractice_rules_generated_sources = $(badpractice_rules_sources_in:.in=)
+badpractice_rules_sources = CloneMethodShouldNotReturnNullRule.cs 
EqualShouldHandleNullArgRule.cs \
+       ImplementingEqualsButNotGetHashCodeAndViceVersaRule.cs 
ToStringReturnsNullRule.cs
+
+badpractice_rules_build_sources = $(addprefix $(srcdir)/, 
$(badpractice_rules_sources))
+badpractice_rules_build_sources += $(badpractice_rules_generated_sources)
+
+Gendarme.Rules.BadPractice.dll: $(badpractice_rules_build_sources)
+       $(MCS) -target:library -pkg:mono-cecil 
-r:../../framework/Gendarme.Framework.dll -out:$@ 
$(badpractice_rules_build_sources)
+
+# Install Unstable Mono Libraries (see configure.ac)
+
+install-data-hook:
+       $(INSTALL) -c -m 0644 Gendarme.Rules.BadPractice.xml 
$(DESTDIR)$(pkglibdir);
+       for ASM in $(INSTALLED_ASSEMBLIES); do \
+               $(INSTALL) -c -m 0755 $$ASM $(DESTDIR)$(pkglibdir); \
+       done;
+
+uninstall-hook:
+       rm -f $(DESTDIR)$(pkglibdir)/`basename Gendarme.Rules.BadPractice.xml`;
+       for ASM in $(INSTALLED_ASSEMBLIES); do \
+               rm -f $(DESTDIR)$(pkglibdir)/`basename $$ASM`; \
+       done;
+
+badpractice_test_sources = CloneMethodShouldNotReturnNullTest.cs 
EqualShouldHandleNullArgTest.cs \
+       ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs 
ToStringReturnsNullTest.cs
+badpractice_test_build_sources = $(addprefix $(srcdir)/Test/, 
$(badpractice_test_sources))
+
+Test.Rules.BadPractice.dll: $(badpractice_rules_build_sources)
+       $(MCS) -target:library -pkg:mono-cecil -pkg:mono-nunit 
-r:../../framework/Gendarme.Framework.dll \
+               -r:Gendarme.Rules.BadPractice.dll -out:$@ 
$(badpractice_test_build_sources)
+
+test: Gendarme.Rules.BadPractice.dll Test.Rules.BadPractice.dll
+
+run-test: Gendarme.Rules.BadPractice.dll Test.Rules.BadPractice.dll
+       MONO_PATH=../../framework/:$(CECIL_PATH):$(MONO_PATH) nunit-console 
Test.Rules.BadPractice.dll
+
+self-test: test
+       gendarme Gendarme.Rules.BadPractice.dll

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog        
2007-10-07 23:12:20 UTC (rev 87065)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog        
2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,10 @@
+2007-10-07  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * CloneMethodShouldNotReturnNullTest.cs: Unit tests (Nidhi Rawal, 
+       GSoC 2007)
+       * EqualShouldHandleNullArgTest.cs: Unit tests (Nidhi Rawal, GSoC 2007)
+       * ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs: Unit tests 
+       (Nidhi Rawal, GSoC 2007)
+       * ToStringReturnsNullTest.cs: Unit tests (Nidhi Rawal, GSoC 2007)
+       * Test.Rules.BadPractice.mdp: Project file for MonoDevelop
+       * Makefile.am: Build file.

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/CloneMethodShouldNotReturnNullTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/CloneMethodShouldNotReturnNullTest.cs
    2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/CloneMethodShouldNotReturnNullTest.cs
    2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,158 @@
+// 
+// Unit tests for CloneMethodShouldNotReturnNullRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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 Gendarme.Framework;
+using Gendarme.Rules.BadPractice;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.BadPractice
+{
+       [TestFixture]
+       public class CloneMethodShouldNotReturnNullTest {
+               
+               public class CloneMethodReturningNull: ICloneable
+               {
+                       public object Clone ()
+                       {
+                               return null;
+                       }
+               }
+               
+               public class CloneMethodNotReturningNull: ICloneable
+               {
+                       public object Clone ()
+                       {
+                               return this.MemberwiseClone ();
+                       }
+               }
+               
+               public class NotUsingICloneableClone
+               {
+                       public object Clone ()
+                       {
+                               return null;
+                       }
+               }
+               
+               public class CloneWithDifferentArgsReturningNull: ICloneable
+               {
+                       public virtual object Clone ()
+                       {
+                               return this.MemberwiseClone ();
+                       }
+                       
+                       public object Clone (int j)
+                       {
+                               return null;
+                       }
+               }
+               
+               public class CloneReturningNullInSomeConditions: ICloneable
+               {
+                       public bool test (int j)
+                       {
+                               if (j > 10)
+                                       return true;
+                               else
+                                       return false;
+                       }
+                       
+                       public object Clone()
+                       {
+                               if (test (11))
+                                       return MemberwiseClone();
+                               else
+                                       return null;
+                       }
+               }
+                       
+               private ITypeRule typeRule;
+               private AssemblyDefinition assembly;
+               private TypeDefinition type;
+               MessageCollection messageCollection;
+               
+               [TestFixtureSetUp]
+               public void FixtureSetUp ()
+               {
+                       string unit = Assembly.GetExecutingAssembly ().Location;
+                       assembly = AssemblyFactory.GetAssembly (unit);
+                       typeRule = new CloneMethodShouldNotReturnNullRule ();
+                       messageCollection = null;
+               }
+               
+               private TypeDefinition GetTest (string name)
+               {
+                       string fullname = 
"Test.Rules.BadPractice.CloneMethodShouldNotReturnNullTest/" + name;
+                       return assembly.MainModule.Types[fullname];
+               }
+                       
+               [Test]
+               public void cloneMethodReturningNullTest ()
+               {
+                       type = GetTest ("CloneMethodReturningNull");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (1, messageCollection.Count);
+               }
+               
+               [Test]
+               public void cloneMethodNotReturningNullTest ()
+               {
+                       type = GetTest ("CloneMethodNotReturningNull");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void notUsingICloneableCloneTest ()
+               {
+                       type = GetTest ("NotUsingICloneableClone");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void cloneWithDifferentArgsReturningNullTest ()
+               {
+                       type = GetTest ("CloneWithDifferentArgsReturningNull");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void cloneReturningNullInSomeConditionsTest ()
+               {
+                       type = GetTest ("CloneReturningNullInSomeConditions");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (1, messageCollection.Count);
+               }
+       }
+}
\ No newline at end of file

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
  2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
  2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,180 @@
+// 
+// Unit tests for EqualShouldHandleNullArgRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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 Gendarme.Framework;
+using Gendarme.Rules.BadPractice;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.BadPractice
+{
+       [TestFixture]
+       public class EqualShouldHandleNullArgTest {
+               public class EqualsChecksForNullArg
+               {
+                       public override bool Equals (System.Object obj)
+                       {
+                               if (obj == null)
+                                       return false;
+                               else
+                                       return this == obj;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+               
+               public class EqualsDoesNotCheckForNullArg
+               {
+                       public override bool Equals (System.Object obj)
+                       {
+                               return this == obj;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+               
+               public class EqualsDoesNotReturnFalseForNullArg
+               {
+                       public override bool Equals (System.Object obj)
+                       {
+                               if (obj == null)
+                                       return true;
+                               else
+                                       return this == obj;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+               
+               public class EqualsNotOverriddenNotCheckingNull
+               {
+                       public bool Equals (System.Object obj)
+                       {
+                               return this == obj;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+               
+               public class EqualsNotOverriddenNotReturningFalseForNull
+               {
+                       public new bool Equals (System.Object obj)
+                       {
+                               if (obj == null)
+                                       return true;
+                               else
+                                       return this == obj;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+               
+               private IMethodRule methodRule;
+               private AssemblyDefinition assembly;
+               private TypeDefinition type;
+               MessageCollection messageCollection;
+
+               [TestFixtureSetUp]
+               public void FixtureSetUp ()
+               {
+                       string unit = Assembly.GetExecutingAssembly ().Location;
+                       assembly = AssemblyFactory.GetAssembly (unit);
+                       methodRule = new EqualShouldHandleNullArgRule ();
+                       messageCollection = null;
+               }
+               
+               private TypeDefinition GetTest (string name)
+               {
+                       string fullname = 
"Test.Rules.BadPractice.EqualShouldHandleNullArgTest/" + name;
+                       return assembly.MainModule.Types[fullname];
+               }
+               
+               [Test]
+               public void equalsChecksForNullArgTest ()
+               {
+                       type = GetTest ("EqualsChecksForNullArg");
+                       foreach (MethodDefinition method in type.Methods)
+                               if (method.Name == "Equals")
+                                       messageCollection = 
methodRule.CheckMethod (method, new MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void equalsDoesNotCheckForNullArgTest ()
+               {
+                       type = GetTest ("EqualsDoesNotCheckForNullArg");
+                       foreach (MethodDefinition method in type.Methods)
+                               if (method.Name == "Equals")
+                                       messageCollection = 
methodRule.CheckMethod (method, new MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (1, messageCollection.Count);
+               }
+               
+               [Test]
+               public void equalsDoesNotReturnFalseForNullArgTest ()
+               {
+                       type = GetTest ("EqualsDoesNotReturnFalseForNullArg");
+                       foreach (MethodDefinition method in type.Methods)
+                               if (method.Name == "Equals")
+                                       messageCollection = 
methodRule.CheckMethod (method, new MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (1, messageCollection.Count);
+               }
+               
+               [Test]
+               public void equalsNotOverriddenNotCheckingNullTest ()
+               {
+                       type = GetTest ("EqualsNotOverriddenNotCheckingNull");
+                       foreach (MethodDefinition method in type.Methods)
+                               if (method.Name == "Equals")
+                                       messageCollection = 
methodRule.CheckMethod (method, new MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void equalsNotOverriddenNotReturningFalseForNullTest ()
+               {
+                       type = GetTest 
("EqualsNotOverriddenNotReturningFalseForNull");
+                       foreach (MethodDefinition method in type.Methods)
+                               if (method.Name == "Equals")
+                                       messageCollection = 
methodRule.CheckMethod (method, new MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+       }
+}
\ No newline at end of file

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
   2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
   2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,207 @@
+// 
+// Unit tests for ImplementingEqualsButNotGetHashCodeAndViceVersaRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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.Collections;
+using System.Reflection;
+
+using Gendarme.Framework;
+using Gendarme.Rules.BadPractice;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.BadPractice {
+       
+       [TestFixture]
+       public class ImplementingEqualsButNotGetHashCodeAndViceVersaTest
+       {
+               public class ImplementsEqualsButNotGetHashCode
+               {
+                       public override bool Equals (Object obj)
+                       {
+                               return this == obj;
+                       }
+               }
+                       
+               public class ImplementsGetHashCodeButNotEquals
+               {
+                       public override int GetHashCode ()
+                       {
+                               return 2;
+                       }
+               }
+               
+               public class ImplementsNoneOfThem
+               {
+                       public void test ()
+                       {
+                       }
+               }
+               
+               public class ImplementsBothOfThem
+               {
+                       public override int GetHashCode ()
+                       {
+                               return 2;
+                       }
+                       public new bool Equals (Object obj)
+                       {
+                               return this == obj;
+                       }
+               }
+
+               public class ImplementsEqualsUsesObjectGetHashCode
+               {
+                       public override bool Equals (Object obj)
+                       {
+                               return this == obj;
+                       }
+                       public static void Main (string [] args)
+                       {
+                               int j = 0;
+                               ImplementsEqualsUsesObjectGetHashCode i = new 
ImplementsEqualsUsesObjectGetHashCode ();
+                               j = i.GetHashCode ();
+                       }
+               }
+               
+               public class ImplementsGetHashCodeUsesObjectEquals
+               {
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+                       public static void Main (string [] args)
+                       {
+                               ImplementsGetHashCodeUsesObjectEquals i = new 
ImplementsGetHashCodeUsesObjectEquals ();
+                               ImplementsGetHashCodeUsesObjectEquals i1 = new 
ImplementsGetHashCodeUsesObjectEquals ();
+                               i.Equals (i1);
+                       }
+               }
+                       
+               public class ImplementingEqualsWithTwoArgs
+               {
+                       public bool Equals (Object obj1, Object obj2)
+                       {
+                               return obj1 == obj2;
+                       }
+               }
+               
+               public class ImplementingGetHashCodeWithOneArg
+               {
+                       public int GetHashCode (int j)
+                       {
+                               return j*2;
+                       }
+               }
+               
+               private ITypeRule typeRule;
+               private AssemblyDefinition assembly;
+               private MessageCollection messageCollection;
+
+               [TestFixtureSetUp]
+               public void FixtureSetUp ()
+               {
+                       string unit = Assembly.GetExecutingAssembly ().Location;
+                       assembly = AssemblyFactory.GetAssembly (unit);
+                       typeRule = new 
ImplementingEqualsButNotGetHashCodeAndViceVersaRule();
+                       messageCollection = null;
+               }
+               
+               private TypeDefinition GetTest (string name)
+               {
+                       string fullname = 
"Test.Rules.BadPractice.ImplementingEqualsButNotGetHashCodeAndViceVersaTest/" + 
name;
+                       return assembly.MainModule.Types[fullname];
+               }
+               
+               [Test]
+               public void EqualsButNotGetHashCodeTest ()
+               { 
+                       TypeDefinition type = GetTest 
("ImplementsEqualsButNotGetHashCode");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (messageCollection.Count, 1);
+               }
+               
+               [Test]
+               public void GetHashCodeButNotEqualsTest ()
+               {
+                       TypeDefinition type = GetTest 
("ImplementsGetHashCodeButNotEquals");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (messageCollection.Count, 1);
+               }
+               
+               [Test]
+               public void NoneOfThemTest ()
+               {
+                       TypeDefinition type = GetTest ("ImplementsNoneOfThem");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void BothOfThemTest ()
+               {
+                       TypeDefinition type = GetTest ("ImplementsBothOfThem");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void ImplementsEqualsUsesObjectGetHashCodeTest ()
+               {
+                       TypeDefinition type = GetTest 
("ImplementsEqualsUsesObjectGetHashCode");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (messageCollection.Count, 1);
+               }
+               
+               [Test]
+               public void ImplementsGetHashCodeUsesObjectEqualsTest ()
+               {
+                       TypeDefinition type = GetTest 
("ImplementsGetHashCodeUsesObjectEquals");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (messageCollection.Count, 1);
+               }
+               
+               [Test]
+               public void EqualsWithTwoArgsTest ()
+               {
+                       TypeDefinition type = GetTest 
("ImplementingEqualsWithTwoArgs");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void GetHashCodeWithOneArgTest ()
+               {
+                       TypeDefinition type = GetTest 
("ImplementingGetHashCodeWithOneArg");
+                       messageCollection = typeRule.CheckType (type, new 
MinimalRunner ());
+                       Assert.IsNull (messageCollection);
+               }
+       }
+}

Added: trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Makefile.am
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Makefile.am      
2007-10-07 23:12:20 UTC (rev 87065)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Makefile.am      
2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,4 @@
+EXTRA_DIST = *.cs Test.Rules.BadPractice.mdp Tests.Rules.BadPractice.csproj
+DISTCLEANFILES = Makefile.in
+
+all:

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Test.Rules.BadPractice.mdp
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Test.Rules.BadPractice.mdp
       2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/Test.Rules.BadPractice.mdp
       2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,30 @@
+<Project name="Test.Rules.BadPractice" fileversion="2.0" language="C#" 
clr-version="Net_1_1" ctype="DotNetProject">
+  <Configurations active="Debug">
+    <Configuration name="Debug" ctype="DotNetProjectConfiguration">
+      <Output directory="../../../bin/Debug" assembly="Test.Rules.BadPractice" 
/>
+      <Build debugmode="True" target="Library" />
+      <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" 
clr-version="Net_1_1" />
+      <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" 
unsafecodeallowed="False" generateoverflowchecks="True" 
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+    </Configuration>
+    <Configuration name="Release" ctype="DotNetProjectConfiguration">
+      <Output directory="../../../bin/Release" 
assembly="Test.Rules.BadPractice" />
+      <Build debugmode="False" target="Library" />
+      <Execution runwithwarnings="True" consolepause="False" runtime="MsNet" 
clr-version="Net_1_1" />
+      <CodeGeneration compiler="Mcs" warninglevel="4" optimize="True" 
unsafecodeallowed="False" generateoverflowchecks="True" 
generatexmldocumentation="False" ctype="CSharpCompilerParameters" />
+    </Configuration>
+  </Configurations>
+  <Contents>
+    <File name="./CloneMethodShouldNotReturnNullTest.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./EqualShouldHandleNullArgTest.cs" subtype="Code" 
buildaction="Compile" />
+    <File name="./ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs" 
subtype="Code" buildaction="Compile" />
+    <File name="./ToStringReturnsNullTest.cs" subtype="Code" 
buildaction="Compile" />
+  </Contents>
+  <References>
+    <ProjectReference type="Gac" localcopy="True" refto="System, 
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+    <ProjectReference type="Assembly" localcopy="True" 
refto="../../../../lib/Mono.Cecil.dll" />
+    <ProjectReference type="Project" localcopy="True" refto="framework" />
+    <ProjectReference type="Gac" localcopy="True" refto="nunit.framework, 
Version=2.2.0.0, Culture=neutral, PublicKeyToken=96d09a1eb7f44a77" />
+    <ProjectReference type="Project" localcopy="True" 
refto="Gendarme.Rules.BadPractice" />
+  </References>
+  <Deployment.LinuxDeployData generatePcFile="False" />
+</Project>

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
       2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
       2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,154 @@
+// 
+// Unit tests for ToStringReturnsNullRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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 Gendarme.Framework;
+using Gendarme.Rules.BadPractice;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.BadPractice
+{
+       [TestFixture]
+       public class ToStringReturnsNullTest {
+               
+               public class ToStringReturningNull
+               {
+                       public override string ToString ()
+                       {
+                               return null;
+                       }
+               }
+               
+               public class ToStringReturningEmptyString
+               {
+                       public override string ToString ()
+                       {
+                               return String.Empty;
+                       }
+               } 
+               
+               public class ToStringNotReturningNull
+               {
+                       string s = "ab";
+                       public override string ToString ()
+                       {
+                               return s;
+                       }
+               } 
+               
+               public class ConvertToStringReturningEmptyString
+               {
+                       object obj = null;
+                       public override string ToString ()
+                       {
+                               return Convert.ToString (obj);
+                       }
+               } 
+               
+               public class ConvertToStringReturningNull
+               {
+                       string s = null;
+                       public override string ToString ()
+                       { 
+                               return Convert.ToString (s);
+                       }
+               } 
+               
+               private IMethodRule methodRule;
+               private AssemblyDefinition assembly;
+               private TypeDefinition type;
+               MessageCollection messageCollection;
+
+               [TestFixtureSetUp]
+               public void FixtureSetUp ()
+               {
+                       string unit = Assembly.GetExecutingAssembly ().Location;
+                       assembly = AssemblyFactory.GetAssembly (unit);
+                       methodRule = new ToStringReturnsNullRule();
+                       messageCollection = null;
+               }
+               
+               private TypeDefinition GetTest (string name)
+               {
+                       string fullname = 
"Test.Rules.BadPractice.ToStringReturnsNullTest/" + name;
+                       return assembly.MainModule.Types[fullname];
+               }
+               
+               [Test]
+               public void toStringReturningNullTest ()
+               {
+                       type = GetTest ("ToStringReturningNull");
+                       foreach (MethodDefinition method in type.Methods) {
+                               messageCollection = methodRule.CheckMethod 
(method, new MinimalRunner ());
+                       }
+                       Assert.IsNotNull (messageCollection);
+                       Assert.AreEqual (1, messageCollection.Count);
+               }
+               
+               [Test]
+               public void toStringReturningEmptyStringTest ()
+               {
+                       type = GetTest ("ToStringReturningEmptyString");
+                       foreach (MethodDefinition method in type.Methods) {
+                               messageCollection = methodRule.CheckMethod 
(method, new MinimalRunner ());
+                       }
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void toStringNotReturningNullTest ()
+               {
+                       type = GetTest ("ToStringNotReturningNull");
+                       foreach (MethodDefinition method in type.Methods) {
+                               messageCollection = methodRule.CheckMethod 
(method, new MinimalRunner ());
+                       }
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void convertToStringReturningEmptyStringTest ()
+               {
+                       type = GetTest ("ConvertToStringReturningEmptyString");
+                       foreach (MethodDefinition method in type.Methods) {
+                               messageCollection = methodRule.CheckMethod 
(method, new MinimalRunner ());
+                       }
+                       Assert.IsNull (messageCollection);
+               }
+               
+               [Test]
+               public void convertToStringReturningNullTest ()
+               {
+                       type = GetTest ("ConvertToStringReturningNull");
+                       foreach (MethodDefinition method in type.Methods) {
+                               messageCollection = methodRule.CheckMethod 
(method, new MinimalRunner ());
+                       }
+                       Assert.IsNotNull (messageCollection);
+               }
+       }
+}
\ No newline at end of file

Added: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ToStringReturnsNullRule.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ToStringReturnsNullRule.cs
    2007-10-07 23:12:20 UTC (rev 87065)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/ToStringReturnsNullRule.cs
    2007-10-07 23:26:01 UTC (rev 87066)
@@ -0,0 +1,153 @@
+// 
+// Gendarme.Rules.BadPractice.ToStringReturnsNullRule
+//
+// Authors:
+//     Nidhi Rawal <[EMAIL PROTECTED]>
+//
+// Copyright (c) <2007> Nidhi Rawal
+//
+// 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.Collections;
+
+using Mono.Cecil;
+using Mono.Cecil.Cil;
+using Gendarme.Framework;
+
+namespace Gendarme.Rules.BadPractice {
+       public class ToStringReturnsNullRule: IMethodRule
+       {
+               public MessageCollection CheckMethod (MethodDefinition method, 
Runner runner)
+               {
+                       MessageCollection messageCollection = new 
MessageCollection ();
+                       Instruction prevIns;
+                       int offset = 0;
+                       bool nullReturned = false;
+                       foreach (Instruction ins in method.Body.Instructions)
+                               if (method.Name == "ToString")
+                                       if (ins.OpCode == OpCodes.Ret) {
+                                               prevIns = ins.Previous;
+                                               if (prevIns.OpCode != 
OpCodes.Ldnull) {
+                                                       string opCodeSt;
+                                                       if (prevIns.OpCode == 
OpCodes.Call || prevIns.OpCode == OpCodes.Callvirt)
+                                                               opCodeSt = 
ReturnSt (method, prevIns.Previous);
+                                                       else 
+                                                               opCodeSt = 
ReturnSt (method, prevIns);
+                                                       if (opCodeSt == null && 
prevIns.Operand.ToString () != "System.String 
System.Convert::ToString(System.Object)")
+                                                               nullReturned = 
true;
+                                                       if (MethodReturnsNull 
(method, opCodeSt)) {
+                                                               nullReturned = 
true;
+                                                               offset = 
ins.Offset;
+                                                       }
+                                               }
+                                               else {
+                                                       nullReturned = true;
+                                                       offset = ins.Offset;
+                                               }
+                                       }
+                       if (nullReturned) {
+                               Location location = new Location 
(method.DeclaringType.FullName, method.Name, offset);
+                               Message message = new Message ("ToString () 
seems to returns null in some condition", location, MessageType.Error);
+                               messageCollection.Add (message);
+                       }
+                       if (messageCollection.Count == 0)
+                               return null;
+                       return messageCollection;
+               }
+               
+               public string ReturnSt (MethodDefinition method, Instruction 
instruc)
+               { 
+                       Hashtable opCodes = InitializeHashTable (method);
+                       string stOpCode = null;
+                       switch (instruc.OpCode.Code) {
+                       case Code.Ldloc_0:
+                               stOpCode = opCodes [0].ToString ();
+                               break;
+                       case Code.Ldloc_1:
+                               stOpCode = opCodes [1].ToString ();
+                               break;
+                       case Code.Ldloc_2:
+                               stOpCode = opCodes [2].ToString ();
+                               break;
+                       case Code.Ldloc_3:
+                               stOpCode = opCodes [3].ToString ();
+                               break;
+                       case Code.Ldloc_S:
+                               string [] s = instruc.Operand.ToString ().Split 
('_');
+                               int i = Convert.ToInt32 (s [1]);
+                               stOpCode = opCodes [i].ToString ();
+                               break;
+                       case Code.Ldfld:
+                               TypeDefinition type = (TypeDefinition) 
method.DeclaringType;
+                               foreach (MethodDefinition ctor in 
type.Constructors)
+                                       foreach (Instruction ctorins in 
ctor.Body.Instructions)
+                                               if (ctorins.Operand !=null && 
ctorins.Operand.ToString () == instruc.Operand.ToString ())
+                                                       stOpCode = 
ctorins.Operand.ToString ();
+                               break;
+                       case Code.Ldsfld:
+                               stOpCode = String.Empty;
+                               break;
+                       }
+                       return stOpCode;
+               }
+               
+               public Hashtable InitializeHashTable (MethodDefinition method)
+               {
+                       int count = 0;
+                       Hashtable hash = new Hashtable ();                      
+                       if (method.Name == "ToString") {
+                               foreach (Instruction ins in 
method.Body.Instructions) {
+                                       if 
(ins.OpCode.Code.ToString().Substring(0,2) == "St") {
+                                               if (ins.OpCode.Code == 
Code.Stloc_S) {
+                                                       hash.Add (count, 
ins.Operand.ToString ());
+                                                       count ++;
+                                               } 
+                                               else {
+                                                       hash.Add (count, 
ins.OpCode.Code.ToString ());
+                                                       count ++;
+                                               }
+                                       }
+                               }
+                       }
+                       return hash;
+               }
+               
+               public bool MethodReturnsNull (MethodDefinition method, string 
code)
+               {
+                       bool isNull = false;
+                       Instruction prevInstruc;
+                       foreach (Instruction ins in method.Body.Instructions) {
+                               prevInstruc = ins.Previous;
+                               if (ins.Operand != null && ins.Operand.ToString 
() == null && ins.OpCode == OpCodes.Stfld)
+                                       isNull = false;
+                               else if (ins.OpCode.Code.ToString () == code || 
(ins.OpCode.ToString ().StartsWith ("st") && ins.Operand !=null && 
ins.Operand.ToString () == code)) {
+                                       if (prevInstruc.OpCode.Code == 
Code.Ldnull)
+                                               isNull = true;
+                                       else if (prevInstruc.OpCode.Code == 
Code.Call || prevInstruc.OpCode.Code == Code.Callvirt) {
+                                               code = ReturnSt (method, 
prevInstruc.Previous);
+                                               isNull = MethodReturnsNull 
(method, code);
+                                       }
+                               }
+                       }
+                       return isNull;
+               }
+       
+       }
+}
\ No newline at end of file

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to