Skip to content

Commit

Permalink
test: DebugTransformer file info output
Browse files Browse the repository at this point in the history
  • Loading branch information
R-unic committed Jul 14, 2024
1 parent 07c6131 commit fd052e4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Roblox CSharp to Lua compiler

# To-do
- Test `DebugTransformer`
- Macro any method that takes an instance name and returns that instance (`FindFirstChild`, etc.)
- Imports
- Inheritance chains 🤮
- Append ` = null` to generated nullable properties
Expand Down
File renamed without changes.
50 changes: 50 additions & 0 deletions RobloxCS.Tests/DebugTransformer_Should.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
namespace RobloxCS.Tests
{
public class DebugTransformer_Should
{
[Theory]
[InlineData("Console.WriteLine(\"hello baby\")")]
[InlineData("Console.Write(\"hello baby\")")]
[InlineData("warn(\"hello baby\")")]
public void FileInfo_PrependsArgument(string input)
{
var cleanTree = TranspilerUtility.ParseTree(input);
var transformedTree = TranspilerUtility.TransformTree(cleanTree, [TransformFactory.Main(), TransformFactory.Debug()]);
var cleanRoot = cleanTree.GetRoot();
var cleanInvocation = cleanRoot.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
var transformedRoot = transformedTree.GetRoot();
var transformedInvocation = transformedRoot.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
var cleanArgs = cleanInvocation.ArgumentList.Arguments;
var transformedArgs = transformedInvocation.ArgumentList.Arguments;
var fileInfoArg = transformedArgs.FirstOrDefault();
Assert.True(cleanArgs.Count == 1);
Assert.True(transformedArgs.Count == 2);
Assert.NotNull(fileInfoArg);

var fileInfoLiteral = (LiteralExpressionSyntax)fileInfoArg.Expression;
Assert.Equal($"[TestFile.cs:1:13]:", fileInfoLiteral.Token.ValueText);
}

[Theory]
[InlineData("error(\"hello baby\")")]
public void FileInfo_ConcatenatesLiteral(string input)
{
var cleanTree = TranspilerUtility.ParseTree(input);
var transformedTree = TranspilerUtility.TransformTree(cleanTree, [TransformFactory.Main(), TransformFactory.Debug()]);
var cleanRoot = cleanTree.GetRoot();
var cleanInvocation = cleanRoot.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
var transformedRoot = transformedTree.GetRoot();
var transformedInvocation = transformedRoot.DescendantNodes().OfType<InvocationExpressionSyntax>().First();
var cleanArgs = cleanInvocation.ArgumentList.Arguments;
var transformedArgs = transformedInvocation.ArgumentList.Arguments;
var fullMessageArg = transformedArgs.FirstOrDefault();
Assert.True(cleanArgs.Count == 1);
Assert.True(transformedArgs.Count == 1);
Assert.NotNull(fullMessageArg);

var fullMessageBinary= (BinaryExpressionSyntax)fullMessageArg.Expression;
var fileInfoLiteral = (LiteralExpressionSyntax)fullMessageBinary.Left;
Assert.Equal("[TestFile.cs:1:13]: ", fileInfoLiteral.Token.ValueText);
}
}
}
2 changes: 1 addition & 1 deletion RobloxCS/TranspilerUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static SyntaxTree TransformTree(SyntaxTree cleanTree, List<Func<SyntaxTre
return tree;
}

public static SyntaxTree ParseTree(string source, string sourceFile = "TransformerTestFile.cs")
public static SyntaxTree ParseTree(string source, string sourceFile = "TestFile.cs")
{
var cleanTree = CSharpSyntaxTree.ParseText(source);
var compilationUnit = (CompilationUnitSyntax)cleanTree.GetRoot();
Expand Down

0 comments on commit fd052e4

Please sign in to comment.