diff options
author | Néstor Salceda <nestor@mono-cvs.ximian.com> | 2008-02-15 21:11:53 +0300 |
---|---|---|
committer | Néstor Salceda <nestor@mono-cvs.ximian.com> | 2008-02-15 21:11:53 +0300 |
commit | edb87aac2df8f06636f16b123edaf0cc0801a58e (patch) | |
tree | 13b3767744687ac699ff58e825a1765b58c54a91 /gendarme/rules | |
parent | f97f486e7d7f49f7eaf4a0bb0beaa284f4e51edb (diff) |
2008-02-15 Nestor Salceda <nestor.salceda@gmail.com>
* Test/*.cs: Refactor the tests for accept the new rule's format.
* *.cs: Adapt the rules to the new framework.
* Makefile.am: Removed the -langversion:linq option. You should use at
least the 1.9 gmcs version to compile.
svn path=/trunk/mono-tools/; revision=95774
Diffstat (limited to 'gendarme/rules')
17 files changed, 194 insertions, 294 deletions
diff --git a/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSameClassRule.cs b/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSameClassRule.cs index 14177f73..898e42bc 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSameClassRule.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSameClassRule.cs @@ -27,34 +27,24 @@ // using System; -using System.Collections; -using System.Collections.Specialized; -using System.Text; using Mono.Cecil; -using Mono.Cecil.Cil; using Gendarme.Framework; namespace Gendarme.Rules.Smells { - public class AvoidCodeDuplicatedInSameClassRule : ITypeRule { - - public MessageCollection CheckType (TypeDefinition type, Runner runner) + [Problem ("There are same code structure in various methods in the same class. Your code will be better if you can unify them.")] + [Solution ("You should apply the Extract Method refactoring and invoke the method from the places.")] + public class AvoidCodeDuplicatedInSameClassRule : Rule, ITypeRule { + public RuleResult CheckType (TypeDefinition type) { - MessageCollection messageCollection = null; CodeDuplicatedLocator locator = new CodeDuplicatedLocator (); foreach (MethodDefinition current in type.Methods) { - foreach (Message message in locator.CompareMethodAgainstTypeMethods (current, type)) { - if (messageCollection == null) - messageCollection = new MessageCollection (); - messageCollection.Add (message); - } + locator.CompareMethodAgainstTypeMethods (this, current, type); locator.CheckedMethods.Add (current.Name); } - if (messageCollection == null || messageCollection.Count == 0) - return runner.RuleSuccess; - return messageCollection; + return Runner.CurrentRuleResult; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSiblingClassesRule.cs b/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSiblingClassesRule.cs index 19ee8cf3..972ee954 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSiblingClassesRule.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/AvoidCodeDuplicatedInSiblingClassesRule.cs @@ -4,7 +4,7 @@ // Authors: // Néstor Salceda <nestor.salceda@gmail.com> // -// (C) 2007 Néstor Salceda +// (C) 2007-2008 Néstor Salceda // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -30,26 +30,20 @@ using System; using System.Collections; using Mono.Cecil; -using Mono.Cecil.Cil; using Gendarme.Framework; namespace Gendarme.Rules.Smells { - - public class AvoidCodeDuplicatedInSiblingClassesRule : ITypeRule { - private MessageCollection messageCollection; + + [Problem ("There are same code structure in various methods in sibling classes. Your code will be better if you can unify them.")] + [Solution ("You can apply the Pull Up Method refactoring.")] + public class AvoidCodeDuplicatedInSiblingClassesRule : Rule, ITypeRule { private CodeDuplicatedLocator codeDuplicatedLocator; private void FindCodeDuplicated (TypeDefinition type, ICollection siblingClasses) { - foreach (MethodDefinition method in type.Methods) { - foreach (TypeDefinition sibling in siblingClasses) { - foreach (Message message in codeDuplicatedLocator.CompareMethodAgainstTypeMethods (method, sibling)) { - if (messageCollection == null) - messageCollection = new MessageCollection (); - messageCollection.Add (message); - } - } - } + foreach (MethodDefinition method in type.Methods) + foreach (TypeDefinition sibling in siblingClasses) + codeDuplicatedLocator.CompareMethodAgainstTypeMethods (this, method, sibling); } private void CompareSiblingClasses (ICollection siblingClasses) @@ -60,21 +54,15 @@ namespace Gendarme.Rules.Smells { } } - public MessageCollection CheckType (TypeDefinition type, Runner runner) + public RuleResult CheckType (TypeDefinition type) { - //As rules are once instantiated and MessageCollection - //is a field, then you will need initialize the - //messageCollection every time you check a type. - messageCollection = null; codeDuplicatedLocator = new CodeDuplicatedLocator (); ICollection siblingClasses = Utilities.GetInheritedClassesFrom (type); if (siblingClasses.Count >= 2) CompareSiblingClasses (siblingClasses); - if (messageCollection == null || messageCollection.Count == 0) - return runner.RuleSuccess; - return messageCollection; + return Runner.CurrentRuleResult; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/AvoidLargeClassesRule.cs b/gendarme/rules/Gendarme.Rules.Smells/AvoidLargeClassesRule.cs index 987de535..e08d2bf3 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/AvoidLargeClassesRule.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/AvoidLargeClassesRule.cs @@ -4,7 +4,7 @@ // Authors: // Néstor Salceda <nestor.salceda@gmail.com> // -// (C) 2007 Néstor Salceda +// (C) 2007-2008 Néstor Salceda // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -37,9 +37,10 @@ namespace Gendarme.Rules.Smells { //SUGGESTION: Public / Private ratios. //SUGGESTION: Lines of Code. //SUGGESTION: Weird distributions. - public class AvoidLargeClassesRule : ITypeRule { + [Problem ("The class is trying to do too much. Generally if a class is too large, duplicated code will not be far.")] + [Solution ("You can apply the Extract Class or Extract Subclass refactoring.")] + public class AvoidLargeClassesRule : Rule,ITypeRule { - private MessageCollection messageCollection; private static int maxFields = 25; public static int MaxFields { @@ -67,19 +68,12 @@ namespace Gendarme.Rules.Smells { return counter >= MaxFields; } - private void CheckForClassFields (TypeDefinition type) + private RuleResult CheckForClassFields (TypeDefinition type) { - if (IsTooLarge (type)) - AddMessage (type, "This class contains a lot of fields. This is a sign for the Large Class Smell", MessageType.Error); - } - - private void AddMessage (TypeDefinition type, string summary, MessageType messageType) - { - Location location = new Location (type); - Message message = new Message (summary, location, messageType); - if (messageCollection == null) - messageCollection = new MessageCollection (); - messageCollection.Add (message); + if (IsTooLarge (type)) { + Runner.Report (type, Severity.High, Confidence.High, "This type contains a lot of fields."); + } + return RuleResult.Success; } private static bool HasPrefixedFields (string prefix, TypeDefinition type) @@ -135,7 +129,7 @@ namespace Gendarme.Rules.Smells { return String.Empty; } - private static bool ExitsCommonPrefixes (TypeDefinition type) + private bool ExitsCommonPrefixes (TypeDefinition type) { foreach (FieldDefinition field in type.Fields) { // skip special, constant and read-only fields @@ -146,34 +140,30 @@ namespace Gendarme.Rules.Smells { continue; string prefix = GetFieldPrefix (field); - if (HasPrefixedFields (prefix, type)) + if (HasPrefixedFields (prefix, type)) { + Runner.Report (type, Severity.Medium, Confidence.High, "This type contains common prefixes."); return true; + } } return false; } - private void CheckForCommonPrefixesInFields (TypeDefinition type) + private RuleResult CheckForCommonPrefixesInFields (TypeDefinition type) { if (ExitsCommonPrefixes (type)) - AddMessage (type, "This type contains some fields with the same prefix. Although this isn't bad, it's a sign for extract a class, for avoid the Large Class smell.", MessageType.Warning); + return RuleResult.Failure; + return RuleResult.Success; } - public MessageCollection CheckType (TypeDefinition type, Runner runner) + public RuleResult CheckType (TypeDefinition type) { - messageCollection = null; - - if (type.IsEnum) - return runner.RuleSuccess; - - if (type.IsGeneratedCode ()) - return runner.RuleSuccess; + if (type.IsEnum || type.IsGeneratedCode ()) + return RuleResult.DoesNotApply; CheckForClassFields (type); CheckForCommonPrefixesInFields (type); - if (messageCollection == null || messageCollection.Count == 0) - return runner.RuleSuccess; - return messageCollection; + return Runner.CurrentRuleResult; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs b/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs index 93eb2630..2b2dbac1 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/AvoidLongMethodsRule.cs @@ -4,7 +4,7 @@ // Authors: // Néstor Salceda <nestor.salceda@gmail.com> // -// (C) 2007 Néstor Salceda +// (C) 2007-2008 Néstor Salceda // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -36,8 +36,9 @@ using Gendarme.Framework; using Gendarme.Framework.Rocks; namespace Gendarme.Rules.Smells { - - public class AvoidLongMethodsRule : IMethodRule { + [Problem ("Long methods are usually hard to understand and maintain. The method can cause problems because contains more code than the maximum allowed.")] + [Solution ("You should apply an Extract Method refactoring, but there are other solutions.")] + public class AvoidLongMethodsRule : Rule,IMethodRule { private int maxInstructions = 170; const int AsignationRatio = 7; const int DefaultAmountOfElements = 13; @@ -105,39 +106,32 @@ namespace Gendarme.Rules.Smells { return counter; } - public MessageCollection CheckMethod (MethodDefinition method, Runner runner) + public RuleResult CheckMethod (MethodDefinition method) { // rule does not apply if method as no code (e.g. abstract, p/invoke) - if (!method.HasBody) - return runner.RuleSuccess; - // rule does not apply to code outside the developer's control - if (method.IsGeneratedCode ()) - return runner.RuleSuccess; - - // rule does not apply to autogenerated code from some + // rule dess not apply to autogenerated code from some // tools - if (IsAutogeneratedByTools (method)) - return runner.RuleSuccess; - + if (!method.HasBody || method.IsGeneratedCode () || IsAutogeneratedByTools (method)) + return RuleResult.DoesNotApply; + // rule applies! // success if the instruction count is below the defined threshold if (method.IsConstructor && method.IsStatic) { if (method.Body.Instructions.Count <= MaxInstructions + (CountStaticFields (method.DeclaringType as TypeDefinition) * AsignationRatio)) - return runner.RuleSuccess; + return RuleResult.Success; } else if (method.IsConstructor && method.IsStatic) { if (method.Body.Instructions.Count <= MaxInstructions + (CountInstanceFields (method.DeclaringType as TypeDefinition) * AsignationRatio)) - return runner.RuleSuccess; + return RuleResult.Success; } else { if (method.Body.Instructions.Count <= MaxInstructions) - return runner.RuleSuccess; + return RuleResult.Success; } - Location location = new Location (method); - Message message = new Message ("The method is too long.", location, MessageType.Error); - return new MessageCollection (message); + Runner.Report (method, Severity.High, Confidence.Normal, "This method is long."); + return RuleResult.Failure; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/AvoidLongParameterListsRule.cs b/gendarme/rules/Gendarme.Rules.Smells/AvoidLongParameterListsRule.cs index 3883c563..65792015 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/AvoidLongParameterListsRule.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/AvoidLongParameterListsRule.cs @@ -29,7 +29,6 @@ using System; using Mono.Cecil; -using Mono.Cecil.Cil; using Gendarme.Framework; namespace Gendarme.Rules.Smells { @@ -37,7 +36,9 @@ namespace Gendarme.Rules.Smells { //uncommon. //SUGGESTION: Different value for public / private / protected methods *may* //be useful. - public class AvoidLongParameterListsRule : IMethodRule { + [Problem ("Generally, long parameter lists are hard to understand because they become hard to use and inconsistent. And you will be forever changing them if you need more data.")] + [Solution ("You should apply the Replace parameter with method refactoring, or preserve whole object or introduce parameter object")] + public class AvoidLongParameterListsRule : Rule,IMethodRule { private int maxParameters = 6; public int MaxParameters { @@ -80,22 +81,17 @@ namespace Gendarme.Rules.Smells { return method.Parameters.Count >= MaxParameters; } - public MessageCollection CheckMethod (MethodDefinition method, Runner runner) + public RuleResult CheckMethod (MethodDefinition method) { // we don't control, nor report, p/invoke declarations - someimes the poor C // guys don't have a choice to make long parameter lists ;-) if (method.IsPInvokeImpl) - return runner.RuleSuccess; + return RuleResult.DoesNotApply; - if (HasLongParameterList (method)) { - MessageCollection messageCollection = new MessageCollection (); - Location location = new Location (method); - Message message = new Message ("The method contains a long parameter list.",location, MessageType.Error); - messageCollection.Add (message); - return messageCollection; - } + if (HasLongParameterList (method)) + Runner.Report (method, Severity.Medium, Confidence.Normal, "This method contains a long parameter list."); - return runner.RuleSuccess; + return Runner.CurrentRuleResult; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/AvoidSpeculativeGeneralityRule.cs b/gendarme/rules/Gendarme.Rules.Smells/AvoidSpeculativeGeneralityRule.cs index 09ef1611..9d9496e2 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/AvoidSpeculativeGeneralityRule.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/AvoidSpeculativeGeneralityRule.cs @@ -36,28 +36,20 @@ using Gendarme.Framework.Rocks; using Gendarme.Rules.Performance; namespace Gendarme.Rules.Smells { - public class AvoidSpeculativeGeneralityRule : ITypeRule { + [Problem ("If you will need the feature in the future then you should implement it in the future.")] + [Solution ("You can apply various refactorings: Collapse Hierarchy, Inline Class, Remove Parameter or Rename Method.")] + public class AvoidSpeculativeGeneralityRule : Rule, ITypeRule { - private MessageCollection messageCollection; private void CheckAbstractClassWithoutResponsability (TypeDefinition type) { if (type.IsAbstract) { ICollection inheritedClasses = Utilities.GetInheritedClassesFrom (type); if (inheritedClasses.Count == 1) - AddMessage (type, "This abstract class only has one class inheritting from. The abstract classes without responsability are a sign for the Speculative Generality smell."); + Runner.Report (type, Severity.Medium, Confidence.Normal, "This abstract class has only one class inheritting from. The abstract classes without responsability are a sign for the Speculative Generality smell."); } } - private void AddMessage (TypeDefinition type, string summary) - { - Location location = new Location (type); - Message message = new Message (summary, location, MessageType.Error); - if (messageCollection == null) - messageCollection = new MessageCollection (); - messageCollection.Add (message); - } - //return true if the method only contains only a single call. private static bool OnlyDelegatesCall (MethodDefinition method) { @@ -95,59 +87,44 @@ namespace Gendarme.Rules.Smells { private void CheckUnnecesaryDelegation (TypeDefinition type) { if (MostlyMethodsDelegatesCall (type) && InheritsOnlyFromObject (type)) - AddMessage (type, "This class contains a lot of methods that only delgates the call to other. This kind of Delegation could be a sign for Speculative Generality"); + Runner.Report (type, Severity.Medium, Confidence.Normal, "This class contains a lot of methods that only delegates the call to other. This king of Delegation could be a sign for Speculative Generality"); } - private static bool AvoidUnusedParametersRuleScheduled (Runner runner) + private bool AvoidUnusedParametersRuleScheduled () { - foreach (IMethodRule rule in runner.Rules.Method) { + foreach (IMethodRule rule in Runner.Rules) { if (rule is AvoidUnusedParametersRule) return true; } return false; } - private void AddExistingMessages (MessageCollection existingMessages) { - if (existingMessages == null) - return; - - foreach (Message violation in existingMessages) { - Message message = new Message ("This method contains unused parameters. This is a sign for the Speculative Generality smell.",violation.Location, MessageType.Error); - if (messageCollection == null) - messageCollection = new MessageCollection (); - messageCollection.Add (message); - } - } - - private void CheckMethods (IMethodRule rule, ICollection methods, Runner runner) + private void CheckMethods (IMethodRule rule, ICollection methods) { foreach (MethodDefinition method in methods) { - AddExistingMessages (rule.CheckMethod (method, runner)); + rule.CheckMethod (method); } } - private void CheckUnusedParameters (TypeDefinition type, Runner runner) + private void CheckUnusedParameters (TypeDefinition type) { IMethodRule avoidUnusedParameters = new AvoidUnusedParametersRule (); - CheckMethods (avoidUnusedParameters, type.Methods, runner); - CheckMethods (avoidUnusedParameters, type.Constructors, runner); + avoidUnusedParameters.Initialize (Runner); + CheckMethods (avoidUnusedParameters, type.Methods); + CheckMethods (avoidUnusedParameters, type.Constructors); } - public MessageCollection CheckType (TypeDefinition type, Runner runner) + public RuleResult CheckType (TypeDefinition type) { if (type.IsGeneratedCode ()) - return runner.RuleSuccess; - - messageCollection = null; - + return RuleResult.DoesNotApply; + CheckAbstractClassWithoutResponsability (type); - if (!AvoidUnusedParametersRuleScheduled (runner)) - CheckUnusedParameters (type, runner); + if (!AvoidUnusedParametersRuleScheduled ()) + CheckUnusedParameters (type); CheckUnnecesaryDelegation (type); - if (messageCollection == null || messageCollection.Count == 0) - return runner.RuleSuccess; - return messageCollection; + return Runner.CurrentRuleResult; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/ChangeLog b/gendarme/rules/Gendarme.Rules.Smells/ChangeLog index 390a33fd..5a98072d 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/ChangeLog +++ b/gendarme/rules/Gendarme.Rules.Smells/ChangeLog @@ -1,3 +1,10 @@ +2008-02-15 Nestor Salceda <nestor.salceda@gmail.com> + + * Test/*.cs: Refactor the tests for accept the new rule's format. + * *.cs: Adapt the rules to the new framework. + * Makefile.am: Removed the -langversion:linq option. You should use at + least the 1.9 gmcs version to compile. + 2008-02-07 Nestor Salceda <nestor.salceda@gmail.com> * AvoidLargeClassesRule.cs: Avoid positive when a class contains diff --git a/gendarme/rules/Gendarme.Rules.Smells/CodeDuplicatedLocator.cs b/gendarme/rules/Gendarme.Rules.Smells/CodeDuplicatedLocator.cs index 2ab3087c..2b8300b2 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/CodeDuplicatedLocator.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/CodeDuplicatedLocator.cs @@ -4,7 +4,7 @@ // Authors: // Néstor Salceda <nestor.salceda@gmail.com> // -// (C) 2007 Néstor Salceda +// (C) 2007-2008 Néstor Salceda // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the @@ -97,19 +97,18 @@ namespace Gendarme.Rules.Smells { return false; } - internal MessageCollection CompareMethodAgainstTypeMethods (MethodDefinition currentMethod, TypeDefinition targetTypeDefinition) + internal bool CompareMethodAgainstTypeMethods (Rule rule, MethodDefinition currentMethod, TypeDefinition targetTypeDefinition) { - MessageCollection messageCollection = new MessageCollection (); + bool containsDuplicated = false; if (!CheckedTypes.Contains (targetTypeDefinition.Name)) { foreach (MethodDefinition targetMethod in targetTypeDefinition.Methods) { if (ContainsDuplicatedCode (currentMethod, targetMethod)) { - Location location = new Location (currentMethod); - Message message = new Message (String.Format ("Exists code duplicated with {0}.{1}", targetTypeDefinition.Name, targetMethod.Name), location, MessageType.Error); - messageCollection.Add (message); + rule.Runner.Report (currentMethod, Severity.High, Confidence.Normal,String.Format ("Exists code duplicated with {0}.{1}", targetTypeDefinition.Name, targetMethod.Name)); + containsDuplicated = true; } } } - return messageCollection; + return containsDuplicated; } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/Expression.cs b/gendarme/rules/Gendarme.Rules.Smells/Expression.cs index 1ca3be77..f1ee9678 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Expression.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Expression.cs @@ -4,7 +4,7 @@ // Authors: // Néstor Salceda <nestor.salceda@gmail.com> // -// (C) 2007 Néstor Salceda +// (C) 2007-2008 Néstor Salceda // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/gendarme/rules/Gendarme.Rules.Smells/ExpressionFillerVisitor.cs b/gendarme/rules/Gendarme.Rules.Smells/ExpressionFillerVisitor.cs index e9c34d34..31923629 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/ExpressionFillerVisitor.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/ExpressionFillerVisitor.cs @@ -4,7 +4,7 @@ // Authors: // Néstor Salceda <nestor.salceda@gmail.com> // -// (C) 2007 Néstor Salceda +// (C) 2007-2008 Néstor Salceda // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the diff --git a/gendarme/rules/Gendarme.Rules.Smells/Makefile.am b/gendarme/rules/Gendarme.Rules.Smells/Makefile.am index 99cc7e52..73385593 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Makefile.am +++ b/gendarme/rules/Gendarme.Rules.Smells/Makefile.am @@ -10,28 +10,39 @@ DISTCLEANFILES = Makefile.in Gendarme.Rules.Smells.xml TestResult.xml smells_rules_sources_in = ../../AssemblyInfo.cs.in smells_rules_generated_sources = $(smells_rules_sources_in:.in=) -smells_rules_sources = AvoidCodeDuplicatedInSameClassRule.cs AvoidCodeDuplicatedInSiblingClassesRule.cs \ -AvoidLargeClassesRule.cs AvoidLongMethodsRule.cs AvoidLongParameterListsRule.cs \ -AvoidSpeculativeGeneralityRule.cs CodeDuplicatedLocator.cs Expression.cs \ -ExpressionFillerVisitor.cs Utilities.cs +smells_rules_sources = \ + AvoidLargeClassesRule.cs \ + AvoidLongMethodsRule.cs \ + AvoidLongParameterListsRule.cs \ + Expression.cs \ + ExpressionFillerVisitor.cs \ + CodeDuplicatedLocator.cs \ + AvoidCodeDuplicatedInSameClassRule.cs \ + AvoidCodeDuplicatedInSiblingClassesRule.cs \ + Utilities.cs \ + AvoidSpeculativeGeneralityRule.cs smells_rules_build_sources = $(addprefix $(srcdir)/, $(smells_rules_sources)) smells_rules_build_sources += $(smells_rules_generated_sources) ../../bin/Gendarme.Rules.Smells.dll: $(smells_rules_build_sources) - $(GMCS) -debug -target:library -langversion:linq -r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll -r:../../bin/Gendarme.Framework.dll \ - -r:../../bin/Gendarme.Rules.Performance.dll -out:$@ $(smells_rules_build_sources) + $(GMCS) -debug -target:library -r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll -r:../../bin/Gendarme.Framework.dll -r:../../bin/Gendarme.Rules.Performance.dll\ + -out:$@ $(smells_rules_build_sources) cp Gendarme.Rules.*.xml ../../bin/ -smells_test_sources = AvoidCodeDuplicatedInSameClassTest.cs AvoidCodeDuplicatedInSiblingClassesTest.cs \ -AvoidLargeClassesTest.cs AvoidLongMethodsTest.cs AvoidLongParameterListsTest.cs \ -AvoidSpeculativeGeneralityTest.cs +smells_test_sources = \ + AvoidCodeDuplicatedInSameClassTest.cs \ + AvoidCodeDuplicatedInSiblingClassesTest.cs \ + AvoidLargeClassesTest.cs \ + AvoidLongMethodsTest.cs \ + AvoidLongParameterListsTest.cs \ + AvoidSpeculativeGeneralityTest.cs smells_test_build_sources = $(addprefix $(srcdir)/Test/, $(smells_test_sources)) Test.Rules.Smells.dll: $(smells_rules_build_sources) $(smells_rules_SCRIPTS) - $(GMCS) -target:library -langversion:linq -r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll -pkg:mono-nunit -r:../../bin/Gendarme.Framework.dll \ - -r:$(smells_rules_SCRIPTS) -r:System.Windows.Forms.dll -out:$@ $(smells_test_build_sources) + $(GMCS) -target:library -r:$(top_builddir)/gendarme/bin/Mono.Cecil.dll -pkg:mono-nunit -r:../../bin/Gendarme.Framework.dll \ + -r:$(smells_rules_SCRIPTS) -r:System.Windows.Forms.dll -out:$@ $(smells_test_build_sources) test: Test.Rules.Smells.dll diff --git a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSameClassTest.cs b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSameClassTest.cs index dbfc4c16..cfdf8b90 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSameClassTest.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSameClassTest.cs @@ -185,7 +185,7 @@ namespace Test.Rules.Smells { private ITypeRule rule; private AssemblyDefinition assembly; private TypeDefinition type; - private MessageCollection messageCollection; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -193,34 +193,22 @@ namespace Test.Rules.Smells { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); rule = new AvoidCodeDuplicatedInSameClassRule (); - messageCollection = null; + runner = new TestRunner (rule); } - private void CheckMessageType (MessageCollection messageCollection, MessageType messageType) - { - IEnumerator enumerator = messageCollection.GetEnumerator (); - if (enumerator.MoveNext ()) { - Message message = (Message) enumerator.Current; - Assert.AreEqual (message.Type, messageType); - } - } - [Test] public void TestClassWithoutCodeDuplicated () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithoutCodeDuplicated"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } [Test] public void TestClassWithCodeDuplicated () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithCodeDuplicated"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (3, messageCollection.Count); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (3, runner.Defects.Count); } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSiblingClassesTest.cs b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSiblingClassesTest.cs index c62f2f0b..8299a8b4 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSiblingClassesTest.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidCodeDuplicatedInSiblingClassesTest.cs @@ -93,7 +93,7 @@ namespace Test.Rules.Smells { private ITypeRule rule; private AssemblyDefinition assembly; private TypeDefinition type; - private MessageCollection messageCollection; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -101,24 +101,22 @@ namespace Test.Rules.Smells { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); rule = new AvoidCodeDuplicatedInSiblingClassesRule (); - messageCollection = null; + runner = new TestRunner (rule); } [Test] public void BaseClassWithCodeDuplicatedTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.BaseClassWithCodeDuplicated"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void BaseClassWithoutCodeDuplicatedTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.BaseClassWithoutCodeDuplicated"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLargeClassesTest.cs b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLargeClassesTest.cs index 606cceb6..1d910691 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLargeClassesTest.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLargeClassesTest.cs @@ -113,7 +113,7 @@ namespace Test.Rules.Smells { private ITypeRule rule; private AssemblyDefinition assembly; private TypeDefinition type; - private MessageCollection messageCollection; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -121,24 +121,22 @@ namespace Test.Rules.Smells { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); rule = new AvoidLargeClassesRule (); - messageCollection = null; + runner = new TestRunner (rule); } [Test] public void LargeClassTest () { type = assembly.MainModule.Types["Test.Rules.Smells.LargeClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (2, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (2, runner.Defects.Count); } [Test] public void NotLargeClassTest () { type = assembly.MainModule.Types["Test.Rules.Smells.NotLargeClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } @@ -146,72 +144,66 @@ namespace Test.Rules.Smells { public void ConstantClassTest () { type = assembly.MainModule.Types["Test.Rules.Smells.ConstantClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } - [Test] public void ClassWithPrefixedFieldsWithCamelCasingTest () { type = assembly.MainModule.Types["Test.Rules.Smells.ClassWithPrefixedFieldsWithCamelCasing"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void ClassWithoutPrefixedFieldsWithMDashCasingTest () { type = assembly.MainModule.Types["Test.Rules.Smells.ClassWithoutPrefixedFieldsWithMDashCasing"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } [Test] public void ClassWithPrefixedFieldsWithDashCasingTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithPrefixedFieldsWithDashCasing"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void ClassWithPrefixedFieldsWithMDashCasingTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithPrefixedFieldsWithMDashCasing"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void ClassWithSomeConstantsFields () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithSomeConstantsFields"]; - Assert.IsNull (rule.CheckType (type, new MinimalRunner ())); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } [Test] public void ClassWithSomeReadOnlyFields () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithSomeReadOnlyFields"]; - Assert.IsNull (rule.CheckType (type, new MinimalRunner ())); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } [Test] public void ClassWithAutoImplementedProperties () { type = assembly.MainModule.Types ["Test.Rules.Smells.AutoImplementedPropertiesClass"]; - Assert.IsNull (rule.CheckType (type, new MinimalRunner ())); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } [Test] public void EnumsShouldNotBeCheckedTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.LargeEnum"]; - Assert.IsNull (rule.CheckType (type, new MinimalRunner ())); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckType (type)); } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs index 568d8cc6..1bde240b 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongMethodsTest.cs @@ -785,7 +785,7 @@ namespace Test.Rules.Smells { private IMethodRule rule; private AssemblyDefinition assembly; private MethodDefinition method; - private MessageCollection messageCollection; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -793,7 +793,7 @@ namespace Test.Rules.Smells { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); rule = new AvoidLongMethodsRule (); - messageCollection = null; + runner = new TestRunner (rule); } public void LongMethod () @@ -1046,121 +1046,106 @@ namespace Test.Rules.Smells { public void LongMethodTest () { method = GetMethodForTest ("LongMethod"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void EmptyMethodTest () { method = GetMethodForTest ("EmptyMethod"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void ShortMethodTest () { method = GetMethodForTest ("ShortMethod"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void FalseBuildMethodTest () { method = GetMethodForTest ("Build"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure ,runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } - + [Test] public void WidgetBuildMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainWidget", "Build"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckMethod (method)); } [Test] public void WidgetInitializeComponentMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainWidget", "InitializeComponent"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } - [Test] public void DialogBuildMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainDialog", "Build"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckMethod (method)); } [Test] public void DialogInitializeComponentMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainDialog", "InitializeComponent"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure ,runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void WindowBuildMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainWindow", "Build"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.DoesNotApply ,rule.CheckMethod (method)); } [Test] public void WindowInitializeComponentMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainWindow", "InitializeComponent"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure ,runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } - + [Test] public void FalseInitializeComponentTest () { method = GetMethodForTest ("InitializeComponent"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void FormInitializeComponentTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainForm", "InitializeComponent"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckMethod (method)); } [Test] public void FormBuildMethodTest () { method = GetMethodForTestFrom ("Test.Rules.Smells.MainForm", "Build"); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure ,runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void LongStaticConstructorWithoutFieldsTest () { MethodDefinition staticConstructor = assembly.MainModule.Types["Test.Rules.Smells.LongStaticConstructorWithoutFields"].Constructors.GetConstructor (true,Type.EmptyTypes); - messageCollection = rule.CheckMethod (staticConstructor, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (staticConstructor)); + Assert.AreEqual (1, runner.Defects.Count); } @@ -1168,8 +1153,7 @@ namespace Test.Rules.Smells { public void LongStaticConstructorWithFieldsTest () { MethodDefinition staticConstructor = assembly.MainModule.Types["Test.Rules.Smells.LongStaticConstructorWithFields"].Constructors.GetConstructor (true,Type.EmptyTypes); - messageCollection = rule.CheckMethod (staticConstructor, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (staticConstructor)); } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongParameterListsTest.cs b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongParameterListsTest.cs index 8857a2de..58afd4b2 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongParameterListsTest.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidLongParameterListsTest.cs @@ -42,7 +42,7 @@ namespace Test.Rules.Smells { private AssemblyDefinition assembly; private MethodDefinition method; private TypeDefinition type; - private MessageCollection messageCollection; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -51,7 +51,7 @@ namespace Test.Rules.Smells { assembly = AssemblyFactory.GetAssembly (unit); type = assembly.MainModule.Types["Test.Rules.Smells.AvoidLongParameterListsTest"]; rule = new AvoidLongParameterListsRule (); - messageCollection = null; + runner = new TestRunner (rule); } private MethodDefinition GetMethodForTest (string methodName, Type[] parameterTypes) @@ -103,74 +103,66 @@ namespace Test.Rules.Smells { public void MethodWithoutParametersTest () { method = GetMethodForTest ("MethodWithoutParameters", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void MethodwithLongParameterListTest () { method = GetMethodForTest ("MethodWithLongParameterList", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void MethodWithoutLongParameterList () { method = GetMethodForTest ("MethodWithoutLongParameterList", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void OverloadedMethodTest () { method = GetMethodForTest ("OverloadedMethod", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void OverloadedMethodWithParametersTest () { method = GetMethodForTest ("OverloadedMethod", new Type[] {typeof (int)}); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void LongOverloadedMethodWithLittleOverloadTest () { method = GetMethodForTest ("OverloadedMethod", new Type[] {typeof (int), typeof (char), typeof (object), typeof (bool), typeof (string), typeof (float), typeof (double), typeof (short), typeof (int), typeof (string[])}); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method)); } [Test] public void LongOverloadedMethodWithoutLittleOverloadTest () { method = GetMethodForTest ("OtherOverloaded", new Type[] {typeof (int), typeof (char), typeof (object), typeof (bool), typeof (string), typeof (float), typeof (double)}); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void OtherLongOverloadedMethodWithoutLittleOverloadTest () { method = GetMethodForTest ("OtherOverloaded", new Type[] {typeof (int), typeof (char), typeof (object), typeof (bool), typeof (string), typeof (float), typeof (double), typeof (short)}); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void IgnoreExternalMethods () { method = GetMethodForTest ("GdipCreateLineBrushFromRectI", new Type[] {typeof (object), typeof (int), typeof (int), typeof (int), typeof (int), typeof (IntPtr) }); - Assert.IsNull (rule.CheckMethod (method, new MinimalRunner ())); + Assert.AreEqual (RuleResult.DoesNotApply ,runner.CheckMethod (method)); } } } diff --git a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidSpeculativeGeneralityTest.cs b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidSpeculativeGeneralityTest.cs index a3de3386..441d6774 100644 --- a/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidSpeculativeGeneralityTest.cs +++ b/gendarme/rules/Gendarme.Rules.Smells/Test/AvoidSpeculativeGeneralityTest.cs @@ -128,7 +128,7 @@ namespace Test.Rules.Smells { private ITypeRule rule; private AssemblyDefinition assembly; private TypeDefinition type; - private MessageCollection messageCollection; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -136,59 +136,53 @@ namespace Test.Rules.Smells { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); rule = new AvoidSpeculativeGeneralityRule (); - messageCollection = null; + runner = new TestRunner (rule); } [Test] public void AbstractClassesWithoutResponsabilityTest () { type = assembly.MainModule.Types["Test.Rules.Smells.AbstractClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void AbstractClassesWithResponsabilityTest () { type = assembly.MainModule.Types["Test.Rules.Smells.OtherAbstractClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } [Test] public void ClassWithUnusedParameterTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithUnusedParameter"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void ClassWithFourUnusedParametersTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.ClassWithFourUnusedParameters"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (4, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (4, runner.Defects.Count); } [Test] public void ClassWithUnnecessaryDelegationTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.UnnecessaryDelegatedClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type)); + Assert.AreEqual (1, runner.Defects.Count); } [Test] public void ClassWithoutUnnecessaryDelegationTest () { type = assembly.MainModule.Types ["Test.Rules.Smells.NotUnnecessaryDelegatedClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type)); } } } |