Add stubbing tests

This commit is contained in:
Tamas Vajk
2021-04-16 12:58:04 +02:00
parent 7e7a52de3c
commit ba238578d1
9 changed files with 152 additions and 5 deletions

View File

@@ -0,0 +1,13 @@
/**
* Tool to generate C# stubs from a qltest snapshot.
*/
import csharp
import Stubs
/** All public declarations from source. */
class AllDeclarations extends GeneratedDeclaration {
AllDeclarations() { not this.fromLibrary() }
}
select generatedCode()

View File

@@ -34,8 +34,7 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
this instanceof DelegateType
) and
not this instanceof ConstructedType and
not this.getALocation() instanceof ExcludedAssembly and
this.fromLibrary()
not this.getALocation() instanceof ExcludedAssembly
}
/**
@@ -103,7 +102,8 @@ abstract private class GeneratedType extends ValueOrRefType, GeneratedElement {
private ValueOrRefType getAnInterestingBaseType() {
result = this.getABaseType() and
not result instanceof ObjectType and
not result.getQualifiedName() = "System.ValueType"
not result.getQualifiedName() = "System.ValueType" and
(not result instanceof Interface or result.(Interface).isEffectivelyPublic())
}
private string stubBaseTypesString() {

View File

@@ -0,0 +1 @@
| // This file contains auto-generated code.\n\nnamespace Test\n{\n// Generated from `Test.Class1` in `Test.cs:3:18:3:23`\npublic class Class1\n{\n public Class1() => throw null;\n// Generated from `Test.Class1.Class11` in `Test.cs:29:22:29:28`\npublic class Class11 : Test.Class1.Interface2, Test.Class1.Interface1\n{\n public Class11(int i) => throw null;\n int Test.Class1.Interface2.this[int i] { get => throw null; }\n public void Method1() => throw null;\n void Test.Class1.Interface2.Method2() => throw null;\n}\n\n\n// Generated from `Test.Class1.Class12` in `Test.cs:46:22:46:28`\npublic class Class12 : Test.Class1.Class11\n{\n public Class12(int i, float j) : base(default(int)) => throw null;\n}\n\n\n// Generated from `Test.Class1.Class13` in `Test.cs:58:31:58:37`\nabstract public class Class13\n{\n protected Class13() => throw null;\n protected internal virtual void M() => throw null;\n public virtual void M1<T>() where T: Test.Class1.Class13 => throw null;\n public abstract void M2();\n}\n\n\n// Generated from `Test.Class1.Class14` in `Test.cs:65:31:65:37`\nabstract public class Class14 : Test.Class1.Class13\n{\n protected Class14() => throw null;\n protected internal override void M() => throw null;\n public override void M1<T>() => throw null;\n public abstract override void M2();\n}\n\n\n// Generated from `Test.Class1.Delegate1<>` in `Test.cs:42:30:42:41`\npublic delegate void Delegate1<T>(T i, int j);\n\n\n public event Test.Class1.Delegate1<int> Event1;\n// Generated from `Test.Class1.GenericType<>` in `Test.cs:51:22:51:35`\npublic class GenericType<T>\n{\n public GenericType() => throw null;\n// Generated from `Test.Class1.GenericType<>.X` in `Test.cs:53:26:53:26`\npublic class X\n{\n public X() => throw null;\n}\n\n\n}\n\n\n// Generated from `Test.Class1.Interface1` in `Test.cs:13:26:13:35`\npublic interface Interface1\n{\n void Method1();\n}\n\n\n// Generated from `Test.Class1.Interface2` in `Test.cs:18:38:18:47`\nprotected internal interface Interface2\n{\n int this[int i] { get; }\n void Method2();\n}\n\n\n public Test.Class1.GenericType<int>.X Prop { get => throw null; }\n// Generated from `Test.Class1.Struct1` in `Test.cs:5:23:5:29`\npublic struct Struct1\n{\n public void Method(Test.Class1.Struct1 s = default(Test.Class1.Struct1)) => throw null;\n // Stub generator skipped constructor \n public int i;\n public static int j = default;\n}\n\n\n}\n\n// Generated from `Test.Class3` in `Test.cs:79:18:79:23`\npublic class Class3\n{\n public Class3() => throw null;\n public object Item { get => throw null; set => throw null; }\n [System.Runtime.CompilerServices.IndexerName("MyItem")]\n public object this[string index] { get => throw null; set => throw null; }\n}\n\n// Generated from `Test.Class4` in `Test.cs:86:18:86:23`\npublic class Class4\n{\n public Class4() => throw null;\n unsafe public void M(int* p) => throw null;\n}\n\n// Generated from `Test.Class5` in `Test.cs:97:18:97:23`\npublic class Class5 : Test.IInterface1\n{\n public Class5() => throw null;\n public void M2() => throw null;\n}\n\n// Generated from `Test.Class6<>` in `Test.cs:102:18:102:26`\npublic class Class6<T> where T: class, Test.IInterface1\n{\n public Class6(int i) => throw null;\n public virtual void M1<T>() where T: class, Test.IInterface1, new() => throw null;\n}\n\n// Generated from `Test.Class7` in `Test.cs:109:18:109:23`\npublic class Class7 : Test.Class6<Test.Class5>\n{\n public Class7(int i) : base(default(int)) => throw null;\n public override void M1<T>() where T: class => throw null;\n}\n\n// Generated from `Test.Class8` in `Test.cs:116:18:116:23`\npublic class Class8\n{\n public Class8() => throw null;\n public static int @this = default;\n}\n\n// Generated from `Test.Class9` in `Test.cs:121:18:121:23`\npublic class Class9\n{\n private Class9(int i) => throw null;\n// Generated from `Test.Class9.Nested` in `Test.cs:125:22:125:27`\npublic class Nested : Test.Class9\n{\n internal Nested(int i) : base(default(int)) => throw null;\n}\n\n\n public Test.Class9.Nested NestedInstance { get => throw null; }\n}\n\n// Generated from `Test.IInterface1` in `Test.cs:91:22:91:32`\npublic interface IInterface1\n{\n void M1() => throw null;\n void M2();\n}\n\n}\n |

View File

@@ -0,0 +1 @@
Stubs/AllStubsFromSource.ql

View File

@@ -0,0 +1,132 @@
namespace Test
{
public class Class1
{
public struct Struct1
{
public int i;
public const int j = 42;
public void Method(Struct1 s = new Struct1()) => throw null;
}
public interface Interface1
{
void Method1();
}
internal protected interface Interface2
{
void Method2();
int this[int i] { get; }
}
private protected interface Interface3
{
void Method3();
}
public class Class11 : Interface1, Interface2, Interface3
{
public Class11(int i) => throw null;
public void Method1() => throw null;
void Interface2.Method2() => throw null;
int Interface2.this[int i] => throw null;
void Interface3.Method3() => throw null;
}
public delegate void Delegate1<T>(T i, int j);
public event Delegate1<int> Event1 { add { } remove { } }
public class Class12 : Class11
{
public Class12(int i, float j) : base(1) => throw null;
}
public class GenericType<T>
{
public class X { }
}
public GenericType<int>.X Prop { get; }
public abstract class Class13
{
protected internal virtual void M() => throw null;
public virtual void M1<T>() where T : Class13 => throw null;
public abstract void M2();
}
public abstract class Class14 : Class13
{
protected internal override void M() => throw null;
public override void M1<T>() => throw null;
public abstract override void M2();
}
}
internal class Class2
{
public void M() => throw null;
}
public class Class3
{
public object Item { get; set; }
[System.Runtime.CompilerServices.IndexerName("MyItem")]
public object this[string index] { get { return null; } set { } }
}
public class Class4
{
unsafe public void M(int* p) => throw null;
}
public interface IInterface1
{
void M1() => throw null;
void M2();
}
public class Class5 : IInterface1
{
public void M2() => throw null;
}
public class Class6<T> where T : class, IInterface1
{
public Class6(int i) => throw null;
public virtual void M1<T>() where T : class, IInterface1, new() => throw null;
}
public class Class7 : Class6<Class5>
{
public Class7(int i) : base(i) => throw null;
public override void M1<T>() where T : class => throw null;
}
public class Class8
{
public const int @this = 10;
}
public class Class9
{
private Class9(int i) => throw null;
public class Nested : Class9
{
internal Nested(int i) : base(i) => throw null;
}
public Class9.Nested NestedInstance { get; } = new Class9.Nested(1);
}
}

File diff suppressed because one or more lines are too long

View File

@@ -1,4 +1,4 @@
// semmle-extractor-options: /r:System.Text.RegularExpressions.dll /r:System.Collections.Specialized.dll /r:System.Net.dll /r:System.Web.dll /r:System.Net.HttpListener.dll /r:System.Collections.Specialized.dll /r:System.Private.Uri.dll /r:System.Runtime.Extensions.dll /r:System.Linq.Parallel.dll /r:System.Collections.Concurrent.dll /r:System.Linq.Expressions.dll /r:System.Collections.dll /r:System.Linq.Queryable.dll /r:System.Linq.dll /r:System.Collections.NonGeneric.dll /r:System.ObjectModel.dll /r:System.ComponentModel.TypeConverter.dll /r:System.IO.Compression.dll /r:System.IO.Pipes.dll /r:System.Net.Primitives.dll /r:System.Net.Security.dll /r:System.Security.Cryptography.Primitives.dll /r:System.Text.RegularExpressions.dll ${testdir}/../../resources/stubs/System.Web.cs /r:System.Runtime.Serialization.Primitives.dll
// semmle-extractor-options: /r:System.Text.RegularExpressions.dll /r:System.Collections.Specialized.dll /r:System.Net.dll /r:System.Web.dll /r:System.Net.HttpListener.dll /r:System.Collections.Specialized.dll /r:System.Private.Uri.dll /r:System.Runtime.Extensions.dll /r:System.Linq.Parallel.dll /r:System.Collections.Concurrent.dll /r:System.Linq.Expressions.dll /r:System.Collections.dll /r:System.Linq.Queryable.dll /r:System.Linq.dll /r:System.Collections.NonGeneric.dll /r:System.ObjectModel.dll /r:System.ComponentModel.TypeConverter.dll /r:System.IO.Compression.dll /r:System.IO.Pipes.dll /r:System.Net.Primitives.dll /r:System.Net.Security.dll /r:System.Security.Cryptography.Primitives.dll /r:System.Text.RegularExpressions.dll ${testdir}/../../../resources/stubs/System.Web.cs /r:System.Runtime.Serialization.Primitives.dll
using System;
using System.IO;

File diff suppressed because one or more lines are too long