Skip to content

Commit

Permalink
Success!
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneidereit committed Sep 9, 2017
1 parent 3f8ff70 commit af960a4
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MsTestToXunitConverter.xUnit
{
class ConstructorA
{

public void SomeInitMethod()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class TestClass
{
public void DoSomethingBad()
{
Assert.Throws<ArgumentNullException>(() => { Foo(); });
Assert.Throws<ArgumentNullException>(() =>
{
Foo();
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MsTestToXunitConverter.xUnit
{
class DisposeA : IDisposable
{

public void Cleanup()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MsTestToXunitConverter.xUnit
{
class DisposeB : IDisposable
{

public void Cleanup()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace MsTestToXunitConverter.xUnit
{
class DisposeC : ICloneable, IDisposable
{

public void Cleanup()
{

Expand Down
34 changes: 28 additions & 6 deletions MsTestToXunitConverter/Transformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,33 @@ private static AttributeArgumentListSyntax CreateArgumentList(string name, Attri

private static MethodDeclarationSyntax Cleanup(this MethodDeclarationSyntax method)
{
AttributeListSyntax al;
while ((al = method.AttributeLists.FirstOrDefault(l => l.Attributes.Count == 0)) != null)
{
var option = al.GetTrailingTrivia().All(t => t.IsKind(SyntaxKind.WhitespaceTrivia) || t.IsKind(SyntaxKind.EndOfLineTrivia))
? SyntaxRemoveOptions.KeepLeadingTrivia
: SyntaxRemoveOptions.KeepExteriorTrivia;

method = method.RemoveNodes(method.AttributeLists.Where(als => als.Attributes.Count == 0), SyntaxRemoveOptions.KeepExteriorTrivia);
method = method.RemoveNode(al, option);
}

return method;
}

internal static ClassDeclarationSyntax Cleanup(this ClassDeclarationSyntax type)
{
type = type.RemoveNodes(type.AttributeLists.Where(als => als.Attributes.Count == 0), SyntaxRemoveOptions.KeepExteriorTrivia);
foreach (var l in type.AttributeLists)
{
if (l.Attributes.Count == 0)
{
var option = l.GetTrailingTrivia().All(t => t.IsKind(SyntaxKind.WhitespaceTrivia) || t.IsKind(SyntaxKind.EndOfLineTrivia))
? SyntaxRemoveOptions.KeepLeadingTrivia
: SyntaxRemoveOptions.KeepExteriorTrivia;

type = type.RemoveNode(l, SyntaxRemoveOptions.KeepExteriorTrivia);
}
}

foreach (var m in type.Members.OfType<MethodDeclarationSyntax>())
{
type = type.ReplaceNode(m, m.Cleanup());
Expand Down Expand Up @@ -107,10 +126,10 @@ internal static MethodDeclarationSyntax StripSurjectiveFactAttributes(this Metho
factAttribute = factAttribute.WithArgumentList(CreateArgumentList("Skip", ignore, factAttribute.ArgumentList).WithAdditionalAnnotations(annotation));
method = method.RemoveNode(method.GetTargetAttribute("Ignore"), SyntaxRemoveOptions.KeepExteriorTrivia);
}

attr = method.GetTargetAttribute("TestMethod");
method = method.ReplaceNode(attr, factAttribute).WithAdditionalAnnotations(annotation);

return method.Cleanup();
}

Expand All @@ -129,8 +148,11 @@ internal static ClassDeclarationSyntax StripTestInitializerAttribute(this ClassD

//Refresh reference
target = type.Members.OfType<MethodDeclarationSyntax>().SingleOrDefault(m => m.GetTargetAttribute("TestInitialize") != null);
type = type.ReplaceNode(target, target.RemoveNode(target.GetTargetAttribute("TestInitialize"), SyntaxRemoveOptions.KeepExteriorTrivia));
var cleanedTarget = target.RemoveNode(target.GetTargetAttribute("TestInitialize"), SyntaxRemoveOptions.KeepExteriorTrivia)
.WithAdditionalAnnotations(annotation);

type = type.ReplaceNode(target, cleanedTarget);

return type.Cleanup();
}

Expand Down Expand Up @@ -169,7 +191,7 @@ BaseListSyntax CreateBaseList(string name, BaseListSyntax other)

target = type.Members.OfType<MethodDeclarationSyntax>().SingleOrDefault(m => m.GetTargetAttribute("TestCleanup") != null);
type = type.ReplaceNode(target, target.RemoveNode(target.GetTargetAttribute("TestCleanup"), SyntaxRemoveOptions.KeepExteriorTrivia));

type = type.ReplaceToken(type.Identifier, type.Identifier.WithTrailingTrivia(type.Identifier.TrailingTrivia.Where(t => !t.IsKind(SyntaxKind.EndOfLineTrivia))));
type = type.WithBaseList(CreateBaseList("IDisposable", type.BaseList)).WithAdditionalAnnotations(annotation);

Expand Down

0 comments on commit af960a4

Please sign in to comment.