public void ShouldAddUsingAtSortedPlace() { const string source = @" using System; using System.Threading; namespace ns.something { public class Test {/*here*/} }"; var document = GetTestDocument(source); int position = source.IndexOf("/*here*/"); var newDoc = new NamespaceResolver().AddNamespaceImportAsync("System.Collections", document, position, CancellationToken.None).Result; var newDocText = newDoc.GetTextAsync().Result.ToString(); var usingIndexes = new[] { "using System;", "using System.Collections;", "using System.Threading;" } .Select(u => newDocText.IndexOf(u)) .ToArray(); Assert.That(usingIndexes, Does.Not.Contain(-1), "Missing namespace!"); Assert.That(usingIndexes, Is.Ordered); }
public void ShouldAddNamespaceRelativeToParentNamespace() { const string source = @" namespace A.B.C { using System; public class Test {/*here*/} }"; var document = GetTestDocument(source); int position = source.IndexOf("/*here*/"); var newDoc = new NamespaceResolver().AddNamespaceImportAsync("A.B.Csmth.D.E", document, position, CancellationToken.None).Result; var newDocText = newDoc.GetTextAsync().Result.ToString(); Assert.That(newDocText, Does.Not.Contain("using A.B.Csmth.D.E;")); Assert.That(newDocText, Does.Contain("using Csmth.D.E;")); }
public void ShouldAddUsingInsideNamespaceIfUsingsArePresent() { const string source = @" namespace ns.something { using System; public class Test {/*here*/} }"; var document = GetTestDocument(source); int position = source.IndexOf("/*here*/"); var newDoc = new NamespaceResolver().AddNamespaceImportAsync("System.Collections", document, position, CancellationToken.None).Result; var newDocText = newDoc.GetTextAsync().Result.ToString(); Assert.That(newDocText, Does.Contain("using System.Collections;")); Assert.That(newDocText.IndexOf("using System.Collections;"), Is.GreaterThan(newDocText.IndexOf("namespace ns.something"))); }