Author: spouliot
Date: 2008-01-23 19:01:57 -0500 (Wed, 23 Jan 2008)
New Revision: 93745

Modified:
   trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ConstructorShouldNotCallVirtualMethodsTest.cs
   
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
Log:
2008-01-23  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * ConstructorShouldNotCallVirtualMethodsTest.cs: Add a bunch
        of test cases to exclude final, include non-virtual calls leading
        to virtual calls, instance versus member/field calls...
        * EqualShouldHandleNullArgTest.cs: Add a bunch of test cases
        to duplicate common patterns (from class lib) leading to false 
        positives.



Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog        
2008-01-24 00:01:40 UTC (rev 93744)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog        
2008-01-24 00:01:57 UTC (rev 93745)
@@ -1,3 +1,12 @@
+2008-01-23  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * ConstructorShouldNotCallVirtualMethodsTest.cs: Add a bunch
+       of test cases to exclude final, include non-virtual calls leading
+       to virtual calls, instance versus member/field calls...
+       * EqualShouldHandleNullArgTest.cs: Add a bunch of test cases
+       to duplicate common patterns (from class lib) leading to false 
+       positives.
+
 2008-01-14  Sebastien Pouliot  <[EMAIL PROTECTED]>
 
        * ConstructorShouldNotCallVirtualMethodsTest.cs: New. Unit 

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ConstructorShouldNotCallVirtualMethodsTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ConstructorShouldNotCallVirtualMethodsTest.cs
    2008-01-24 00:01:40 UTC (rev 93744)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ConstructorShouldNotCallVirtualMethodsTest.cs
    2008-01-24 00:01:57 UTC (rev 93745)
@@ -3,8 +3,10 @@
 //
 // Authors:
 //     Daniel Abramov <[EMAIL PROTECTED]>
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
 //
-// Copyright (C) Daniel Abramov
+// Copyright (C) 2008 Daniel Abramov
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to 
deal
@@ -36,16 +38,82 @@
 
 namespace Test.Rules.BadPractice {
 
+       internal class ClassWithStaticCtor {
+               static ClassWithStaticCtor ()
+               {
+                       ClassWithStaticCtor test = new ClassWithStaticCtor ();
+                       // this is ok, since we calling the instance virtual 
method
+                       test.Method ();
+               }
+
+               public virtual void Method ()
+               {
+               }
+       }
+
+       internal class ClassWithInstanceOfItself {
+
+               private ClassWithInstanceOfItself itself;
+
+               public ClassWithInstanceOfItself ()
+               {
+                       itself = null;
+               }
+
+               public ClassWithInstanceOfItself (ClassWithInstanceOfItself 
self)
+               {
+                       itself = self;
+                       itself.Method (0);
+               }
+
+               public virtual void Method (int x)
+               {
+               }
+       }
+
        internal class ClassNotCallingVirtualMethods {
+
                public ClassNotCallingVirtualMethods ()
                {
                        this.NormalMethod ();
                }
 
-               private void NormalMethod () { }
+               private void NormalMethod () 
+               {
+               }
+
+               protected virtual void VirtualMethod ()
+               {
+               }
+
+               public virtual bool VirtualProperty {
+                       get { return true; }
+                       set { ; }
+               }
        }
 
+       internal sealed class SealedClassWithVirtualCall : 
ClassNotCallingVirtualMethods {
+               public SealedClassWithVirtualCall ()
+               {
+                       VirtualMethod ();
+               }
+       }
+
+       internal class ClassWithRecursiveVirtualCall {
+               public ClassWithRecursiveVirtualCall ()
+               {
+                       VirtualMethod (5);
+               }
+
+               protected virtual void VirtualMethod (int n)
+               {
+                       while (n > 0)
+                               VirtualMethod (n--);
+               }
+       }
+
        internal class ClassCallingVirtualMethodOnce {
+
                public ClassCallingVirtualMethodOnce ()
                {
                        this.NormalMethod ();
@@ -54,11 +122,22 @@
                        this.NormalMethod ();
                }
 
-               protected virtual void VirtualMethod () { }
-               private void NormalMethod () { }
+               protected virtual void VirtualMethod ()
+               {
+               }
+
+               public void NormalMethod ()
+               {
+               }
+
+               public void CallingVirtualMethod ()
+               {
+                       VirtualMethod ();
+               }
        }
 
        internal class ClassCallingVirtualMethodThreeTimes {
+
                public ClassCallingVirtualMethodThreeTimes ()
                {
                        this.NormalMethod ();
@@ -68,11 +147,94 @@
                        this.VirtualMethod ();
                        this.NormalMethod ();
                }
-               protected virtual void VirtualMethod () { }
-               protected virtual void VirtualMethod2 () { }
-               private void NormalMethod () { }
+
+               protected virtual void VirtualMethod () 
+               {
+               }
+
+               protected virtual void VirtualMethod2 ()
+               {
+               }
+
+               private void NormalMethod ()
+               {
+               }
        }
 
+       internal class ClassCallingVirtualMethodFromBaseClass : 
ClassCallingVirtualMethodOnce {
+
+               public ClassCallingVirtualMethodFromBaseClass ()
+               {
+                       base.VirtualMethod ();
+               }
+       }
+
+       internal class ClassCallingVirtualPropertyFromBaseClass : 
ClassNotCallingVirtualMethods {
+
+               public ClassCallingVirtualPropertyFromBaseClass ()
+               {
+                       this.VirtualProperty = false;
+               }
+       }
+
+       internal class ClassIndirectlyCallingVirtualMethod {
+
+               public ClassIndirectlyCallingVirtualMethod ()
+               {
+                       NormalMethod ();
+               }
+
+               public virtual void VirtualMethod ()
+               {
+               }
+
+               private void NormalMethod ()
+               {
+                       VirtualMethod ();
+               }
+       }
+
+       internal class ClassIndirectlyCallingVirtualMethodFromBaseClass : 
ClassNotCallingVirtualMethods {
+
+               public ClassIndirectlyCallingVirtualMethodFromBaseClass ()
+               {
+                       Method ();
+               }
+
+               private void Method ()
+               {
+                       base.VirtualMethod ();
+               }
+       }
+
+       internal class ClassIndirectlyCallingVirtualPropertyFromBaseClass : 
ClassNotCallingVirtualMethods {
+
+               public ClassIndirectlyCallingVirtualPropertyFromBaseClass ()
+               {
+                       Method ();
+               }
+
+               public bool NonVirtualProperty {
+                       get { return false; }
+               }
+
+               private void Method ()
+               {
+                       base.VirtualProperty = NonVirtualProperty;
+               }
+       }
+
+       internal class ClassCallingVirtualMethodFromOtherClasses {
+
+               ClassIndirectlyCallingVirtualMethod test;
+
+               public ClassCallingVirtualMethodFromOtherClasses ()
+               {
+                       test = new ClassIndirectlyCallingVirtualMethod ();
+                       test.VirtualMethod ();
+               }
+       }
+
        [TestFixture]
        public class ConstructorShouldNotCallVirtualMethodsTest {
 
@@ -95,7 +257,26 @@
                        return assembly.MainModule.Types [typeof (T).FullName];
                }
 
+
                [Test]
+               public void TestClassWithStaticCtor ()
+               {
+                       Assert.IsNull (rule.CheckType 
(GetTest<ClassWithStaticCtor> (), runner));
+               }
+
+               [Test]
+               public void TestSealedClassWithVirtualCall ()
+               {
+                       Assert.IsNull (rule.CheckType 
(GetTest<SealedClassWithVirtualCall> (), runner));
+               }
+
+               [Test]
+               public void TestClassWithInstanceOfItself ()
+               {
+                       Assert.IsNull (rule.CheckType 
(GetTest<ClassWithInstanceOfItself> (), runner));
+               }
+
+               [Test]
                public void TestClassCallingVirtualMethodOnce ()
                {
                        MessageCollection messages = rule.CheckType 
(GetTest<ClassCallingVirtualMethodOnce> (), runner);
@@ -117,5 +298,60 @@
                        MessageCollection messages = rule.CheckType 
(GetTest<ClassNotCallingVirtualMethods> (), runner);
                        Assert.IsNull (messages);
                }
+
+               [Test]
+               public void TestClassCallingVirtualMethodFromBaseClass ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassCallingVirtualMethodFromBaseClass> (), runner);
+                       Assert.IsNotNull (messages);
+                       Assert.AreEqual (1, messages.Count);
+               }
+
+               [Test]
+               public void TestClassCallingVirtualPropertyFromBaseClass ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassCallingVirtualPropertyFromBaseClass> (), runner);
+                       Assert.IsNotNull (messages);
+                       Assert.AreEqual (1, messages.Count);
+               }
+
+               [Test]
+               public void TestClassIndirectlyCallingVirtualMethod ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassIndirectlyCallingVirtualMethod> (), runner);
+                       Assert.IsNotNull (messages);
+                       Assert.AreEqual (1, messages.Count);
+               }
+
+               [Test]
+               public void 
TestClassIndirectlyCallingVirtualMethodFromBaseClass ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassIndirectlyCallingVirtualMethodFromBaseClass> (), runner);
+                       Assert.IsNotNull (messages);
+                       Assert.AreEqual (1, messages.Count);
+               }
+
+               [Test]
+               public void 
TestClassIndirectlyCallingVirtualPropertyFromBaseClass ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassIndirectlyCallingVirtualPropertyFromBaseClass> (), runner);
+                       Assert.IsNotNull (messages);
+                       Assert.AreEqual (1, messages.Count);
+               }
+
+               [Test]
+               public void TestClassCallingVirtualMethodFromOtherClasses ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassCallingVirtualMethodFromOtherClasses> (), runner);
+                       Assert.IsNull (messages);
+               }
+
+               [Test]
+               public void TestClassWithRecursiveVirtualCall ()
+               {
+                       MessageCollection messages = rule.CheckType 
(GetTest<ClassWithRecursiveVirtualCall> (), runner);
+                       Assert.IsNotNull (messages);
+                       Assert.AreEqual (1, messages.Count);
+               }
        }
 }

Modified: 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
===================================================================
--- 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
  2008-01-24 00:01:40 UTC (rev 93744)
+++ 
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
  2008-01-24 00:01:57 UTC (rev 93745)
@@ -3,8 +3,10 @@
 //
 // Authors:
 //     Nidhi Rawal <[EMAIL PROTECTED]>
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
 //
 // Copyright (c) <2007> Nidhi Rawal
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to 
deal
@@ -32,13 +34,13 @@
 using Mono.Cecil;
 using NUnit.Framework;
 
-namespace Test.Rules.BadPractice
-{
+namespace Test.Rules.BadPractice {
+
        [TestFixture]
        public class EqualShouldHandleNullArgTest {
-               public class EqualsChecksForNullArg
-               {
-                       public override bool Equals (System.Object obj)
+
+               public class EqualsChecksForNullArg {
+                       public override bool Equals (object obj)
                        {
                                if (obj == null)
                                        return false;
@@ -51,10 +53,12 @@
                        }
                }
                
-               public class EqualsDoesNotCheckForNullArg
-               {
-                       public override bool Equals (System.Object obj)
+               public class EqualsDoesNotReturnFalseForNullArg {
+                       public override bool Equals (object obj)
                        {
+                               if (obj == null)
+                                       return true;
+
                                return this == obj;
                        }
                        public override int GetHashCode ()
@@ -63,13 +67,9 @@
                        }
                }
                
-               public class EqualsDoesNotReturnFalseForNullArg
-               {
-                       public override bool Equals (System.Object obj)
+               public class EqualsNotOverriddenNotCheckingNull {
+                       public bool Equals (object obj)
                        {
-                               if (obj == null)
-                                       return true;
-
                                return this == obj;
                        }
                        public override int GetHashCode ()
@@ -78,25 +78,34 @@
                        }
                }
                
-               public class EqualsNotOverriddenNotCheckingNull
-               {
-                       public bool Equals (System.Object obj)
+               public class EqualsNotOverriddenNotReturningFalseForNull {
+                       public new bool Equals (object obj)
                        {
-                               return this == obj;
+                               if (obj != null)
+                                       return this == obj;
+
+                               return true;
                        }
                        public override int GetHashCode ()
                        {
                                return 1;
                        }
                }
-               
-               public class EqualsNotOverriddenNotReturningFalseForNull
-               {
-                       public new bool Equals (System.Object obj)
+
+               public class EqualsReturnsFalse {
+                       public override bool Equals (object obj)
                        {
-                               if (obj != null)
-                                       return this == obj;
+                               return false;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
 
+               public class EqualsReturnsTrue {
+                       public override bool Equals (object obj)
+                       {
                                return true;
                        }
                        public override int GetHashCode ()
@@ -105,9 +114,11 @@
                        }
                }
 
-               public class EqualsReturnsFalse {
+               public struct EqualsUsingIsReturnFalse {
                        public override bool Equals (object obj)
                        {
+                               if (obj is EqualsUsingIsReturnFalse)
+                                       return Object.ReferenceEquals (this, 
obj);
                                return false;
                        }
                        public override int GetHashCode ()
@@ -115,19 +126,69 @@
                                return 1;
                        }
                }
+
+               public struct EqualsUsingIsReturnTrue {
+                       public override bool Equals (object obj)
+                       {
+                               if (obj is EqualsUsingIsReturnTrue)
+                                       return Object.ReferenceEquals (this, 
obj);
+                               return true;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+
+               public class EqualsCallBase : EqualsReturnsTrue {
+                       public override bool Equals (object obj)
+                       {
+                               return base.Equals (obj);
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+
+               public class EqualsCheckThis {
+                       // System.Object does this
+                       public override bool Equals (object obj)
+                       {
+                               return (this == obj);
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
+
+               public class EqualsCheckType {
+                       // common pattern in corlib
+                       public override bool Equals (object obj)
+                       {
+                               if (obj == null || GetType () != obj.GetType ())
+                                       return false;
+                               return true;
+                       }
+                       public override int GetHashCode ()
+                       {
+                               return 1;
+                       }
+               }
                
-               private IMethodRule methodRule;
+               private ITypeRule rule;
                private AssemblyDefinition assembly;
                private TypeDefinition type;
-               MessageCollection messageCollection;
+               private Runner runner;
 
                [TestFixtureSetUp]
                public void FixtureSetUp ()
                {
                        string unit = Assembly.GetExecutingAssembly ().Location;
                        assembly = AssemblyFactory.GetAssembly (unit);
-                       methodRule = new EqualShouldHandleNullArgRule ();
-                       messageCollection = null;
+                       rule = new EqualShouldHandleNullArgRule ();
+                       runner = new MinimalRunner ();
                }
                
                private TypeDefinition GetTest (string name)
@@ -140,69 +201,70 @@
                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);
+                       Assert.IsNull (rule.CheckType (type, runner));
                }
                
                [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);
+                       Assert.IsNotNull (rule.CheckType (type, runner));
                }
                
                [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);
+                       Assert.IsNull (rule.CheckType (type, runner));
                }
                
                [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);
+                       Assert.IsNull (rule.CheckType (type, runner));
                }
 
                [Test]
                public void EqualsReturnConstant ()
                {
                        type = GetTest ("EqualsReturnsFalse");
-                       foreach (MethodDefinition method in type.Methods) {
-                               switch (method.Name) {
-                               case "Equals":
-                                       Assert.IsNotNull 
(methodRule.CheckMethod (method, new MinimalRunner ()), method.Name);
-                                       break;
-                               default:
-                                       // otherwise rule does not apply
-                                       Assert.IsNull (methodRule.CheckMethod 
(method, new MinimalRunner ()), method.Name);
-                                       break;
-                               }
-                       }
+                       Assert.IsNull (rule.CheckType (type, runner), 
"EqualsReturnsFalse");
+
+                       type = GetTest ("EqualsReturnsTrue");
+                       Assert.IsNotNull (rule.CheckType (type, runner), 
"EqualsReturnsTrue");
                }
+
+               [Test]
+               public void EqualsUsingIs ()
+               {
+                       type = GetTest ("EqualsUsingIsReturnFalse");
+                       Assert.IsNull (rule.CheckType (type, runner), 
"EqualsUsingIsReturnFalse");
+
+                       type = GetTest ("EqualsUsingIsReturnTrue");
+                       Assert.IsNotNull (rule.CheckType (type, runner), 
"EqualsUsingIsReturnTrue");
+               }
+
+               [Test]
+               public void EqualsCallBaseClass ()
+               {
+                       type = GetTest ("EqualsCallBase");
+                       // we can't be sure so we shut up (else false positives 
gets really bad)
+                       Assert.IsNull (rule.CheckType (type, runner), 
"EqualsCallBase");
+               }
+
+               [Test]
+               public void EqualsCheckThisTest ()
+               {
+                       type = GetTest ("EqualsCheckThis");
+                       Assert.IsNull (rule.CheckType (type, runner), 
"EqualsCheckThis");
+               }
+
+               [Test]
+               public void EqualsCheckTypeTest ()
+               {
+                       type = GetTest ("EqualsCheckType");
+                       Assert.IsNull (rule.CheckType (type, runner), 
"EqualsCheckType");
+               }
        }
 }

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

Reply via email to