2
// ConvertToInitializerTests.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
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
26
using NUnit.Framework;
27
using ICSharpCode.NRefactory.CSharp.Refactoring;
28
using ICSharpCode.NRefactory.PatternMatching;
31
namespace ICSharpCode.NRefactory.CSharp.CodeActions
34
public class ConvertToInitializerTests : ContextActionTestBase
36
// TODO: Remove this when the formatter handles object and collection initializers
37
// This tests the expected code vs the actual code based on their ASTs instead of the text they produce.
38
public new void Test<T>(string input, string output, int action = 0, bool expectErrors = false)
39
where T : ICodeActionProvider, new ()
41
string result = RunContextAction(new T(), HomogenizeEol(input), action, expectErrors);
43
var expectedContext = TestRefactoringContext.Create(output, expectErrors);
44
var actualContext = TestRefactoringContext.Create(result, expectErrors);
46
bool passed = expectedContext.RootNode.IsMatch(actualContext.RootNode);
48
Console.WriteLine("-----------Expected:");
49
Console.WriteLine(output);
50
Console.WriteLine("-----------Got:");
51
Console.WriteLine(result);
53
Assert.IsTrue(passed, "The generated code and the expected code was not syntactically identical. See output for details.");
59
public string Property { get; set; }
61
public TestClass Nested { get; set; }
68
public void SimpleObject()
70
Test<ConvertToInitializerAction>(baseText + @"
71
var variable = new Test$Class ();
72
variable.Property = ""Value"";
75
var variable = new TestClass () {
83
public void SimpleCollection()
85
Test<ConvertToInitializerAction>(baseText + @"
86
var collection = new System.Collections.Generic.List$<string> ();
87
collection.Add(""string1"");
88
collection.Add(""string2"");
91
var collection = new System.Collections.Generic.List<string> () {
100
public void MultiElementCollection()
102
Test<ConvertToInitializerAction>(baseText + @"
103
var tc0 = new TestClass();
104
var collection = new System.Collections.Generic.Dictionary$<string, TestClass> ();
105
var tc1 = new TestClass();
106
tc1.Property = ""tc1"";
107
collection.Add(""string1"", tc1);
108
var tc2 = new TestClass();
109
tc2.Property = ""tc2"";
110
collection.Add(""string2"", tc2);
111
collection.Add(""string0"", tc0);
114
var tc0 = new TestClass();
115
var collection = new System.Collections.Generic.Dictionary<string, TestClass> () {
116
{""string1"", new TestClass() { Property = ""tc1"" }},
117
{""string2"", new TestClass() { Property = ""tc2"" }}
119
collection.Add(""string0"", tc0);
125
public void CollectionOfObjects()
127
Test<ConvertToInitializerAction>(baseText + @"
128
var collection = new System.Collections.Generic.List$<string> ();
129
var item = new TestClass ();
130
item.Property = ""Value1"";
131
collection.Add(item);
132
item = new TestClass ();
133
item.Property = ""Value2"";
134
item.Nested = new TestClass ();
135
item.Nested.Property = ""Value3"";
136
collection.Add(item);
139
var collection = new System.Collections.Generic.List<string> () {
141
Property = ""Value1""
144
Property = ""Value2"",
145
Nested = new TestClass () {
146
Property = ""Value3""
155
public void UnknownTargetForAdd()
157
Test<ConvertToInitializerAction>(baseText + @"
158
var collection = new System.Collections.Generic.List<string> ();
159
var item $= new TestClass ();
160
item.Property = ""Value1"";
161
collection.Add(item);
164
var collection = new System.Collections.Generic.List<string> ();
165
var item = new TestClass () {
166
Property = ""Value1""
168
collection.Add(item);
174
public void ObjectContainingCollections()
176
Test<ConvertToInitializerAction>(baseText + @"
177
var variable = new Test$Class ();
178
variable.Property = ""Value1"";
179
var collection = new System.Collections.Generic.List<TestClass>();
180
collection.Add (new TestClass());
181
collection.Add (new TestClass());
182
collection.Add (new TestClass());
183
variable.Children = collection;
186
public System.Collections.Generic.IList<TestClass> Children;
188
var variable = new TestClass () {
189
Property = ""Value1"",
190
Children = new System.Collections.Generic.List<TestClass> () {
198
public System.Collections.Generic.IList<TestClass> Children;
203
public void SplitAssignmentAndInitialization()
205
Test<ConvertToInitializerAction>(baseText + @"
207
variable = new Test$Class ();
208
variable.Property = ""Value"";
212
variable = new TestClass () {
220
public void MemberAssignment()
222
Test<ConvertToInitializerAction>(baseText + @"
223
Nested = new Test$Class ();
224
Nested.Property = ""Value"";
227
Nested = new TestClass () {
235
public void MultipleMemberAssignments()
237
Test<ConvertToInitializerAction>(baseText + @"
238
Nested = new Test$Class ();
239
Nested = new TestClass ();
240
Nested.Property = ""Value"";
243
Nested = new TestClass () {
251
public void NonCreatingInitialization()
253
Test<ConvertToInitializerAction>(baseText + @"
254
var variable = new Test$Class ();
255
variable.Nested.Nested.Property = ""Value"";
258
var variable = new TestClass () {
270
public void IgnoresLinesPreceedingInitialization()
272
Test<ConvertToInitializerAction>(baseText + @"
273
System.Console.WriteLine(""One"");
275
System.Console.WriteLine(""Two"");
277
System.Console.WriteLine(""Three"");
278
TestClass variable = new Test$Class ();
279
variable.Property = ""Value"";
282
System.Console.WriteLine(""One"");
284
System.Console.WriteLine(""Two"");
286
System.Console.WriteLine(""Three"");
287
TestClass variable = new TestClass () {
295
public void NestedObject()
297
Test<ConvertToInitializerAction>(baseText + @"
298
var variable = new Test$Class ();
299
variable.Property = ""Value"";
300
variable.Nested = new TestClass();
301
variable.Nested.Property = ""NestedValue"";
302
variable.Nested.Nested = new TestClass();
303
variable.Nested.Nested.Property = ""NestedNestedValue"";
306
var variable = new TestClass () {
307
Property = ""Value"",
308
Nested = new TestClass () {
309
Property = ""NestedValue"",
310
Nested = new TestClass () {
311
Property = ""NestedNestedValue""
320
public void NestedObjectWithSkippedLevel()
322
Test<ConvertToInitializerAction>(baseText + @"
323
var variable = new Test$Class ();
324
variable.Property = ""Value"";
325
variable.Nested.Nested = new TestClass();
326
variable.Nested.Nested.Property = ""NestedNestedValue"";
329
var variable = new TestClass () {
330
Property = ""Value"",
332
Nested = new TestClass () {
333
Property = ""NestedNestedValue""
342
public void KeepsOriginalInitializer()
344
Test<ConvertToInitializerAction>(baseText + @"
345
var variable = new Test$Class () {
348
variable.Nested = new TestClass();
351
var variable = new TestClass () {
352
Property = ""Value"",
353
Nested = new TestClass ()
360
[Ignore("The pattern matching comparison does not find differences in comments. UnIgnore this when the regular test method is used again.")]
361
public void MovesComments()
363
Test<ConvertToInitializerAction>(baseText + @"
364
var variable = new Test$Class () {
367
// This comment should be placed above the initializer corresponding to the statement below
368
// This line should too.
369
variable.Nested = new TestClass();
372
var variable = new TestClass () {
373
Property = ""Value"",
374
// This comment should be placed above the initializer corresponding to the statement below
375
// This line should too.
376
Nested = new TestClass ()
383
public void StopsAtArbitraryStatement()
385
Test<ConvertToInitializerAction>(baseText + @"
386
var variable = new Test$Class ();
387
variable.Property = ""Value"";
388
variable.Nested = new TestClass();
389
System.Console.WriteLine("""");
390
variable.Nested.Property = ""NestedValue"";
393
var variable = new TestClass () {
394
Property = ""Value"",
395
Nested = new TestClass ()
397
System.Console.WriteLine("""");
398
variable.Nested.Property = ""NestedValue"";
404
public void StopsAtVariableDependentStatement()
406
Test<ConvertToInitializerAction>(baseText + @"
407
var variable = new Test$Class ();
408
variable.Property = ""Value"";
409
variable.Nested = new TestClass();
410
variable.Nested.Property = variable.ToString();
413
var variable = new TestClass () {
414
Property = ""Value"",
415
Nested = new TestClass ()
417
variable.Nested.Property = variable.ToString();
423
public void StopsAtVariableDependentCreationStatement()
425
Test<ConvertToInitializerAction>(baseText + @"
426
var variable = new Test$Class ();
427
variable.Property = ""Value"";
428
var nested = new TestClass(variable);
429
variable.Nested = nested;
432
var variable = new TestClass () {
435
var nested = new TestClass(variable);
436
variable.Nested = nested;
442
public void HandlesIrrelevantAccessesAtTheEnd()
444
Test<ConvertToInitializerAction>(baseText + @"
445
var tc = new TestClass();
447
var tc$2 = new TestClass();
448
tc2.Property = ""2"";
449
tc2.Nested = new TestClass();
450
tc2.Nested.Property = ""3"";
454
var tc = new TestClass();
456
var tc2 = new TestClass () {
458
Nested = new TestClass () {
468
public void HandlesAssignmentWhereRightExpressionIsNotVisited()
470
Test<ConvertToInitializerAction>(baseText + @"
471
var tc = new TestClass();
473
var tc$2 = new TestClass();
474
tc2.Property = ""2"";
479
var tc = new TestClass();
481
var tc2 = new TestClass () {
491
public void HandlesAssignmentToExistingVariable()
493
Test<ConvertToInitializerAction>(baseText + @"
494
var variable = new Test$Class ();
495
variable.Property = ""Value"";
496
variable = new TestClass();
497
variable.Nested = new TestClass();
500
var variable = new TestClass () {
501
Nested = new TestClass ()
508
public void HandlesAssignmentToFinalVariable()
510
Test<ConvertToInitializerAction>(baseText + @"
511
var _variable = new Test$Class ();
512
_variable.Property = ""Value"";
513
var variable = _variable;
516
var variable = new TestClass () {
524
public void FinalVariableAssignmentOfOtherInitializer()
526
Test<ConvertToInitializerAction>(baseText + @"
527
var tc $= new TestClass ();
528
tc.Property = ""Value"";
529
var _variable = new TestClass ();
530
var variable = _variable;
533
var tc = new TestClass () {
536
var _variable = new TestClass ();
537
var variable = _variable;
543
public void StopsAtMemberDependentStatement()
545
Test<ConvertToInitializerAction>(baseText + @"
546
Nested = new Test$Class ();
547
Nested.Nested = new TestClass();
548
Nested.Property = ""Value"";
549
Nested.Nested.Property = Nested.Property;
550
Nested.Nested.Nested = new TestClass();
553
Nested = new TestClass () {
554
Nested = new TestClass (),
557
Nested.Nested.Property = Nested.Property;
558
Nested.Nested.Nested = new TestClass();
564
public void KeepsStatementsWhichAreNotAdded()
566
Test<ConvertToInitializerAction>(baseText + @"
567
var collection = new System.Collections.Generic.List$<string> ();
568
var item = new TestClass ();
569
item.Property = ""Value1"";
570
collection.Add(item);
571
item = new TestClass ();
572
item.Property = ""Value2"";
573
item.Nested = new TestClass ();
574
item.Nested.Property = ""Value3"";
575
collection.Add(item);
576
// This should stay here
577
item = new TestClass();
580
var collection = new System.Collections.Generic.List<string> () {
582
Property = ""Value1""
585
Property = ""Value2"",
586
Nested = new TestClass () {
587
Property = ""Value3""
591
// This should stay here
592
item = new TestClass();
598
public void StopsAtUnresolvableAssignmentTarget()
600
Test<ConvertToInitializerAction>(baseText + @"
601
var variable = new Test$Class ();
602
variable.Property = ""Value"";
603
Variable.Nested = new TestClass();
606
var variable = new TestClass () {
609
Variable.Nested = new TestClass();
616
public void NoActionForSingleStepInitializers()
618
TestWrongContext<ConvertToInitializerAction>(baseText + @"
619
var variable = new Test$Class ();
620
var s = string.Empty;
626
public void IgnoresNonCreation()
628
TestWrongContext<ConvertToInitializerAction>(@"
633
System.Console.$WriteLine("""");
634
var variable = new TestClass();
640
public void IgnoresSingleCreation()
642
TestWrongContext<ConvertToInitializerAction>(@"
647
var variable = new $TestClass();
653
public void DoesNotCrashOnDeclarationInitializedToVariable()
655
TestWrongContext<ConvertToInitializerAction>(@"
662
// And another statement to make the converter even try
669
public void DoesNotCrashOnVariableDeclarationWithoutInitializer()
671
TestWrongContext<ConvertToInitializerAction>(@"
676
TestClass $s1 = new TestClass();
683
public void DoesNotCrashOnDelegateAssignment()
685
TestWrongContext<ConvertToInitializerAction>(@"
687
using System.Collections.Generic;
690
((CheckBox)ControlDictionary[""ReplaceCheckBox""]).CheckedChanged = new E$ventHandler(ReplaceCheckBox_CheckedChanged);
692
Dictionary<string, Control> ControlDictionary;
693
void ReplaceCheckBox_CheckedChanged(object sender, EventArgs e) {}
696
class CheckBox : Control {
697
public EventHandler CheckedChanged;