PageRenderTime 45ms CodeModel.GetById 16ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/EditPointTests.cs

https://github.com/ajadex/SharpDevelop
C# | 184 lines | 138 code | 29 blank | 17 comment | 0 complexity | 99cd957c2721001b473226d9fb573f96 MD5 | raw file
  1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2// 
  3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4// software and associated documentation files (the "Software"), to deal in the Software
  5// without restriction, including without limitation the rights to use, copy, modify, merge,
  6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7// to whom the Software is furnished to do so, subject to the following conditions:
  8// 
  9// The above copyright notice and this permission notice shall be included in all copies or
 10// substantial portions of the Software.
 11// 
 12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
 15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 17// DEALINGS IN THE SOFTWARE.
 18
 19using System;
 20using System.Linq;
 21using ICSharpCode.NRefactory.Editor;
 22using ICSharpCode.NRefactory.TypeSystem;
 23using ICSharpCode.PackageManagement;
 24using ICSharpCode.PackageManagement.EnvDTE;
 25using ICSharpCode.SharpDevelop;
 26using NUnit.Framework;
 27using Rhino.Mocks;
 28
 29namespace PackageManagement.Tests.EnvDTE
 30{
 31	[TestFixture]
 32	public class EditPointTests : CodeModelTestBase
 33	{
 34		CodeVariable codeVariable;
 35		IField field;
 36		CodeFunction2 codeFunction;
 37		TextPoint endPoint;
 38		EditPoint editPoint;
 39		IDocument document;
 40		IDocumentView documentView;
 41		IDocumentLoader documentLoader;
 42		
 43		[SetUp]
 44		public void Init()
 45		{
 46			CreateDocumentLoader();
 47		}
 48		
 49		void CreateDocumentLoader()
 50		{
 51			document = MockRepository.GenerateStub<IDocument>();
 52			documentView = MockRepository.GenerateStub<IDocumentView>();
 53			documentView.Stub(view => view.Document).Return(document);
 54			documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
 55		}
 56		
 57		void CreateField(DomRegion region)
 58		{
 59			AddCodeFile("class.cs", "class c {}");
 60			codeModelContext.DocumentLoader = documentLoader;
 61			
 62			field = MockRepository.GenerateStub<IField>();
 63			field.Stub(f => f.Region).Return(region);
 64			
 65			codeVariable = new CodeVariable(codeModelContext, field);
 66		}
 67		
 68		void CreateMethod(DomRegion region)
 69		{
 70			AddCodeFile("class.cs", "class c {}");
 71			codeModelContext.DocumentLoader = documentLoader;
 72			
 73			IMethod method = MockRepository.GenerateStub<IMethod>();
 74			method.Stub(m => m.Region).Return(region);
 75			
 76			codeFunction = new CodeFunction2(codeModelContext, method);
 77		}
 78		
 79		void DocumentOffsetToReturn(int line, int column, int offset)
 80		{
 81			document.Stub(d => d.PositionToOffset(line, column)).Return(offset);
 82		}
 83		
 84		void CreateFieldEditPoint()
 85		{
 86			var startPoint = (TextPoint)codeVariable.GetStartPoint();
 87			endPoint = (TextPoint)codeVariable.GetEndPoint();
 88			editPoint = (EditPoint)startPoint.CreateEditPoint();
 89		}
 90		
 91		void CreateMethodEditPoint()
 92		{
 93			var startPoint = (TextPoint)codeFunction.GetStartPoint();
 94			endPoint = (TextPoint)codeFunction.GetEndPoint();
 95			editPoint = (EditPoint)startPoint.CreateEditPoint();
 96		}
 97		
 98		void ReplaceText(string text)
 99		{
100			editPoint.ReplaceText(endPoint, text, (int)global::EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
101		}
102		
103		void DocumentFileName(string fileName)
104		{
105			documentLoader.Stub(loader => loader.LoadDocumentView(fileName)).Return(documentView);
106		}
107		
108		void AssertDocumentViewIndentLinesWasNotCalled()
109		{
110			documentView.AssertWasNotCalled(view => view.IndentLines(Arg<int>.Is.Anything, Arg<int>.Is.Anything));
111		}
112		
113		[Test]
114		public void ReplaceText_EditPointCreatedFromFieldStartPoint_ReplacesTextBetweenStartAndEndPoint()
115		{
116			string fileName = @"d:\projects\test.cs";
117			var fieldRegion = new DomRegion(fileName, 1, 5, 3, 12);
118			CreateField(fieldRegion);
119			DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
120			DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
121			DocumentFileName(fileName);
122			CreateFieldEditPoint();
123			
124			ReplaceText("Test");
125			
126			document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
127		}
128		
129		[Test]
130		public void ReplaceText_EditPointCreatedFromMethodStartPoint_ReplacesTextBetweenStartAndEndPoint()
131		{
132			string fileName = @"d:\projects\test.cs";
133			var methodRegion = new DomRegion(fileName, 1, 5, 3, 12);
134			CreateMethod(methodRegion);
135			DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
136			DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
137			DocumentFileName(fileName);
138			CreateMethodEditPoint();
139			
140			ReplaceText("Test");
141			
142			document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
143		}
144		
145		[Test]
146		public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsFourLines_IndentsLinesTwoThreeFourFiveAndSix()
147		{
148			string fileName = @"d:\projects\test.cs";
149			var fieldRegion = new DomRegion(fileName, 1, 5, 1, 10);
150			CreateField(fieldRegion);
151			DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
152			DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
153			DocumentFileName(fileName);
154			CreateFieldEditPoint();
155			
156			string replacementText = 
157				"First\r\n" +
158				"Second\r\n" +
159				"Third\r\n" +
160				"Fourth\r\n" +
161				"Five";
162			
163			ReplaceText(replacementText);
164			
165			documentView.AssertWasCalled(view => view.IndentLines(2, 6));
166		}
167		
168		[Test]
169		public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsSingleLine_TextIsNotIndented()
170		{
171			string fileName = @"d:\projects\test.cs";
172			var fieldRegion = new DomRegion(fileName, 1, 5, 1, 10);
173			CreateField(fieldRegion);
174			DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
175			DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
176			DocumentFileName(fileName);
177			CreateFieldEditPoint();
178			
179			ReplaceText("Test");
180			
181			AssertDocumentViewIndentLinesWasNotCalled();
182		}
183	}
184}