Skip to content

Commit

Permalink
Small housekeeping changes for authoring projects (#1236)
Browse files Browse the repository at this point in the history
  • Loading branch information
j0shuams authored Aug 5, 2022
1 parent d037354 commit 73e1759
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 91 deletions.
23 changes: 0 additions & 23 deletions src/Authoring/WinRT.SourceGenerator/DiagnosticHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,6 @@

namespace Generator
{
// Helper Class, makes for clean collection of types and namespaces, needed for checking
internal class TypeCollector
{
private HashSet<INamedTypeSymbol> types;
private HashSet<INamedTypeSymbol> structs;
private HashSet<INamespaceSymbol> namespaces;

public TypeCollector()
{
types = new HashSet<INamedTypeSymbol>();
structs = new HashSet<INamedTypeSymbol>();
namespaces = new HashSet<INamespaceSymbol>();
}

public void AddType(INamedTypeSymbol newType) { types.Add(newType); }
public void AddStruct(INamedTypeSymbol newType) { structs.Add(newType); }
public void AddNamespace(INamespaceSymbol newType) { namespaces.Add(newType); }

public HashSet<INamedTypeSymbol> GetTypes() { return types; }
public HashSet<INamedTypeSymbol> GetStructs() { return structs; }
public HashSet<INamespaceSymbol> GetNamespaces() { return namespaces; }
}

public partial class WinRTComponentScanner
{
private void Flag() { _flag |= true; }
Expand Down
6 changes: 3 additions & 3 deletions src/Tests/AuthoringTest/AuthoringTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<TargetFramework>net5.0</TargetFramework>
<Platforms>x64;x86</Platforms>
<CsWinRTComponent>true</CsWinRTComponent>
<!-- Enable to diagnose generation issues -->
<CsWinRTEnableLogging>true</CsWinRTEnableLogging>
<!--<CsWinRTKeepGeneratedSources>true</CsWinRTKeepGeneratedSources>-->
<!-- CsWinRTEnableLogging helps to diagnose generation issues -->
<!--<CsWinRTEnableLogging>true</CsWinRTEnableLogging>-->
<!--<CsWinRTKeepGeneratedSources>true</CsWinRTKeepGeneratedSources>-->
</PropertyGroup>

<ItemGroup>
Expand Down
129 changes: 64 additions & 65 deletions src/Tests/DiagnosticTests/UnitTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@
using System.Collections.Immutable;
using System.Collections.Generic;
using System.Linq;
using System;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Diagnostics.CodeAnalysis;

using System;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace DiagnosticTests
{
class ConfigOptions : AnalyzerConfigOptions
{
public Dictionary<string, string> Values { get; set; } = new();
public override bool TryGetValue(string key, [NotNullWhen(true)] out string value)
{
return Values.TryGetValue(key, out value);
}
}

class ConfigProvider : AnalyzerConfigOptionsProvider
{
public override AnalyzerConfigOptions GlobalOptions { get; } = new ConfigOptions();

public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
{
return GlobalOptions;
}

public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
{
return GlobalOptions;
}
{
class ConfigOptions : AnalyzerConfigOptions
{
public Dictionary<string, string> Values { get; set; } = new();
public override bool TryGetValue(string key, [NotNullWhen(true)] out string value)
{
return Values.TryGetValue(key, out value);
}
}

class ConfigProvider : AnalyzerConfigOptionsProvider
{
public override AnalyzerConfigOptions GlobalOptions { get; } = new ConfigOptions();

public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
{
return GlobalOptions;
}

public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
{
return GlobalOptions;
}
}

[TestFixture]
public sealed partial class UnitTesting
{

private static AnalyzerConfigOptionsProvider Options
{
get
{
var o = new ConfigProvider();
var config = o.GlobalOptions as ConfigOptions;
config.Values["build_property.AssemblyName"] = "DiagnosticTests";
config.Values["build_property.AssemblyVersion"] = "1.0.0.0";
config.Values["build_property.CsWinRTComponent"] = "true";
return o;
}
{

private static AnalyzerConfigOptionsProvider Options
{
get
{
var o = new ConfigProvider();
var config = o.GlobalOptions as ConfigOptions;
config.Values["build_property.AssemblyName"] = "DiagnosticTests";
config.Values["build_property.AssemblyVersion"] = "1.0.0.0";
config.Values["build_property.CsWinRTComponent"] = "true";
return o;
}
}

/// <summary>
Expand All @@ -57,27 +57,26 @@ private static AnalyzerConfigOptionsProvider Options
/// <param name="source"></param>
[Test, TestCaseSource(nameof(ValidCases))]
public void CheckNoDiagnostic(string source)
{
Assert.DoesNotThrow(() =>
{
Compilation compilation = CreateCompilation(source);
RunGenerators(compilation, out var diagnosticsFound, out var result, Options, new Generator.SourceGenerator());

var WinRTDiagnostics = diagnosticsFound.Where(diag =>
diag.Id.StartsWith("CsWinRT", StringComparison.Ordinal)
//|| diag.Id == "CS8785"
);
// warning CS8785: Generator 'SourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result
if (WinRTDiagnostics.Any())
{
var foundDiagnostics = string.Join("\n", WinRTDiagnostics.Select(x => x.GetMessage()));
Exception inner = null;
if (!result.Results.IsEmpty)
{
inner = result.Results[0].Exception;
}

throw new AssertionException("Expected no diagnostics. But found:\n" + foundDiagnostics, inner);
{
Assert.DoesNotThrow(() =>
{
Compilation compilation = CreateCompilation(source);
RunGenerators(compilation, out var diagnosticsFound, out var result, Options, new Generator.SourceGenerator());

var WinRTDiagnostics = diagnosticsFound.Where(diag =>
diag.Id.StartsWith("CsWinRT", StringComparison.Ordinal)
);

if (WinRTDiagnostics.Any())
{
var foundDiagnostics = string.Join("\n", WinRTDiagnostics.Select(x => x.GetMessage()));
Exception inner = null;
if (!result.Results.IsEmpty)
{
inner = result.Results[0].Exception;
}

throw new AssertionException("Expected no diagnostics. But found:\n" + foundDiagnostics, inner);
}
});
}
Expand All @@ -98,9 +97,9 @@ public void CodeHasDiagnostic(string testCode, DiagnosticDescriptor rule)
{
var foundDiagnostics = string.Join("\n", diagDescsFound.Select(x => x.Description));
Exception inner = null;
if (!result.Results.IsEmpty)
{
inner = result.Results[0].Exception;
if (!result.Results.IsEmpty)
{
inner = result.Results[0].Exception;
}
throw new SuccessException("Didn't find the expected diagnostic, found:\n" + foundDiagnostics, inner);
}
Expand All @@ -109,7 +108,7 @@ public void CodeHasDiagnostic(string testCode, DiagnosticDescriptor rule)
throw new SuccessException("No diagnostics found.");
}
}
}
}

#region InvalidTests
private static IEnumerable<TestCaseData> InvalidCases
Expand Down

0 comments on commit 73e1759

Please sign in to comment.