PageRenderTime 56ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/Python/Tests/Core.UI/AddImportTests.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 329 lines | 205 code | 51 blank | 73 comment | 7 complexity | 3b6c388828d428c459740152e1588f92 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.Linq;
  17. using Microsoft.VisualStudio.TestTools.UnitTesting;
  18. using TestUtilities;
  19. using TestUtilities.Python;
  20. using TestUtilities.UI;
  21. namespace PythonToolsUITests {
  22. [TestClass]
  23. public class AddImportTests {
  24. [ClassInitialize]
  25. public static void DoDeployment(TestContext context) {
  26. AssertListener.Initialize();
  27. PythonTestData.Deploy();
  28. }
  29. /// <summary>
  30. /// Imports get added after a doc string
  31. /// </summary>
  32. [TestMethod, Priority(1)]
  33. [HostType("VSTestHost"), TestCategory("Installed")]
  34. public void DocString() {
  35. string expectedText = @"'''fob'''
  36. import itertools
  37. itertools";
  38. AddSmartTagTest("DocString.py", 3, 10, new[] { "import itertools" }, 0, expectedText);
  39. }
  40. /// <summary>
  41. /// Imports get added after a unicode doc string
  42. /// </summary>
  43. [TestMethod, Priority(1)]
  44. [HostType("VSTestHost"), TestCategory("Installed")]
  45. public void UnicodeDocString() {
  46. string expectedText = @"u'''fob'''
  47. import itertools
  48. itertools";
  49. AddSmartTagTest("UnicodeDocString.py", 3, 10, new[] { "import itertools" }, 0, expectedText);
  50. }
  51. /// <summary>
  52. /// Future import gets added after doc string, but before other imports.
  53. /// </summary>
  54. [TestMethod, Priority(1)]
  55. [HostType("VSTestHost"), TestCategory("Installed")]
  56. public void DocStringFuture() {
  57. string expectedText = @"'''fob'''
  58. from __future__ import with_statement
  59. import itertools
  60. with_statement";
  61. AddSmartTagTest("DocStringFuture.py", 4, 10, new[] { "from __future__ import with_statement" }, 0, expectedText);
  62. }
  63. /// <summary>
  64. /// Add a from .. import for a function in another module
  65. /// </summary>
  66. [TestMethod, Priority(1)]
  67. [HostType("VSTestHost"), TestCategory("Installed")]
  68. public void ImportFunctionFrom() {
  69. string expectedText = @"from test_module import module_func
  70. module_func()";
  71. AddSmartTagTest("ImportFunctionFrom.py", 1, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  72. }
  73. /// <summary>
  74. /// Add a from .. import for a function in a subpackage
  75. /// </summary>
  76. [TestMethod, Priority(1)]
  77. [HostType("VSTestHost"), TestCategory("Installed")]
  78. public void ImportFunctionFromSubpackage() {
  79. string expectedText = @"from test_package.sub_package import subpackage_method
  80. subpackage_method()";
  81. AddSmartTagTest("ImportFunctionFromSubpackage.py", 1, 1, new[] { "from test_package.sub_package import subpackage_method" }, 0, expectedText);
  82. }
  83. /// <summary>
  84. /// We should understand assignment from import statements even in the face of errors
  85. /// </summary>
  86. [TestMethod, Priority(1)]
  87. [HostType("VSTestHost"), TestCategory("Installed")]
  88. public void ImportWithErrors() {
  89. // http://pytools.codeplex.com/workitem/547
  90. AddSmartTagTest("ImportWithError.py", 1, 9, _NoSmartTags);
  91. AddSmartTagTest("ImportWithError.py", 2, 3, _NoSmartTags);
  92. }
  93. /// <summary>
  94. /// Add a from .. import for a function in a built-in module
  95. /// </summary>
  96. [TestMethod, Priority(1)]
  97. [HostType("VSTestHost"), TestCategory("Installed")]
  98. public void ImportBuiltinFunction() {
  99. string expectedText = @"from sys import getrecursionlimit
  100. getrecursionlimit()";
  101. AddSmartTagTest("ImportBuiltinFunction.py", 1, 1, new[] { "from sys import getrecursionlimit" }, 0, expectedText);
  102. }
  103. /// <summary>
  104. /// Add a from ... import for a function in another module when a from import already exists for the same module.
  105. /// </summary>
  106. [TestMethod, Priority(1)]
  107. [HostType("VSTestHost"), TestCategory("Installed")]
  108. public void ImportFunctionFromExistingFromImport() {
  109. string expectedText = @"from test_module import module_func_2, module_func
  110. module_func()";
  111. AddSmartTagTest("ImportFunctionFromExistingFromImport.py", 2, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  112. }
  113. /// <summary>
  114. /// Add a from ... import for a function in another module when a from import already exists for the same module and
  115. /// the existing import is an "from ... import oar as baz" import.
  116. /// </summary>
  117. [TestMethod, Priority(1)]
  118. [HostType("VSTestHost"), TestCategory("Installed")]
  119. public void ImportFunctionFromExistingFromImportAsName() {
  120. string expectedText = @"from test_module import module_func_2 as oar, module_func
  121. module_func()";
  122. AddSmartTagTest("ImportFunctionFromExistingFromImportAsName.py", 2, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  123. }
  124. /// <summary>
  125. /// Add a from ... import for a function in another module when a from import already exists for the same module and
  126. /// the existing import contains parens around the imported items list.
  127. /// </summary>
  128. [TestMethod, Priority(1)]
  129. [HostType("VSTestHost"), TestCategory("Installed")]
  130. public void ImportFunctionFromExistingFromImportParens() {
  131. string expectedText = @"from test_module import (module_func_2, module_func)
  132. module_func()";
  133. AddSmartTagTest("ImportFunctionFromExistingFromImportParens.py", 2, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  134. }
  135. /// <summary>
  136. /// Add a from ... import for a function in another module when a from import already exists for the same module and
  137. /// the existing import contains parens around the imported items list and the existing import contains an "as" import.
  138. /// </summary>
  139. [TestMethod, Priority(1)]
  140. [HostType("VSTestHost"), TestCategory("Installed")]
  141. public void ImportFunctionFromExistingFromImportParensAsName() {
  142. string expectedText = @"from test_module import (module_func_2 as oar, module_func)
  143. module_func()";
  144. AddSmartTagTest("ImportFunctionFromExistingFromImportParensAsName.py", 2, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  145. }
  146. /// <summary>
  147. /// Add a from ... import for a function in another module when a from import already exists for the same module and
  148. /// the existing import contains parens around the imported items list and the existing import contains an "as" import
  149. /// and there's a trailing comma at the end.
  150. /// </summary>
  151. [TestMethod, Priority(1)]
  152. [HostType("VSTestHost"), TestCategory("Installed")]
  153. public void ImportFunctionFromExistingFromImportParensAsNameTrailingComma() {
  154. string expectedText = @"from test_module import (module_func_2 as oar, module_func)
  155. module_func()";
  156. AddSmartTagTest("ImportFunctionFromExistingFromImportParensAsNameTrailingComma.py", 2, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  157. }
  158. /// <summary>
  159. /// Add a from ... import for a function in another module when a from import already exists for the same module and
  160. /// the existing import contains parens around the imported items list and there's a trailing comma at the end.
  161. /// </summary>
  162. [TestMethod, Priority(1)]
  163. [HostType("VSTestHost"), TestCategory("Installed")]
  164. public void ImportFunctionFromExistingFromImportParensTrailingComma() {
  165. string expectedText = @"from test_module import (module_func_2, module_func)
  166. module_func()";
  167. AddSmartTagTest("ImportFunctionFromExistingFromImportParensTrailingComma.py", 2, 1, new[] { "from test_module import module_func" }, 0, expectedText);
  168. }
  169. /// <summary>
  170. /// Adds an import statement for a package.
  171. /// </summary>
  172. [TestMethod, Priority(1)]
  173. [HostType("VSTestHost"), TestCategory("Installed")]
  174. public void ImportPackage() {
  175. string expectedText = @"import test_package
  176. test_package";
  177. AddSmartTagTest("ImportPackage.py", 1, 1, new[] { "*", "import test_package" }, 1, expectedText);
  178. }
  179. /// <summary>
  180. /// Adds an import statement for a package.
  181. /// </summary>
  182. [TestMethod, Priority(1)]
  183. [HostType("VSTestHost"), TestCategory("Installed")]
  184. public void ImportSubPackage() {
  185. string expectedText = @"from test_package import sub_package
  186. sub_package";
  187. AddSmartTagTest("ImportSubPackage.py", 1, 1, new[] { "from test_package import sub_package" }, 0, expectedText);
  188. }
  189. private static string[] _NoSmartTags = new string[0];
  190. /// <summary>
  191. /// Adds an import statement for a package.
  192. /// </summary>
  193. [TestMethod, Priority(1)]
  194. [HostType("VSTestHost"), TestCategory("Installed")]
  195. public void Parameters() {
  196. var getreclimit = new[] { "from sys import getrecursionlimit" };
  197. using (var app = new VisualStudioApp()) {
  198. var project = app.OpenProject(@"TestData\AddImport.sln");
  199. var item = project.ProjectItems.Item("Parameters.py");
  200. var window = item.Open();
  201. window.Activate();
  202. var doc = app.GetDocument(item.Document.FullName);
  203. AddSmartTagTest(doc, 1, 19, _NoSmartTags);
  204. AddSmartTagTest(doc, 1, 30, getreclimit);
  205. AddSmartTagTest(doc, 4, 18, _NoSmartTags);
  206. AddSmartTagTest(doc, 7, 18, _NoSmartTags);
  207. AddSmartTagTest(doc, 10, 20, _NoSmartTags);
  208. AddSmartTagTest(doc, 13, 22, _NoSmartTags);
  209. AddSmartTagTest(doc, 16, 22, _NoSmartTags);
  210. AddSmartTagTest(doc, 19, 22, _NoSmartTags);
  211. AddSmartTagTest(doc, 19, 35, getreclimit);
  212. AddSmartTagTest(doc, 22, 25, _NoSmartTags);
  213. AddSmartTagTest(doc, 22, 56, getreclimit);
  214. AddSmartTagTest(doc, 25, 38, _NoSmartTags);
  215. AddSmartTagTest(doc, 25, 38, _NoSmartTags);
  216. AddSmartTagTest(doc, 25, 48, getreclimit);
  217. AddSmartTagTest(doc, 29, 12, _NoSmartTags);
  218. AddSmartTagTest(doc, 29, 42, getreclimit);
  219. AddSmartTagTest(doc, 34, 26, _NoSmartTags);
  220. AddSmartTagTest(doc, 34, 31, getreclimit);
  221. AddSmartTagTest(doc, 42, 16, _NoSmartTags);
  222. AddSmartTagTest(doc, 51, 16, _NoSmartTags);
  223. }
  224. }
  225. /// <summary>
  226. /// Adds an import statement for a package.
  227. /// </summary>
  228. [TestMethod, Priority(1)]
  229. [HostType("VSTestHost"), TestCategory("Installed")]
  230. public void AssignedWithoutTypeInfo() {
  231. AddSmartTagTest("Assignments.py", 1, 2, _NoSmartTags);
  232. AddSmartTagTest("Assignments.py", 1, 8, _NoSmartTags);
  233. }
  234. private static void AddSmartTagTest(EditorWindow doc, int line, int column, string[] expectedActions, int invokeAction = -1, string expectedText = null) {
  235. doc.Invoke(() => {
  236. var point = doc.TextView.TextBuffer.CurrentSnapshot.GetLineFromLineNumber(line - 1).Start.Add(column - 1);
  237. doc.TextView.Caret.MoveTo(point);
  238. });
  239. if (expectedActions.Length > 0) {
  240. using (var sh = doc.StartSmartTagSession()) {
  241. var actions = sh.Session.Actions.ToList();
  242. if (expectedActions[0] == "*") {
  243. AssertUtil.ContainsAtLeast(
  244. actions.Select(a => a.DisplayText.Replace("__", "_")),
  245. expectedActions.Skip(1)
  246. );
  247. } else {
  248. AssertUtil.AreEqual(
  249. actions.Select(a => a.DisplayText.Replace("__", "_")).ToArray(),
  250. expectedActions
  251. );
  252. }
  253. if (invokeAction >= 0) {
  254. var action = actions.FirstOrDefault(a => a.DisplayText.Replace("__", "_") == expectedActions[invokeAction]);
  255. Assert.IsNotNull(action, "No action named " + expectedActions[invokeAction]);
  256. doc.Invoke(() => action.Invoke());
  257. doc.WaitForText(expectedText);
  258. }
  259. }
  260. } else {
  261. doc.StartSmartTagSessionNoSession();
  262. }
  263. }
  264. private static void AddSmartTagTest(string filename, int line, int column, string[] expectedActions, int invokeAction = -1, string expectedText = null) {
  265. using (var app = new VisualStudioApp()) {
  266. var project = app.OpenProject(@"TestData\AddImport.sln");
  267. var item = project.ProjectItems.Item(filename);
  268. var window = item.Open();
  269. window.Activate();
  270. var doc = app.GetDocument(item.Document.FullName);
  271. AddSmartTagTest(doc, line, column, expectedActions, invokeAction, expectedText);
  272. }
  273. }
  274. }
  275. }