-
Notifications
You must be signed in to change notification settings - Fork 14
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
Enable internal classes #43
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
using System; | ||
using System.Linq; | ||
using AutomaticInterface; | ||
using FluentAssertions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Xunit; | ||
|
||
namespace Tests; | ||
|
||
public partial class AccessibilityTests | ||
{ | ||
private static string GenerateCode(string code) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, this dublicates code in the original tests. |
||
{ | ||
var syntaxTree = CSharpSyntaxTree.ParseText(code); | ||
var references = AppDomain | ||
.CurrentDomain.GetAssemblies() | ||
.Where(assembly => !assembly.IsDynamic) | ||
.Select(assembly => MetadataReference.CreateFromFile(assembly.Location)) | ||
.Cast<MetadataReference>(); | ||
|
||
var compilation = CSharpCompilation.Create( | ||
"SourceGeneratorTests", | ||
new[] { syntaxTree }, | ||
references, | ||
new(OutputKind.DynamicallyLinkedLibrary) | ||
); | ||
|
||
var generator = new AutomaticInterfaceGenerator(); | ||
|
||
CSharpGeneratorDriver | ||
.Create(generator) | ||
.RunGeneratorsAndUpdateCompilation( | ||
compilation, | ||
out var outputCompilation, | ||
out var diagnostics | ||
); | ||
|
||
diagnostics.Where(d => d.Severity == DiagnosticSeverity.Error).Should().BeEmpty(); | ||
|
||
return outputCompilation.SyntaxTrees.Skip(1).LastOrDefault()?.ToString(); | ||
} | ||
|
||
[Fact] | ||
public void AddsPublicMethodToInterface() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this test test? |
||
{ | ||
const string code = """ | ||
|
||
using AutomaticInterfaceAttribute; | ||
using System.IO; | ||
|
||
namespace AutomaticInterfaceExample | ||
{ | ||
|
||
[GenerateAutomaticInterface] | ||
public class DemoClass | ||
{ | ||
|
||
public string Hello(){return "";} | ||
} | ||
} | ||
|
||
"""; | ||
|
||
const string expected = """ | ||
//-------------------------------------------------------------------------------------------------- | ||
// <auto-generated> | ||
// This code was generated by a tool. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. | ||
// </auto-generated> | ||
//-------------------------------------------------------------------------------------------------- | ||
|
||
using System.CodeDom.Compiler; | ||
using AutomaticInterfaceAttribute; | ||
using System.IO; | ||
|
||
namespace AutomaticInterfaceExample | ||
{ | ||
[GeneratedCode("AutomaticInterface", "")] | ||
public partial interface IDemoClass | ||
{ | ||
/// <inheritdoc /> | ||
string Hello(); | ||
|
||
} | ||
} | ||
|
||
"""; | ||
GenerateCode(code).Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void IgnoresNotPublicMethodToInterface() | ||
{ | ||
const string code = """ | ||
|
||
using AutomaticInterfaceAttribute; | ||
using System.IO; | ||
|
||
namespace AutomaticInterfaceExample | ||
{ | ||
|
||
[GenerateAutomaticInterface] | ||
public class DemoClass | ||
{ | ||
private string Hello(string x, int y, double z){return x;} | ||
internal string Hello2(string x, int y, double z){return x;} | ||
} | ||
} | ||
|
||
"""; | ||
|
||
const string expected = """ | ||
//-------------------------------------------------------------------------------------------------- | ||
// <auto-generated> | ||
// This code was generated by a tool. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. | ||
// </auto-generated> | ||
//-------------------------------------------------------------------------------------------------- | ||
|
||
using System.CodeDom.Compiler; | ||
using AutomaticInterfaceAttribute; | ||
using System.IO; | ||
|
||
namespace AutomaticInterfaceExample | ||
{ | ||
[GeneratedCode("AutomaticInterface", "")] | ||
public partial interface IDemoClass | ||
{ | ||
} | ||
} | ||
|
||
"""; | ||
GenerateCode(code).Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void WorksWithInternal() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see internal here? |
||
{ | ||
const string code = """ | ||
|
||
using AutomaticInterfaceAttribute; | ||
using System.Threading.Tasks; | ||
|
||
namespace AutomaticInterfaceExample; | ||
|
||
[GenerateAutomaticInterface] | ||
internal class InternalClass | ||
{ | ||
public int AProperty { get; set; } | ||
} | ||
|
||
"""; | ||
|
||
const string expected = """ | ||
//-------------------------------------------------------------------------------------------------- | ||
// <auto-generated> | ||
// This code was generated by a tool. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. | ||
// </auto-generated> | ||
//-------------------------------------------------------------------------------------------------- | ||
|
||
using System.CodeDom.Compiler; | ||
using AutomaticInterfaceAttribute; | ||
using System.Threading.Tasks; | ||
|
||
namespace AutomaticInterfaceExample | ||
{ | ||
[GeneratedCode("AutomaticInterface", "")] | ||
internal partial interface IInternalClass | ||
{ | ||
/// <inheritdoc /> | ||
int AProperty { get; set; } | ||
|
||
} | ||
} | ||
|
||
"""; | ||
GenerateCode(code).Should().Be(expected); | ||
} | ||
|
||
[Fact] | ||
public void WorksWithDefaultAccessModifier() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like the above test? |
||
{ | ||
const string code = """ | ||
|
||
using AutomaticInterfaceAttribute; | ||
using System.Threading.Tasks; | ||
|
||
namespace AutomaticInterfaceExample; | ||
|
||
[GenerateAutomaticInterface] | ||
class InternalClass | ||
{ | ||
public int AProperty { get; set; } | ||
} | ||
|
||
"""; | ||
|
||
const string expected = """ | ||
//-------------------------------------------------------------------------------------------------- | ||
// <auto-generated> | ||
// This code was generated by a tool. | ||
// | ||
// Changes to this file may cause incorrect behavior and will be lost if the code is regenerated. | ||
// </auto-generated> | ||
//-------------------------------------------------------------------------------------------------- | ||
|
||
using System.CodeDom.Compiler; | ||
using AutomaticInterfaceAttribute; | ||
using System.Threading.Tasks; | ||
|
||
namespace AutomaticInterfaceExample | ||
{ | ||
[GeneratedCode("AutomaticInterface", "")] | ||
internal partial interface IInternalClass | ||
{ | ||
/// <inheritdoc /> | ||
int AProperty { get; set; } | ||
|
||
} | ||
} | ||
|
||
"""; | ||
GenerateCode(code).Should().Be(expected); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, definitely needed.
I would suggest also adding examples, changelog and your name to Readme.md - especially calling out that you fixed a bug where classes without access modifier where wrongly set to public instead of internal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!