Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support external libraries #11

Merged
merged 14 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,5 @@ paket-files/

# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
*.pyc
/PullThroughDoc/PullThroughDoc.Test/PullThroughDoc.Test.xml
8 changes: 4 additions & 4 deletions PullThroughDoc.sln
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28010.2036
# Visual Studio Version 16
VisualStudioVersion = 16.0.31112.23
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PullThroughDoc", "PullThroughDoc\PullThroughDoc\PullThroughDoc.csproj", "{5EFB85EF-6407-4F22-8884-B914E494F648}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PullThroughDoc", "PullThroughDoc\PullThroughDoc\PullThroughDoc.csproj", "{5EFB85EF-6407-4F22-8884-B914E494F648}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PullThroughDoc.Test", "PullThroughDoc\PullThroughDoc.Test\PullThroughDoc.Test.csproj", "{D26CB3C7-4BD7-43C7-B576-CC4F83C265DC}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PullThroughDoc.Test", "PullThroughDoc\PullThroughDoc.Test\PullThroughDoc.Test.csproj", "{D26CB3C7-4BD7-43C7-B576-CC4F83C265DC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PullThroughDoc.Vsix", "PullThroughDoc\PullThroughDoc.Vsix\PullThroughDoc.Vsix.csproj", "{8955632C-49A5-4847-AA0B-EB89738700CE}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PullThroughDoc.Test
{
Expand All @@ -9,11 +10,7 @@ namespace PullThroughDoc.Test
public class BaseClassDocumentationTests : PullThroughDocCodeFixVerifier
{

[TestInitialize]
public void Init()
{
CodeFixProvider = new PullThroughDocCodeFixProvider();
}
protected override CodeFixProvider CodeFixProvider => new PullThroughDocCodeFixProvider();


[TestMethod]
Expand Down
123 changes: 123 additions & 0 deletions PullThroughDoc/PullThroughDoc.Test/ExternalReferenceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PullThroughDoc.Test.Helpers;
using System.Collections.Generic;

namespace PullThroughDoc.Test
{
[TestClass]
public class ExternalReferenceTests : PullThroughDocCodeFixVerifier
{
private string _docXml;
protected override CodeFixProvider CodeFixProvider => new PullThroughDocCodeFixProvider();

public override List<MetadataReference> References
{
get
{
var refs = base.References;
refs[0] = MetadataReference.CreateFromFile(typeof(object).Assembly.Location, documentation: new FakeVisualStudioDocumentationProvider(_docXml));
refs.Add(MetadataReference.CreateFromFile(typeof(OverridableClass).Assembly.Location));
return refs;
}
}

[TestInitialize]
public void Initialize()
{
_docXml = "";
}

[TestMethod]
public void OverrideExternalMethod_PullsThrough()
{

_docXml = @"
<doc>
<summary>Searches things.</summary>
<param name=""value"">The thing to find</param>
<returns>The result</returns>
</doc>
";

var test = @"
using System.Collections;

namespace ConsoleApplication1
{
class ArrayListOverride : ArrayList
{
public override int BinarySearch(object value) => 1;
}
}";

ExpectPullThroughDiagnosticAt(test, "BinarySearch", 8, 23);

var fixtest = @"
using System.Collections;

namespace ConsoleApplication1
{
class ArrayListOverride : ArrayList
{
/// <summary>Searches things.</summary>
/// <param name=""value"">The thing to find</param>
/// <returns>The result</returns>
public override int BinarySearch(object value) => 1;
}
}";
VerifyCSharpFix(test, fixtest);
}

// This example is taken from JsonConverter in Newtonsoft.Json
// Also, still not perfect... some weird stuff with the "returns" and line breaks is happening, but who cares
[TestMethod]
public void MultiLineDocumentation_PullsThroughMultiLine()
{
_docXml = @"
<doc>
<summary>
Summary Part
</summary>
<param name=""objectType"">Type of the object.</param>
<returns>
<c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
</returns>
</doc>";



var test = @"
using System.Collections;

namespace ConsoleApplication1
{
class ArrayListOverride : ArrayList
{
public override int BinarySearch(object value) => 1;
}
}";

var fixtest = @"
using System.Collections;

namespace ConsoleApplication1
{
class ArrayListOverride : ArrayList
{
/// <summary>
/// Summary Part
/// </summary>
/// <param name=""objectType"">Type of the object.</param>
/// <returns><c>true</c> if this instance can convert the specified object type; otherwise, <c>false</c>.
/// </returns>
public override int BinarySearch(object value) => 1;
}
}";
VerifyCSharpFix(test, fixtest);
}

}

}
71 changes: 51 additions & 20 deletions PullThroughDoc/PullThroughDoc.Test/GeneralTests.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace PullThroughDoc.Test
{
[TestClass]
public class GeneralTests : PullThroughDocCodeFixVerifier
{
[TestInitialize]
public void Init()
{
CodeFixProvider = new PullThroughDocCodeFixProvider();
}
protected override CodeFixProvider CodeFixProvider => new PullThroughDocCodeFixProvider();

//No diagnostics expected to show up
[TestMethod]
Expand All @@ -20,21 +17,6 @@ public void Doesnt_Trigger_For_Nothing()
VerifyCSharpDiagnostic(test);
}

[TestMethod]
public void BaseObjectMethodOverride_NoAnalyzer()
{
var test = @"
namespace ConsoleApplication1
{
class TypeName
{
public override string ToString() {}
}
}";

VerifyCSharpDiagnostic(test);
}

[TestMethod]
public void Regions_Documentation_ExcludingRegion()
{
Expand Down Expand Up @@ -74,5 +56,54 @@ public override string DoThing() {}
VerifyCSharpFix(test, fixtest);
}

[TestMethod]
public void Parameters_DocumentationIncluded()
{
var test = @"
namespace ConsoleApplication1
{
class BaseClass
{
/// <summary>
/// Returns a thing
/// </summary>
/// <param name=""pullThroughInfo""></param>
/// <param name=""targetMember""></param>
/// <returns></returns>
public virtual string DoThing(string variable) { }
}
class TypeName : BaseClass
{
public override string DoThing(string variable) {}
}
}";


var fixtest = @"
namespace ConsoleApplication1
{
class BaseClass
{
/// <summary>
/// Returns a thing
/// </summary>
/// <param name=""pullThroughInfo""></param>
/// <param name=""targetMember""></param>
/// <returns></returns>
public virtual string DoThing(string variable) { }
}
class TypeName : BaseClass
{
/// <summary>
/// Returns a thing
/// </summary>
/// <param name=""pullThroughInfo""></param>
/// <param name=""targetMember""></param>
/// <returns></returns>
public override string DoThing(string variable) {}
}
}";
VerifyCSharpFix(test, fixtest);
}
}
}
10 changes: 5 additions & 5 deletions PullThroughDoc/PullThroughDoc.Test/Helpers/DiagnosticResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ public DiagnosticResultLocation(string path, int line, int column)
/// </summary>
public struct DiagnosticResult
{
private DiagnosticResultLocation[] locations;
private DiagnosticResultLocation[] _locations;

public DiagnosticResultLocation[] Locations
{
get
{
if (this.locations == null)
if (this._locations == null)
{
this.locations = new DiagnosticResultLocation[] { };
this._locations = Array.Empty<DiagnosticResultLocation>();
}
return this.locations;
return this._locations;
}

set
{
this.locations = value;
this._locations = value;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ namespace TestHelper
/// </summary>
public abstract partial class DiagnosticVerifier
{
private static readonly MetadataReference CorlibReference = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
private static readonly MetadataReference SystemCoreReference = MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location);
private static readonly MetadataReference CSharpSymbolsReference = MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location);
private static readonly MetadataReference CodeAnalysisReference = MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location);

internal static string DefaultFilePathPrefix = "Test";
internal static string CSharpDefaultFileExt = "cs";
internal static string VisualBasicDefaultExt = "vb";
internal static string TestProjectName = "TestProject";

public virtual List<MetadataReference> References
{
get
{
return new List<MetadataReference>()
{
MetadataReference.CreateFromFile(typeof(object).Assembly.Location), // Corlib
MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), // System.Core
MetadataReference.CreateFromFile(typeof(CSharpCompilation).Assembly.Location), //CSharpSymbolsReference
MetadataReference.CreateFromFile(typeof(Compilation).Assembly.Location) // CodeAnalysisReference
};
}
}

#region Get Diagnostics

/// <summary>
Expand All @@ -34,7 +43,7 @@ public abstract partial class DiagnosticVerifier
/// <param name="language">The language the source classes are in</param>
/// <param name="analyzer">The analyzer to be run on the sources</param>
/// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location</returns>
private static Diagnostic[] GetSortedDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer)
private Diagnostic[] GetSortedDiagnostics(string[] sources, string language, DiagnosticAnalyzer analyzer)
{
return GetSortedDiagnosticsFromDocuments(analyzer, GetDocuments(sources, language));
}
Expand All @@ -46,7 +55,7 @@ private static Diagnostic[] GetSortedDiagnostics(string[] sources, string langua
/// <param name="analyzer">The analyzer to run on the documents</param>
/// <param name="documents">The Documents that the analyzer will be run on</param>
/// <returns>An IEnumerable of Diagnostics that surfaced in the source code, sorted by Location</returns>
protected static Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents)
protected Diagnostic[] GetSortedDiagnosticsFromDocuments(DiagnosticAnalyzer analyzer, Document[] documents)
{
var projects = new HashSet<Project>();
foreach (var document in documents)
Expand Down Expand Up @@ -104,7 +113,7 @@ private static Diagnostic[] SortDiagnostics(IEnumerable<Diagnostic> diagnostics)
/// <param name="sources">Classes in the form of strings</param>
/// <param name="language">The language the source code is in</param>
/// <returns>A Tuple containing the Documents produced from the sources and their TextSpans if relevant</returns>
private static Document[] GetDocuments(string[] sources, string language)
private Document[] GetDocuments(string[] sources, string language)
{
if (language != LanguageNames.CSharp && language != LanguageNames.VisualBasic)
{
Expand All @@ -128,7 +137,7 @@ private static Document[] GetDocuments(string[] sources, string language)
/// <param name="source">Classes in the form of a string</param>
/// <param name="language">The language the source code is in</param>
/// <returns>A Document created from the source string</returns>
protected static Document CreateDocument(string source, string language = LanguageNames.CSharp)
protected Document CreateDocument(string source, string language = LanguageNames.CSharp)
{
return CreateProject(new[] { source }, language).Documents.First();
}
Expand All @@ -139,7 +148,7 @@ protected static Document CreateDocument(string source, string language = Langua
/// <param name="sources">Classes in the form of strings</param>
/// <param name="language">The language the source code is in</param>
/// <returns>A Project created out of the Documents created from the source strings</returns>
private static Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
private Project CreateProject(string[] sources, string language = LanguageNames.CSharp)
{
string fileNamePrefix = DefaultFilePathPrefix;
string fileExt = language == LanguageNames.CSharp ? CSharpDefaultFileExt : VisualBasicDefaultExt;
Expand All @@ -149,10 +158,7 @@ private static Project CreateProject(string[] sources, string language = Languag
var solution = new AdhocWorkspace()
.CurrentSolution
.AddProject(projectId, TestProjectName, TestProjectName, language)
.AddMetadataReference(projectId, CorlibReference)
.AddMetadataReference(projectId, SystemCoreReference)
.AddMetadataReference(projectId, CSharpSymbolsReference)
.AddMetadataReference(projectId, CodeAnalysisReference);
.AddMetadataReferences(projectId, References);

int count = 0;
foreach (var source in sources)
Expand Down
Loading