diff options
author | Sebastien Pouliot <sebastien@ximian.com> | 2008-02-15 21:25:52 +0300 |
---|---|---|
committer | Sebastien Pouliot <sebastien@ximian.com> | 2008-02-15 21:25:52 +0300 |
commit | 5aa5c5f91fd895a7afad07098cdbafc629ffe2af (patch) | |
tree | 060a004bdcf56cc99dca5c411c55aede9c26b11a /gendarme/rules | |
parent | fcc74c28467c3a3ddb2641f5f90a6de9cb45918e (diff) |
2008-02-15 Sebastien Pouliot <sebastien@ximian.com>
* AvoidConstructorsInStaticTypesTest.cs
* BadRecursiveInvocationTest.cs
* CallingEqualsWithNullArgTest.cs
* DontCompareWithNaNTest.cs
* FloatComparisonRuleTest.cs
* MethodCanBeMadeStaticTest.cs
* UseValueInPropertySetterTest.cs:
Update unit tests wrt framework changes.
svn path=/trunk/mono-tools/; revision=95782
Diffstat (limited to 'gendarme/rules')
8 files changed, 235 insertions, 180 deletions
diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/AvoidConstructorsInStaticTypesTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/AvoidConstructorsInStaticTypesTest.cs index d3be0ffd..d325a2da 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/AvoidConstructorsInStaticTypesTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/AvoidConstructorsInStaticTypesTest.cs @@ -3,8 +3,10 @@ // // Authors: // Lukasz Knop <lukasz.knop@gmail.com> +// Sebastien Pouliot <sebastien@ximian.com> // // Copyright (C) 2007 Lukasz Knop +// 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 @@ -34,55 +36,50 @@ using Gendarme.Rules.Correctness; using Mono.Cecil; using NUnit.Framework; +namespace Test.Rules.Correctness { -namespace Test.Rules.Correctness -{ + [TestFixture] + public class AvoidConstructorsInStaticTypesTest { + public class CannotBeMadeStatic { - [TestFixture] - public class AvoidConstructorsInStaticTypesTest - { - public class CannotBeMadeStatic - { public void Method() { - } } - public class CanBeMadeStatic - { - public static void Method() + public class CanBeMadeStatic { + + public static void Method () { } } - public static class IsStatic - { - public static void Method() + public static class IsStatic { + + public static void Method () { } } - public class IsMadeStatic - { - private IsMadeStatic() + public class IsMadeStatic { + + private IsMadeStatic () { } - public static void Method() + public static void Method () { } } - public class EmptyClass - { + public class EmptyClass { } private ITypeRule rule; + private TestRunner runner; private AssemblyDefinition assembly; private TypeDefinition type; - private MessageCollection messageCollection; [TestFixtureSetUp] public void FixtureSetUp() @@ -90,49 +87,47 @@ namespace Test.Rules.Correctness string unit = Assembly.GetExecutingAssembly().Location; assembly = AssemblyFactory.GetAssembly(unit); rule = new AvoidConstructorsInStaticTypesRule(); - messageCollection = null; + runner = new TestRunner (rule); } - [Test] public void TestClassHasNoPublicConstructors() { type = assembly.MainModule.Types["Test.Rules.Correctness.AvoidConstructorsInStaticTypesTest/IsMadeStatic"]; - messageCollection = rule.CheckType(type, new MinimalRunner()); - Assert.IsNull(messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestClassIsDeclaredStatic() { type = assembly.MainModule.Types["Test.Rules.Correctness.AvoidConstructorsInStaticTypesTest/IsStatic"]; - messageCollection = rule.CheckType(type, new MinimalRunner()); - Assert.IsNull(messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckType (type), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } - [Test] public void TestClassCannotBeMadeStatic() { type = assembly.MainModule.Types["Test.Rules.Correctness.AvoidConstructorsInStaticTypesTest/CannotBeMadeStatic"]; - messageCollection = rule.CheckType(type, new MinimalRunner()); - Assert.IsNull(messageCollection); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckType (type), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestClassCanBeMadeStatic() { type = assembly.MainModule.Types["Test.Rules.Correctness.AvoidConstructorsInStaticTypesTest/CanBeMadeStatic"]; - messageCollection = rule.CheckType(type, new MinimalRunner()); - Assert.IsNotNull(messageCollection); + Assert.AreEqual (RuleResult.Failure, runner.CheckType (type), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestEmptyClass () { type = assembly.MainModule.Types["Test.Rules.Correctness.AvoidConstructorsInStaticTypesTest/EmptyClass"]; - messageCollection = rule.CheckType (type, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckType (type), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } } } diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/BadRecursiveInvocationTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/BadRecursiveInvocationTest.cs index cb5fee06..acfacf1d 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/BadRecursiveInvocationTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/BadRecursiveInvocationTest.cs @@ -146,7 +146,7 @@ namespace Test.Rules.Correctness { private AssemblyDefinition assembly; private TypeDefinition type; private ModuleDefinition module; - private Runner runner; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () @@ -156,7 +156,7 @@ namespace Test.Rules.Correctness { module = assembly.MainModule; type = module.Types["Test.Rules.Correctness.BadRecursiveInvocationTest/BadRec"]; rule = new BadRecursiveInvocationRule (); - runner = new MinimalRunner (); + runner = new TestRunner (rule); } private MethodDefinition GetTest (string name) @@ -171,90 +171,105 @@ namespace Test.Rules.Correctness { [Test] public void RecursiveProperties () { - MethodDefinition method = GetTest ("get_Foo"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Foo"); + MethodDefinition method = GetTest ("get_Foo"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult1"); + Assert.AreEqual (1, runner.Defects.Count, "Count1"); method = GetTest ("get_OnePlusFoo"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "OnePlusFoo"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult2"); + Assert.AreEqual (1, runner.Defects.Count, "Count2"); method = GetTest ("get_FooPlusOne"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "FooPlusOne"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult3"); + Assert.AreEqual (1, runner.Defects.Count, "Count3"); } [Test] public void Property () { MethodDefinition method = GetTest ("get_Bar"); - Assert.IsNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test, Ignore ("uncatched by rule")] public void IndirectRecursiveProperty () { MethodDefinition method = GetTest ("get_FooBar"); - Assert.IsNotNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void OverriddenMethod () { MethodDefinition method = GetTest ("GetHashCode"); - Assert.IsNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void BadRecursiveMethod () { MethodDefinition method = GetTest ("Equals"); - Assert.IsNotNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void BadFibo () { MethodDefinition method = GetTest ("BadFibo"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "instance"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult1"); + Assert.AreEqual (1, runner.Defects.Count, "Count1"); method = GetTest ("StaticBadFibo"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "static"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult2"); + Assert.AreEqual (1, runner.Defects.Count, "Count2"); } [Test] public void Fibonacci () { MethodDefinition method = GetTest ("Fibonacci"); - Assert.IsNull (rule.CheckMethod (method, runner), "instance"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult1"); + Assert.AreEqual (0, runner.Defects.Count, "Count1"); method = GetTest ("StaticFibonacci"); - Assert.IsNull (rule.CheckMethod (method, runner), "static"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult2"); + Assert.AreEqual (0, runner.Defects.Count, "Count2"); } [Test, Ignore ("uncatched by rule")] public void CodeUsingAnInstanceOfItself () { MethodDefinition method = GetTest ("AnotherInstance"); - Assert.IsNotNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestAssert () { MethodDefinition method = GetTest ("Assert"); - Assert.IsNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestStaticCallingAnotherClassWithSameMethodName () { MethodDefinition method = GetTest ("Write"); - Assert.IsNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestUnreachable () { MethodDefinition method = GetTest ("Unreachable"); - Assert.IsNull (rule.CheckMethod (method, runner)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } } } diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/CallingEqualsWithNullArgTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/CallingEqualsWithNullArgTest.cs index 071e5056..9a066dc8 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/CallingEqualsWithNullArgTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/CallingEqualsWithNullArgTest.cs @@ -3,8 +3,10 @@ // // Authors: // Nidhi Rawal <sonu2404@gmail.com> +// Sebastien Pouliot <sebastien@ximian.com> // // 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 @@ -131,18 +133,18 @@ namespace Test.Rules.Correctness } } - private IMethodRule methodRule; + private IMethodRule rule; + private TestRunner runner; private AssemblyDefinition assembly; private TypeDefinition type; - MessageCollection messageCollection; [TestFixtureSetUp] public void FixtureSetUp () { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); - methodRule = new CallingEqualsWithNullArgRule (); - messageCollection = null; + rule = new CallingEqualsWithNullArgRule (); + runner = new TestRunner (rule); } private TypeDefinition GetTest (string name) @@ -155,82 +157,92 @@ namespace Test.Rules.Correctness public void callToEqualsWithNullArgTest () { type = GetTest ("CallToEqualsWithNullArg"); - foreach (MethodDefinition method in type.Methods) - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + foreach (MethodDefinition method in type.Methods) { + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult." + method.Name); + Assert.AreEqual (1, runner.Defects.Count, "Count." + method.Name); + } } [Test] public void callingEqualsWithNonNullArgTest () { type = GetTest ("CallingEqualsWithNonNullArg"); - foreach (MethodDefinition method in type.Methods) - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + foreach (MethodDefinition method in type.Methods) { + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult." + method.Name); + Assert.AreEqual (0, runner.Defects.Count, "Count." + method.Name); + } } [Test] public void enumPassingArgNullInEqualsTest () { type = GetTest ("CallingEqualsOnEnum"); - foreach (MethodDefinition method in type.Methods) - if (method.Name == "PassingArgNullInEquals") - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + foreach (MethodDefinition method in type.Methods) { + if (method.Name == "PassingArgNullInEquals") { + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); + } + } } [Test] public void enumNotPassingArgNullInEqualsTest () { type = GetTest ("CallingEqualsOnEnum"); - foreach (MethodDefinition method in type.Methods) - if (method.Name == "NotPassingNullArgInEquals") - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + foreach (MethodDefinition method in type.Methods) { + if (method.Name == "NotPassingNullArgInEquals") { + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); + } + } } [Test] public void passingNullArgumentThanStructTest () { type = GetTest ("CallingEqualsOnStruct"); - foreach (MethodDefinition method in type.Methods) - if (method.Name == "PassingNullArgument") - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + foreach (MethodDefinition method in type.Methods) { + if (method.Name == "PassingNullArgument") { + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); + } + } } [Test] public void passingNonNullStructArgTest () { type = GetTest ("CallingEqualsOnStruct"); - foreach (MethodDefinition method in type.Methods) - if (method.Name == "PassingNonNullArg") - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + foreach (MethodDefinition method in type.Methods) { + if (method.Name == "PassingNonNullArg") { + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); + } + } } [Test] public void passingNullArgumentThanArrayTest () { type = GetTest ("CallingEqualsOnArray"); - foreach (MethodDefinition method in type.Methods) - if (method.Name == "PassingNullArg") - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (1, messageCollection.Count); + foreach (MethodDefinition method in type.Methods) { + if (method.Name == "PassingNullArg") { + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); + } + } } [Test] public void passingNonNullArrayArgumentTest () { type = GetTest ("CallingEqualsOnArray"); - foreach (MethodDefinition method in type.Methods) - if (method.Name == "PassingNonNullArg") - messageCollection = methodRule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + foreach (MethodDefinition method in type.Methods) { + if (method.Name == "PassingNonNullArg") { + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); + } + } } } -}
\ No newline at end of file +} diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/ChangeLog b/gendarme/rules/Gendarme.Rules.Correctness/Test/ChangeLog index 816d0ca2..b627f47e 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/ChangeLog +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/ChangeLog @@ -1,3 +1,14 @@ +2008-02-15 Sebastien Pouliot <sebastien@ximian.com> + + * AvoidConstructorsInStaticTypesTest.cs + * BadRecursiveInvocationTest.cs + * CallingEqualsWithNullArgTest.cs + * DontCompareWithNaNTest.cs + * FloatComparisonRuleTest.cs + * MethodCanBeMadeStaticTest.cs + * UseValueInPropertySetterTest.cs: + Update unit tests wrt framework changes. + 2008-01-29 Sebastien Pouliot <sebastien@ximian.com> * FloatComparisonRuleTest.cs: Add test case where we compare with diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/DontCompareWithNaNTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/DontCompareWithNaNTest.cs index 9d39604a..32ca2183 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/DontCompareWithNaNTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/DontCompareWithNaNTest.cs @@ -1,5 +1,5 @@ // -// Unit test for DontCompareWithNaNRule +// Unit test for DoNotCompareWithNaNRule // // Authors: // Sebastien Pouliot <sebastien@ximian.com> @@ -39,7 +39,7 @@ using NUnit.Framework; namespace Test.Rules.Correctness { [TestFixture] - public class DontCompareWithNaNTest { + public class DoNotCompareWithNaNTest { public class SingleCases { @@ -146,15 +146,15 @@ namespace Test.Rules.Correctness { private IMethodRule rule; private AssemblyDefinition assembly; private TypeDefinition type; - private Runner runner; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); - rule = new DontCompareWithNaNRule (); - runner = new MinimalRunner (); + rule = new DoNotCompareWithNaNRule (); + runner = new TestRunner (rule); } private MethodDefinition GetTest (string typeName, string name) @@ -172,74 +172,74 @@ namespace Test.Rules.Correctness { public void EqualityOperator () { MethodDefinition method = GetTest ("Single", "EqualityOperatorLeft"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Single-EqualityOperatorLeft"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Single-EqualityOperatorLeft"); method = GetTest ("Single", "EqualityOperatorRight"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Single-EqualityOperatorRight"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Single-EqualityOperatorRight"); method = GetTest ("Single", "Equality"); - Assert.IsNull (rule.CheckMethod (method, runner), "Single-Equality"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "Single-Equality"); method = GetTest ("Double", "EqualityOperatorLeft"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Double-EqualityOperatorLeft"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Double-EqualityOperatorLeft"); method = GetTest ("Double", "EqualityOperatorRight"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Double-EqualityOperatorRight"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Double-EqualityOperatorRight"); method = GetTest ("Double", "Equality"); - Assert.IsNull (rule.CheckMethod (method, runner), "Double-Equality"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "Double-Equality"); } [Test] public void InequalityOperator () { MethodDefinition method = GetTest ("Single", "InequalityOperatorLeft"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Single-InequalityOperatorLeft"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Single-InequalityOperatorLeft"); method = GetTest ("Single", "InequalityOperatorRight"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Single-InequalityOperatorRight"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Single-InequalityOperatorRight"); method = GetTest ("Single", "Inequality"); - Assert.IsNull (rule.CheckMethod (method, runner), "Single-Inequality"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "Single-Inequality"); method = GetTest ("Double", "InequalityOperatorLeft"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Double-InequalityOperatorLeft"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Double-InequalityOperatorLeft"); method = GetTest ("Double", "InequalityOperatorRight"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Double-InequalityOperatorRight"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Double-InequalityOperatorRight"); method = GetTest ("Double", "Inequality"); - Assert.IsNull (rule.CheckMethod (method, runner), "Double-Inequality"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "Double-Inequality"); } [Test] public void NaNEquals () { MethodDefinition method = GetTest ("Single", "NaNEquals"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Single-NaNEquals"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Single-NaNEquals"); method = GetTest ("Double", "NaNEquals"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Double-NaNEquals"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Double-NaNEquals"); } [Test] public void EqualsNaN () { MethodDefinition method = GetTest ("Single", "EqualsNaN"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Single-EqualsNaN"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Single-EqualsNaN"); method = GetTest ("Double", "EqualsNaN"); - Assert.IsNotNull (rule.CheckMethod (method, runner), "Double-EqualsNaN"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "Double-EqualsNaN"); } [Test] public void Equals () { MethodDefinition method = GetTest ("Single", "Equals"); - Assert.IsNull (rule.CheckMethod (method, runner), "Single-Equals"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "Single-Equals"); method = GetTest ("Double", "Equals"); - Assert.IsNull (rule.CheckMethod (method, runner), "Double-Equals"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "Double-Equals"); } } } diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/FloatComparisonRuleTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/FloatComparisonRuleTest.cs index c4ee8933..c32d6ffe 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/FloatComparisonRuleTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/FloatComparisonRuleTest.cs @@ -272,22 +272,20 @@ namespace Test.Rules.Correctness { private IMethodRule rule; private AssemblyDefinition assembly; private TypeDefinition type; - private ModuleDefinition module; - private Runner runner; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp () { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); - module = assembly.MainModule; rule = new FloatComparisonRule (); - runner = new MinimalRunner (); + runner = new TestRunner (rule); } MethodDefinition GetTest (string typeName, string name) { - type = module.Types ["Test.Rules.Correctness.FloatComparisonRuleTest/" + typeName]; + type = assembly.MainModule.Types ["Test.Rules.Correctness.FloatComparisonRuleTest/" + typeName]; foreach (MethodDefinition method in type.Methods) { if (method.Name == name) return method; @@ -296,130 +294,148 @@ namespace Test.Rules.Correctness { return null; } - MessageCollection CheckMethod (MethodDefinition method) - { - return rule.CheckMethod (method, runner); - } - [Test] public void TestFields () { MethodDefinition method = GetTest ("Float", "Fields"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (2, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "Fields"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (2, runner.Defects.Count, "Count-Double"); } [Test] public void TestMethodResult () { MethodDefinition method = GetTest("Float", "MethodResult"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (2, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "MethodResult"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (2, runner.Defects.Count, "Count-Double"); } [Test] public void TestParameters () { MethodDefinition method = GetTest ("Float", "Parameters"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "Parameters"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Double"); } [Test] public void TestStaticParameters () { MethodDefinition method = GetTest ("Float", "StaticParameters"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "StaticParameters"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Double"); } [Test] public void TestLocal () { MethodDefinition method = GetTest ("Float", "Local"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (4, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "Local"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (4, runner.Defects.Count, "Count-Double"); } [Test] public void TestLocalAbove4 () { MethodDefinition method = GetTest ("Float", "LocalAbove4"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (4, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "LocalAbove4"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (4, runner.Defects.Count, "Count-Double"); } [Test] public void TestComparisonAfterArithmeticOperation () { MethodDefinition method = GetTest ("Float", "ComparisonAfterArithmeticOperation"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "ComparisonAfterArithmeticOperation"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Double"); } [Test] public void TestLegalComparisons () { MethodDefinition method = GetTest("Float", "LegalComparisons"); - Assert.IsNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (0, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "LegalComparisons"); - Assert.IsNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (0, runner.Defects.Count, "Count-Double"); } [Test] public void TestLegalIntegerComparisons () { MethodDefinition method = GetTest ("Legal", "IntegerComparisons"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestEqualsCall () { MethodDefinition method = GetTest("Float", "EqualsCall"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "EqualsCall"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (3, runner.Defects.Count, "Count-Double"); } [Test] public void TestNoFloatComparison() { MethodDefinition method = GetTest ("Legal", "NoComparison"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestCompareWithArray () { MethodDefinition method = GetTest ("Float", "CompareWithArray"); - Assert.IsNotNull (CheckMethod (method), "Float"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Float"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Float"); method = GetTest ("Double", "CompareWithArray"); - Assert.IsNotNull (CheckMethod (method), "Double"); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult-Double"); + Assert.AreEqual (1, runner.Defects.Count, "Count-Double"); } [Test] public void TestLegalStuff () { MethodDefinition method = GetTest ("Legal", "get_Property"); - Assert.IsNull (CheckMethod (method), "get_Property"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } } } diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/MethodCanBeMadeStaticTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/MethodCanBeMadeStaticTest.cs index 623a43ce..e3e0b55f 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/MethodCanBeMadeStaticTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/MethodCanBeMadeStaticTest.cs @@ -63,18 +63,18 @@ namespace Test.Rules.Correctness { } private IMethodRule rule; + private TestRunner runner; private AssemblyDefinition assembly; private TypeDefinition type; - private ModuleDefinition module; [TestFixtureSetUp] public void FixtureSetUp () { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); - module = assembly.MainModule; - type = module.Types ["Test.Rules.Correctness.MethodCanBeMadeStaticTest/Item"]; + type = assembly.MainModule.Types ["Test.Rules.Correctness.MethodCanBeMadeStaticTest/Item"]; rule = new MethodCanBeMadeStaticRule (); + runner = new TestRunner (rule); } MethodDefinition GetTest (string name) @@ -86,27 +86,26 @@ namespace Test.Rules.Correctness { return null; } - MessageCollection CheckMethod (MethodDefinition method) - { - return rule.CheckMethod (method, new MinimalRunner ()); - } - [Test] public void TestGoodCandidate () { MethodDefinition method = GetTest ("Foo"); - Assert.IsNotNull (CheckMethod(method)); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestNotGoodCandidate () { MethodDefinition method = GetTest ("Bar"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult1"); + Assert.AreEqual (0, runner.Defects.Count, "Count1"); method = GetTest ("Baz"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckMethod (method), "RuleResult2"); + Assert.AreEqual (0, runner.Defects.Count, "Count2"); method = GetTest ("Gazonk"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckMethod (method), "RuleResult3"); + Assert.AreEqual (0, runner.Defects.Count, "Count3"); } } } diff --git a/gendarme/rules/Gendarme.Rules.Correctness/Test/UseValueInPropertySetterTest.cs b/gendarme/rules/Gendarme.Rules.Correctness/Test/UseValueInPropertySetterTest.cs index 486a7e08..917752c4 100644 --- a/gendarme/rules/Gendarme.Rules.Correctness/Test/UseValueInPropertySetterTest.cs +++ b/gendarme/rules/Gendarme.Rules.Correctness/Test/UseValueInPropertySetterTest.cs @@ -135,7 +135,7 @@ namespace Test.Rules.Correctness { private AssemblyDefinition assembly; private TypeDefinition type; private ModuleDefinition module; - private Runner runner; + private TestRunner runner; [TestFixtureSetUp] public void FixtureSetUp() @@ -145,10 +145,10 @@ namespace Test.Rules.Correctness { module = assembly.MainModule; type = module.Types["Test.Rules.Correctness.UseValueInPropertySetterTest/Item"]; rule = new UseValueInPropertySetterRule (); - runner = new MinimalRunner (); + runner = new TestRunner (rule); } - MethodDefinition GetTest(string name) + MethodDefinition GetTest (string name) { foreach (MethodDefinition method in type.Methods) if (method.Name == name) @@ -157,62 +157,66 @@ namespace Test.Rules.Correctness { return null; } - MessageCollection CheckMethod (MethodDefinition method) - { - return rule.CheckMethod (method, runner); - } - [Test] public void TestUsesValue() { - MethodDefinition method = GetTest("set_UsesValue"); - Assert.IsNull(CheckMethod(method)); + MethodDefinition method = GetTest ("set_UsesValue"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestDoesNotUseValue() { MethodDefinition method = GetTest("set_DoesNotUseValue"); - Assert.IsNotNull(CheckMethod(method)); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestNotAProperty() { MethodDefinition method = GetTest("set_NotAProperty"); - Assert.IsNull(CheckMethod(method)); + Assert.AreEqual (RuleResult.DoesNotApply, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void ThrowException () { MethodDefinition method = GetTest ("set_Throw1"); - Assert.IsNull (CheckMethod (method), "1"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult1"); + Assert.AreEqual (0, runner.Defects.Count, "Count1"); method = GetTest ("set_Throw2"); - Assert.IsNull (CheckMethod (method), "2"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult2"); + Assert.AreEqual (0, runner.Defects.Count, "Count2"); method = GetTest ("set_Throw3"); - Assert.IsNull (CheckMethod (method), "3"); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult3"); + Assert.AreEqual (0, runner.Defects.Count, "Count3"); } [Test] public void TestStaticUsesValue () { MethodDefinition method = GetTest ("set_MaxFields"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestStaticDoesNotUseValue () { MethodDefinition method = GetTest ("set_MinFields"); - Assert.IsNotNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestDateValue () { MethodDefinition method = GetTest ("set_Time"); - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] @@ -221,7 +225,8 @@ namespace Test.Rules.Correctness { MethodDefinition method = GetTest ("set_Empty"); // too many false positive, it seems too common to have empty set to report them // at least for this specific rule - Assert.IsNull (CheckMethod (method)); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] @@ -229,8 +234,10 @@ namespace Test.Rules.Correctness { { TypeDefinition type = module.Types ["Test.Rules.Correctness.UseValueInPropertySetterTest/BitVector32"]; foreach (MethodDefinition method in type.Methods) { - if (method.Name == "set_Item") - Assert.IsNull (CheckMethod (method)); + if (method.Name == "set_Item") { + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); + } } } } |