Skip to content

Commit

Permalink
Add tests for syntax API
Browse files Browse the repository at this point in the history
Issue link: #57
  • Loading branch information
MSDN-WhiteKnight committed Mar 5, 2021
1 parent bb95872 commit 92574d5
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 1 deletion.
21 changes: 21 additions & 0 deletions tests/CilTools.BytecodeAnalysis.Tests.NetCore/SyntaxTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* CIL Tools
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CilTools.Tests.Common;

namespace CilTools.BytecodeAnalysis.Tests.NetCore
{
[TestClass]
public class SyntaxTests
{
[TestMethod]
public void Test_ToSyntaxTree()
{
MethodInfo mi = typeof(SampleMethods).GetMethod("PrintHelloWorld");
SyntaxTestsCore.Test_ToSyntaxTree(mi);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<Compile Include="CilGraphTests_Text.cs" />
<Compile Include="ReflectionTests.cs" />
<Compile Include="CilGraphNodeTests.cs" />
<Compile Include="SyntaxTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\CilTools.BytecodeAnalysis\CilTools.BytecodeAnalysis.csproj">
Expand Down
21 changes: 21 additions & 0 deletions tests/CilTools.BytecodeAnalysis.Tests/SyntaxTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* CIL Tools
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CilTools.Tests.Common;

namespace CilTools.BytecodeAnalysis.Tests
{
[TestClass]
public class SyntaxTests
{
[TestMethod]
public void Test_ToSyntaxTree()
{
MethodInfo mi = typeof(SampleMethods).GetMethod("PrintHelloWorld");
SyntaxTestsCore.Test_ToSyntaxTree(mi);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<Compile Include="CilReaderTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionTests.cs" />
<Compile Include="SyntaxTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
1 change: 1 addition & 0 deletions tests/CilTools.Metadata.Tests/ReflectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public void Test_TypeAttributes()

//syntax
IEnumerable<SyntaxNode> syntax = SyntaxNode.GetTypeDefSyntax(t);
AssertThat.IsSyntaxTreeCorrect(syntax);

StringBuilder sb = new StringBuilder(1000);
foreach (SyntaxNode node in syntax) sb.Append(node.ToString());
Expand Down
28 changes: 28 additions & 0 deletions tests/CilTools.Metadata.Tests/SyntaxTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* CIL Tools
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CilTools.Tests.Common;

namespace CilTools.Metadata.Tests
{
[TestClass]
public class SyntaxTests
{
[TestMethod]
public void Test_ToSyntaxTree()
{
AssemblyReader reader = new AssemblyReader();

using (reader)
{
Assembly ass = reader.LoadFrom(typeof(SampleMethods).Assembly.Location);
Type t = ass.GetType("CilTools.Tests.Common.SampleMethods");
MethodBase mi = t.GetMember("PrintHelloWorld")[0] as MethodBase;
SyntaxTestsCore.Test_ToSyntaxTree(mi);
}
}
}
}
35 changes: 34 additions & 1 deletion tests/CilTools.Tests.Common/AssertThat.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* CIL Tools
* Copyright (c) 2020, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Text;
using CilTools.BytecodeAnalysis;
using CilTools.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CilTools.Tests.Common
Expand Down Expand Up @@ -66,6 +67,38 @@ public static void IsMatch(string s, MatchElement[] match, string message="")

Assert.IsTrue(Regex.IsMatch(s, pattern), message);
}

static void IsCorrectRecusive(SyntaxNode node,SyntaxNode parent, int c)
{
if (c > 100000)
{
Assert.Fail("Recursion is too deep in AssertThat.IsCorrectRecusive");
}

Assert.IsNotNull(node.Parent, "Parent node should not be null");
Assert.AreSame(parent, node.Parent);

foreach (SyntaxNode child in node.EnumerateChildNodes())
{
IsCorrectRecusive(child, node, c+1);
}
}

public static void IsSyntaxTreeCorrect(SyntaxNode root)
{
foreach (SyntaxNode child in root.EnumerateChildNodes())
{
IsCorrectRecusive(child,root, 0);
}
}

public static void IsSyntaxTreeCorrect(IEnumerable<SyntaxNode> nodes)
{
foreach (SyntaxNode node in nodes)
{
IsSyntaxTreeCorrect(node);
}
}
}

public abstract class MatchElement
Expand Down
2 changes: 2 additions & 0 deletions tests/CilTools.Tests.Common/CilGraphTestsCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public static void Test_CilGraph_Pointer(MethodBase mi)

//syntax
MethodDefSyntax mds = graph.ToSyntaxTree();
AssertThat.IsSyntaxTreeCorrect(mds);
SyntaxNode[] chilren = mds.Body.GetChildNodes();

AssertThat.HasOnlyOneMatch(
Expand Down Expand Up @@ -362,6 +363,7 @@ ldarga.s x

//syntax
MethodDefSyntax mds = graph.ToSyntaxTree();
AssertThat.IsSyntaxTreeCorrect(mds);
SyntaxNode[] chilren = mds.Body.GetChildNodes();

AssertThat.HasOnlyOneMatch(
Expand Down
45 changes: 45 additions & 0 deletions tests/CilTools.Tests.Common/SyntaxTestsCore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* CIL Tools
* Copyright (c) 2021, MSDN.WhiteKnight (https://github.com/MSDN-WhiteKnight)
* License: BSD 2.0 */
using System;
using System.Reflection;
using CilTools.BytecodeAnalysis;
using CilTools.Reflection;
using CilTools.Syntax;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CilTools.Tests.Common
{
public static class SyntaxTestsCore
{
public static void Test_ToSyntaxTree(MethodBase mi)
{
CilGraph graph = CilGraph.Create(mi);
MethodDefSyntax mds = graph.ToSyntaxTree();
AssertThat.IsSyntaxTreeCorrect(mds);
Assert.AreEqual("method", mds.Signature.Name);

AssertThat.HasOnlyOneMatch(
mds.Signature.EnumerateChildNodes(),
(x) => { return x is KeywordSyntax && (x as KeywordSyntax).Content == "public"; },
"Method signature should contain 'public' keyword"
);

AssertThat.HasOnlyOneMatch(
mds.Signature.EnumerateChildNodes(),
(x) => {
return x is IdentifierSyntax && (x as IdentifierSyntax).Content == "PrintHelloWorld";
},
"Method signature should contain mathod name identifier"
);

AssertThat.HasOnlyOneMatch(
mds.Body.Content,
(x) => {
return x is InstructionSyntax && (x as InstructionSyntax).Operation == "ldstr";
},
"Method body should contain 'ldstr' instruction"
);
}
}
}

0 comments on commit 92574d5

Please sign in to comment.