2
// CreatePropertyTests.cs
5
// Mike KrĆ¼ger <mkrueger@xamarin.com>
7
// Copyright (c) 2012 Xamarin <http://xamarin.com>
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;
30
namespace ICSharpCode.NRefactory.CSharp.CodeActions
33
public class CreatePropertyTests : ContextActionTestBase
36
public void TestSimpleMethodCall ()
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 +
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 +
65
public void TestAssignment ()
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 +
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 +
95
public void TestOutParamCall ()
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 +
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 +
127
public void TestStaticProperty ()
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 +
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 +
156
public void TestCreateProperty (string input, string output)
158
string result = RunContextAction (new CreatePropertyAction (), CreateMethodDeclarationTests.HomogenizeEol (input));
159
bool passed = result == output;
161
Console.WriteLine ("-----------Expected:");
162
Console.WriteLine (output);
163
Console.WriteLine ("-----------Got:");
164
Console.WriteLine (result);
166
Assert.AreEqual (CreateMethodDeclarationTests.HomogenizeEol (output), result);
170
public void TestExternProperty ()
183
fb.$NonExistantProperty = 5;
189
int NonExistantProperty {
200
fb.NonExistantProperty = 5;
207
public void TestWrongContext1 ()
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 +
223
public void TestStaticClassProperty ()
225
// Not 100% correct input code, but should work in that case as well.
226
Test<CreatePropertyAction> (@"static class TestClass
232
}", @"static class TestClass
246
public void CreateStaticPropertyInCurrentType()
248
Test<CreatePropertyAction> (@"class TestClass
254
}", @"class TestClass
268
public void TestEnumCase()
270
TestWrongContext<CreatePropertyAction>(@"
276
AEnum e = AEnum.B$ar;
283
public void TestPropertyFromObjectInitializer ()
291
$NonExistantProperty = 5
297
int NonExistantProperty {
304
NonExistantProperty = 5
312
public void TestPropertyFromObjectInitializerInStaticMember ()
317
static void TestMethod ()
320
$NonExistantProperty = 5
326
int NonExistantProperty {
330
static void TestMethod ()
333
NonExistantProperty = 5
341
public void TestNonStaticPropertyInStaticMethod ()
343
TestCreateProperty (@"class TestClass
347
new TestClass ().$NonExistantProperty = 5;
349
}", @"class TestClass
351
int NonExistantProperty {
357
new TestClass ().NonExistantProperty = 5;
b'\\ No newline at end of file'