2
// RedundantNullCheckTests.cs
5
// Ji Kun <jikun.nus@gmail.com>
7
// Copyright (c) 2013 Ji Kun
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using NUnit.Framework;
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
29
using ICSharpCode.NRefactory.CSharp.CodeActions;
31
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
34
public class RedundantNullCheckTests : InspectionActionTestBase
37
public void TestInspectorCase1()
39
var input = @"using System;class Test {public void test(){int a = 0;if(a is int && a != null){a = 1;}}}";
41
TestRefactoringContext context;
42
var issues = GetIssues(new RedundantNullCheckIssue(), input, out context);
43
Assert.AreEqual(1, issues.Count);
46
CheckFix(context, issues, @"using System;class Test {public void test(){int a = 0;
53
public void TestResharperDisable()
55
var input = @"using System;
59
//Resharper disable RedundantNullCheck
60
if(a is int && a != null)
62
//Resharper restore RedundantNullCheck
66
TestRefactoringContext context;
67
var issues = GetIssues(new RedundantNullCheckIssue(), input, out context);
68
Assert.AreEqual(0, issues.Count);
72
public void TestInspectorCase2()
74
var input = @"using System;class Test {public void test(){int a = 0;while(a != null && a is int){a = 1;}}}";
76
TestRefactoringContext context;
77
var issues = GetIssues(new RedundantNullCheckIssue(), input, out context);
78
Assert.AreEqual(1, issues.Count);
79
CheckFix(context, issues, @"using System;class Test {public void test(){int a = 0;
87
public void TestCaseWithFullParens()
93
public void Test(object o)
95
if (!((o is int) && (o != null))) {
100
TestRefactoringContext context;
101
var issues = GetIssues(new RedundantNullCheckIssue(), input, out context);
102
Assert.AreEqual(1, issues.Count);
103
CheckFix(context, issues, @"using System;
106
public void Test(object o)
114
[Ignore("Extended version")]
116
public void TestNegatedCase()
122
public void Test(object o)
124
if (null == o || !(o is int)) {
129
TestRefactoringContext context;
130
var issues = GetIssues(new RedundantNullCheckIssue(), input, out context);
131
Assert.AreEqual(1, issues.Count);
132
CheckFix(context, issues, @"using System;
135
public void Test(object o)
b'\\ No newline at end of file'