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

« back to all changes in this revision

Viewing changes to external/nrefactory/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.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
// CreatePropertyTests.cs
 
3
//  
 
4
// Author:
 
5
//       Mike KrĆ¼ger <mkrueger@xamarin.com>
 
6
// 
 
7
// Copyright (c) 2012 Xamarin <http://xamarin.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
using System;
 
27
using NUnit.Framework;
 
28
using ICSharpCode.NRefactory.CSharp.Refactoring;
 
29
 
 
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
 
31
{
 
32
        [TestFixture]
 
33
        public class CreatePropertyTests : ContextActionTestBase
 
34
        {
 
35
                [Test()]
 
36
                public void TestSimpleMethodCall ()
 
37
                {
 
38
                        string result = RunContextAction (
 
39
                                new CreatePropertyAction (),
 
40
                                "using System;" + Environment.NewLine +
 
41
                                        "class TestClass" + Environment.NewLine +
 
42
                                        "{" + Environment.NewLine +
 
43
                                        "       void Test ()" + Environment.NewLine +
 
44
                                        "       {" + Environment.NewLine +
 
45
                                        "               Console.WriteLine ($Foo);" + Environment.NewLine +
 
46
                                        "       }" + Environment.NewLine +
 
47
                                        "}"
 
48
                        );
 
49
                        Assert.AreEqual (
 
50
                                "using System;" + Environment.NewLine +
 
51
                                "class TestClass" + Environment.NewLine +
 
52
                                "{" + Environment.NewLine +
 
53
                                "       object Foo {" + Environment.NewLine +
 
54
                                "               get;" + Environment.NewLine +
 
55
                                "               set;" + Environment.NewLine +
 
56
                                "       }" + Environment.NewLine +
 
57
                                "       void Test ()" + Environment.NewLine +
 
58
                                "       {" + Environment.NewLine +
 
59
                                "               Console.WriteLine (Foo);" + Environment.NewLine +
 
60
                                "       }" + Environment.NewLine +
 
61
                                "}", result);
 
62
                }
 
63
 
 
64
                [Test()]
 
65
                public void TestAssignment ()
 
66
                {
 
67
                        string result = RunContextAction (
 
68
                                new CreatePropertyAction (),
 
69
                                "using System;" + Environment.NewLine +
 
70
                                        "class TestClass" + Environment.NewLine +
 
71
                                        "{" + Environment.NewLine +
 
72
                                        "       void Test ()" + Environment.NewLine +
 
73
                                        "       {" + Environment.NewLine +
 
74
                                        "               $Foo = 0x10;" + Environment.NewLine +
 
75
                                        "       }" + Environment.NewLine +
 
76
                                        "}"
 
77
                        );
 
78
 
 
79
                        Assert.AreEqual (
 
80
                                "using System;" + Environment.NewLine +
 
81
                                "class TestClass" + Environment.NewLine +
 
82
                                "{" + Environment.NewLine +
 
83
                                "       int Foo {" + Environment.NewLine +
 
84
                                "               get;" + Environment.NewLine +
 
85
                                "               set;" + Environment.NewLine +
 
86
                                "       }" + Environment.NewLine +
 
87
                                "       void Test ()" + Environment.NewLine +
 
88
                                "       {" + Environment.NewLine +
 
89
                                "               Foo = 0x10;" + Environment.NewLine +
 
90
                                "       }" + Environment.NewLine +
 
91
                                "}", result);
 
92
                }
 
93
 
 
94
                [Test()]
 
95
                public void TestOutParamCall ()
 
96
                {
 
97
                        string result = RunContextAction (
 
98
                                new CreatePropertyAction (),
 
99
                                "using System;" + Environment.NewLine +
 
100
                                        "class TestClass" + Environment.NewLine +
 
101
                                        "{" + Environment.NewLine +
 
102
                                        "       void FooBar(out string par) {}" + Environment.NewLine +
 
103
                                        "       void Test ()" + Environment.NewLine +
 
104
                                        "       {" + Environment.NewLine +
 
105
                                        "               FooBar(out $Foo);" + Environment.NewLine +
 
106
                                        "       }" + Environment.NewLine +
 
107
                                        "}"
 
108
                        );
 
109
                        
 
110
                        Assert.AreEqual (
 
111
                                "using System;" + Environment.NewLine +
 
112
                                "class TestClass" + Environment.NewLine +
 
113
                                "{" + Environment.NewLine +
 
114
                                "       void FooBar(out string par) {}" + Environment.NewLine +
 
115
                                "       string Foo {" + Environment.NewLine +
 
116
                                "               get;" + Environment.NewLine +
 
117
                                "               set;" + Environment.NewLine +
 
118
                                "       }" + Environment.NewLine +
 
119
                                "       void Test ()" + Environment.NewLine +
 
120
                                "       {" + Environment.NewLine +
 
121
                                "               FooBar(out Foo);" + Environment.NewLine +
 
122
                                "       }" + Environment.NewLine +
 
123
                                "}", result);
 
124
                }
 
125
 
 
126
                [Test()]
 
127
                public void TestStaticProperty ()
 
128
                {
 
129
                        string result = RunContextAction (
 
130
                                new CreatePropertyAction (),
 
131
                                "using System;" + Environment.NewLine +
 
132
                                        "class TestClass" + Environment.NewLine +
 
133
                                        "{" + Environment.NewLine +
 
134
                                        "       static void Test ()" + Environment.NewLine +
 
135
                                        "       {" + Environment.NewLine +
 
136
                                        "               $Foo = 0x10;" + Environment.NewLine +
 
137
                                        "       }" + Environment.NewLine +
 
138
                                        "}"
 
139
                        );
 
140
 
 
141
                        Assert.AreEqual (
 
142
                                "using System;" + Environment.NewLine +
 
143
                                "class TestClass" + Environment.NewLine +
 
144
                                "{" + Environment.NewLine +
 
145
                                "       static int Foo {" + Environment.NewLine +
 
146
                                "               get;" + Environment.NewLine +
 
147
                                "               set;" + Environment.NewLine +
 
148
                                "       }" + Environment.NewLine +
 
149
                                "       static void Test ()" + Environment.NewLine +
 
150
                                "       {" + Environment.NewLine +
 
151
                                "               Foo = 0x10;" + Environment.NewLine +
 
152
                                "       }" + Environment.NewLine +
 
153
                                "}", result);
 
154
                }
 
155
 
 
156
                public void TestCreateProperty (string input, string output)
 
157
                {
 
158
                        string result = RunContextAction (new CreatePropertyAction (), CreateMethodDeclarationTests.HomogenizeEol (input));
 
159
                        bool passed = result == output;
 
160
                        if (!passed) {
 
161
                                Console.WriteLine ("-----------Expected:");
 
162
                                Console.WriteLine (output);
 
163
                                Console.WriteLine ("-----------Got:");
 
164
                                Console.WriteLine (result);
 
165
                        }
 
166
                        Assert.AreEqual (CreateMethodDeclarationTests.HomogenizeEol (output), result);
 
167
                }
 
168
 
 
169
                [Test()]
 
170
                public void TestExternProperty ()
 
171
                {
 
172
                        TestCreateProperty (
 
173
@"
 
174
interface FooBar
 
175
{
 
176
}
 
177
 
 
178
class TestClass
 
179
{
 
180
        void TestMethod ()
 
181
        {
 
182
                FooBar fb;
 
183
                fb.$NonExistantProperty = 5;
 
184
        }
 
185
}
 
186
", @"
 
187
interface FooBar
 
188
{
 
189
        int NonExistantProperty {
 
190
                get;
 
191
                set;
 
192
        }
 
193
}
 
194
 
 
195
class TestClass
 
196
{
 
197
        void TestMethod ()
 
198
        {
 
199
                FooBar fb;
 
200
                fb.NonExistantProperty = 5;
 
201
        }
 
202
}
 
203
");
 
204
                }
 
205
 
 
206
                [Test()]
 
207
                public void TestWrongContext1 ()
 
208
                {
 
209
                        // May be syntactically possible, but very unlikely.
 
210
                        TestWrongContext<CreatePropertyAction> (
 
211
                                "using System;" + Environment.NewLine +
 
212
                                        "class TestClass" + Environment.NewLine +
 
213
                                        "{" + Environment.NewLine +
 
214
                                        "       void Test ()" + Environment.NewLine +
 
215
                                        "       {" + Environment.NewLine +
 
216
                                        "               $Foo();" + Environment.NewLine +
 
217
                                        "       }" + Environment.NewLine +
 
218
                                        "}"
 
219
                        );
 
220
                }
 
221
 
 
222
                [Test()]
 
223
                public void TestStaticClassProperty ()
 
224
                {
 
225
                        // Not 100% correct input code, but should work in that case as well.
 
226
                        Test<CreatePropertyAction> (@"static class TestClass
 
227
{
 
228
        public TestClass ()
 
229
        {
 
230
                $Foo = 5;
 
231
        }
 
232
}", @"static class TestClass
 
233
{
 
234
        static int Foo {
 
235
                get;
 
236
                set;
 
237
        }
 
238
        public TestClass ()
 
239
        {
 
240
                Foo = 5;
 
241
        }
 
242
}");
 
243
                }
 
244
 
 
245
                [Test()]
 
246
                public void CreateStaticPropertyInCurrentType()
 
247
                {
 
248
                        Test<CreatePropertyAction> (@"class TestClass
 
249
{
 
250
        public TestClass ()
 
251
        {
 
252
                TestClass.$Foo = 5;
 
253
        }
 
254
}", @"class TestClass
 
255
{
 
256
        static int Foo {
 
257
                get;
 
258
                set;
 
259
        }
 
260
        public TestClass ()
 
261
        {
 
262
                TestClass.Foo = 5;
 
263
        }
 
264
}");
 
265
                }
 
266
 
 
267
                [Test]
 
268
                public void TestEnumCase()
 
269
                {
 
270
                        TestWrongContext<CreatePropertyAction>(@"
 
271
enum AEnum { A }
 
272
class Foo
 
273
{
 
274
        public void Test ()
 
275
        {
 
276
                AEnum e = AEnum.B$ar;
 
277
        }
 
278
}
 
279
");
 
280
                }
 
281
 
 
282
                [Test()]
 
283
                public void TestPropertyFromObjectInitializer ()
 
284
                {
 
285
                        TestCreateProperty (
 
286
                                @"class TestClass
 
287
{
 
288
        void TestMethod ()
 
289
        {
 
290
                new TestClass {
 
291
                        $NonExistantProperty = 5
 
292
                };
 
293
        }
 
294
}
 
295
", @"class TestClass
 
296
{
 
297
        int NonExistantProperty {
 
298
                get;
 
299
                set;
 
300
        }
 
301
        void TestMethod ()
 
302
        {
 
303
                new TestClass {
 
304
                        NonExistantProperty = 5
 
305
                };
 
306
        }
 
307
}
 
308
");
 
309
                }
 
310
 
 
311
                [Test()]
 
312
                public void TestPropertyFromObjectInitializerInStaticMember ()
 
313
                {
 
314
                        TestCreateProperty (
 
315
                                @"class TestClass
 
316
{
 
317
        static void TestMethod ()
 
318
        {
 
319
                new TestClass {
 
320
                        $NonExistantProperty = 5
 
321
                };
 
322
        }
 
323
}
 
324
", @"class TestClass
 
325
{
 
326
        int NonExistantProperty {
 
327
                get;
 
328
                set;
 
329
        }
 
330
        static void TestMethod ()
 
331
        {
 
332
                new TestClass {
 
333
                        NonExistantProperty = 5
 
334
                };
 
335
        }
 
336
}
 
337
");
 
338
                }
 
339
 
 
340
                [Test()]
 
341
                public void TestNonStaticPropertyInStaticMethod ()
 
342
                {
 
343
                        TestCreateProperty (@"class TestClass
 
344
{
 
345
        static void Foo ()
 
346
        {
 
347
                new TestClass ().$NonExistantProperty = 5;
 
348
        }
 
349
}", @"class TestClass
 
350
{
 
351
        int NonExistantProperty {
 
352
                get;
 
353
                set;
 
354
        }
 
355
        static void Foo ()
 
356
        {
 
357
                new TestClass ().NonExistantProperty = 5;
 
358
        }
 
359
}");
 
360
                }
 
361
        }
 
362
}
 
 
b'\\ No newline at end of file'