PageRenderTime 82ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Python/Tests/Core.UI/FormattingUITests.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 228 lines | 177 code | 23 blank | 28 comment | 9 complexity | 0eb56739b127bb6229fd87a489bab41d MD5 | raw file
  1. // Python Tools for Visual Studio
  2. // Copyright(c) Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the License); you may not use
  6. // this file except in compliance with the License. You may obtain a copy of the
  7. // License at http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  10. // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
  11. // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. //
  14. // See the Apache Version 2.0 License for specific language governing
  15. // permissions and limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows.Automation;
  21. using EnvDTE;
  22. using Microsoft.PythonTools;
  23. using Microsoft.VisualStudio.ComponentModelHost;
  24. using Microsoft.VisualStudio.TestTools.UnitTesting;
  25. using Microsoft.VisualStudio.Text;
  26. using Microsoft.VisualStudio.Text.Document;
  27. using Microsoft.VisualStudio.Text.Tagging;
  28. using Microsoft.VisualStudioTools.VSTestHost;
  29. using TestUtilities;
  30. using TestUtilities.Python;
  31. using TestUtilities.UI;
  32. using TestUtilities.UI.Python;
  33. namespace PythonToolsUITests {
  34. [TestClass]
  35. public class FormattingUITests {
  36. [ClassInitialize]
  37. public static void DoDeployment(TestContext context) {
  38. AssertListener.Initialize();
  39. PythonTestData.Deploy();
  40. }
  41. [TestMethod, Priority(1)]
  42. [HostType("VSTestHost"), TestCategory("Installed")]
  43. public void ToggleableOptionTest() {
  44. using (var app = new PythonVisualStudioApp()) {
  45. var pyService = app.ServiceProvider.GetPythonToolsService();
  46. pyService.SetFormattingOption("SpaceBeforeClassDeclarationParen", true);
  47. foreach (var expectedResult in new bool?[] { false, null, true }) {
  48. using (var dialog = ToolsOptionsDialog.FromDte(app)) {
  49. dialog.SelectedView = "Text Editor/Python/Formatting/Spacing";
  50. var spacingView = FormattingOptionsTreeView.FromDialog(dialog);
  51. var value = spacingView.WaitForItem(
  52. "Class Definitions",
  53. "Insert space between a class declaration's name and bases list"
  54. );
  55. Assert.IsNotNull(value, "Did not find item");
  56. value.SetFocus();
  57. Mouse.MoveTo(value.GetClickablePoint());
  58. Mouse.Click(System.Windows.Input.MouseButton.Left);
  59. dialog.OK();
  60. Assert.AreEqual(
  61. expectedResult,
  62. pyService.GetFormattingOption("SpaceBeforeClassDeclarationParen")
  63. );
  64. }
  65. }
  66. }
  67. }
  68. [TestMethod, Priority(1)]
  69. [HostType("VSTestHost"), TestCategory("Installed")]
  70. public void FormatDocument() {
  71. FormattingTest("document.py", null, @"# the quick brown fox jumped over the slow lazy dog the quick brown fox jumped
  72. # over the slow lazy dog
  73. def f():
  74. pass
  75. # short comment
  76. def g():
  77. pass", new[] { Span.FromBounds(0, 78), Span.FromBounds(80, 186) });
  78. }
  79. [TestMethod, Priority(1)]
  80. [HostType("VSTestHost"), TestCategory("Installed")]
  81. public void FormatSelection() {
  82. FormattingTest("selection.py", new Span(0, 121), @"# the quick brown fox jumped over the slow lazy dog the quick brown fox jumped
  83. # over the slow lazy dog
  84. def f():
  85. pass
  86. # short comment
  87. def g():
  88. pass", new[] { Span.FromBounds(0, 78), Span.FromBounds(80, 186) });
  89. }
  90. [TestMethod, Priority(1)]
  91. [HostType("VSTestHost"), TestCategory("Installed")]
  92. public void FormatSelectionNoSelection() {
  93. FormattingTest("selection2.py", new Span(5, 0), @"x=1
  94. y=2
  95. z=3", new Span[0]);
  96. }
  97. [TestMethod, Priority(1)]
  98. [HostType("VSTestHost"), TestCategory("Installed")]
  99. public void FormatReduceLines() {
  100. var pyService = VSTestContext.ServiceProvider.GetPythonToolsService();
  101. pyService.SetFormattingOption("SpacesAroundBinaryOperators", true);
  102. FormattingTest("linereduction.py", null, "(a + b + c + d + e + f)\r\n", new[] { new Span(0, 23), Span.FromBounds(25, 50) });
  103. }
  104. /// <summary>
  105. /// Runs a single formatting test
  106. /// </summary>
  107. /// <param name="filename">The filename of the document to perform formatting in (lives in FormattingTests.sln)</param>
  108. /// <param name="selection">The selection to format, or null if formatting the entire document</param>
  109. /// <param name="expectedText">The expected source code after the formatting</param>
  110. /// <param name="changedSpans">The spans which should be marked as changed in the buffer after formatting</param>
  111. private static void FormattingTest(string filename, Span? selection, string expectedText, Span[] changedSpans) {
  112. using (var app = new VisualStudioApp()) {
  113. var project = app.OpenProject(@"TestData\FormattingTests\FormattingTests.sln");
  114. var item = project.ProjectItems.Item(filename);
  115. var window = item.Open();
  116. window.Activate();
  117. var doc = app.GetDocument(item.Document.FullName);
  118. var aggFact = app.ComponentModel.GetService<IViewTagAggregatorFactoryService>();
  119. var changeTags = aggFact.CreateTagAggregator<ChangeTag>(doc.TextView);
  120. // format the selection or document
  121. if (selection == null) {
  122. DoFormatDocument();
  123. } else {
  124. doc.Invoke(() => doc.TextView.Selection.Select(new SnapshotSpan(doc.TextView.TextBuffer.CurrentSnapshot, selection.Value), false));
  125. DoFormatSelection();
  126. }
  127. // verify the contents are correct
  128. string actual = null;
  129. for (int i = 0; i < 100; i++) {
  130. actual = doc.TextView.TextBuffer.CurrentSnapshot.GetText();
  131. if (expectedText == actual) {
  132. break;
  133. }
  134. System.Threading.Thread.Sleep(100);
  135. }
  136. Assert.AreEqual(expectedText, actual);
  137. // verify the change tags are correct
  138. var snapshot = doc.TextView.TextBuffer.CurrentSnapshot;
  139. var tags = changeTags.GetTags(
  140. new SnapshotSpan(
  141. doc.TextView.TextBuffer.CurrentSnapshot,
  142. new Span(0, doc.TextView.TextBuffer.CurrentSnapshot.Length)
  143. )
  144. );
  145. List<Span> result = new List<Span>();
  146. foreach (var tag in tags) {
  147. result.Add(
  148. new Span(
  149. tag.Span.Start.GetPoint(doc.TextView.TextBuffer.CurrentSnapshot, PositionAffinity.Successor).Value.Position,
  150. tag.Span.End.GetPoint(doc.TextView.TextBuffer.CurrentSnapshot, PositionAffinity.Successor).Value.Position
  151. )
  152. );
  153. }
  154. // dump the spans for creating tests easier
  155. foreach (var span in result) {
  156. Console.WriteLine(span);
  157. }
  158. Assert.AreEqual(result.Count, changedSpans.Length);
  159. for (int i = 0; i < result.Count; i++) {
  160. Assert.AreEqual(result[i], changedSpans[i]);
  161. }
  162. }
  163. }
  164. private static void DoFormatSelection() {
  165. try {
  166. Task.Factory.StartNew(() => {
  167. for (int i = 0; i < 3; i++) {
  168. try {
  169. // wait for the command to become available if it's not already
  170. VSTestContext.DTE.ExecuteCommand("Edit.FormatSelection");
  171. return;
  172. } catch {
  173. System.Threading.Thread.Sleep(1000);
  174. }
  175. }
  176. throw new Exception();
  177. }).Wait();
  178. } catch {
  179. Assert.Fail("Failed to format selection");
  180. }
  181. }
  182. private static void DoFormatDocument() {
  183. try {
  184. Task.Factory.StartNew(() => {
  185. for (int i = 0; i < 3; i++) {
  186. try {
  187. // wait for the command to become available if it's not already
  188. VSTestContext.DTE.ExecuteCommand("Edit.FormatDocument");
  189. return;
  190. } catch {
  191. System.Threading.Thread.Sleep(1000);
  192. }
  193. }
  194. throw new Exception();
  195. }).Wait();
  196. } catch {
  197. Assert.Fail("Failed to format document");
  198. }
  199. }
  200. }
  201. }