Skip to content

Commit

Permalink
chore: add more test
Browse files Browse the repository at this point in the history
  • Loading branch information
mika-f committed Feb 28, 2024
1 parent f60bced commit 0e76cf4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,18 @@ public async Task RemoveRegionAndEndregionPreprocessors()
Assert.False(await source.ContainsAsync("#region"));
Assert.False(await source.ContainsAsync("#endregion"));
}

[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"));
}
}
18 changes: 18 additions & 0 deletions src/Plana.Composition.ShuffleDeclarations/RemovePragmaRewriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// ------------------------------------------------------------------------------------------
// Copyright (c) Natsuneko. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
// ------------------------------------------------------------------------------------------

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Plana.Composition.ShuffleDeclarations;

internal class RemovePragmaRewriter() : CSharpSyntaxRewriter(true)
{
public override SyntaxNode? VisitPragmaWarningDirectiveTrivia(PragmaWarningDirectiveTriviaSyntax node)
{
return SyntaxFactory.SkippedTokensTrivia();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public async Task ObfuscateAsync(IPlanaPluginRunContext context)
var transformers = new List<CSharpSyntaxRewriter>
{
new RemoveRegionAndEndRegionRewriter(),
new RemovePragmaRewriter(),
new CSharpDeclarationRewriter(context.SecureRandom)
};

Expand Down
3 changes: 2 additions & 1 deletion src/Plana.Testing/PlanaContainer{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ public Task<T> Instantiate()
return Task.FromResult(instance);
}

#pragma warning disable CS8774
[MemberNotNull(nameof(Workspace), nameof(Sources), nameof(_root))]
public async Task RunAsync(string path = "../../../../Plana.sln", int seed = 150)
{
var logger = new Logger();
var source = new CancellationTokenSource();

Workspace = path.EndsWith(".sln") ? await SolutionWorkspace.CreateWorkspaceAsync(new FileInfo(path), logger, CancellationToken.None) : await ProjectWorkspace.CreateWorkspaceAsync(new FileInfo(path), logger, CancellationToken.None);

Expand All @@ -67,6 +67,7 @@ public async Task RunAsync(string path = "../../../../Plana.sln", int seed = 150

Sources = await obfuscator.RunAsync(RunKind.Obfuscate, random, random, CancellationToken.None);
}
#pragma warning restore CS8774

public async Task<InlineSource> GetSourceByPathAsync(string path)
{
Expand Down

0 comments on commit 0e76cf4

Please sign in to comment.