2
// Test.Rules.Fixtures.RuleTestFixture<T>
3
// Base class for rule test fixtures that simplifies writing unit tests for Gendarme.
6
// Daniel Abramov <ex@vingrad.ru>
8
// Copyright (C) 2008 Daniel Abramov
10
// Permission is hereby granted, free of charge, to any person obtaining
11
// a copy of this software and associated documentation files (the
12
// "Software"), to deal in the Software without restriction, including
13
// without limitation the rights to use, copy, modify, merge, publish,
14
// distribute, sublicense, and/or sell copies of the Software, and to
15
// permit persons to whom the Software is furnished to do so, subject to
16
// the following conditions:
18
// The above copyright notice and this permission notice shall be
19
// included in all copies or substantial portions of the Software.
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31
using System.Reflection;
33
using Gendarme.Framework;
34
using Test.Rules.Helpers;
37
using NUnit.Framework;
39
namespace Test.Rules.Fixtures {
42
/// Base class inRuleTestFixture helpers hierarchy providing methods for making assertions on rule execution results.
44
/// <typeparam name="TRule">Type of rule to be tested (e.g. IMethodRule, ITypeRule).</typeparam>
45
/// <typeparam name="TMetadataToken">Type of Cecil metadata token that TRule handles (e.g. MethodDefinition for IMethodRule).</typeparam>
46
public abstract class RuleTestFixture<TRule, TMetadataToken>
47
where TRule : IRule, new () {
50
private TestRunner runner;
56
public Runner Runner {
57
get { return runner; }
60
public bool FireEvents { get; set; }
63
/// Creates a RuleTestFixture instance, the runner and the rule itself.
65
protected RuleTestFixture ()
67
runner = new TestRunner (rule);
71
/// Asserts that the rule does not apply to a particular token.
73
/// <param name="token">Cecil metadata token to check.</param>
74
protected void AssertRuleDoesNotApply (TMetadataToken token)
76
RunRuleAndCheckResults (token, RuleResult.DoesNotApply, null);
80
/// Asserts that the rule has been executed successfully.
82
/// <param name="token">Cecil metadata token to check.</param>
83
protected void AssertRuleSuccess (TMetadataToken token)
85
RunRuleAndCheckResults (token, RuleResult.Success, null);
89
/// Asserts that the rule has failed to execute successfully.
91
/// <param name="token">Cecil metadata token to check.</param>
92
protected void AssertRuleFailure (TMetadataToken token)
94
RunRuleAndCheckResults (token, RuleResult.Failure, null);
98
/// Asserts that the rule has failed to execute successfully.
100
/// <param name="token">Cecil metadata token to check.</param>
101
/// <param name="expectedCount">Expected defects count.</param>
102
protected void AssertRuleFailure (TMetadataToken token, int expectedCount)
104
RunRuleAndCheckResults (token, RuleResult.Failure, expectedCount);
108
/// Runs the rule and checks the results against the specified matcher.
110
private void RunRuleAndCheckResults (TMetadataToken token, RuleResult expectedResult, int? expectedDefectCount)
112
RuleResult result = RunRule (token);
113
Assert.AreEqual (expectedResult, result, "{0} failed on {1}: result should be {2} but got {3}.",
114
typeof (TRule).Name, token, expectedResult, result);
116
if (expectedDefectCount.HasValue) {
117
Assert.AreEqual (expectedDefectCount.Value, runner.Defects.Count,
118
"{0} failed on {1}: should have {2} defects but got {3}.",
119
typeof (TRule).Name, token, expectedDefectCount.Value, runner.Defects.Count);
124
/// Runs the rule, depending on its type.
126
private RuleResult RunRule (TMetadataToken token)
129
throw new ArgumentNullException ("token");
131
MethodDefinition md = (token as MethodDefinition);
134
runner.OnAssembly (md.DeclaringType.Module.Assembly);
135
runner.OnType (md.DeclaringType);
136
runner.OnMethod (md);
138
return runner.CheckMethod (token as MethodDefinition);
141
TypeDefinition td = (token as TypeDefinition);
144
runner.OnAssembly (md.DeclaringType.Module.Assembly);
145
runner.OnType (md.DeclaringType);
147
return runner.CheckType (td);
150
AssemblyDefinition ad = (token as AssemblyDefinition);
153
runner.OnAssembly (td.Module.Assembly);
154
return runner.CheckAssembly (ad);
157
throw new NotImplementedException (token.GetType ().ToString ());