~ubuntu-branches/ubuntu/saucy/monodevelop/saucy

« back to all changes in this revision

Viewing changes to external/mono-tools/gendarme/rules/Test.Rules/Fixtures/RuleTestFixture.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2012-05-27 18:08:20 UTC
  • mfrom: (1.8.5) (1.5.8 sid)
  • Revision ID: package-import@ubuntu.com-20120527180820-f1ub6lhg0s50wci1
Tags: 3.0.2+dfsg-3
* [fcecfe7] Fix monodevelop-core-addins.pc.in to point to actual 
  installed location of assemblies.
* [26e1a07] DebSrc 3.0 does not support Quilt's -p parameter, so 
  manually adjust the path in the patch file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// Test.Rules.Fixtures.RuleTestFixture<T>
 
3
// Base class for rule test fixtures that simplifies writing unit tests for Gendarme.
 
4
//
 
5
// Authors:
 
6
//      Daniel Abramov <ex@vingrad.ru>
 
7
//
 
8
// Copyright (C) 2008 Daniel Abramov
 
9
//
 
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:
 
17
//
 
18
// The above copyright notice and this permission notice shall be
 
19
// included in all copies or substantial portions of the Software.
 
20
//
 
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.
 
28
//
 
29
 
 
30
using System;
 
31
using System.Reflection;
 
32
 
 
33
using Gendarme.Framework;
 
34
using Test.Rules.Helpers;
 
35
 
 
36
using Mono.Cecil;
 
37
using NUnit.Framework;
 
38
 
 
39
namespace Test.Rules.Fixtures {
 
40
        
 
41
        /// <summary>
 
42
        /// Base class inRuleTestFixture helpers hierarchy providing methods for making assertions on rule execution results.
 
43
        /// </summary>
 
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 () {
 
48
 
 
49
                private TRule rule;
 
50
                private TestRunner runner;
 
51
 
 
52
                public TRule Rule {
 
53
                        get { return rule; }
 
54
                }
 
55
 
 
56
                public Runner Runner {
 
57
                        get { return runner; }
 
58
                }
 
59
 
 
60
                public bool FireEvents { get; set; }
 
61
 
 
62
                /// <summary>
 
63
                /// Creates a RuleTestFixture instance, the runner and the rule itself.
 
64
                /// </summary>
 
65
                protected RuleTestFixture ()
 
 
b'\t\t{'
 
66
                        rule = new TRule ();
 
67
                        runner = new TestRunner (rule);
 
68
                }
 
69
                
 
70
                /// <summary>
 
71
                /// Asserts that the rule does not apply to a particular token. 
 
72
                /// </summary>
 
73
                /// <param name="token">Cecil metadata token to check.</param>
 
74
                protected void AssertRuleDoesNotApply (TMetadataToken token)
 
75
                {
 
76
                        RunRuleAndCheckResults (token, RuleResult.DoesNotApply, null);
 
77
                }               
 
78
                
 
79
                /// <summary>
 
80
                /// Asserts that the rule has been executed successfully. 
 
81
                /// </summary>
 
82
                /// <param name="token">Cecil metadata token to check.</param>
 
83
                protected void AssertRuleSuccess (TMetadataToken token)
 
84
                {
 
85
                        RunRuleAndCheckResults (token, RuleResult.Success, null);
 
86
                }               
 
87
                                                
 
88
                /// <summary>
 
89
                /// Asserts that the rule has failed to execute successfully. 
 
90
                /// </summary>
 
91
                /// <param name="token">Cecil metadata token to check.</param>
 
92
                protected void AssertRuleFailure (TMetadataToken token)
 
93
                {
 
94
                        RunRuleAndCheckResults (token, RuleResult.Failure, null);
 
95
                }
 
96
 
 
97
                /// <summary>
 
98
                /// Asserts that the rule has failed to execute successfully. 
 
99
                /// </summary>
 
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)
 
103
                {
 
104
                        RunRuleAndCheckResults (token, RuleResult.Failure, expectedCount);
 
105
                }
 
106
                                        
 
107
                /// <summary>
 
108
                /// Runs the rule and checks the results against the specified matcher.
 
109
                /// </summary>
 
110
                private void RunRuleAndCheckResults (TMetadataToken token, RuleResult expectedResult, int? expectedDefectCount)
 
111
                {
 
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);
 
115
 
 
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);
 
120
                        }
 
121
                }
 
122
                                                
 
123
                /// <summary>
 
124
                /// Runs the rule, depending on its type.
 
125
                /// </summary>
 
126
                private RuleResult RunRule (TMetadataToken token)
 
127
                {
 
128
                        if (token == null)
 
129
                                throw new ArgumentNullException ("token");
 
130
 
 
131
                        MethodDefinition md = (token as MethodDefinition);
 
132
                        if (md != null) {
 
133
                                if (FireEvents) {
 
134
                                        runner.OnAssembly (md.DeclaringType.Module.Assembly);
 
135
                                        runner.OnType (md.DeclaringType);
 
136
                                        runner.OnMethod (md);
 
137
                                }
 
138
                                return runner.CheckMethod (token as MethodDefinition);
 
139
                        }
 
140
 
 
141
                        TypeDefinition td = (token as TypeDefinition);
 
142
                        if (td != null) {
 
143
                                if (FireEvents) {
 
144
                                        runner.OnAssembly (md.DeclaringType.Module.Assembly);
 
145
                                        runner.OnType (md.DeclaringType);
 
146
                                }
 
147
                                return runner.CheckType (td);
 
148
                        }
 
149
 
 
150
                        AssemblyDefinition ad = (token as AssemblyDefinition);
 
151
                        if (ad != null) {
 
152
                                if (FireEvents)
 
153
                                        runner.OnAssembly (td.Module.Assembly);
 
154
                                return runner.CheckAssembly (ad);
 
155
                        }
 
156
 
 
157
                        throw new NotImplementedException (token.GetType ().ToString ());
 
158
                }
 
159
        }
 
160
}