Skip to content

Commit ee91763

Browse files
committed
First usable version.
1 parent e857433 commit ee91763

File tree

74 files changed

+3598
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+3598
-2
lines changed

.gitattributes

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Auto-detect text files so that they can be stored with LF endings
2+
# in github, and CRLF endings locally in Windows.
3+
* text=auto
4+
5+
# Avoid potential misdetection of some common file types
6+
*.cs text
7+
*.csproj text
8+
*.Config text
9+
*.config text
10+
*.StyleCop text
11+
*.resx text
12+
13+
# Apparently ReSharper uses LF line endings in its settings files,
14+
# so tell git that we know this to avoid warnings.
15+
*.DotSettings text eol=lf

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ publish/
9797
*.pubxml
9898

9999
# NuGet Packages Directory
100-
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
101-
#packages/
100+
packages/
101+
102+
# NuGet packages
103+
*.nupkg
102104

103105
# Windows Azure Build Output
104106
csx

nuget/OrderUsings.nuspec

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0"?>
2+
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
3+
<metadata>
4+
<id>OrderUsings</id>
5+
<title>Order and space using directives</title>
6+
<version>1.0.0</version>
7+
<authors>Ian Griffiths</authors>
8+
<owners>Ian Griffiths</owners>
9+
<description>Enables configurable fine control over the ordering, grouping, and spacing of C# using directives. Instead of being limited to two groups - System*, and everything else - you can define any number of groups in any order to arrange using directives however makes most sense for your project.</description>
10+
<summary>Control over the ordering, grouping, and spacing of C# using directives.</summary>
11+
<copyright>Copyright &#x00A9; 2014 Ian Griffiths</copyright>
12+
<releaseNotes>&#x2022; 1.0.0 - First release (supporting only ReSharper 8.1)</releaseNotes>
13+
<projectUrl>https://github.com/idg10/order-usings</projectUrl>
14+
<licenseUrl>https://github.com/idg10/order-usings/blob/master/LICENSE</licenseUrl>
15+
<dependencies>
16+
<dependency id="ReSharper" version="[8.1, 8.2)" />
17+
</dependencies>
18+
</metadata>
19+
<files>
20+
<file src="..\src\ReSharper810\bin\Release\OrderUsings.ReSharper810.dll"
21+
target="ReSharper\v8.1\plugins" />
22+
<file src="..\src\OrderUsings.Core\bin\Release\OrderUsings.Core.dll"
23+
target="ReSharper\v8.1\plugins" />
24+
</files>
25+
</package>

src/.nuget/packages.config

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="NUnit.Runners" version="2.6.3" />
4+
</packages>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using System.Collections.Generic;
4+
5+
using OrderUsings.Configuration;
6+
7+
public abstract class SinglePatternMatchesAllTestBase : OrderAndSpacingDeterminationTestBase
8+
{
9+
internal override List<ConfigurationRule> GetRules()
10+
{
11+
return new List<ConfigurationRule>
12+
{
13+
ConfigurationRule.ForGroupRule(new GroupRule
14+
{
15+
Type = MatchType.ImportOrAlias,
16+
NamespacePattern = "*",
17+
AliasPattern = "*",
18+
Priority = 1,
19+
OrderAliasesBy = OrderAliasBy.Alias
20+
})
21+
};
22+
}
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenAliasesOrderedByAlias : SinglePatternMatchesAllTestBase
6+
{
7+
[Test]
8+
public void ProducesSingleGroupOrderedByAliasWhenAliasesAndNamespaceOtherwise()
9+
{
10+
Verify(
11+
new[]
12+
{
13+
AliasSystemPathAsPath,
14+
ImportSystem,
15+
ImportRuhroh,
16+
AliasSystemLaterAsEarlier
17+
},
18+
new[]
19+
{
20+
new[]
21+
{
22+
AliasSystemLaterAsEarlier,
23+
AliasSystemPathAsPath,
24+
ImportRuhroh,
25+
ImportSystem
26+
}
27+
});
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using System.Collections.Generic;
4+
5+
using NUnit.Framework;
6+
7+
using OrderUsings.Configuration;
8+
9+
public class WhenAliasesOrderedByNamespace : SinglePatternMatchesAllTestBase
10+
{
11+
internal override List<ConfigurationRule> GetRules()
12+
{
13+
var r = base.GetRules();
14+
r[0].Rule.OrderAliasesBy = OrderAliasBy.Namespace;
15+
return r;
16+
}
17+
18+
[Test]
19+
public void ProducesSingleGroupOrderedByNamespace()
20+
{
21+
Verify(
22+
new[]
23+
{
24+
AliasSystemPathAsPath,
25+
ImportSystem,
26+
ImportRuhroh,
27+
AliasSystemLaterAsEarlier
28+
},
29+
new[]
30+
{
31+
new[]
32+
{
33+
ImportRuhroh,
34+
ImportSystem,
35+
AliasSystemPathAsPath,
36+
AliasSystemLaterAsEarlier
37+
}
38+
});
39+
}
40+
41+
// This class introduces a change in config, so we should verify that
42+
// empty input handling still works.
43+
[Test]
44+
public void EmptyUsingsProducesNoGroups()
45+
{
46+
VerifyEmptyHandling();
47+
}
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenImportAndAliasShareName : SinglePatternMatchesAllTestBase
6+
{
7+
[Test]
8+
public void GroupItemsShouldPutNonAliasFirst()
9+
{
10+
// Bizarre though it seems, this:
11+
// using System;
12+
// using System = System.Text;
13+
// is legal. If a group orders using alias directives by Alias (which is the default)
14+
// we need to pick one to go first. We put the non-alias one first (i.e., the order
15+
// shown above), since this seems most consistent with the behaviour of ordering
16+
// usings lexographically within a group.
17+
Verify(
18+
new[]
19+
{
20+
AliasSystemTextAsSystem,
21+
ImportSystem
22+
},
23+
new[]
24+
{
25+
new[]
26+
{
27+
ImportSystem,
28+
AliasSystemTextAsSystem
29+
}
30+
});
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenNoUsings : SinglePatternMatchesAllTestBase
6+
{
7+
[Test]
8+
public void ProducesNoGroups()
9+
{
10+
VerifyEmptyHandling();
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenOneImport : SinglePatternMatchesAllTestBase
6+
{
7+
[Test]
8+
public void ProducesOneGroup()
9+
{
10+
Verify(new[] { ImportSystem }, new[] { new[] { ImportSystem } });
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
2+
{
3+
using NUnit.Framework;
4+
5+
public class WhenThreeImports : SinglePatternMatchesAllTestBase
6+
{
7+
[Test]
8+
public void ProducesOneGroupInAlphabeticalOrder()
9+
{
10+
Verify(
11+
new[]
12+
{
13+
ImportSystem,
14+
ImportSystemCollectionsGeneric,
15+
ImportSystemLinq
16+
},
17+
new[]
18+
{
19+
new[]
20+
{
21+
ImportSystem,
22+
ImportSystemCollectionsGeneric,
23+
ImportSystemLinq
24+
}
25+
});
26+
}
27+
}
28+
}

0 commit comments

Comments
 (0)