Author: spouliot
Date: 2008-02-14 12:35:40 -0500 (Thu, 14 Feb 2008)
New Revision: 95668

Added:
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs
Removed:
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/MinimalRunner.cs
Modified:
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/Defect.cs
   trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs
Log:
2008-02-14  Sebastien Pouliot  <[EMAIL PROTECTED]> 

        * Defect.cs: Remove IMetadataTokenProviderRock (moved to 
        CecilRocks.cs). Add more logic to find source code associated
        with the defect (even if we don't have the instruction for it).
        * MinimalRunner.cs: Removed.
        * Runner.cs: Make some stuff internal for TestRunner.cs
        * TestRunner.cs: New. Runner useful for unit testing as it has
        some internal knowledge about Runner state and provide helpers.



Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog    
2008-02-14 17:34:51 UTC (rev 95667)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/ChangeLog    
2008-02-14 17:35:40 UTC (rev 95668)
@@ -1,3 +1,13 @@
+2008-02-14  Sebastien Pouliot  <[EMAIL PROTECTED]> 
+
+       * Defect.cs: Remove IMetadataTokenProviderRock (moved to 
+       CecilRocks.cs). Add more logic to find source code associated
+       with the defect (even if we don't have the instruction for it).
+       * MinimalRunner.cs: Removed.
+       * Runner.cs: Make some stuff internal for TestRunner.cs
+       * TestRunner.cs: New. Runner useful for unit testing as it has
+       some internal knowledge about Runner state and provide helpers.
+
 2008-02-13  Sebastien Pouliot  <[EMAIL PROTECTED]> 
 
        * Defect.cs: Merge Defect and Defect<T> into a non-generic class.

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/Defect.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/Defect.cs    
2008-02-14 17:34:51 UTC (rev 95667)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/Defect.cs    
2008-02-14 17:35:40 UTC (rev 95668)
@@ -30,37 +30,10 @@
 using Mono.Cecil;
 using Mono.Cecil.Cil;
 
+using Gendarme.Framework.Rocks;
+
 namespace Gendarme.Framework {
 
-       // to be moved elsewhere
-       public static class IMetadataTokenProviderRock {
-
-               public static AssemblyDefinition GetAssembly (this 
IMetadataTokenProvider self)
-               {
-                       AssemblyDefinition ad = (self as AssemblyDefinition);
-                       if (ad != null)
-                               return ad;
-
-                       TypeDefinition td = (self as TypeDefinition);
-                       if (td != null)
-                               return td.Module.Assembly;
-
-                       MethodDefinition md = (self as MethodDefinition);
-                       if (md != null)
-                               return md.DeclaringType.Module.Assembly;
-
-                       FieldDefinition fd = (self as FieldDefinition);
-                       if (fd != null)
-                               return fd.DeclaringType.Module.Assembly;
-
-                       ParameterDefinition pd = (self as ParameterDefinition);
-                       if (pd != null)
-                               return pd.Method.DeclaringType.Module.Assembly;
-
-                       return null;
-               }
-       }
-
        public class Defect {
 
                private IRule rule;
@@ -117,23 +90,104 @@
                        get { return severity; }
                }
 
+               private Instruction ExtractFirst (TypeDefinition type)
+               {
+                       if (type == null)
+                               return null;
+                       foreach (MethodDefinition ctor in type.Constructors) {
+                               Instruction ins = ExtractFirst (ctor);
+                               if (ins != null)
+                                       return ins;
+                       }
+                       return null;
+               }
+
+               private Instruction ExtractFirst (MethodDefinition method)
+               {
+                       if ((method == null) || !method.HasBody)
+                               return null;
+                       Instruction ins = method.Body.Instructions [0];
+                       return (ins.SequencePoint != null) ? ins : null;
+               }
+
+               private TypeDefinition GetTypeFromLocation ()
+               {
+                       MethodDefinition method = (location as 
MethodDefinition);
+                       if (method != null)
+                               return (method.DeclaringType as TypeDefinition);
+
+                       FieldDefinition field = (location as FieldDefinition);
+                       if (field != null)
+                               return (field.DeclaringType as TypeDefinition);
+
+                       ParameterDefinition parameter = (location as 
ParameterDefinition);
+                       if (parameter != null)
+                               return (parameter.Method.DeclaringType as 
TypeDefinition);
+
+                       return (location as TypeDefinition);
+               }
+
+               private MethodDefinition GetMethodFromLocation ()
+               {
+                       ParameterDefinition parameter = (location as 
ParameterDefinition);
+                       if (parameter != null)
+                               return (parameter.Method as MethodDefinition);
+
+                       return (location as MethodDefinition);
+               }
+
                public string Source {
                        get {
-                               if (ins == null)
-                                       return String.Empty;
+                               if (ins != null) {
+                                       // try to find the closed sequence 
point for this instruction
+                                       Instruction search = ins;
+                                       while (search != null) {
+                                               if (search.SequencePoint != 
null) {
+                                                       // real details were 
provided
+                                                       return String.Format 
(CultureInfo.InvariantCulture, "{0}({1},{2})",
+                                                               
search.SequencePoint.Document.Url,
+                                                               
search.SequencePoint.StartLine,
+                                                               
search.SequencePoint.StartColumn);
+                                               }
+                                               search = search.Previous;
+                                       }
+                                       // no details, we only have the IL 
offset to report
+                                       return String.Format 
(CultureInfo.InvariantCulture, "debugging information unavailable, IL offset 
0x{0:x4}", ins.Offset);
+                               }
 
-                               // try to find the closed sequence point for 
this instruction
-                               Instruction search = ins;
-                               while (search != null) {
-                                       if (search.SequencePoint != null) {
-                                               return String.Format 
(CultureInfo.InvariantCulture, "{0}({1},{2})", 
-                                                       
search.SequencePoint.Document.Url, search.SequencePoint.StartLine, 
-                                                       
search.SequencePoint.StartColumn);
+                               // rule didn't provide an Instruction but we do 
our best to
+                               // find something since this is our only link 
to the source code
+
+                               Instruction candidate;
+                               TypeDefinition type = null;
+
+                               // MethodDefinition, ParameterDefinition
+                               //      return the method source file with 
(appromixate) line number
+                               MethodDefinition method = GetMethodFromLocation 
();
+                               if (method != null) {
+                                       candidate = ExtractFirst (method);
+                                       if (candidate != null) {
+                                               // we approximated the results 
(line - 1, no column)
+                                               // to get (close) to the method 
definition
+                                               return String.Format 
(CultureInfo.InvariantCulture, "{0}({1})",
+                                                       
candidate.SequencePoint.Document.Url,
+                                                       
candidate.SequencePoint.StartLine - 1);
                                        }
-                                       search = search.Previous;
+                                       // we may still be lucky to find the 
(a) source file for the type itself
+                                       type = (method.DeclaringType as 
TypeDefinition);
                                }
 
-                               return String.Format 
(CultureInfo.InvariantCulture, "IL offset: (0x{0:x4})", ins.Offset);
+                               // TypeDefinition, FieldDefinition
+                               //      return the type source file (based on 
the first ctor)
+                               if (type == null)
+                                       type = GetTypeFromLocation ();
+                               candidate = ExtractFirst (type);
+                               if (candidate != null) {
+                                       // we report only the source file of 
the first ctor (that reported something)
+                                       return 
(candidate.SequencePoint.Document.Url);
+                               }
+
+                               return String.Empty;
                        }
                }
 

Deleted: trunk/mono-tools/gendarme/framework/Gendarme.Framework/MinimalRunner.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/MinimalRunner.cs     
2008-02-14 17:34:51 UTC (rev 95667)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/MinimalRunner.cs     
2008-02-14 17:35:40 UTC (rev 95668)
@@ -1,31 +0,0 @@
-// 
-// Gendarme.Framework.MinimalRunner
-//
-// Authors:
-//     Aaron Tomb <[EMAIL PROTECTED]>
-//
-// Copyright (c) 2005 Aaron Tomb
-//
-// 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.
-
-namespace Gendarme.Framework {
-
-       public class MinimalRunner : Runner {
-       }
-}

Modified: trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs    
2008-02-14 17:34:51 UTC (rev 95667)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/Runner.cs    
2008-02-14 17:35:40 UTC (rev 95668)
@@ -47,8 +47,9 @@
                private IEnumerable<IAssemblyRule> assembly_rules;
                private IEnumerable<ITypeRule> type_rules;
                private IEnumerable<IMethodRule> method_rules;
-               private IRule currentRule;
-               private IMetadataTokenProvider currentTarget;
+               // needed for TestRunner, we'll eventually open our internals 
to a Gendarme test assembly
+               internal IRule currentRule;
+               internal IMetadataTokenProvider currentTarget;
 
                private int defectCountBeforeCheck;
 

Added: trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs
===================================================================
--- trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs        
2008-02-14 17:34:51 UTC (rev 95667)
+++ trunk/mono-tools/gendarme/framework/Gendarme.Framework/TestRunner.cs        
2008-02-14 17:35:40 UTC (rev 95668)
@@ -0,0 +1,84 @@
+// 
+// Gendarme.Framework.ITypeRule interface
+//
+// Authors:
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// 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
+// 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 Mono.Cecil;
+
+namespace Gendarme.Framework {
+
+       // need to be moved to another assembly dedicated to Gendarme unit 
+       // tests where Gendarme.Framework.dll internals are visible
+
+       // FIXME: 
http://code.google.com/p/google-highly-open-participation-mono/issues/detail?id=75
+
+       /// <summary>
+       /// To execute properly Gendarme.Framework.Runner keeps the state of
+       /// two variables internally, the current I[Assembly|Type|Method]Rule
+       /// and the current target (IMetadataTokenProvider to match the 
+       /// [Assembly|Module|Type|Method]Definition being analyzed). This 
+       /// class emulate this behavior and also reset the Defects count 
+       /// before each Check[Assembly|Type|Method] calls so we can easily
+       /// Assert on Defects.Count.
+       /// </summary>
+       public class TestRunner : Runner {
+
+               public TestRunner (IRule rule)
+               {
+                       currentRule = rule;
+                       currentRule.Initialize (this);
+               }
+/*
+               public IRule CurrentRule {
+                       get { return currentRule; }
+                       set { currentRule = value; }
+               }
+
+               public IMetadataTokenProvider CurrentTarget {
+                       get { return currentTarget; }
+                       set { currentTarget = value; }
+               }
+*/
+               public RuleResult CheckAssembly (AssemblyDefinition assembly)
+               {
+                       Reset ();
+                       currentTarget = assembly;
+                       return (currentRule as IAssemblyRule).CheckAssembly 
(assembly);
+               }
+
+               public RuleResult CheckType (TypeDefinition type)
+               {
+                       Reset ();
+                       currentTarget = type;
+                       return (currentRule as ITypeRule).CheckType (type);
+               }
+
+               public RuleResult CheckMethod (MethodDefinition method)
+               {
+                       Reset ();
+                       currentTarget = method;
+                       return (currentRule as IMethodRule).CheckMethod 
(method);
+               }
+       }
+}

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

Reply via email to