diff options
author | Sebastien Pouliot <sebastien@ximian.com> | 2008-02-15 22:08:46 +0300 |
---|---|---|
committer | Sebastien Pouliot <sebastien@ximian.com> | 2008-02-15 22:08:46 +0300 |
commit | 6eb27f47fd57f0a9f937db5a1512dfeceb426935 (patch) | |
tree | ce7e0915c57d2edfdecc1d0ea2555cb88bf905c1 /gendarme/rules | |
parent | fe5915eea71a19498b2bf26d0adf60a5893095e9 (diff) | |
parent | d7967e2385b4fb3c8af16029c2bd8896de8371b8 (diff) |
2008-02-15 Sebastien Pouliot <sebastien@ximian.com>
* DontDestroyStackTraceTest.cs: (renamed from TestPatterns.cs)
* DontSwallowErrorsCatchingNonspecificExceptionsTest.cs:
Update unit tests wrt framework changes.
svn path=/trunk/mono-tools/; revision=95790
Diffstat (limited to 'gendarme/rules')
-rw-r--r-- | gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog | 6 | ||||
-rw-r--r-- | gendarme/rules/Gendarme.Rules.Exceptions/Test/DontDestroyStackTraceTest.cs (renamed from gendarme/rules/Gendarme.Rules.Exceptions/Test/TestPatterns.cs) | 76 | ||||
-rw-r--r-- | gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs | 107 |
3 files changed, 82 insertions, 107 deletions
diff --git a/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog b/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog index bba81b67..1f177911 100644 --- a/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog +++ b/gendarme/rules/Gendarme.Rules.Exceptions/Test/ChangeLog @@ -1,3 +1,9 @@ +2008-02-15 Sebastien Pouliot <sebastien@ximian.com> + + * DontDestroyStackTraceTest.cs: (renamed from TestPatterns.cs) + * DontSwallowErrorsCatchingNonspecificExceptionsTest.cs: + Update unit tests wrt framework changes. + 2007-10-07 Sebastien Pouliot <sebastien@ximian.com> * Test.Rules.Exeptions.mdp: Update project file. diff --git a/gendarme/rules/Gendarme.Rules.Exceptions/Test/TestPatterns.cs b/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontDestroyStackTraceTest.cs index 9c89d4b7..294e4557 100644 --- a/gendarme/rules/Gendarme.Rules.Exceptions/Test/TestPatterns.cs +++ b/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontDestroyStackTraceTest.cs @@ -1,3 +1,28 @@ +// +// Unit Test for DontDestroyStackTraceTest Rule +// +// 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.Collections; using System.Reflection; @@ -12,8 +37,8 @@ namespace Test.Rules.Exceptions { public class DontDestroyStackTraceTest { private IMethodRule rule; + private TestRunner runner; private AssemblyDefinition assembly; - private ModuleDefinition module; private TypeDefinition type; // Test setup @@ -22,10 +47,9 @@ namespace Test.Rules.Exceptions { { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); - module = assembly.MainModule; - string fullname = "Test.Rules.Exceptions.DontDestroyStackTraceTest"; - type = module.Types [fullname]; + type = assembly.MainModule.Types ["Test.Rules.Exceptions.DontDestroyStackTraceTest"]; rule = new DontDestroyStackTrace (); + runner = new TestRunner (rule); } // Test infrastructure @@ -38,60 +62,46 @@ namespace Test.Rules.Exceptions { [Test] public void TestThrowOriginalEx () { - string testName = "ThrowOriginalEx"; - MethodDefinition method = GetMethodToTest (testName); - + MethodDefinition method = GetMethodToTest ("ThrowOriginalEx"); // Should result in 1 warning message - MessageCollection list = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsTrue (list != null, "Warnings were not generated for the test named " + testName); - Assert.AreEqual (list.Count, 1, "One warning should have been generated for the test named " + testName); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestThrowOriginalExWithJunk () { - string testName = "ThrowOriginalExWithJunk"; - MethodDefinition method = GetMethodToTest (testName); - + MethodDefinition method = GetMethodToTest ("ThrowOriginalExWithJunk"); // Should result in 1 warning message - MessageCollection list = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsTrue (list != null, "Warnings were not generated for the test named " + testName); - Assert.AreEqual (list.Count, 1, "One warning should have been generated for the test named " + testName); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestRethrowOriginalEx () { - string testName = "RethrowOriginalEx"; - MethodDefinition method = GetMethodToTest (testName); - + MethodDefinition method = GetMethodToTest ("RethrowOriginalEx"); // Should result in 0 warning messages - MessageCollection list = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsTrue (list == null, "Warnings were generated for the test named " + testName); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void TestThrowOriginalExAndRethrowWithJunk () { - string testName = "ThrowOriginalExAndRethrowWithJunk"; - MethodDefinition method = GetMethodToTest (testName); - + MethodDefinition method = GetMethodToTest ("ThrowOriginalExAndRethrowWithJunk"); // Should result in one warning message - MessageCollection list = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsTrue (list != null, "Warnings were not generated for the test named " + testName); - Assert.AreEqual (list.Count, 1, "One warning should have been generated for the test named " + testName); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void TestRethrowOriginalExAndThrowWithJunk () { - string testName = "RethrowOriginalExAndThrowWithJunk"; - MethodDefinition method = GetMethodToTest (testName); - + MethodDefinition method = GetMethodToTest ("RethrowOriginalExAndThrowWithJunk"); // Should result in one warning message - MessageCollection list = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsTrue (list != null, "Warnings were not generated for the test named " + testName); - Assert.AreEqual (list.Count, 1, "One warning should have been generated for the test named " + testName); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } // Functions whose IL is used by the test cases for the DontDestroyStackTrace rule diff --git a/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs b/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs index cc8fd8aa..7723ed8b 100644 --- a/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs +++ b/gendarme/rules/Gendarme.Rules.Exceptions/Test/DontSwallowErrorsCatchingNonspecificExceptionsTest.cs @@ -38,15 +38,14 @@ using NUnit.Framework; namespace Test.Rules.Exceptions { - [TestFixture] public class DontSwallowErrorsCatchingNonspecificExceptionsTest { private IMethodRule rule; + private TestRunner runner; private AssemblyDefinition assembly; private MethodDefinition method; private TypeDefinition type; - private MessageCollection messageCollection; [TestFixtureSetUp] public void FixtureSetUp () @@ -54,109 +53,80 @@ namespace Test.Rules.Exceptions { string unit = Assembly.GetExecutingAssembly ().Location; assembly = AssemblyFactory.GetAssembly (unit); rule = new DontSwallowErrorsCatchingNonspecificExceptionsRule (); + runner = new TestRunner (rule); type = assembly.MainModule.Types ["Test.Rules.Exceptions.DontSwallowErrorsCatchingNonspecificExceptionsTest"]; - messageCollection = null; - } - - 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 SwallowErrorsCatchingExceptionsEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingExceptionEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } - [Test] public void SwallowErrorsCatchingExceptionsNoEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingExceptionNoEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void SwallowErrorsCatchingSystemExceptionEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingSystemExceptionEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } - [Test] public void SwallowErrorsCatchingSystemExceptionNoEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingSystemExceptionNoEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void SwallowErrorsCatchingTypeExceptionEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingTypeExceptionEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void SwallowErrorsCatchingTypeExceptionNoEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingTypeExceptionNoEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void SwallowErrorsCatchingAllEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingAllEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void SwallowErrorsCatchingAllNoEmptyCatchBlockTest () { method = type.Methods.GetMethod ("SwallowErrorsCatchingAllNoEmptyCatchBlock", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void NotSwallowRethrowingExceptionTest () { method = type.Methods.GetMethod ("NotSwallowRethrowingException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } @@ -164,8 +134,8 @@ namespace Test.Rules.Exceptions { public void NotSwallowRethrowingGeneralExceptionTest () { method = type.Methods.GetMethod ("NotSwallowRethrowingGeneralException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } @@ -173,48 +143,40 @@ namespace Test.Rules.Exceptions { public void NotSwallowCatchingSpecificExceptionTest () { method = type.Methods.GetMethod ("NotSwallowCatchingSpecificException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNull (messageCollection); + Assert.AreEqual (RuleResult.Success, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (0, runner.Defects.Count, "Count"); } [Test] public void NotSwallowThrowingANewExceptionTest () { method = type.Methods.GetMethod ("NotSwallowThrowingANewException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void NotSwallowCatchingAllThrowingANewExceptionTest () { method = type.Methods.GetMethod ("NotSwallowCatchingAllThrowingANewException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void NotSwallowCatchingTypeExceptionThrowingANewExceptionTest () { method = type.Methods.GetMethod ("NotSwallowCatchingTypeExceptionThrowingANewException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } [Test] public void NotSwallowCatchingSystemExceptionThrowingANewExceptionTest () { method = type.Methods.GetMethod ("NotSwallowCatchingSystemExceptionThrowingANewException", Type.EmptyTypes); - messageCollection = rule.CheckMethod (method, new MinimalRunner ()); - Assert.IsNotNull (messageCollection); - Assert.AreEqual (messageCollection.Count, 1); - CheckMessageType (messageCollection, MessageType.Error); + Assert.AreEqual (RuleResult.Failure, runner.CheckMethod (method), "RuleResult"); + Assert.AreEqual (1, runner.Defects.Count, "Count"); } //Methods for make the tests @@ -238,7 +200,6 @@ namespace Test.Rules.Exceptions { } } - public void SwallowErrorsCatchingSystemExceptionEmptyCatchBlock () { try { @@ -248,8 +209,6 @@ namespace Test.Rules.Exceptions { } } - - public void SwallowErrorsCatchingSystemExceptionNoEmptyCatchBlock () { try { |