Skip to content

Commit

Permalink
Add test for TextureExistsRewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Chik3r committed Aug 3, 2021
1 parent ad9cbd3 commit 654dbe3
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions tModPorter.Tests/RewriterTests/TextureExistsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using tModPorter.Rewriters;
using tModPorter.Rewriters.MemberAccessRewriters;
using Xunit;

namespace tModPorter.Tests.RewriterTests
{
public class TextureExistsTest
{
[Theory]
[InlineData("TextureExists(\"string\")", 0)]
[InlineData("ModContent.TextureExists(\"string\")", 1)]
[InlineData("if (ModContent.TextureExists(field)) { Write(ModContent.TextureExists(field)) }", 2)]
public void VisitNode_ShouldMatchNodeCount(string statement, int nodeCount)
{
CreateSimpleRewriter(statement, out TextureExistsRewriter rewriter,
out HashSet<(BaseRewriter rewriter, SyntaxNode originalNode)> nodeSet, out CompilationUnitSyntax root);

foreach (SyntaxNode assigmentNode in root.DescendantNodes())
rewriter.VisitNode(assigmentNode);

Assert.Equal(nodeCount, nodeSet.Count);
}

[Fact]
public void RewriteNode_ShouldMatchTarget()
{
const string statement = "ModContent.TextureExists(\"string\")";
const string target = "ModContent.HasAsset(\"string\")";

CreateSimpleRewriter(statement, out TextureExistsRewriter rewriter,
out HashSet<(BaseRewriter rewriter, SyntaxNode originalNode)> nodeSet, out CompilationUnitSyntax root);

foreach (SyntaxNode assigmentNode in root.DescendantNodes())
rewriter.VisitNode(assigmentNode);

root = root.RewriteMultipleNodes(nodeSet);

Assert.Equal(target, root.ToFullString());
}

private static void CreateSimpleRewriter(string source, out TextureExistsRewriter rewriter,
out HashSet<(BaseRewriter rewriter, SyntaxNode originalNode)> nodeSet, out CompilationUnitSyntax root) {
Utils.CreateCSharpCompilation(source, nameof(FindTypeRewriterTest), out _, out root, out SemanticModel model);

nodeSet = new HashSet<(BaseRewriter rewriter, SyntaxNode originalNode)>();
rewriter = new TextureExistsRewriter(model, null, nodeSet);
}
}
}

0 comments on commit 654dbe3

Please sign in to comment.