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