~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to tests/UnitTests/MonoDevelop.Refactoring/ExtractMethodTests.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
using MonoDevelop.Projects.Dom.Parser;
32
32
using MonoDevelop.Refactoring;
33
33
using MonoDevelop.CSharpBinding.Tests;
34
 
using MonoDevelop.Refactoring.ExtractMethod;
 
34
using MonoDevelop.CSharp.Refactoring.ExtractMethod;
35
35
using System.Collections.Generic;
36
36
using MonoDevelop.CSharpBinding;
37
37
using System.Text;
85
85
                        ProjectDomService.Load (project);
86
86
                        ProjectDom dom = ProjectDomService.GetProjectDom (project);
87
87
                        dom.ForceUpdate (true);
88
 
                        ProjectDomService.Parse (project, file, null, delegate { return parsedText; });
89
 
                        ProjectDomService.Parse (project, file, null, delegate { return parsedText; });
 
88
                        ProjectDomService.Parse (project, file, delegate { return parsedText; });
 
89
                        ProjectDomService.Parse (project, file, delegate { return parsedText; });
90
90
                        
91
91
                        sev.Project = project;
92
92
                        sev.ContentName = file;
95
95
                        
96
96
                        tww.ViewContent = sev;
97
97
                        var doc = new MonoDevelop.Ide.Gui.Document (tww);
98
 
                        
99
 
                        doc.ParsedDocument = new NRefactoryParser ().Parse (null, sev.ContentName, parsedText);
 
98
                        doc.Editor.Document.MimeType = "text/x-csharp";
 
99
                        doc.Editor.Document.FileName = file;
 
100
                        doc.ParsedDocument = new McsParser ().Parse (null, sev.ContentName, parsedText);
100
101
                        foreach (var e in doc.ParsedDocument.Errors)
101
102
                                Console.WriteLine (e);
102
103
                        if (cursorPosition >= 0)
103
 
                                doc.TextEditor.CursorPosition = cursorPosition;
 
104
                                doc.Editor.Caret.Offset = cursorPosition;
104
105
                        if (selectionStart >= 0) 
105
 
                                doc.TextEditor.Select (selectionStart, selectionEnd);
 
106
                                doc.Editor.SetSelection (selectionStart, selectionEnd);
106
107
                        
107
108
                        NRefactoryResolver resolver = new NRefactoryResolver (dom, 
108
109
                                                                              doc.ParsedDocument.CompilationUnit, 
109
 
                                                                              MonoDevelop.Ide.Gui.TextEditor.GetTextEditor (sev), 
 
110
                                                                              sev.Data, 
110
111
                                                                              file);
111
112
                        
112
113
                        ExpressionResult expressionResult;
114
115
                                expressionResult = new ExpressionResult (editorText.Substring (selectionStart, selectionEnd - selectionStart).Trim ());
115
116
                                endPos = selectionEnd;
116
117
                        } else {
117
 
                                expressionResult = new NewCSharpExpressionFinder (dom).FindFullExpression (editorText, cursorPosition + 1);
 
118
                                expressionResult = new NewCSharpExpressionFinder (dom).FindFullExpression (doc.Editor, cursorPosition + 1);
118
119
                        }
119
 
                        ResolveResult resolveResult = endPos >= 0 ? resolver.Resolve (expressionResult, new DomLocation (doc.TextEditor.CursorLine, doc .TextEditor.CursorColumn)) : null;
 
120
                        ResolveResult resolveResult = endPos >= 0 ? resolver.Resolve (expressionResult, new DomLocation (doc.Editor.Caret.Line, doc.Editor.Caret.Column)) : null;
120
121
                        
121
122
                        RefactoringOptions result = new RefactoringOptions {
122
123
                                Document = doc,
152
153
                internal static string GetOutput (RefactoringOptions options, List<Change> changes)
153
154
                {
154
155
                        RefactoringService.AcceptChanges (null, options.Dom, changes, new FileProvider (options));
155
 
                        return options.Document.TextEditor.Text;
 
156
                        return options.Document.Editor.Text;
156
157
                }
157
158
                        
158
159
                
180
181
                        ExtractMethodRefactoring.ExtractMethodParameters parameters = refactoring.CreateParameters (options);
181
182
                        Assert.IsNotNull (parameters);
182
183
                        parameters.Name = "NewMethod";
183
 
                        parameters.InsertionPoint = new Mono.TextEditor.InsertionPoint (new DocumentLocation (options.ResolveResult.CallingMember.BodyRegion.End.Line, 0), NewLineInsertion.BlankLine, NewLineInsertion.None);
 
184
                        parameters.InsertionPoint = new Mono.TextEditor.InsertionPoint (new DocumentLocation (options.ResolveResult.CallingMember.BodyRegion.End.Line + 1, 1), NewLineInsertion.BlankLine, NewLineInsertion.None);
184
185
                        List<Change> changes = refactoring.PerformChanges (options, parameters);
185
186
                        string output = GetOutput (options, changes);
186
187
                        Assert.IsTrue (CompareSource (output, outputString), "Expected:" + Environment.NewLine + outputString + Environment.NewLine + "was:" + Environment.NewLine + output);
205
206
        void TestMethod ()
206
207
        {
207
208
                int i = 5;
208
 
                i = NewMethod ();
 
209
                NewMethod (ref i);
209
210
                Console.WriteLine (i);
210
211
        }
211
212
 
212
 
        int NewMethod ()
 
213
        void NewMethod (ref int i)
213
214
        {
214
 
                int i;
215
215
                i = member + 1;
216
 
                return i;
217
216
        }
218
217
}
219
218
");
265
264
        void TestMethod ()
266
265
        {
267
266
                int i = 5;
268
 
                i = NewMethod (i);
 
267
                NewMethod (ref i);
269
268
                Console.WriteLine (i);
270
269
        }
271
270
 
272
 
        static int NewMethod (int i)
 
271
        static void NewMethod (ref int i)
273
272
        {
274
273
                i = i + 1;
275
 
                return i;
276
274
        }
277
275
}
278
276
");
318
316
                j = i + j;
319
317
                k = j + member;
320
318
                ->
321
 
                Console.WriteLine (k);
 
319
                Console.WriteLine (k + j);
322
320
        }
323
321
}
324
322
", @"class TestClass
328
326
        {
329
327
                int i = 5, j = 10, k;
330
328
                NewMethod (i, ref j, out k);
331
 
                Console.WriteLine (k);
 
329
                Console.WriteLine (k + j);
332
330
        }
333
331
        
334
332
        void NewMethod (int i, ref int j, out int k)
372
370
");
373
371
                }
374
372
                
 
373
                
 
374
                /// <summary>
 
375
                /// Bug 616193 - Extract method passes param with does not exists any more in main method
 
376
                /// </summary>
 
377
                [Test()]
 
378
                public void TestBug616193 ()
 
379
                {
 
380
                        TestExtractMethod (@"class TestClass
 
381
{
 
382
        void TestMethod ()
 
383
        {
 
384
                string ret;
 
385
                string x;
 
386
                IEnumerable<string> y;
 
387
                <-string z = ret + y;
 
388
                ret = x + z;->
 
389
        }
 
390
}
 
391
", @"class TestClass
 
392
{
 
393
        void TestMethod ()
 
394
        {
 
395
                string ret;
 
396
                string x;
 
397
                IEnumerable<string> y;
 
398
                NewMethod (out ret, x, y);
 
399
        }
 
400
        
 
401
        static void NewMethod (out string ret, string x, IEnumerable<string> y)
 
402
        {
 
403
                string z = ret + y;
 
404
                ret = x + z;
 
405
        }
 
406
}
 
407
");
 
408
                }
 
409
                
 
410
                /// <summary>
 
411
                /// Bug 616199 - Extract method forgets to return a local var which is used in main method
 
412
                /// </summary>
 
413
                [Test()]
 
414
                public void TestBug616199 ()
 
415
                {
 
416
                        TestExtractMethod (@"class TestClass
 
417
{
 
418
        void TestMethod ()
 
419
        {
 
420
                <-string z = ""test"" + ""x"";->
 
421
                string ret = ""test1"" + z;
 
422
        }
 
423
}
 
424
", @"class TestClass
 
425
{
 
426
        void TestMethod ()
 
427
        {
 
428
                string z = NewMethod ();
 
429
                string ret = ""test1"" + z;
 
430
        }
 
431
        
 
432
        static string NewMethod ()
 
433
        {
 
434
                string z = ""test"" + ""x"";
 
435
                return z;
 
436
        }
 
437
}
 
438
");
 
439
                }
 
440
                
 
441
                /// <summary>
 
442
                /// Bug 666271 - "Extract Method" on single line adds two semi-colons in method, none in replaced text
 
443
                /// </summary>
 
444
                [Test()]
 
445
                public void TestBug666271 ()
 
446
                {
 
447
                        TestExtractMethod (@"class TestClass
 
448
{
 
449
        void TestMethod ()
 
450
        {
 
451
                <-TestMethod ();->
 
452
        }
 
453
}
 
454
", @"class TestClass
 
455
{
 
456
        void TestMethod ()
 
457
        {
 
458
                NewMethod ();
 
459
        }
 
460
        
 
461
        void NewMethod ()
 
462
        {
 
463
                TestMethod ();
 
464
        }
 
465
}
 
466
");
 
467
                }
 
468
                
375
469
                /* Currently not possible to implement, would cause serve bugs:
376
470
                [Test()]
377
471
                public void ExtractMethodMultiVariableWithLocalReturnVariableTest ()