Skip to content

Commit

Permalink
First usable version.
Browse files Browse the repository at this point in the history
  • Loading branch information
idg10 committed Mar 5, 2014
1 parent e857433 commit ee91763
Show file tree
Hide file tree
Showing 74 changed files with 3,598 additions and 2 deletions.
15 changes: 15 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Auto-detect text files so that they can be stored with LF endings
# in github, and CRLF endings locally in Windows.
* text=auto

# Avoid potential misdetection of some common file types
*.cs text
*.csproj text
*.Config text
*.config text
*.StyleCop text
*.resx text

# Apparently ReSharper uses LF line endings in its settings files,
# so tell git that we know this to avoid warnings.
*.DotSettings text eol=lf
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ publish/
*.pubxml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/
packages/

# NuGet packages
*.nupkg

# Windows Azure Build Output
csx
Expand Down
25 changes: 25 additions & 0 deletions nuget/OrderUsings.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>OrderUsings</id>
<title>Order and space using directives</title>
<version>1.0.0</version>
<authors>Ian Griffiths</authors>
<owners>Ian Griffiths</owners>
<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>
<summary>Control over the ordering, grouping, and spacing of C# using directives.</summary>
<copyright>Copyright &#x00A9; 2014 Ian Griffiths</copyright>
<releaseNotes>&#x2022; 1.0.0 - First release (supporting only ReSharper 8.1)</releaseNotes>
<projectUrl>https://github.com/idg10/order-usings</projectUrl>
<licenseUrl>https://github.com/idg10/order-usings/blob/master/LICENSE</licenseUrl>
<dependencies>
<dependency id="ReSharper" version="[8.1, 8.2)" />
</dependencies>
</metadata>
<files>
<file src="..\src\ReSharper810\bin\Release\OrderUsings.ReSharper810.dll"
target="ReSharper\v8.1\plugins" />
<file src="..\src\OrderUsings.Core\bin\Release\OrderUsings.Core.dll"
target="ReSharper\v8.1\plugins" />
</files>
</package>
4 changes: 4 additions & 0 deletions src/.nuget/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NUnit.Runners" version="2.6.3" />
</packages>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination
{
using System.Collections.Generic;
using System.Linq;

using NUnit.Framework;

using OrderUsings.Configuration;
using OrderUsings.Processing;

public abstract class OrderAndSpacingDeterminationTestBase
{
internal static readonly UsingDirective AliasSystemPathAsPath = new UsingDirective { Namespace = "System.IO.Path", Alias = "Path" };
internal static readonly UsingDirective AliasSystemLaterAsEarlier = new UsingDirective { Namespace = "System.Later", Alias = "Earlier" };
internal static readonly UsingDirective AliasSystemTextAsSystem = new UsingDirective { Namespace = "System.Text", Alias = "System" };

internal static readonly UsingDirective ImportSystem = new UsingDirective { Namespace = "System" };
internal static readonly UsingDirective ImportSystemCollectionsGeneric = new UsingDirective { Namespace = "System.Collections.Generic" };
internal static readonly UsingDirective ImportSystemLinq = new UsingDirective { Namespace = "System.Linq" };

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

internal static readonly UsingDirective ImportOther = new UsingDirective { Namespace = "Other" };
internal static readonly UsingDirective ImportOtherA = new UsingDirective { Namespace = "Other.A" };
internal static readonly UsingDirective ImportOtherB = new UsingDirective { Namespace = "Other.B" };

internal static readonly UsingDirective ImportMyLocal = new UsingDirective { Namespace = "MyLocal" };
internal static readonly UsingDirective ImportMyLocalA = new UsingDirective { Namespace = "MyLocal.A" };
internal static readonly UsingDirective ImportMyLocalB = new UsingDirective { Namespace = "MyLocal.B" };

internal static readonly UsingDirective ImportRuhroh = new UsingDirective { Namespace = "Ruhroh" };


internal OrderUsingsConfiguration Configuration { get; private set; }

[SetUp]
public void BaseInitialize()
{
Configuration = new OrderUsingsConfiguration
{
GroupsAndSpaces = GetRules()
};
}

internal abstract List<ConfigurationRule> GetRules();

internal void Verify(UsingDirective[] directivesIn, params UsingDirective[][] expectedGroups)
{
foreach (var permutation in AllOrderings(directivesIn))
{
var results = OrderAndSpacingGenerator.DetermineOrderAndSpacing(
permutation, Configuration);

Assert.AreEqual(expectedGroups.Length, results.Count);
for (int i = 0; i < expectedGroups.Length; ++i)
{
Assert.AreEqual(results[i].Count, expectedGroups[i].Length, "Item count in group " + i);
for (int j = 0; j < expectedGroups[i].Length; ++j)
{
UsingDirective expectedUsing = expectedGroups[i][j];
UsingDirective actualUsing = results[i][j];
string message = string.Format(
"Expected {0} at {1},{2}, found {3}", expectedUsing, i, j, actualUsing);
Assert.AreSame(expectedUsing, actualUsing, message);
}
}
}
}

// This is the same for all configurations. We only want to run the test
// once per config, so we don't make this a [Test] in this base class - that
// would run it once per derived class. Instead, just one derived classes
// per config will defer to this.
protected void VerifyEmptyHandling()
{
Verify(new UsingDirective[0], new UsingDirective[0][]);
}

private static IEnumerable<IEnumerable<T>> AllOrderings<T>(IEnumerable<T> items)
{
bool returnedAtLeastOne = false;
int index = 0;
// ReSharper disable PossibleMultipleEnumeration
foreach (T item in items)
{
returnedAtLeastOne = true;
int thisIndex = index;
foreach (var remainders in AllOrderings(items.Where((x, i) => i != thisIndex)))
{
yield return new[] { item }.Concat(remainders);
}
index += 1;
}
// ReSharper restore PossibleMultipleEnumeration

if (!returnedAtLeastOne)
{
yield return Enumerable.Empty<T>();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using System.Collections.Generic;

using OrderUsings.Configuration;

public abstract class SinglePatternMatchesAllTestBase : OrderAndSpacingDeterminationTestBase
{
internal override List<ConfigurationRule> GetRules()
{
return new List<ConfigurationRule>
{
ConfigurationRule.ForGroupRule(new GroupRule
{
Type = MatchType.ImportOrAlias,
NamespacePattern = "*",
AliasPattern = "*",
Priority = 1,
OrderAliasesBy = OrderAliasBy.Alias
})
};
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using NUnit.Framework;

public class WhenAliasesOrderedByAlias : SinglePatternMatchesAllTestBase
{
[Test]
public void ProducesSingleGroupOrderedByAliasWhenAliasesAndNamespaceOtherwise()
{
Verify(
new[]
{
AliasSystemPathAsPath,
ImportSystem,
ImportRuhroh,
AliasSystemLaterAsEarlier
},
new[]
{
new[]
{
AliasSystemLaterAsEarlier,
AliasSystemPathAsPath,
ImportRuhroh,
ImportSystem
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using System.Collections.Generic;

using NUnit.Framework;

using OrderUsings.Configuration;

public class WhenAliasesOrderedByNamespace : SinglePatternMatchesAllTestBase
{
internal override List<ConfigurationRule> GetRules()
{
var r = base.GetRules();
r[0].Rule.OrderAliasesBy = OrderAliasBy.Namespace;
return r;
}

[Test]
public void ProducesSingleGroupOrderedByNamespace()
{
Verify(
new[]
{
AliasSystemPathAsPath,
ImportSystem,
ImportRuhroh,
AliasSystemLaterAsEarlier
},
new[]
{
new[]
{
ImportRuhroh,
ImportSystem,
AliasSystemPathAsPath,
AliasSystemLaterAsEarlier
}
});
}

// This class introduces a change in config, so we should verify that
// empty input handling still works.
[Test]
public void EmptyUsingsProducesNoGroups()
{
VerifyEmptyHandling();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using NUnit.Framework;

public class WhenImportAndAliasShareName : SinglePatternMatchesAllTestBase
{
[Test]
public void GroupItemsShouldPutNonAliasFirst()
{
// Bizarre though it seems, this:
// using System;
// using System = System.Text;
// is legal. If a group orders using alias directives by Alias (which is the default)
// we need to pick one to go first. We put the non-alias one first (i.e., the order
// shown above), since this seems most consistent with the behaviour of ordering
// usings lexographically within a group.
Verify(
new[]
{
AliasSystemTextAsSystem,
ImportSystem
},
new[]
{
new[]
{
ImportSystem,
AliasSystemTextAsSystem
}
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using NUnit.Framework;

public class WhenNoUsings : SinglePatternMatchesAllTestBase
{
[Test]
public void ProducesNoGroups()
{
VerifyEmptyHandling();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using NUnit.Framework;

public class WhenOneImport : SinglePatternMatchesAllTestBase
{
[Test]
public void ProducesOneGroup()
{
Verify(new[] { ImportSystem }, new[] { new[] { ImportSystem } });
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace OrderUsings.Tests.OrderAndSpacingDetermination.SinglePatternMatchesAll
{
using NUnit.Framework;

public class WhenThreeImports : SinglePatternMatchesAllTestBase
{
[Test]
public void ProducesOneGroupInAlphabeticalOrder()
{
Verify(
new[]
{
ImportSystem,
ImportSystemCollectionsGeneric,
ImportSystemLinq
},
new[]
{
new[]
{
ImportSystem,
ImportSystemCollectionsGeneric,
ImportSystemLinq
}
});
}
}
}
Loading

0 comments on commit ee91763

Please sign in to comment.