Author: spouliot
Date: 2008-01-24 18:36:42 -0500 (Thu, 24 Jan 2008)
New Revision: 93862
Removed:
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
Log:
2008-01-24 Sebastien Pouliot <[EMAIL PROTECTED]>
* EqualShouldHandleNullArgTest.cs: Changed my mind about "new" ;-)
* ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs: Tests
move (with rules) into Design since it affects the API.
* ToStringReturnsNullTest.cs: Add a bunch of new test cases.
Modified: trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
===================================================================
--- trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
2008-01-24 23:32:53 UTC (rev 93861)
+++ trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ChangeLog
2008-01-24 23:36:42 UTC (rev 93862)
@@ -1,3 +1,10 @@
+2008-01-24 Sebastien Pouliot <[EMAIL PROTECTED]>
+
+ * EqualShouldHandleNullArgTest.cs: Changed my mind about "new" ;-)
+ * ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs: Tests
+ move (with rules) into Design since it affects the API.
+ * ToStringReturnsNullTest.cs: Add a bunch of new test cases.
+
2008-01-23 Sebastien Pouliot <[EMAIL PROTECTED]>
* ConstructorShouldNotCallVirtualMethodsTest.cs: Add a bunch
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
2008-01-24 23:32:53 UTC (rev 93861)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/EqualShouldHandleNullArgTest.cs
2008-01-24 23:36:42 UTC (rev 93862)
@@ -222,7 +222,7 @@
public void equalsNotOverriddenNotReturningFalseForNullTest ()
{
type = GetTest
("EqualsNotOverriddenNotReturningFalseForNull");
- Assert.IsNull (rule.CheckType (type, runner));
+ Assert.IsNotNull (rule.CheckType (type, runner));
}
[Test]
Deleted:
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
2008-01-24 23:32:53 UTC (rev 93861)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ImplementingEqualsButNotGetHashCodeAndViceVersaTest.cs
2008-01-24 23:36:42 UTC (rev 93862)
@@ -1,207 +0,0 @@
-//
-// Unit tests for ImplementingEqualsButNotGetHashCodeAndViceVersaRule
-//
-// Authors:
-// Nidhi Rawal <[EMAIL PROTECTED]>
-//
-// Copyright (c) <2007> Nidhi Rawal
-//
-// 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.Collections;
-using System.Reflection;
-
-using Gendarme.Framework;
-using Gendarme.Rules.BadPractice;
-using Mono.Cecil;
-using NUnit.Framework;
-
-namespace Test.Rules.BadPractice {
-
- [TestFixture]
- public class ImplementingEqualsButNotGetHashCodeAndViceVersaTest
- {
- public class ImplementsEqualsButNotGetHashCode
- {
- public override bool Equals (Object obj)
- {
- return this == obj;
- }
- }
-
- public class ImplementsGetHashCodeButNotEquals
- {
- public override int GetHashCode ()
- {
- return 2;
- }
- }
-
- public class ImplementsNoneOfThem
- {
- public void test ()
- {
- }
- }
-
- public class ImplementsBothOfThem
- {
- public override int GetHashCode ()
- {
- return 2;
- }
- public new bool Equals (Object obj)
- {
- return this == obj;
- }
- }
-
- public class ImplementsEqualsUsesObjectGetHashCode
- {
- public override bool Equals (Object obj)
- {
- return this == obj;
- }
- public static void Main (string [] args)
- {
- int j = 0;
- ImplementsEqualsUsesObjectGetHashCode i = new
ImplementsEqualsUsesObjectGetHashCode ();
- j = i.GetHashCode ();
- }
- }
-
- public class ImplementsGetHashCodeUsesObjectEquals
- {
- public override int GetHashCode ()
- {
- return 1;
- }
- public static void Main (string [] args)
- {
- ImplementsGetHashCodeUsesObjectEquals i = new
ImplementsGetHashCodeUsesObjectEquals ();
- ImplementsGetHashCodeUsesObjectEquals i1 = new
ImplementsGetHashCodeUsesObjectEquals ();
- i.Equals (i1);
- }
- }
-
- public class ImplementingEqualsWithTwoArgs
- {
- public bool Equals (Object obj1, Object obj2)
- {
- return obj1 == obj2;
- }
- }
-
- public class ImplementingGetHashCodeWithOneArg
- {
- public int GetHashCode (int j)
- {
- return j*2;
- }
- }
-
- private ITypeRule typeRule;
- private AssemblyDefinition assembly;
- private MessageCollection messageCollection;
-
- [TestFixtureSetUp]
- public void FixtureSetUp ()
- {
- string unit = Assembly.GetExecutingAssembly ().Location;
- assembly = AssemblyFactory.GetAssembly (unit);
- typeRule = new
ImplementingEqualsButNotGetHashCodeAndViceVersaRule();
- messageCollection = null;
- }
-
- private TypeDefinition GetTest (string name)
- {
- string fullname =
"Test.Rules.BadPractice.ImplementingEqualsButNotGetHashCodeAndViceVersaTest/" +
name;
- return assembly.MainModule.Types[fullname];
- }
-
- [Test]
- public void EqualsButNotGetHashCodeTest ()
- {
- TypeDefinition type = GetTest
("ImplementsEqualsButNotGetHashCode");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNotNull (messageCollection);
- Assert.AreEqual (messageCollection.Count, 1);
- }
-
- [Test]
- public void GetHashCodeButNotEqualsTest ()
- {
- TypeDefinition type = GetTest
("ImplementsGetHashCodeButNotEquals");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNotNull (messageCollection);
- Assert.AreEqual (messageCollection.Count, 1);
- }
-
- [Test]
- public void NoneOfThemTest ()
- {
- TypeDefinition type = GetTest ("ImplementsNoneOfThem");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNull (messageCollection);
- }
-
- [Test]
- public void BothOfThemTest ()
- {
- TypeDefinition type = GetTest ("ImplementsBothOfThem");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNull (messageCollection);
- }
-
- [Test]
- public void ImplementsEqualsUsesObjectGetHashCodeTest ()
- {
- TypeDefinition type = GetTest
("ImplementsEqualsUsesObjectGetHashCode");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNotNull (messageCollection);
- Assert.AreEqual (messageCollection.Count, 1);
- }
-
- [Test]
- public void ImplementsGetHashCodeUsesObjectEqualsTest ()
- {
- TypeDefinition type = GetTest
("ImplementsGetHashCodeUsesObjectEquals");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNotNull (messageCollection);
- Assert.AreEqual (messageCollection.Count, 1);
- }
-
- [Test]
- public void EqualsWithTwoArgsTest ()
- {
- TypeDefinition type = GetTest
("ImplementingEqualsWithTwoArgs");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNull (messageCollection);
- }
-
- [Test]
- public void GetHashCodeWithOneArgTest ()
- {
- TypeDefinition type = GetTest
("ImplementingGetHashCodeWithOneArg");
- messageCollection = typeRule.CheckType (type, new
MinimalRunner ());
- Assert.IsNull (messageCollection);
- }
- }
-}
Modified:
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
===================================================================
---
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
2008-01-24 23:32:53 UTC (rev 93861)
+++
trunk/cecil/gendarme/rules/Gendarme.Rules.BadPractice/Test/ToStringReturnsNullTest.cs
2008-01-24 23:36:42 UTC (rev 93862)
@@ -2,9 +2,10 @@
// Unit tests for ToStringReturnsNullRule
//
// Authors:
-// Nidhi Rawal <[EMAIL PROTECTED]>
+// Sebastien Pouliot <[EMAIL PROTECTED]>
//
// Copyright (c) <2007> Nidhi Rawal
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
deal
@@ -32,66 +33,102 @@
using Mono.Cecil;
using NUnit.Framework;
-namespace Test.Rules.BadPractice
-{
+namespace Test.Rules.BadPractice {
+
[TestFixture]
public class ToStringReturnsNullTest {
-
- public class ToStringReturningNull
- {
+
+ public class ToStringReturningNull {
public override string ToString ()
{
+ // this is bad
return null;
}
}
- public class ToStringReturningEmptyString
- {
+ public class ToStringReturningEmptyString {
public override string ToString ()
{
+ // this is Ok
return String.Empty;
}
}
- public class ToStringNotReturningNull
- {
+ public class ToStringReturningField {
string s = "ab";
public override string ToString ()
{
+ // this is Ok (even if we're not sure) ???
return s;
}
- }
-
- public class ConvertToStringReturningEmptyString
- {
- object obj = null;
+ }
+
+ public class ToStringReturningConstField {
+ const string s = "ab";
public override string ToString ()
{
- return Convert.ToString (obj);
+ // this is Ok
+ return s;
}
- }
+ }
+
+ public class ToStringReturningReadOnlyField {
+ readonly string s = "ab";
+ public override string ToString ()
+ {
+ // this is Ok
+ return s;
+ }
+ }
+
+ public class ToStringReturningNewString {
+ public override string ToString ()
+ {
+ // this is Ok
+ return new string ('!', 2);
+ }
+ }
- public class ConvertToStringReturningNull
- {
- string s = null;
+ public class ToStringReturningStringFormat {
public override string ToString ()
+ {
+ return String.Format ("{0}-{1}", 1, 2);
+ }
+ }
+
+ public class ToStringReturningConvertToStringObject {
+ public override string ToString ()
+ {
+ return Convert.ToString ((object)null);
+ }
+ }
+
+ public class ToStringReturningConvertToStringString {
+ public override string ToString ()
{
- return Convert.ToString (s);
+ return Convert.ToString ((string)null);
}
- }
+ }
+
+ public class ToStringReturningTypeName {
+ public override string ToString ()
+ {
+ return GetType ().FullName;
+ }
+ }
- private IMethodRule methodRule;
+ private ITypeRule rule;
private AssemblyDefinition assembly;
private TypeDefinition type;
- MessageCollection messageCollection;
+ private MinimalRunner runner;
[TestFixtureSetUp]
public void FixtureSetUp ()
{
string unit = Assembly.GetExecutingAssembly ().Location;
assembly = AssemblyFactory.GetAssembly (unit);
- methodRule = new ToStringReturnsNullRule();
- messageCollection = null;
+ rule = new ToStringReturnsNullRule ();
+ runner = new MinimalRunner ();
}
private TypeDefinition GetTest (string name)
@@ -101,54 +138,80 @@
}
[Test]
- public void toStringReturningNullTest ()
+ public void ReturningNullTest ()
{
type = GetTest ("ToStringReturningNull");
- foreach (MethodDefinition method in type.Methods) {
- messageCollection = methodRule.CheckMethod
(method, new MinimalRunner ());
- }
- Assert.IsNotNull (messageCollection);
- Assert.AreEqual (1, messageCollection.Count);
+ Assert.IsNotNull (rule.CheckType (type, runner));
}
[Test]
- public void toStringReturningEmptyStringTest ()
+ public void ReturningEmptyStringTest ()
{
type = GetTest ("ToStringReturningEmptyString");
- foreach (MethodDefinition method in type.Methods) {
- messageCollection = methodRule.CheckMethod
(method, new MinimalRunner ());
- }
- Assert.IsNull (messageCollection);
+ Assert.IsNull (rule.CheckType (type, runner));
}
[Test]
- public void toStringNotReturningNullTest ()
+ public void ReturningField ()
{
- type = GetTest ("ToStringNotReturningNull");
- foreach (MethodDefinition method in type.Methods) {
- messageCollection = methodRule.CheckMethod
(method, new MinimalRunner ());
- }
- Assert.IsNull (messageCollection);
+ type = GetTest ("ToStringReturningField");
+ // there's doubt but it's not easy (i.e. false
positives) to be sure
+ Assert.IsNull (rule.CheckType (type, runner));
}
+
+ [Test]
+ public void ReturningConstField ()
+ {
+ type = GetTest ("ToStringReturningConstField");
+ Assert.IsNull (rule.CheckType (type, runner));
+ }
+
+ [Test]
+ public void ReturningReadOnlyField ()
+ {
+ type = GetTest ("ToStringReturningReadOnlyField");
+ Assert.IsNull (rule.CheckType (type, runner));
+ }
+
+ [Test]
+ public void ReturningNewString ()
+ {
+ type = GetTest ("ToStringReturningNewString");
+ Assert.IsNull (rule.CheckType (type, runner));
+ }
+
+ [Test]
+ public void ReturningStringFormat ()
+ {
+ type = GetTest ("ToStringReturningStringFormat");
+ Assert.IsNull (rule.CheckType (type, runner));
+ }
[Test]
- public void convertToStringReturningEmptyStringTest ()
+ public void ReturningConvertToStringObject ()
{
- type = GetTest ("ConvertToStringReturningEmptyString");
- foreach (MethodDefinition method in type.Methods) {
- messageCollection = methodRule.CheckMethod
(method, new MinimalRunner ());
- }
- Assert.IsNull (messageCollection);
+ Assert.AreEqual (String.Empty, Convert.ToString
((object) null), "Convert.ToString(object)");
+ type = GetTest
("ToStringReturningConvertToStringObject");
+ Assert.IsNull (rule.CheckType (type, runner));
}
[Test]
- public void convertToStringReturningNullTest ()
+ [Ignore ("requires to analyze what called methods returns")]
+ public void ReturningConvertToStringString ()
{
- type = GetTest ("ConvertToStringReturningNull");
- foreach (MethodDefinition method in type.Methods) {
- messageCollection = methodRule.CheckMethod
(method, new MinimalRunner ());
- }
- Assert.IsNotNull (messageCollection);
+ // converting a null string to a string return null
+ // however this is a special case since, most times, we
won't know
+ // if the value passed to Convert.ToString is null or
not
+ Assert.IsNull (Convert.ToString ((string) null),
"Convert.ToString((string)null)");
+ type = GetTest
("ToStringReturningConvertToStringString");
+ Assert.IsNotNull (rule.CheckType (type, runner));
}
+
+ [Test]
+ public void ReturningTypeName ()
+ {
+ type = GetTest ("ToStringReturningTypeName");
+ Assert.IsNull (rule.CheckType (type, runner));
+ }
}
-}
\ No newline at end of file
+}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches