forked from mono/mono-tools
/
PreferTryParseTest.cs
217 lines (190 loc) · 5.58 KB
/
PreferTryParseTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//
// Unit tests for PreferTryParseRule
//
// Authors:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2010 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.Globalization;
using Gendarme.Framework;
using Gendarme.Rules.BadPractice;
using NUnit.Framework;
using Test.Rules.Definitions;
using Test.Rules.Fixtures;
using System.Diagnostics.CodeAnalysis;
using Test.Rules.Helpers;
namespace Test.Rules.BadPractice {
[TestFixture]
public class PreferTryParseTest : MethodRuleTestFixture<PreferTryParseRule> {
[Test]
public void DoesNotApply ()
{
// no IL
AssertRuleDoesNotApply (SimpleMethods.ExternalMethod);
// no CALL[VIRT] instruction
AssertRuleDoesNotApply (SimpleMethods.EmptyMethod);
}
class ParseWithTryCatch {
private int Int32Parse ()
{
try {
return Int32.Parse ("12") + Int32.Parse ("-12", NumberStyles.AllowCurrencySymbol);
}
catch {
return 0;
}
}
}
[Test]
public void Bad ()
{
AssertRuleFailure<ParseWithTryCatch> ("Int32Parse", 2);
Assert.AreEqual (Severity.Medium, Runner.Defects [0].Severity, "Severity");
Assert.AreEqual (Confidence.Normal, Runner.Defects [0].Confidence, "Confidence");
}
class ParseWithoutTryCatch {
private int Int32Parse ()
{
return Int32.Parse ("12") + Int32.Parse ("-12", NumberStyles.AllowCurrencySymbol);
}
}
class ParseOutsideTryCatch {
private int Int32Parse (string s)
{
try {
if (String.IsNullOrEmpty (s))
return 0;
return (int) (s [0] - '0');
}
catch (Exception) {
return Int32.Parse (s);
}
}
}
[Test]
public void Worse ()
{
AssertRuleFailure<ParseWithoutTryCatch> ("Int32Parse", 2);
Assert.AreEqual (Severity.High, Runner.Defects [0].Severity, "Severity-1");
Assert.AreEqual (Confidence.High, Runner.Defects [0].Confidence, "Confidence-1");
AssertRuleFailure<ParseOutsideTryCatch> ("Int32Parse", 1);
Assert.AreEqual (Severity.High, Runner.Defects [0].Severity, "Severity-2");
Assert.AreEqual (Confidence.High, Runner.Defects [0].Confidence, "Confidence-2");
}
class TryParse {
private int Int32Parse ()
{
int i;
if (!Int32.TryParse ("12", out i))
i = 0;
return i;
}
}
class AtypicalParse {
private bool Parse ()
{
return true;
}
public void CallParse ()
{
if (!Parse ())
Console.WriteLine ();
}
}
class NoTryParse {
static NoTryParse Parse (string s)
{
return new NoTryParse ();
}
public void CallParse ()
{
NoTryParse.Parse (null);
}
}
class BadTryParse {
// bad candidate - does not return 'bool'
static void TryParse (string s, out BadTryParse btp)
{
btp = new BadTryParse ();
}
// bad candidate - first parameter is not 'string'
static bool TryParse (char c, out BadTryParse btp)
{
btp = new BadTryParse ();
return true;
}
// bad candidate - last parameter is not 'out <type>'
static bool TryParse (string s, BadTryParse btp)
{
btp = new BadTryParse ();
return true;
}
static BadTryParse Parse (string s)
{
return new BadTryParse ();
}
public void CallParse ()
{
BadTryParse btp = null;
BadTryParse.TryParse (String.Empty, out btp);
BadTryParse.TryParse ('a', out btp);
BadTryParse.TryParse (String.Empty, btp);
// below is valid since there was no valid TryParse cnadidate
BadTryParse.Parse (null);
}
}
[Test]
public void Good ()
{
AssertRuleSuccess<TryParse> ("Int32Parse");
AssertRuleSuccess<AtypicalParse> ("CallParse");
AssertRuleSuccess<NoTryParse> ("CallParse");
AssertRuleSuccess<BadTryParse> ("CallParse");
}
}
[TestFixture]
public class PreferTryParseWithSuppressionsTest : MethodRuleTestFixture<PreferTryParseRule>
{
public static class DelegateHolder
{
public static Func<DateTime> Now = () => DateTime.Now;
}
public class SuppressionTest
{
public void ParseDateInDelegate()
{
DelegateHolder.Now = () => DateTime.Parse("4/6/2011 9:38:12 PM", CultureInfo.CurrentCulture);
}
}
[Test]
public void ProperlySuppresses()
{
Runner.IgnoreList.Add("Gendarme.Rules.BadPractice.PreferTryParseRule", DefinitionLoader.GetMethodDefinition<SuppressionTest>("ParseDateInDelegate"));
foreach (var method in DefinitionLoader.GetTypeDefinition<SuppressionTest>().Methods)
{
//the delegate in ParseDataInDelegate (which shows up as another method on the given type) should pick up the rule applied above, but doesn't
if (!method.IsConstructor)
AssertRuleDoesNotApply(method);
}
}
}
}