Author: spouliot
Date: 2008-02-06 22:03:20 -0500 (Wed, 06 Feb 2008)
New Revision: 95106
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnsealedUninheritedInternalClassesTest.cs
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/UseIsOperatorTest.cs
Modified:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/Test.Rules.Performance.mdp
Log:
2008-02-06 Sebastien Pouliot <[EMAIL PROTECTED]>
* AvoidUnsealedUninheritedInternalClassesTest.cs: New. Unit tests
by Scott Peterson.
* UseIsOperatorTest.cs: New. Unit tests.
* Test.Rules.Performance.mdp: Update project file.
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnsealedUninheritedInternalClassesTest.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnsealedUninheritedInternalClassesTest.cs
2008-02-07 02:00:08 UTC (rev 95105)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/AvoidUnsealedUninheritedInternalClassesTest.cs
2008-02-07 03:03:20 UTC (rev 95106)
@@ -0,0 +1,143 @@
+//
+// Unit tests for AvoidUnsealedUninheritedInternalClassesRule
+//
+// Authors:
+// Scott Peterson <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Scott Peterson
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to
deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+using System;
+
+using Mono.Cecil;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Performance;
+
+using NUnit.Framework;
+
+namespace Test.Rules.Performance {
+
+ public class Visible {
+ }
+
+ public class Outer {
+ internal class UnsealedInner {
+ }
+
+ internal sealed class SealedInner {
+ }
+ }
+
+ internal abstract class Abstract {
+ }
+
+ internal class Concrete : Abstract {
+ }
+
+ internal sealed class Sealed {
+ }
+
+ internal class Unsealed {
+ }
+
+ [TestFixture]
+ public class AvoidUnsealedUninheritedInternalClassesTest {
+
+ private ITypeRule rule;
+ private AssemblyDefinition assembly;
+ private Runner runner;
+
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit =
System.Reflection.Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ rule = new AvoidUnsealedUninheritedInternalClassesRule
();
+ runner = new MinimalRunner ();
+ }
+
+ private TypeDefinition GetTest<T> ()
+ {
+ return assembly.MainModule.Types [typeof (T).FullName];
+ }
+
+ // note: the generic version doesn't work with inner types
since Cecil/IL and
+ // reflection do not use the same naming convention
+ private TypeDefinition GetTest (string name)
+ {
+ string fullname = "Test.Rules.Performance." + name;
+ return assembly.MainModule.Types [fullname];
+ }
+
+ [Test]
+ public void TestVisable ()
+ {
+ MessageCollection messages = rule.CheckType
(GetTest<Visible> (), runner);
+ Assert.IsNull (messages);
+ }
+
+ [Test]
+ public void TestUnsealedInner ()
+ {
+ MessageCollection messages = rule.CheckType (GetTest
("Outer/UnsealedInner"), runner);
+ Assert.IsNotNull (messages);
+ Assert.AreEqual (1, messages.Count);
+ }
+
+ [Test]
+ public void TestSealedInner ()
+ {
+ MessageCollection messages = rule.CheckType (GetTest
("Outer/SealedInner"), runner);
+ Assert.IsNull (messages);
+ }
+
+ [Test]
+ public void TestAbstract ()
+ {
+ MessageCollection messages = rule.CheckType
(GetTest<Abstract> (), runner);
+ Assert.IsNull (messages);
+ }
+
+ [Test]
+ public void TestConcrete ()
+ {
+ MessageCollection messages = rule.CheckType
(GetTest<Concrete> (), runner);
+ Assert.IsNotNull (messages);
+ Assert.AreEqual (1, messages.Count);
+ }
+
+ [Test]
+ public void TestSealed ()
+ {
+ MessageCollection messages = rule.CheckType
(GetTest<Sealed> (), runner);
+ Assert.IsNull (messages);
+ }
+
+ [Test]
+ public void TestUnsealed ()
+ {
+ MessageCollection messages = rule.CheckType
(GetTest<Unsealed> (), runner);
+ Assert.IsNotNull (messages);
+ Assert.AreEqual (1, messages.Count);
+ }
+ }
+}
Modified:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
2008-02-07 02:00:08 UTC (rev 95105)
+++ trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/ChangeLog
2008-02-07 03:03:20 UTC (rev 95106)
@@ -1,3 +1,10 @@
+2008-02-06 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * AvoidUnsealedUninheritedInternalClassesTest.cs: New. Unit tests
+ by Scott Peterson.
+ * UseIsOperatorTest.cs: New. Unit tests.
+ * Test.Rules.Performance.mdp: Update project file.
+
2008-02-03 Sebastien Pouliot <[EMAIL PROTECTED]>
* AvoidUninstantiatedInternalClassesTest.cs: Add more test cases.
Modified:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/Test.Rules.Performance.mdp
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/Test.Rules.Performance.mdp
2008-02-07 02:00:08 UTC (rev 95105)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/Test.Rules.Performance.mdp
2008-02-07 03:03:20 UTC (rev 95106)
@@ -24,6 +24,9 @@
<File name="AvoidToStringOnStringsTest.cs" subtype="Code"
buildaction="Compile" />
<File name="DontIgnoreMethodResultTest.cs" subtype="Code"
buildaction="Compile" />
<File name="AvoidUnsealedConcreteAttributesTest.cs" subtype="Code"
buildaction="Compile" />
+ <File name="AvoidUnsealedUninheritedInternalClassesTest.cs" subtype="Code"
buildaction="Compile" />
+ <File name="AvoidReturningArraysOnPropertiesTest.cs" subtype="Code"
buildaction="Compile" />
+ <File name="UseIsOperatorTest.cs" subtype="Code" buildaction="Compile" />
</Contents>
<References>
<ProjectReference type="Project" localcopy="True"
refto="Gendarme.Rules.Performance" />
Added:
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/UseIsOperatorTest.cs
===================================================================
---
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/UseIsOperatorTest.cs
2008-02-07 02:00:08 UTC (rev 95105)
+++
trunk/mono-tools/gendarme/rules/Gendarme.Rules.Performance/Test/UseIsOperatorTest.cs
2008-02-07 03:03:20 UTC (rev 95106)
@@ -0,0 +1,168 @@
+//
+// Unit tests for UseIsOperatorRule
+//
+// 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 System;
+using System.Reflection;
+
+using Gendarme.Framework;
+using Gendarme.Rules.Performance;
+using Mono.Cecil;
+using NUnit.Framework;
+
+namespace Test.Rules.Performance {
+
+ [TestFixture]
+ public class UseIsOperatorTest {
+
+ private bool ReturnEqualityBad (object value)
+ {
+ return ((value as UseIsOperatorTest) == null);
+ }
+
+ private bool ReturnInequalityBad (object value)
+ {
+ return ((value as UseIsOperatorTest) != null);
+ }
+
+ private bool ReturnEqualityOk (object value)
+ {
+ return (value is UseIsOperatorTest);
+ }
+
+ private bool ReturnInequalityOk (object value)
+ {
+ return !(value is UseIsOperatorTest);
+ }
+
+ // [g]mcs compiles this like an 'is', csc does too when
compiling with optimizations
+ private void ConditionEqualityBad (object value)
+ {
+ if ((value as UseIsOperatorTest) == null) {
+ Console.WriteLine ("Bad");
+ }
+ }
+
+ private void ConditionInequalityBad (object value)
+ {
+ if ((value as UseIsOperatorTest) != null) {
+ Console.WriteLine ("Bad");
+ }
+ }
+
+ private void ConditionIsOk (object value)
+ {
+ if (value is UseIsOperatorTest) {
+ Console.WriteLine ("Ok");
+ }
+ }
+
+ private void ConditionAsOk (object value)
+ {
+ UseIsOperatorTest test = (value as UseIsOperatorTest);
+ if (test != null) {
+ // 'is' would not be optimal since we use the
'as' result
+ Console.WriteLine (test.ToString ());
+ }
+ }
+
+ private void ConditionSplitBad (object value)
+ {
+ UseIsOperatorTest test = (value as UseIsOperatorTest);
+ // 'test' is unused after the test
+ if (test != null) {
+ Console.WriteLine ("Bad");
+ }
+ }
+
+ private IMethodRule rule;
+ private AssemblyDefinition assembly;
+ private TypeDefinition type;
+ private Runner runner;
+
+ [TestFixtureSetUp]
+ public void FixtureSetUp ()
+ {
+ string unit = Assembly.GetExecutingAssembly ().Location;
+ assembly = AssemblyFactory.GetAssembly (unit);
+ type = assembly.MainModule.Types
["Test.Rules.Performance.UseIsOperatorTest"];
+ rule = new UseIsOperatorRule ();
+ runner = new MinimalRunner ();
+ }
+
+ private MethodDefinition GetTest (string name)
+ {
+ foreach (MethodDefinition md in type.Methods) {
+ if (md.Name == name)
+ return md;
+ }
+ Assert.Fail ("Method '{0}' not found.");
+ return null;
+ }
+
+ [Test]
+ public void Return ()
+ {
+ MethodDefinition method = GetTest ("ReturnEqualityBad");
+ Assert.IsNotNull (rule.CheckMethod (method, runner),
"ReturnEqualityBad");
+
+ method = GetTest ("ReturnInequalityBad");
+ Assert.IsNotNull (rule.CheckMethod (method, runner),
"ReturnInequalityBad");
+
+ method = GetTest ("ReturnEqualityOk");
+ Assert.IsNull (rule.CheckMethod (method, runner),
"ReturnEqualityOk");
+
+ method = GetTest ("ReturnInequalityOk");
+ Assert.IsNull (rule.CheckMethod (method, runner),
"ReturnInequalityOk");
+ }
+
+ [Test]
+ public void Conditions ()
+ {
+ MethodDefinition method = GetTest ("ConditionIsOk");
+ Assert.IsNull (rule.CheckMethod (method, runner),
"ConditionIsOk");
+
+ method = GetTest ("ConditionAsOk");
+ Assert.IsNull (rule.CheckMethod (method, runner),
"ConditionAsOk");
+ }
+
+ [Test]
+ [Ignore ("Compiler optimization (default for [g]mcs) can fix
this")]
+ public void ConditionsOptimized ()
+ {
+ // missed opportunities are less problematic than false
positives ;-)
+ MethodDefinition method = GetTest
("ConditionEqualityBad");
+ Assert.IsNotNull (rule.CheckMethod (method, runner),
"ConditionEqualityBad");
+
+ method = GetTest ("ConditionInequalityBad");
+ Assert.IsNotNull (rule.CheckMethod (method, runner),
"ConditionInequalityBad");
+
+ method = GetTest ("ConditionSplitBad");
+ Assert.IsNotNull (rule.CheckMethod (method, runner),
"ConditionSplitBad");
+ }
+ }
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches