Skip to content

Commit

Permalink
chore: reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
mika-f committed Mar 6, 2024
1 parent 3420734 commit d51e51a
Show file tree
Hide file tree
Showing 10 changed files with 224 additions and 221 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public async Task CanInstantiateWithDefaults()
}

[Fact]
public async Task CanInstantiateWithTypeArgs()
public async Task CanInstantiateWithMethodArgs()
{
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=T:System.Diagnostics.Debug");
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Console.WriteLine");
var instance = await container.InstantiateWithBind();

Assert.NotNull(instance);
Expand All @@ -36,9 +36,9 @@ public async Task CanInstantiateWithTypeArgs()
}

[Fact]
public async Task CanInstantiateWithMethodArgs()
public async Task CanInstantiateWithTypeArgs()
{
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Console.WriteLine");
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=T:System.Diagnostics.Debug");
var instance = await container.InstantiateWithBind();

Assert.NotNull(instance);
Expand All @@ -62,43 +62,43 @@ public async Task CanInstantiateWithUnknownArgs()
}

[Fact]
public async Task RemoveType()
public async Task RemoveMethod()
{
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=T:System.Diagnostics.Debug");
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Console.WriteLine");
await container.RunAsync();

var source = await container.GetSourceByPathAsync("Plana.Logging/Logger.cs");

Assert.False(await source.ContainsAsync("Debug.WriteLine"));
Assert.False(await source.ContainsAsync("Console.WriteLine"));
Assert.True(await source.ContainsAsync("private void __LogStubInternal()"));
}

[Fact]
public async Task RemoveMethod()
public async Task RemoveType()
{
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Console.WriteLine");
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=T:System.Diagnostics.Debug");
await container.RunAsync();

var source = await container.GetSourceByPathAsync("Plana.Logging/Logger.cs");

Assert.False(await source.ContainsAsync("Console.WriteLine"));
Assert.False(await source.ContainsAsync("Debug.WriteLine"));
Assert.True(await source.ContainsAsync("private void __LogStubInternal()"));
}

[Fact]
public async Task RemoveUnknownType_NoSourceDiffs()
public async Task RemoveUnknownMethod_NoSourceDiffs()
{
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Diagnostics.Console");
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Console.WriteLine22");
await container.RunAsync();

var source = await container.GetSourceByPathAsync("Plana.Logging/Logger.cs");
await source.NoDiffs();
}

[Fact]
public async Task RemoveUnknownMethod_NoSourceDiffs()
public async Task RemoveUnknownType_NoSourceDiffs()
{
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Console.WriteLine22");
var container = new PlanaContainer<DisableConsoleOutputPlugin>("disable-console-output", "disable-symbols=M:System.Diagnostics.Console");
await container.RunAsync();

var source = await container.GetSourceByPathAsync("Plana.Logging/Logger.cs");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ namespace Plana.Composition.Extensions.Tests;
public class CSharpSyntaxNodeExtensionsTest
{
[Fact]
public void HasAnnotationCommentReturnTrueWhenSyntaxHasAnnotationComment()
public void HasAnnotationCommentReturnFalseWhenSyntaxHasNotAnnotationComment()
{
var source = SyntaxFactory.ParseCompilationUnit("/* plana:disable */ namespace TestNamespace {}");
var source = SyntaxFactory.ParseCompilationUnit("namespace TestNamespace {}");
var @namespace = source.Members[0];

Assert.True(@namespace.HasAnnotationComment());
Assert.False(@namespace.HasAnnotationComment());
}

[Fact]
public void HasAnnotationCommentReturnFalseWhenSyntaxHasNotAnnotationComment()
public void HasAnnotationCommentReturnTrueWhenSyntaxHasAnnotationComment()
{
var source = SyntaxFactory.ParseCompilationUnit("namespace TestNamespace {}");
var source = SyntaxFactory.ParseCompilationUnit("/* plana:disable */ namespace TestNamespace {}");
var @namespace = source.Members[0];

Assert.False(@namespace.HasAnnotationComment());
Assert.True(@namespace.HasAnnotationComment());
}
}
40 changes: 20 additions & 20 deletions src/Plana.Composition.Extensions.Tests/PlanaPluginOptionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,24 @@ namespace Plana.Composition.Extensions.Tests;
public class PlanaPluginOptionTest
{
[Fact]
public void NameDoesNotStartsWithHyphen()
public void FriendlyNameIsNotSameValueWithNamePassedInConstructor()
{
Assert.Throws<InvalidFormatException>(() => PlanaPluginOption.ValidateName("-name"));
Assert.Throws<InvalidFormatException>(() => PlanaPluginOption.ValidateName("--name"));
var instance = new PlanaPluginOption("name", "friendly name", "description", false);
Assert.Equal("friendly name", instance.FriendlyName);
}

[Fact]
public void FriendlyNameIsSameValueWithNameNotPassedInConstructor()
{
var instance = new PlanaPluginOption("name", "description", false);
Assert.Equal(instance.Name, instance.FriendlyName);
}

[Fact]
public void HasDescription()
{
var instance = new PlanaPluginOption("name", "description", false);
Assert.Equal("description", instance.Description);
}

[Fact]
Expand All @@ -39,23 +53,9 @@ public void NameContainsOnlyAlphaNumerical()
}

[Fact]
public void FriendlyNameIsSameValueWithNameNotPassedInConstructor()
{
var instance = new PlanaPluginOption("name", "description", false);
Assert.Equal(instance.Name, instance.FriendlyName);
}

[Fact]
public void FriendlyNameIsNotSameValueWithNamePassedInConstructor()
{
var instance = new PlanaPluginOption("name", "friendly name", "description", false);
Assert.Equal("friendly name", instance.FriendlyName);
}

[Fact]
public void HasDescription()
public void NameDoesNotStartsWithHyphen()
{
var instance = new PlanaPluginOption("name", "description", false);
Assert.Equal("description", instance.Description);
Assert.Throws<InvalidFormatException>(() => PlanaPluginOption.ValidateName("-name"));
Assert.Throws<InvalidFormatException>(() => PlanaPluginOption.ValidateName("--name"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,69 +106,6 @@ bool IsMethodsIsGetHashCodeSignature(MethodDeclarationSyntax w, SemanticModel sm
Assert.Equal(nameof(MeaningEqualitySymbolComparator.GetHashCode), getHashCode.Identifier.ToString());
}

[Fact]
public async Task RenameMethods_InterfaceMethods()
{
var container = new PlanaContainer<RenameSymbolsPlugin>("rename-methods");
await container.RunAsync();

var rootAbstraction = await container.GetSourceByPathAsync("Plana.Composition.Abstractions/IPlanaPlugin.cs");
var inheritAbstraction = await container.GetSourceByPathAsync("Plana.Composition.Extensions/IPlanaPlugin2.cs");
var implementation = await container.GetSourceByPathAsync("Plana.Composition.RenameSymbols/RenameSymbolsPlugin.cs");
var reference = await container.GetSourceByPathAsync("Plana/Obfuscator.cs");

// IPlanaPlugin.RunAsync -> _0xa79a150d
const string runAsyncIdentifier = "_0xa79a150d";

bool IsMethodsHasRunAsyncSignature(MethodDeclarationSyntax w, SemanticModel sm)
{
var identifier = w.ParameterList.Parameters[0].Type;
if (identifier == null)
return false;

var si = sm.GetSymbolInfo(identifier);
if (si.Symbol is not INamedTypeSymbol param)
return false;

return param.Equals(typeof(IPlanaPluginRunContext).ToSymbol(sm), SymbolEqualityComparer.Default);
}

var rootAbstractionDecl = await rootAbstraction.GetFirstSyntax<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature);
Assert.Equal(runAsyncIdentifier, rootAbstractionDecl.Identifier.ToString());

var inheritAbstractionDecl = await inheritAbstraction.GetFirstSyntax<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature);
Assert.Equal(runAsyncIdentifier, inheritAbstractionDecl.Identifier.ToString());

var referenceDecl = await reference.GetFirstSyntax<InvocationExpressionSyntax>((w, sm) =>
{
var expression = w.Expression;
if (expression is not MemberAccessExpressionSyntax access)
return false;

var receiver = access.Expression;
var si1 = sm.GetSymbolInfo(receiver);
if (si1.Symbol is not ILocalSymbol local1)
return false;

var param = w.ArgumentList.Arguments[0];
var si2 = sm.GetSymbolInfo(param.Expression);
if (si2.Symbol is not ILocalSymbol local2)
return false;

return local1.Type.Equals(typeof(IPlanaPlugin).ToSymbol(sm), SymbolEqualityComparer.Default) && local2.Type.Interfaces[0].Equals(typeof(IPlanaPluginRunContext).ToSymbol(sm), SymbolEqualityComparer.Default);
});
Assert.Equal(runAsyncIdentifier, ((MemberAccessExpressionSyntax)referenceDecl.Expression).Name.Identifier.ToString());

// IPlanaPlugin2.ObfuscateAsync -> _0xba881b8e
const string obfuscateAsyncIdentifier = "_0xba881b8e";

var inheritAbstractionDecl2 = (await inheritAbstraction.GetSyntaxList<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature))[1];
Assert.Equal(obfuscateAsyncIdentifier, inheritAbstractionDecl2.Identifier.ToString());

var implementationDecl = await implementation.GetFirstSyntax<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature);
Assert.Equal(obfuscateAsyncIdentifier, implementationDecl.Identifier.ToString());
}

[Fact]
public async Task RenameMethods_GenericsInterfaceMethods()
{
Expand Down Expand Up @@ -244,6 +181,69 @@ public async Task RenameMethods_GenericsMethods()
Assert.Equal($"{identifier}<T>", reference.Expression.ToString());
}

[Fact]
public async Task RenameMethods_InterfaceMethods()
{
var container = new PlanaContainer<RenameSymbolsPlugin>("rename-methods");
await container.RunAsync();

var rootAbstraction = await container.GetSourceByPathAsync("Plana.Composition.Abstractions/IPlanaPlugin.cs");
var inheritAbstraction = await container.GetSourceByPathAsync("Plana.Composition.Extensions/IPlanaPlugin2.cs");
var implementation = await container.GetSourceByPathAsync("Plana.Composition.RenameSymbols/RenameSymbolsPlugin.cs");
var reference = await container.GetSourceByPathAsync("Plana/Obfuscator.cs");

// IPlanaPlugin.RunAsync -> _0xa79a150d
const string runAsyncIdentifier = "_0xa79a150d";

bool IsMethodsHasRunAsyncSignature(MethodDeclarationSyntax w, SemanticModel sm)
{
var identifier = w.ParameterList.Parameters[0].Type;
if (identifier == null)
return false;

var si = sm.GetSymbolInfo(identifier);
if (si.Symbol is not INamedTypeSymbol param)
return false;

return param.Equals(typeof(IPlanaPluginRunContext).ToSymbol(sm), SymbolEqualityComparer.Default);
}

var rootAbstractionDecl = await rootAbstraction.GetFirstSyntax<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature);
Assert.Equal(runAsyncIdentifier, rootAbstractionDecl.Identifier.ToString());

var inheritAbstractionDecl = await inheritAbstraction.GetFirstSyntax<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature);
Assert.Equal(runAsyncIdentifier, inheritAbstractionDecl.Identifier.ToString());

var referenceDecl = await reference.GetFirstSyntax<InvocationExpressionSyntax>((w, sm) =>
{
var expression = w.Expression;
if (expression is not MemberAccessExpressionSyntax access)
return false;

var receiver = access.Expression;
var si1 = sm.GetSymbolInfo(receiver);
if (si1.Symbol is not ILocalSymbol local1)
return false;

var param = w.ArgumentList.Arguments[0];
var si2 = sm.GetSymbolInfo(param.Expression);
if (si2.Symbol is not ILocalSymbol local2)
return false;

return local1.Type.Equals(typeof(IPlanaPlugin).ToSymbol(sm), SymbolEqualityComparer.Default) && local2.Type.Interfaces[0].Equals(typeof(IPlanaPluginRunContext).ToSymbol(sm), SymbolEqualityComparer.Default);
});
Assert.Equal(runAsyncIdentifier, ((MemberAccessExpressionSyntax)referenceDecl.Expression).Name.Identifier.ToString());

// IPlanaPlugin2.ObfuscateAsync -> _0xba881b8e
const string obfuscateAsyncIdentifier = "_0xba881b8e";

var inheritAbstractionDecl2 = (await inheritAbstraction.GetSyntaxList<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature))[1];
Assert.Equal(obfuscateAsyncIdentifier, inheritAbstractionDecl2.Identifier.ToString());

var implementationDecl = await implementation.GetFirstSyntax<MethodDeclarationSyntax>(IsMethodsHasRunAsyncSignature);
Assert.Equal(obfuscateAsyncIdentifier, implementationDecl.Identifier.ToString());
}

[Fact]
public async Task RenameMethods_OriginalDefinitionMethods()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,40 @@ public async Task CanInstantiate()
Assert.Empty(instance.Options);
}

[Fact(Skip = "Not Implemented Yet")]
public Task KeepIfAndEndIfPreprocessors()
{
return Task.CompletedTask;
}

[Fact]
public async Task RemovePragmaDisableAndRestorePreprocessors()
{
var container = new PlanaContainer<ShuffleMemberDeclarations>();
await container.RunAsync("../../../../Plana.Testing/Plana.Testing.csproj");

var source = await container.GetSourceByPathAsync("PlanaContainer{T}.cs");

await source.HasDiffs();

Assert.False(await source.ContainsAsync("#pragma warning disable"));
Assert.False(await source.ContainsAsync("#pragma warning restore"));
}

[Fact]
public async Task RemoveRegionAndEndregionPreprocessors()
{
var container = new PlanaContainer<ShuffleMemberDeclarations>();
await container.RunAsync("../../../../Plana.Composition.RenameSymbols/Plana.Composition.RenameSymbols.csproj");

var source = await container.GetSourceByPathAsync("CSharpSymbolsWalker.cs");

await source.HasDiffs();

Assert.False(await source.ContainsAsync("#region"));
Assert.False(await source.ContainsAsync("#endregion"));
}

[Fact]
public async Task ShuffleWithRandom()
{
Expand Down Expand Up @@ -88,38 +122,4 @@ public PlanaRandom()
}
}".Trim());
}

[Fact]
public async Task RemoveRegionAndEndregionPreprocessors()
{
var container = new PlanaContainer<ShuffleMemberDeclarations>();
await container.RunAsync("../../../../Plana.Composition.RenameSymbols/Plana.Composition.RenameSymbols.csproj");

var source = await container.GetSourceByPathAsync("CSharpSymbolsWalker.cs");

await source.HasDiffs();

Assert.False(await source.ContainsAsync("#region"));
Assert.False(await source.ContainsAsync("#endregion"));
}

[Fact(Skip = "Not Implemented Yet")]
public Task KeepIfAndEndIfPreprocessors()
{
return Task.CompletedTask;
}

[Fact]
public async Task RemovePragmaDisableAndRestorePreprocessors()
{
var container = new PlanaContainer<ShuffleMemberDeclarations>();
await container.RunAsync("../../../../Plana.Testing/Plana.Testing.csproj");

var source = await container.GetSourceByPathAsync("PlanaContainer{T}.cs");

await source.HasDiffs();

Assert.False(await source.ContainsAsync("#pragma warning disable"));
Assert.False(await source.ContainsAsync("#pragma warning restore"));
}
}
Loading

0 comments on commit d51e51a

Please sign in to comment.