~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeIssues/RedundantTypeCastIssueTests.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
Import upstream version 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// RedundantTypeCastIssueTests.cs
 
3
// 
 
4
// Author:
 
5
//      Mansheng Yang <lightyang0@gmail.com>
 
6
// 
 
7
// Copyright (c) 2012 Mansheng Yang <lightyang0@gmail.com>
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
28
using NUnit.Framework;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeIssues
 
31
{
 
32
        [TestFixture]
 
33
        public class RedundantTypeCastIssueTests : InspectionActionTestBase
 
34
        {
 
35
                [Test]
 
36
                public void TestSameType ()
 
37
                {
 
38
                        var input = @"
 
39
class TestClass
 
40
{
 
41
        void TestMethod ()
 
42
        {
 
43
                int i = 0;
 
44
                var i2 = ((int)i);
 
45
        }
 
46
}";
 
47
                        var output = @"
 
48
class TestClass
 
49
{
 
50
        void TestMethod ()
 
51
        {
 
52
                int i = 0;
 
53
                var i2 = i;
 
54
        }
 
55
}";
 
56
                        Test<RedundantTypeCastIssue> (input, 1, output);
 
57
                }
 
58
 
 
59
                [Test]
 
60
                public void TestInvocation ()
 
61
                {
 
62
                        var input = @"
 
63
class TestClass
 
64
{
 
65
        void Test (object obj)
 
66
        {
 
67
        }
 
68
        void TestMethod (object obj)
 
69
        {
 
70
                Test ((int)obj);
 
71
        }
 
72
}";
 
73
                        var output = @"
 
74
class TestClass
 
75
{
 
76
        void Test (object obj)
 
77
        {
 
78
        }
 
79
        void TestMethod (object obj)
 
80
        {
 
81
                Test (obj);
 
82
        }
 
83
}";
 
84
                        Test<RedundantTypeCastIssue> (input, 1, output);
 
85
                }
 
86
 
 
87
                [Test]
 
88
                public void TestLambdaInvocation ()
 
89
                {
 
90
                        var input = @"
 
91
class TestClass
 
92
{
 
93
        void TestMethod (object obj)
 
94
        {
 
95
                System.Action<object> a;
 
96
                a ((int)obj);
 
97
        }
 
98
}";
 
99
                        var output = @"
 
100
class TestClass
 
101
{
 
102
        void TestMethod (object obj)
 
103
        {
 
104
                System.Action<object> a;
 
105
                a (obj);
 
106
        }
 
107
}";
 
108
                        Test<RedundantTypeCastIssue> (input, 1, output);
 
109
                }
 
110
 
 
111
                [Test]
 
112
                public void TestMember ()
 
113
                {
 
114
                        var input = @"
 
115
class TestClass
 
116
{
 
117
        void TestMethod (object obj)
 
118
        {
 
119
                var str = (obj as TestClass).ToString ();
 
120
        }
 
121
}";
 
122
                        var output = @"
 
123
class TestClass
 
124
{
 
125
        void TestMethod (object obj)
 
126
        {
 
127
                var str = obj.ToString ();
 
128
        }
 
129
}";
 
130
                        Test<RedundantTypeCastIssue> (input, 1, output);
 
131
                }
 
132
 
 
133
                [Test]
 
134
                public void TestNoIssue ()
 
135
                {
 
136
                        var input = @"
 
137
class TestClass
 
138
{
 
139
        void Test (int k) { }
 
140
        void TestMethod (object obj)
 
141
        {
 
142
                int i = (int)obj + 1;
 
143
                Test ((long) obj);
 
144
                (obj as TestClass).Test (0);
 
145
        }
 
146
}";
 
147
                        Test<RedundantTypeCastIssue> (input, 0);
 
148
                }
 
149
 
 
150
                /// <summary>
 
151
                /// Bug 7065 - "remove redundant type cast" false positive for explicit interface implementation
 
152
                /// </summary>
 
153
                [Test]
 
154
                public void TestBug7065 ()
 
155
                {
 
156
                        var input = @"
 
157
using System;
 
158
public class TestClass : IDisposable
 
159
{
 
160
        void IDisposable.Dispose()
 
161
        {
 
162
        }
 
163
 
 
164
        void Foo()
 
165
        {
 
166
            ((IDisposable)this).Dispose();
 
167
        }
 
168
}
 
169
";
 
170
                        Test<RedundantTypeCastIssue> (input, 0);
 
171
                }
 
172
 
 
173
        }
 
174
}