Skip to content

Commit

Permalink
Adjust tests accordingly.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossanlop committed Jan 30, 2025
1 parent a41dee8 commit f6d2342
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@

<ItemGroup>
<ProjectReference Include="$(RepoRoot)src\Compatibility\ApiCompat\Microsoft.DotNet.ApiCompatibility\Microsoft.DotNet.ApiCompatibility.csproj" />
<ProjectReference Include="$(RepoRoot)src\Compatibility\GenAPI\Microsoft.DotNet.GenAPI\Microsoft.DotNet.GenAPI.csproj" />
<ProjectReference Include="$(RepoRoot)src\Compatibility\Microsoft.DotNet.ApiSymbolExtensions\Microsoft.DotNet.ApiSymbolExtensions.csproj" />
<ProjectReference Include="..\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" />
</ItemGroup>

</Project>
11 changes: 6 additions & 5 deletions test/Microsoft.DotNet.ApiSymbolExtensions.Tests/SymbolFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ public static string EmitAssemblyFromSyntax(string syntax,
}

public static Stream EmitAssemblyStreamFromSyntax(string syntax,
Dictionary<string, ReportDiagnostic> diagnosticOptions = null,
bool enableNullable = false,
byte[] publicKey = null,
[CallerMemberName] string assemblyName = "",
bool allowUnsafe = false)
{
CSharpCompilation compilation = CreateCSharpCompilationFromSyntax(syntax, assemblyName, enableNullable, publicKey, allowUnsafe);
CSharpCompilation compilation = CreateCSharpCompilationFromSyntax(syntax, assemblyName, enableNullable, publicKey, allowUnsafe, diagnosticOptions);

Assert.Empty(compilation.GetDiagnostics());

Expand Down Expand Up @@ -76,9 +77,9 @@ public static IAssemblySymbol GetAssemblyFromSyntaxWithReferences(string syntax,
return compilation.Assembly;
}

private static CSharpCompilation CreateCSharpCompilationFromSyntax(string syntax, string name, bool enableNullable, byte[] publicKey, bool allowUnsafe)
private static CSharpCompilation CreateCSharpCompilationFromSyntax(string syntax, string name, bool enableNullable, byte[] publicKey, bool allowUnsafe, Dictionary<string, ReportDiagnostic> diagnosticOptions = null)
{
CSharpCompilation compilation = CreateCSharpCompilation(name, enableNullable, publicKey, allowUnsafe);
CSharpCompilation compilation = CreateCSharpCompilation(name, enableNullable, publicKey, allowUnsafe, diagnosticOptions);
return compilation.AddSyntaxTrees(GetSyntaxTree(syntax));
}

Expand All @@ -94,15 +95,15 @@ private static SyntaxTree GetSyntaxTree(string syntax)
return CSharpSyntaxTree.ParseText(syntax, ParseOptions);
}

private static CSharpCompilation CreateCSharpCompilation(string name, bool enableNullable, byte[] publicKey, bool allowUnsafe)
private static CSharpCompilation CreateCSharpCompilation(string name, bool enableNullable, byte[] publicKey, bool allowUnsafe, Dictionary<string, ReportDiagnostic> diagnosticOptions = null)
{
bool publicSign = publicKey != null ? true : false;
var compilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary,
publicSign: publicSign,
cryptoPublicKey: publicSign ? publicKey.ToImmutableArray() : default,
nullableContextOptions: enableNullable ? NullableContextOptions.Enable : NullableContextOptions.Disable,
allowUnsafe: allowUnsafe,
specificDiagnosticOptions: DiagnosticOptions);
specificDiagnosticOptions: diagnosticOptions ?? DiagnosticOptions);

return CSharpCompilation.Create(name, options: compilationOptions, references: DefaultReferences);
}
Expand Down
6 changes: 3 additions & 3 deletions test/Microsoft.DotNet.GenAPI.Tests/CSharpFileBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
using Microsoft.DotNet.ApiSymbolExtensions;
using Microsoft.DotNet.ApiSymbolExtensions.Filtering;
using Microsoft.DotNet.ApiSymbolExtensions.Logging;
using Microsoft.DotNet.ApiSymbolExtensions.Tests;
using Moq;

namespace Microsoft.DotNet.GenAPI.Tests
{
public class CSharpFileBuilderTests
{
private readonly ILog _logger = new ConsoleLog(MessageImportance.High);

class AllowAllFilter : ISymbolFilter
{
public bool Include(ISymbol symbol) => true;
Expand All @@ -40,7 +40,7 @@ private void RunTest(string original,
Mock<ILog> log = new();

(IAssemblySymbolLoader loader, Dictionary<string, IAssemblySymbol> assemblySymbols) = TestAssemblyLoaderFactory
.CreateFromTexts(log.Object, assemblyTexts: [(assemblyName, original)], respectInternals: includeInternalSymbols, allowUnsafe);
.CreateFromTexts(log.Object, assemblyTexts: [(assemblyName, original)], respectInternals: includeInternalSymbols, allowUnsafe: allowUnsafe);

ISymbolFilter symbolFilter = SymbolFilterFactory.GetFilterFromList([], null, includeInternalSymbols, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols);
ISymbolFilter attributeDataSymbolFilter = SymbolFilterFactory.GetFilterFromList(excludedAttributeList, null, includeInternalSymbols, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols);
Expand Down
14 changes: 10 additions & 4 deletions test/Microsoft.DotNet.GenAPI.Tests/TestAssemblyLoaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,27 @@ namespace Microsoft.DotNet.GenAPI.Tests;

public class TestAssemblyLoaderFactory
{
public static (IAssemblySymbolLoader, Dictionary<string, IAssemblySymbol>) CreateFromTexts(ILog log, (string, string)[] assemblyTexts, bool respectInternals = false, bool allowUnsafe = false)
public static (IAssemblySymbolLoader, Dictionary<string, IAssemblySymbol>) CreateFromTexts(ILog log,
(string, string)[] assemblyTexts,
Dictionary<string, ReportDiagnostic>? diagnosticOptions = null,
bool respectInternals = false,
bool allowUnsafe = false)
{
AssemblySymbolLoader loader;
if (assemblyTexts.Length == 0)
{
return (new AssemblySymbolLoader(log, resolveAssemblyReferences: true, includeInternalSymbols: respectInternals), new Dictionary<string, IAssemblySymbol>());
loader = new AssemblySymbolLoader(log, diagnosticOptions, resolveAssemblyReferences: true, includeInternalSymbols: respectInternals);
return (loader, new Dictionary<string, IAssemblySymbol>());
}

AssemblySymbolLoader loader = new(log, resolveAssemblyReferences: true, includeInternalSymbols: respectInternals);
loader = new AssemblySymbolLoader(log, diagnosticOptions, resolveAssemblyReferences: true, includeInternalSymbols: respectInternals);
loader.AddReferenceSearchPaths(typeof(object).Assembly!.Location!);
loader.AddReferenceSearchPaths(typeof(DynamicAttribute).Assembly!.Location!);

Dictionary<string, IAssemblySymbol> assemblySymbols = new();
foreach ((string assemblyName, string assemblyText) in assemblyTexts)
{
using Stream assemblyStream = SymbolFactory.EmitAssemblyStreamFromSyntax(assemblyText, enableNullable: true, allowUnsafe: allowUnsafe, assemblyName: assemblyName);
using Stream assemblyStream = SymbolFactory.EmitAssemblyStreamFromSyntax(assemblyText, diagnosticOptions, enableNullable: true, allowUnsafe: allowUnsafe, assemblyName: assemblyName);
if (loader.LoadAssembly(assemblyName, assemblyStream) is IAssemblySymbol assemblySymbol)
{
assemblySymbols.Add(assemblyName, assemblySymbol);
Expand Down

0 comments on commit f6d2342

Please sign in to comment.