forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStubGenerator.cs
More file actions
94 lines (83 loc) · 2.31 KB
/
StubGenerator.cs
File metadata and controls
94 lines (83 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using Xunit;
using System.IO;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Semmle.Extraction.CSharp.StubGenerator;
namespace Semmle.Extraction.Tests;
/// <summary>
/// Tests for the stub generator.
///
/// These tests can be used to more easily step debug the stub generator SymbolVisitor.
/// </summary>
public class StubGeneratorTests
{
[Fact]
public void StubGeneratorFieldTest()
{
// Setup
const string source = @"
public class MyTest {
public static readonly int MyField1;
public const string MyField2 = ""hello"";
}
";
// Execute
var stub = GenerateStub(source);
// Verify
const string expected = @"public class MyTest {
public static readonly int MyField1;
public const string MyField2 = default;
}
";
Assert.Equal(expected, stub);
}
[Fact]
public void StubGeneratorMethodTest()
{
// Setup
const string source = @"
public class MyTest {
public int M1(string arg1) { return 0; }
}";
// Execute
var stub = GenerateStub(source);
// Verify
const string expected = @"public class MyTest {
public int M1(string arg1) => throw null;
}
";
Assert.Equal(expected, stub);
}
[Fact]
public void StubGeneratorRefReadonlyParameterTest()
{
// Setup
const string source = @"
public class MyTest {
public int M1(ref readonly Guid guid) { return 0; }
}";
// Execute
var stub = GenerateStub(source);
// Verify
const string expected = @"public class MyTest {
public int M1(ref readonly Guid guid) => throw null;
}
";
Assert.Equal(expected, stub);
}
private static string GenerateStub(string source)
{
var st = CSharpSyntaxTree.ParseText(source);
var compilation = CSharpCompilation.Create(null, new[] { st });
var sb = new StringBuilder();
var visitor = new StubVisitor(new StringWriter(sb) { NewLine = "\n" }, new RelevantSymbolStub());
compilation.GlobalNamespace.Accept(visitor);
return sb.ToString();
}
private class RelevantSymbolStub : IRelevantSymbol
{
public bool IsRelevantNamedType(INamedTypeSymbol symbol) => true;
public bool IsRelevantNamespace(INamespaceSymbol symbol) => true;
}
}