Skip to content

Commit 9fb3f07

Browse files
committed
Fixed alias ordering bug:
#1 Aliases match rules were not being handled correctly.
1 parent 911cc20 commit 9fb3f07

17 files changed

+649
-9
lines changed

src/OrderUsings.Core.Tests/OrderAndSpacingDetermination/OrderAndSpacingDeterminationTestBase.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010

1111
public abstract class OrderAndSpacingDeterminationTestBase
1212
{
13-
internal static readonly UsingDirective AliasSystemPathAsPath = new UsingDirective { Namespace = "System.IO.Path", Alias = "Path" };
13+
internal static readonly UsingDirective AliasSystemIoPathAsPath = new UsingDirective { Namespace = "System.IO.Path", Alias = "Path" };
1414
internal static readonly UsingDirective AliasSystemLaterAsEarlier = new UsingDirective { Namespace = "System.Later", Alias = "Earlier" };
1515
internal static readonly UsingDirective AliasSystemTextAsSystem = new UsingDirective { Namespace = "System.Text", Alias = "System" };
16+
internal static readonly UsingDirective AliasSystemAbcAsXyz = new UsingDirective { Namespace = "System.Abc", Alias = "Xyz" };
17+
internal static readonly UsingDirective AliasMicrosoftOfficeInteropPowerPointAsPowerPoint = new UsingDirective { Namespace = "Microsoft.Office.Interop.PowerPoint", Alias = "PowerPoint" };
1618

1719
internal static readonly UsingDirective ImportSystem = new UsingDirective { Namespace = "System" };
1820
internal static readonly UsingDirective ImportSystemCollectionsGeneric = new UsingDirective { Namespace = "System.Collections.Generic" };
1921
internal static readonly UsingDirective ImportSystemLinq = new UsingDirective { Namespace = "System.Linq" };
22+
internal static readonly UsingDirective ImportSystemData = new UsingDirective { Namespace = "System.Data" };
23+
internal static readonly UsingDirective ImportSystemRuntimeInteropServices = new UsingDirective { Namespace = "System.Runtime.InteropServices" };
24+
internal static readonly UsingDirective ImportSystemWindowsForms = new UsingDirective { Namespace = "System.Windows.Forms" };
2025

2126
internal static readonly UsingDirective ImportMicrosoftCSharp = new UsingDirective { Namespace = "Microsoft.CSharp" };
2227

@@ -28,6 +33,8 @@ public abstract class OrderAndSpacingDeterminationTestBase
2833
internal static readonly UsingDirective ImportMyLocalA = new UsingDirective { Namespace = "MyLocal.A" };
2934
internal static readonly UsingDirective ImportMyLocalB = new UsingDirective { Namespace = "MyLocal.B" };
3035

36+
internal static readonly UsingDirective ImportMmCorePpt = new UsingDirective { Namespace = "Mm.Core.Ppt" };
37+
3138
internal static readonly UsingDirective ImportRuhroh = new UsingDirective { Namespace = "Ruhroh" };
3239

3340

src/OrderUsings.Core.Tests/OrderAndSpacingDetermination/SinglePatternMatchesAll/WhenAliasesOrderedByAlias.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public void ProducesSingleGroupOrderedByAliasWhenAliasesAndNamespaceOtherwise()
1010
Verify(
1111
new[]
1212
{
13-
AliasSystemPathAsPath,
13+
AliasSystemIoPathAsPath,
1414
ImportSystem,
1515
ImportRuhroh,
1616
AliasSystemLaterAsEarlier
@@ -20,7 +20,7 @@ public void ProducesSingleGroupOrderedByAliasWhenAliasesAndNamespaceOtherwise()
2020
new[]
2121
{
2222
AliasSystemLaterAsEarlier,
23-
AliasSystemPathAsPath,
23+
AliasSystemIoPathAsPath,
2424
ImportRuhroh,
2525
ImportSystem
2626
}

src/OrderUsings.Core.Tests/OrderAndSpacingDetermination/SinglePatternMatchesAll/WhenAliasesOrderedByNamespace.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void ProducesSingleGroupOrderedByNamespace()
2121
Verify(
2222
new[]
2323
{
24-
AliasSystemPathAsPath,
24+
AliasSystemIoPathAsPath,
2525
ImportSystem,
2626
ImportRuhroh,
2727
AliasSystemLaterAsEarlier
@@ -32,7 +32,7 @@ public void ProducesSingleGroupOrderedByNamespace()
3232
{
3333
ImportRuhroh,
3434
ImportSystem,
35-
AliasSystemPathAsPath,
35+
AliasSystemIoPathAsPath,
3636
AliasSystemLaterAsEarlier
3737
}
3838
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SpecificThenFallbackThenSpecificThenAliases
2+
{
3+
using System.Collections.Generic;
4+
5+
using OrderUsings.Configuration;
6+
7+
public abstract class SpecificThenFallbackThenSpecificThenAliasesBase : OrderAndSpacingDeterminationTestBase
8+
{
9+
protected virtual OrderAliasBy AliasOrder { get { return OrderAliasBy.Alias; } }
10+
11+
internal override List<ConfigurationRule> GetRules()
12+
{
13+
return new List<ConfigurationRule>
14+
{
15+
ConfigurationRule.ForGroupRule(new GroupRule
16+
{
17+
Type = MatchType.Import,
18+
NamespacePattern = "System*",
19+
Priority = 1
20+
}),
21+
ConfigurationRule.ForGroupRule(new GroupRule
22+
{
23+
Type = MatchType.Import,
24+
NamespacePattern = "Microsoft*",
25+
Priority = 1
26+
}),
27+
28+
ConfigurationRule.ForSpace(),
29+
30+
ConfigurationRule.ForGroupRule(new GroupRule
31+
{
32+
Type = MatchType.Import,
33+
NamespacePattern = "*",
34+
Priority = 9999
35+
}),
36+
37+
ConfigurationRule.ForSpace(),
38+
39+
ConfigurationRule.ForGroupRule(new GroupRule
40+
{
41+
Type = MatchType.Import,
42+
NamespacePattern = "*",
43+
Priority = 1
44+
}),
45+
46+
ConfigurationRule.ForSpace(),
47+
48+
ConfigurationRule.ForGroupRule(new GroupRule
49+
{
50+
Type = MatchType.Alias,
51+
OrderAliasesBy = AliasOrder,
52+
AliasPattern = "*",
53+
NamespacePattern = "*",
54+
Priority = 9999
55+
})
56+
};
57+
}
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SpecificThenFallbackThenSpecificThenAliases
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenSpecificAndAliasGroupsPopulated : SpecificThenFallbackThenSpecificThenAliasesBase
6+
{
7+
// This was the particular failure case that I encountered in a real life issue:
8+
// https://github.com/idg10/order-usings/issues/1
9+
[Test]
10+
public void ProducesThreeGroupsWhenInOrder()
11+
{
12+
Verify(
13+
new[]
14+
{
15+
ImportSystem,
16+
ImportSystemData,
17+
ImportSystemRuntimeInteropServices,
18+
ImportSystemWindowsForms,
19+
20+
ImportMmCorePpt,
21+
22+
AliasMicrosoftOfficeInteropPowerPointAsPowerPoint
23+
},
24+
new[]
25+
{
26+
new[] { ImportSystem, ImportSystemData, ImportSystemRuntimeInteropServices, ImportSystemWindowsForms },
27+
new[] { ImportMmCorePpt },
28+
new[] { AliasMicrosoftOfficeInteropPowerPointAsPowerPoint }
29+
});
30+
}
31+
32+
[Test]
33+
public void ProducesThreeGroupsWhenOutOfOrder()
34+
{
35+
Verify(
36+
new[]
37+
{
38+
ImportSystem,
39+
ImportSystemRuntimeInteropServices,
40+
AliasMicrosoftOfficeInteropPowerPointAsPowerPoint,
41+
ImportSystemWindowsForms,
42+
ImportSystemData,
43+
ImportMmCorePpt
44+
},
45+
new[]
46+
{
47+
new[] { ImportSystem, ImportSystemData, ImportSystemRuntimeInteropServices, ImportSystemWindowsForms },
48+
new[] { ImportMmCorePpt },
49+
new[] { AliasMicrosoftOfficeInteropPowerPointAsPowerPoint }
50+
});
51+
}
52+
}
53+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.TwoSpecificThenDefaultThenAliases
2+
{
3+
using System.Collections.Generic;
4+
5+
using OrderUsings.Configuration;
6+
7+
public abstract class TwoGroupsThenAliasesBase : OrderAndSpacingDeterminationTestBase
8+
{
9+
protected virtual OrderAliasBy AliasOrder { get { return OrderAliasBy.Alias; } }
10+
11+
internal override List<ConfigurationRule> GetRules()
12+
{
13+
return new List<ConfigurationRule>
14+
{
15+
ConfigurationRule.ForGroupRule(new GroupRule
16+
{
17+
Type = MatchType.Import,
18+
NamespacePattern = "System*",
19+
Priority = 1
20+
}),
21+
ConfigurationRule.ForGroupRule(new GroupRule
22+
{
23+
Type = MatchType.Import,
24+
NamespacePattern = "Microsoft*",
25+
Priority = 1
26+
}),
27+
28+
ConfigurationRule.ForSpace(),
29+
30+
ConfigurationRule.ForGroupRule(new GroupRule
31+
{
32+
Type = MatchType.Import,
33+
NamespacePattern = "*",
34+
Priority = 9999
35+
}),
36+
37+
ConfigurationRule.ForSpace(),
38+
39+
ConfigurationRule.ForGroupRule(new GroupRule
40+
{
41+
Type = MatchType.Alias,
42+
OrderAliasesBy = AliasOrder,
43+
AliasPattern = "*",
44+
NamespacePattern = "*",
45+
Priority = 9999
46+
})
47+
};
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.TwoSpecificThenDefaultThenAliases
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenAliasesAndImportsOrderedByAlias : TwoGroupsThenAliasesBase
6+
{
7+
[Test]
8+
public void TwoMatchFirstGroupThirdIsAlias()
9+
{
10+
Verify(
11+
new[]
12+
{
13+
ImportSystem,
14+
ImportMicrosoftCSharp,
15+
AliasSystemIoPathAsPath
16+
},
17+
new[]
18+
{
19+
new[] { ImportSystem, ImportMicrosoftCSharp },
20+
new[] { AliasSystemIoPathAsPath }
21+
});
22+
}
23+
24+
[Test]
25+
public void TwoMatchFirstGroupTwoAreAliasesInOrder()
26+
{
27+
Verify(
28+
new[]
29+
{
30+
ImportSystem,
31+
ImportMicrosoftCSharp,
32+
AliasSystemIoPathAsPath,
33+
AliasSystemAbcAsXyz
34+
},
35+
new[]
36+
{
37+
new[] { ImportSystem, ImportMicrosoftCSharp },
38+
new[] { AliasSystemIoPathAsPath, AliasSystemAbcAsXyz }
39+
});
40+
}
41+
42+
43+
[Test]
44+
public void TwoMatchFirstGroupTwoAreAliasesOutOfOrder()
45+
{
46+
Verify(
47+
new[]
48+
{
49+
AliasSystemIoPathAsPath,
50+
ImportMicrosoftCSharp,
51+
AliasSystemAbcAsXyz,
52+
ImportSystem
53+
},
54+
new[]
55+
{
56+
new[] { ImportSystem, ImportMicrosoftCSharp },
57+
new[] { AliasSystemIoPathAsPath, AliasSystemAbcAsXyz }
58+
});
59+
}
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.TwoSpecificThenDefaultThenAliases
2+
{
3+
using NUnit.Framework;
4+
5+
using OrderUsings.Configuration;
6+
7+
public class WhenAliasesAndImportsOrderedByNamespace : TwoGroupsThenAliasesBase
8+
{
9+
protected override OrderAliasBy AliasOrder { get { return OrderAliasBy.Namespace; } }
10+
11+
[Test]
12+
public void TwoMatchFirstGroupThirdIsAlias()
13+
{
14+
Verify(
15+
new[]
16+
{
17+
ImportSystem,
18+
ImportMicrosoftCSharp,
19+
AliasSystemIoPathAsPath
20+
},
21+
new[]
22+
{
23+
new[] { ImportSystem, ImportMicrosoftCSharp },
24+
new[] { AliasSystemIoPathAsPath }
25+
});
26+
}
27+
28+
[Test]
29+
public void TwoMatchFirstGroupTwoAreAliasesInOrder()
30+
{
31+
Verify(
32+
new[]
33+
{
34+
ImportSystem,
35+
ImportMicrosoftCSharp,
36+
AliasSystemIoPathAsPath,
37+
AliasSystemAbcAsXyz
38+
},
39+
new[]
40+
{
41+
new[] { ImportSystem, ImportMicrosoftCSharp },
42+
new[] { AliasSystemAbcAsXyz, AliasSystemIoPathAsPath }
43+
});
44+
}
45+
46+
47+
[Test]
48+
public void TwoMatchFirstGroupTwoAreAliasesOutOfOrder()
49+
{
50+
Verify(
51+
new[]
52+
{
53+
AliasSystemAbcAsXyz,
54+
ImportMicrosoftCSharp,
55+
AliasSystemIoPathAsPath,
56+
ImportSystem
57+
},
58+
new[]
59+
{
60+
new[] { ImportSystem, ImportMicrosoftCSharp },
61+
new[] { AliasSystemAbcAsXyz, AliasSystemIoPathAsPath }
62+
});
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)