|
| 1 | +namespace OrderUsings.Tests.OrderAndSpacingDetermination |
| 2 | +{ |
| 3 | + using System.Collections.Generic; |
| 4 | + using System.Linq; |
| 5 | + |
| 6 | + using NUnit.Framework; |
| 7 | + |
| 8 | + using OrderUsings.Configuration; |
| 9 | + using OrderUsings.Processing; |
| 10 | + |
| 11 | + public abstract class OrderAndSpacingDeterminationTestBase |
| 12 | + { |
| 13 | + internal static readonly UsingDirective AliasSystemPathAsPath = new UsingDirective { Namespace = "System.IO.Path", Alias = "Path" }; |
| 14 | + internal static readonly UsingDirective AliasSystemLaterAsEarlier = new UsingDirective { Namespace = "System.Later", Alias = "Earlier" }; |
| 15 | + internal static readonly UsingDirective AliasSystemTextAsSystem = new UsingDirective { Namespace = "System.Text", Alias = "System" }; |
| 16 | + |
| 17 | + internal static readonly UsingDirective ImportSystem = new UsingDirective { Namespace = "System" }; |
| 18 | + internal static readonly UsingDirective ImportSystemCollectionsGeneric = new UsingDirective { Namespace = "System.Collections.Generic" }; |
| 19 | + internal static readonly UsingDirective ImportSystemLinq = new UsingDirective { Namespace = "System.Linq" }; |
| 20 | + |
| 21 | + internal static readonly UsingDirective ImportMicrosoftCSharp = new UsingDirective { Namespace = "Microsoft.CSharp" }; |
| 22 | + |
| 23 | + internal static readonly UsingDirective ImportOther = new UsingDirective { Namespace = "Other" }; |
| 24 | + internal static readonly UsingDirective ImportOtherA = new UsingDirective { Namespace = "Other.A" }; |
| 25 | + internal static readonly UsingDirective ImportOtherB = new UsingDirective { Namespace = "Other.B" }; |
| 26 | + |
| 27 | + internal static readonly UsingDirective ImportMyLocal = new UsingDirective { Namespace = "MyLocal" }; |
| 28 | + internal static readonly UsingDirective ImportMyLocalA = new UsingDirective { Namespace = "MyLocal.A" }; |
| 29 | + internal static readonly UsingDirective ImportMyLocalB = new UsingDirective { Namespace = "MyLocal.B" }; |
| 30 | + |
| 31 | + internal static readonly UsingDirective ImportRuhroh = new UsingDirective { Namespace = "Ruhroh" }; |
| 32 | + |
| 33 | + |
| 34 | + internal OrderUsingsConfiguration Configuration { get; private set; } |
| 35 | + |
| 36 | + [SetUp] |
| 37 | + public void BaseInitialize() |
| 38 | + { |
| 39 | + Configuration = new OrderUsingsConfiguration |
| 40 | + { |
| 41 | + GroupsAndSpaces = GetRules() |
| 42 | + }; |
| 43 | + } |
| 44 | + |
| 45 | + internal abstract List<ConfigurationRule> GetRules(); |
| 46 | + |
| 47 | + internal void Verify(UsingDirective[] directivesIn, params UsingDirective[][] expectedGroups) |
| 48 | + { |
| 49 | + foreach (var permutation in AllOrderings(directivesIn)) |
| 50 | + { |
| 51 | + var results = OrderAndSpacingGenerator.DetermineOrderAndSpacing( |
| 52 | + permutation, Configuration); |
| 53 | + |
| 54 | + Assert.AreEqual(expectedGroups.Length, results.Count); |
| 55 | + for (int i = 0; i < expectedGroups.Length; ++i) |
| 56 | + { |
| 57 | + Assert.AreEqual(results[i].Count, expectedGroups[i].Length, "Item count in group " + i); |
| 58 | + for (int j = 0; j < expectedGroups[i].Length; ++j) |
| 59 | + { |
| 60 | + UsingDirective expectedUsing = expectedGroups[i][j]; |
| 61 | + UsingDirective actualUsing = results[i][j]; |
| 62 | + string message = string.Format( |
| 63 | + "Expected {0} at {1},{2}, found {3}", expectedUsing, i, j, actualUsing); |
| 64 | + Assert.AreSame(expectedUsing, actualUsing, message); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // This is the same for all configurations. We only want to run the test |
| 71 | + // once per config, so we don't make this a [Test] in this base class - that |
| 72 | + // would run it once per derived class. Instead, just one derived classes |
| 73 | + // per config will defer to this. |
| 74 | + protected void VerifyEmptyHandling() |
| 75 | + { |
| 76 | + Verify(new UsingDirective[0], new UsingDirective[0][]); |
| 77 | + } |
| 78 | + |
| 79 | + private static IEnumerable<IEnumerable<T>> AllOrderings<T>(IEnumerable<T> items) |
| 80 | + { |
| 81 | + bool returnedAtLeastOne = false; |
| 82 | + int index = 0; |
| 83 | +// ReSharper disable PossibleMultipleEnumeration |
| 84 | + foreach (T item in items) |
| 85 | + { |
| 86 | + returnedAtLeastOne = true; |
| 87 | + int thisIndex = index; |
| 88 | + foreach (var remainders in AllOrderings(items.Where((x, i) => i != thisIndex))) |
| 89 | + { |
| 90 | + yield return new[] { item }.Concat(remainders); |
| 91 | + } |
| 92 | + index += 1; |
| 93 | + } |
| 94 | + // ReSharper restore PossibleMultipleEnumeration |
| 95 | + |
| 96 | + if (!returnedAtLeastOne) |
| 97 | + { |
| 98 | + yield return Enumerable.Empty<T>(); |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments