PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/vim/bundle/YouCompleteMe/third_party/ycmd/third_party/OmniSharpServer/OmniSharp.Tests/FixUsings/FixUsingsTests.cs

https://bitbucket.org/tetonedge/linux
C# | 313 lines | 291 code | 21 blank | 1 comment | 0 complexity | 4b97491a50144c40ca1a2ffa25907bd9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, CC-BY-SA-3.0, MIT, WTFPL
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using OmniSharp.CodeIssues;
  6. using OmniSharp.Common;
  7. using OmniSharp.Configuration;
  8. using OmniSharp.Parser;
  9. using Should;
  10. using OmniSharp.Tests.Rename;
  11. namespace OmniSharp.Tests.FixUsings
  12. {
  13. [TestFixture]
  14. public class FixUsingsTests
  15. {
  16. [Test]
  17. public void Should_remove_unused_using()
  18. {
  19. @"
  20. using System;
  21. public class {}"
  22. .FixUsings().ShouldBeEmpty();
  23. }
  24. [Test]
  25. public void Should_remove_multiple_unused_usings()
  26. {
  27. @"
  28. using System;
  29. using System;
  30. using System;
  31. using System;
  32. using System;
  33. using System;
  34. using System;
  35. using System;
  36. using System;
  37. public class {}"
  38. .FixUsings().ShouldBeEmpty();
  39. }
  40. [Test]
  41. public void Should_sort_usings()
  42. {
  43. @"
  44. using ns2;
  45. using ns1;
  46. public class test {
  47. class1 ns1 = new class1();
  48. class2 ns2 = new class2();
  49. }
  50. namespace ns1
  51. {
  52. public class class1{}
  53. }
  54. namespace ns2
  55. {
  56. public class class2{}
  57. }
  58. "
  59. .FixUsings()
  60. .ShouldEqual("using ns1;", "using ns2;");
  61. }
  62. [Test]
  63. public void Should_add_using()
  64. {
  65. @"
  66. public class test {
  67. class1 ns1 = new class1();
  68. }
  69. namespace ns1
  70. {
  71. public class class1{}
  72. }"
  73. .FixUsings()
  74. .ShouldEqual("using ns1;");
  75. }
  76. [Test]
  77. public void Should_add_two_usings()
  78. {
  79. @"
  80. public class test {
  81. class1 ns1 = new class1();
  82. class2 ns2 = new class2();
  83. }
  84. namespace ns1
  85. {
  86. public class class1{}
  87. }
  88. namespace ns2
  89. {
  90. public class class2{}
  91. }
  92. ".FixUsings()
  93. .ShouldEqual("using ns1;", "using ns2;");
  94. }
  95. [Test]
  96. public void Should_add_using_for_extension_method()
  97. {
  98. @"
  99. public class test {
  100. public test()
  101. {
  102. ""string"".Whatever();
  103. }
  104. }
  105. namespace ns1
  106. {
  107. public static class StringExtension
  108. {
  109. public static void Whatever(this string astring) {}
  110. }
  111. }
  112. ".FixUsings()
  113. .ShouldEqual("using ns1;");
  114. }
  115. [Test]
  116. public void Should_add_using_for_method()
  117. {
  118. @"
  119. public class test {
  120. public test()
  121. {
  122. Console.WriteLine(""test"");
  123. }
  124. }
  125. ".FixUsings()
  126. .ShouldEqual("using System;");
  127. }
  128. [Test]
  129. public void Should_add_using_for_new_class()
  130. {
  131. @"
  132. public class test {
  133. public test()
  134. {
  135. var s = new String('x');
  136. }
  137. }
  138. ".FixUsings()
  139. .ShouldEqual("using System;");
  140. }
  141. [Test]
  142. public void Should_add_linq()
  143. {
  144. @"
  145. public class test {
  146. public test()
  147. {
  148. var first = new List<string>().First();
  149. }
  150. }
  151. ".FixUsings()
  152. .ShouldEqual("using System.Collections.Generic;", "using System.Linq;");
  153. }
  154. [Test]
  155. public void Should_also_add_linq()
  156. {
  157. @"
  158. public class test {
  159. public test()
  160. {
  161. var first = new List<string>().First(c => c == 'x');
  162. }
  163. }
  164. ".FixUsings()
  165. .ShouldEqual("using System.Collections.Generic;", "using System.Linq;");
  166. }
  167. [Test]
  168. public void Should_add_linq_for_query()
  169. {
  170. @"
  171. using System.Collections.Generic;
  172. public class test {
  173. public test()
  174. {
  175. var list = new List<string>();
  176. var whatever = (from s in list select s);
  177. }
  178. }
  179. ".FixUsings()
  180. .ShouldEqual("using System.Collections.Generic;", "using System.Linq;");
  181. }
  182. [Test]
  183. public void Should_not_add_linq_twice()
  184. {
  185. @"
  186. using System.Collections.Generic;
  187. using System.Linq;
  188. public class test {
  189. public test()
  190. {
  191. var list = new List<string>();
  192. var whatever = (from s in list select s);
  193. }
  194. }
  195. ".FixUsings()
  196. .ShouldEqual("using System.Collections.Generic;", "using System.Linq;");
  197. }
  198. [Test]
  199. public void Should_not_add_ambiguous_usings()
  200. {
  201. @"
  202. public class test {
  203. class1 ns1 = new class1();
  204. }
  205. namespace ns1
  206. {
  207. public class class1{}
  208. }
  209. namespace ns2
  210. {
  211. public class class1{}
  212. }
  213. ".FixUsings()
  214. .ShouldBeEmpty();
  215. }
  216. [Test]
  217. public void Should_not_abort_when_ambiguous_usings_found()
  218. {
  219. @"
  220. public class test {
  221. var l = new List<class1>()
  222. var ns1 = new class1();
  223. var s = new String('x');
  224. }
  225. namespace ns1
  226. {
  227. public class class1{}
  228. }
  229. namespace ns2
  230. {
  231. public class class1{}
  232. }
  233. ".FixUsings()
  234. .ShouldEqual("using System;", "using System.Collections.Generic;");
  235. }
  236. [Test]
  237. public void Should_set_ambiguous_results()
  238. {
  239. @"
  240. public class test {
  241. class1 ns1 = new class1();
  242. var s = new String('x');
  243. }
  244. namespace ns1
  245. {
  246. public class class1{}
  247. }
  248. namespace ns2
  249. {
  250. public class class1{}
  251. }
  252. ".GetFixUsingsResponse().AmbiguousResults.First().Text
  253. .ShouldEqual("`class1` is ambiguous");
  254. }
  255. }
  256. public static class FixUsingsExtension
  257. {
  258. public static FixUsingsResponse GetFixUsingsResponse(this string buffer)
  259. {
  260. var solution = new FakeSolutionBuilder().AddFile(buffer).Build();
  261. var bufferParser = new BufferParser(solution);
  262. var handler = new FixUsingsHandler(bufferParser, new Logger(Verbosity.Quiet), new OmniSharpConfiguration());
  263. var request = new Request();
  264. request.Buffer = buffer;
  265. request.FileName = "myfile";
  266. // line number should be irrelevant
  267. request.Line = int.MaxValue;
  268. var response = handler.FixUsings(request);
  269. return response;
  270. }
  271. public static IEnumerable<string> FixUsings(this string buffer)
  272. {
  273. var response = GetFixUsingsResponse(buffer);
  274. return response.Buffer.Split(new [] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Where(line => line.Contains("using"));
  275. }
  276. }
  277. }