/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. using System;
  19. using System.Linq;
  20. using ICSharpCode.NRefactory.Editor;
  21. using ICSharpCode.NRefactory.TypeSystem;
  22. using ICSharpCode.PackageManagement;
  23. using ICSharpCode.PackageManagement.EnvDTE;
  24. using ICSharpCode.SharpDevelop;
  25. using NUnit.Framework;
  26. using Rhino.Mocks;
  27. namespace PackageManagement.Tests.EnvDTE
  28. {
  29. [TestFixture]
  30. public class EditPointTests : CodeModelTestBase
  31. {
  32. CodeVariable codeVariable;
  33. IField field;
  34. CodeFunction2 codeFunction;
  35. TextPoint endPoint;
  36. EditPoint editPoint;
  37. IDocument document;
  38. IDocumentView documentView;
  39. IDocumentLoader documentLoader;
  40. [SetUp]
  41. public void Init()
  42. {
  43. CreateDocumentLoader();
  44. }
  45. void CreateDocumentLoader()
  46. {
  47. document = MockRepository.GenerateStub<IDocument>();
  48. documentView = MockRepository.GenerateStub<IDocumentView>();
  49. documentView.Stub(view => view.Document).Return(document);
  50. documentLoader = MockRepository.GenerateStub<IDocumentLoader>();
  51. }
  52. void CreateField(DomRegion region)
  53. {
  54. AddCodeFile("class.cs", "class c {}");
  55. codeModelContext.DocumentLoader = documentLoader;
  56. field = MockRepository.GenerateStub<IField>();
  57. field.Stub(f => f.Region).Return(region);
  58. codeVariable = new CodeVariable(codeModelContext, field);
  59. }
  60. void CreateMethod(DomRegion region)
  61. {
  62. AddCodeFile("class.cs", "class c {}");
  63. codeModelContext.DocumentLoader = documentLoader;
  64. IMethod method = MockRepository.GenerateStub<IMethod>();
  65. method.Stub(m => m.Region).Return(region);
  66. codeFunction = new CodeFunction2(codeModelContext, method);
  67. }
  68. void DocumentOffsetToReturn(int line, int column, int offset)
  69. {
  70. document.Stub(d => d.PositionToOffset(line, column)).Return(offset);
  71. }
  72. void CreateFieldEditPoint()
  73. {
  74. var startPoint = (TextPoint)codeVariable.GetStartPoint();
  75. endPoint = (TextPoint)codeVariable.GetEndPoint();
  76. editPoint = (EditPoint)startPoint.CreateEditPoint();
  77. }
  78. void CreateMethodEditPoint()
  79. {
  80. var startPoint = (TextPoint)codeFunction.GetStartPoint();
  81. endPoint = (TextPoint)codeFunction.GetEndPoint();
  82. editPoint = (EditPoint)startPoint.CreateEditPoint();
  83. }
  84. void ReplaceText(string text)
  85. {
  86. editPoint.ReplaceText(endPoint, text, (int)global::EnvDTE.vsEPReplaceTextOptions.vsEPReplaceTextAutoformat);
  87. }
  88. void DocumentFileName(string fileName)
  89. {
  90. documentLoader.Stub(loader => loader.LoadDocumentView(fileName)).Return(documentView);
  91. }
  92. void AssertDocumentViewIndentLinesWasNotCalled()
  93. {
  94. documentView.AssertWasNotCalled(view => view.IndentLines(Arg<int>.Is.Anything, Arg<int>.Is.Anything));
  95. }
  96. [Test]
  97. public void ReplaceText_EditPointCreatedFromFieldStartPoint_ReplacesTextBetweenStartAndEndPoint()
  98. {
  99. string fileName = @"d:\projects\test.cs";
  100. var fieldRegion = new DomRegion(fileName, 1, 5, 3, 12);
  101. CreateField(fieldRegion);
  102. DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
  103. DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
  104. DocumentFileName(fileName);
  105. CreateFieldEditPoint();
  106. ReplaceText("Test");
  107. document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
  108. }
  109. [Test]
  110. public void ReplaceText_EditPointCreatedFromMethodStartPoint_ReplacesTextBetweenStartAndEndPoint()
  111. {
  112. string fileName = @"d:\projects\test.cs";
  113. var methodRegion = new DomRegion(fileName, 1, 5, 3, 12);
  114. CreateMethod(methodRegion);
  115. DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
  116. DocumentOffsetToReturn(line: 3, column: 12, offset: 20);
  117. DocumentFileName(fileName);
  118. CreateMethodEditPoint();
  119. ReplaceText("Test");
  120. document.AssertWasCalled(d => d.Replace(5, 15, "Test"));
  121. }
  122. [Test]
  123. public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsFourLines_IndentsLinesTwoThreeFourFiveAndSix()
  124. {
  125. string fileName = @"d:\projects\test.cs";
  126. var fieldRegion = new DomRegion(fileName, 1, 5, 1, 10);
  127. CreateField(fieldRegion);
  128. DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
  129. DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
  130. DocumentFileName(fileName);
  131. CreateFieldEditPoint();
  132. string replacementText =
  133. "First\r\n" +
  134. "Second\r\n" +
  135. "Third\r\n" +
  136. "Fourth\r\n" +
  137. "Five";
  138. ReplaceText(replacementText);
  139. documentView.AssertWasCalled(view => view.IndentLines(2, 6));
  140. }
  141. [Test]
  142. public void ReplaceText_EditPointCreatedFromFieldStartPointAndTextIsSingleLine_TextIsNotIndented()
  143. {
  144. string fileName = @"d:\projects\test.cs";
  145. var fieldRegion = new DomRegion(fileName, 1, 5, 1, 10);
  146. CreateField(fieldRegion);
  147. DocumentOffsetToReturn(line: 1, column: 5, offset: 5);
  148. DocumentOffsetToReturn(line: 1, column: 12, offset: 10);
  149. DocumentFileName(fileName);
  150. CreateFieldEditPoint();
  151. ReplaceText("Test");
  152. AssertDocumentViewIndentLinesWasNotCalled();
  153. }
  154. }
  155. }