Skip to content

Commit 30e1925

Browse files
authored
Merge pull request github#14325 from michaelnebel/csharp/stubgentests
C#: Add a couple of stub generator unit tests
2 parents dc2acf5 + 53c947d commit 30e1925

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.CodeAnalysis;
2+
3+
namespace Semmle.Extraction.CSharp.StubGenerator;
4+
5+
internal interface IRelevantSymbol
6+
{
7+
bool IsRelevantNamedType(INamedTypeSymbol symbol);
8+
bool IsRelevantNamespace(INamespaceSymbol symbol);
9+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Semmle.Extraction.CSharp.StubGenerator")]
9+
[assembly: AssemblyDescription("Stub generator for C#")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Semmle.Extraction.CSharp.StubGenerator")]
13+
[assembly: AssemblyCopyright("Copyright © 2023")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("6d55ca39-615a-4c1b-a648-a4010995e1ea")]
24+
25+
// Expose internals for testing purposes.
26+
[assembly: InternalsVisibleTo("Semmle.Extraction.Tests")]
27+
28+
// Version information for an assembly consists of the following four values:
29+
//
30+
// Major Version
31+
// Minor Version
32+
// Build Number
33+
// Revision
34+
//
35+
// You can specify all the values or you can default the Build and Revision Numbers
36+
// by using the '*' as shown below:
37+
// [assembly: AssemblyVersion("1.0.*")]
38+
[assembly: AssemblyVersion("1.0.0.0")]
39+
[assembly: AssemblyFileVersion("1.0.0.0")]

csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/RelevantSymbol.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
using System.Linq;
2-
32
using Microsoft.CodeAnalysis;
4-
using Microsoft.CodeAnalysis.CSharp;
5-
63
using Semmle.Util;
74

85
namespace Semmle.Extraction.CSharp.StubGenerator;
96

10-
internal class RelevantSymbol
7+
internal class RelevantSymbol : IRelevantSymbol
118
{
129
private readonly IAssemblySymbol assembly;
1310
private readonly MemoizedFunc<INamedTypeSymbol, bool> isRelevantNamedType;

csharp/extractor/Semmle.Extraction.CSharp.StubGenerator/StubVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace Semmle.Extraction.CSharp.StubGenerator;
1212
internal sealed class StubVisitor : SymbolVisitor
1313
{
1414
private readonly TextWriter stubWriter;
15-
private readonly RelevantSymbol relevantSymbol;
15+
private readonly IRelevantSymbol relevantSymbol;
1616

17-
public StubVisitor(TextWriter stubWriter, RelevantSymbol relevantSymbol)
17+
public StubVisitor(TextWriter stubWriter, IRelevantSymbol relevantSymbol)
1818
{
1919
this.stubWriter = stubWriter;
2020
this.relevantSymbol = relevantSymbol;

csharp/extractor/Semmle.Extraction.Tests/Semmle.Extraction.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
1717
</ItemGroup>
1818
<ItemGroup>
19+
<ProjectReference Include="..\Semmle.Extraction.CSharp.StubGenerator\Semmle.Extraction.CSharp.StubGenerator.csproj" />
1920
<ProjectReference Include="..\Semmle.Extraction.CSharp.Standalone\Semmle.Extraction.CSharp.Standalone.csproj" />
2021
<ProjectReference Include="..\Semmle.Extraction.CSharp\Semmle.Extraction.CSharp.csproj" />
2122
<ProjectReference Include="..\Semmle.Extraction\Semmle.Extraction.csproj" />
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using Xunit;
2+
using System.IO;
3+
using System.Text;
4+
using Microsoft.CodeAnalysis;
5+
using Microsoft.CodeAnalysis.CSharp;
6+
using Semmle.Extraction.CSharp.StubGenerator;
7+
8+
namespace Semmle.Extraction.Tests;
9+
/// <summary>
10+
/// Tests for the stub generator.
11+
///
12+
/// These tests can be used to more easily step debug the stub generator SymbolVisitor.
13+
/// </summary>
14+
public class StubGeneratorTests
15+
{
16+
[Fact]
17+
public void StubGeneratorFieldTest()
18+
{
19+
// Setup
20+
const string source = @"
21+
public class MyTest {
22+
public static readonly int MyField1;
23+
public const string MyField2 = ""hello"";
24+
}
25+
";
26+
27+
// Execute
28+
var stub = GenerateStub(source);
29+
30+
// Verify
31+
const string expected = @"public class MyTest {
32+
public static readonly int MyField1;
33+
public const string MyField2 = default;
34+
}
35+
";
36+
Assert.Equal(expected, stub);
37+
}
38+
39+
[Fact]
40+
public void StubGeneratorMethodTest()
41+
{
42+
// Setup
43+
const string source = @"
44+
public class MyTest {
45+
public int M1(string arg1) { return 0;}
46+
}";
47+
48+
// Execute
49+
var stub = GenerateStub(source);
50+
51+
// Verify
52+
const string expected = @"public class MyTest {
53+
public int M1(string arg1) => throw null;
54+
}
55+
";
56+
Assert.Equal(expected, stub);
57+
}
58+
59+
private static string GenerateStub(string source)
60+
{
61+
var st = CSharpSyntaxTree.ParseText(source);
62+
var compilation = CSharpCompilation.Create(null, new[] { st });
63+
var sb = new StringBuilder();
64+
var visitor = new StubVisitor(new StringWriter(sb), new RelevantSymbolStub());
65+
compilation.GlobalNamespace.Accept(visitor);
66+
return sb.ToString();
67+
}
68+
69+
private class RelevantSymbolStub : IRelevantSymbol
70+
{
71+
public bool IsRelevantNamedType(INamedTypeSymbol symbol) => true;
72+
public bool IsRelevantNamespace(INamespaceSymbol symbol) => true;
73+
}
74+
}

0 commit comments

Comments
 (0)