Skip to content

Commit

Permalink
Added support for limiting the number of lines shown in the generated…
Browse files Browse the repository at this point in the history
… comment.

Reorganized tests for completeness.
Fixed custom item properties not being available to the generator.
  • Loading branch information
yugabe committed Jul 17, 2024
1 parent 10d3fcb commit 518974e
Show file tree
Hide file tree
Showing 10 changed files with 162 additions and 87 deletions.
11 changes: 8 additions & 3 deletions src/EmbeddedTexts/EmbeddedTextsGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class EmbeddedTextsGenerator : IIncrementalGenerator
public const string EmbedTextClassNameMetadataProperty = "PodNet_EmbedTextClassName";
public const string EmbedTextIsConstMetadataProperty = "PodNet_EmbedTextIsConst";
public const string EmbedTextIdentifierMetadataProperty = "PodNet_EmbedTextIdentifier";
public const string EmbedTextCommentContentLinesMetadataProperty = "PodNet_EmbedTextCommentContentLines";

public record EmbeddedTextItemOptions(
string? RootNamespace,
Expand All @@ -21,6 +22,7 @@ public record EmbeddedTextItemOptions(
string? ItemClassName,
bool? IsConst,
string? Identifier,
uint? CommentContentLines,
bool Enabled,
AdditionalText Text);

Expand All @@ -42,6 +44,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
ItemClassName: itemOptions.GetAdditionalTextMetadata(EmbedTextClassNameMetadataProperty),
IsConst: string.Equals(itemOptions.GetAdditionalTextMetadata(EmbedTextIsConstMetadataProperty), "true", StringComparison.OrdinalIgnoreCase),
Identifier: itemOptions.GetAdditionalTextMetadata(EmbedTextIdentifierMetadataProperty),
CommentContentLines: uint.TryParse(itemOptions.GetAdditionalTextMetadata(EmbedTextCommentContentLinesMetadataProperty), out var commentLines) ? commentLines : null,
Enabled: (globalEnabled || itemEnabled) && !itemDisabled,
Text: text);
});
Expand Down Expand Up @@ -94,15 +97,17 @@ public static partial class {{className}}
/// <code>
""");

foreach (var line in lines.Take(10))
var commentLines = (int)item.CommentContentLines.GetValueOrDefault();

foreach (var line in commentLines > 0 ? lines.Take(commentLines) : lines)
{
sourceBuilder.AppendLine($$"""
/// {{line.ToString().Replace("<", "&lt;").Replace(">", "&gt;")}}
""");
}
if (lines.Count > 10)
if (commentLines > 0 && lines.Count > commentLines)
{
sourceBuilder.AppendLine($"/// [{lines.Count - 10} more lines ({lines.Count} total)] ");
sourceBuilder.AppendLine($"/// [{lines.Count - commentLines} more lines ({lines.Count} total)] ");
}

sourceBuilder.AppendLine($$"""
Expand Down
4 changes: 1 addition & 3 deletions src/EmbeddedTexts/PodNet.EmbeddedTexts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
</PropertyGroup>

<ItemGroup>
<InternalsVisibleTo Include="PodNet.EmbeddedTexts.Tests" />

<PackageReference Include="PodNet.Analyzers.Core" Version="1.0.10" PrivateAssets="all" />
<PackageReference Include="PodNet.Analyzers.Core" Version="1.0.11" PrivateAssets="all" />
<PackageReference Include="PodNet.NuGet.Core" Version="1.0.4" PrivateAssets="all" />

<Content Include="build/*" PackagePath="build" />
Expand Down
3 changes: 3 additions & 0 deletions src/EmbeddedTexts/build/PodNet.EmbeddedTexts.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="PodNet_EmbedText" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="PodNet_EmbedTextNamespace" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="PodNet_EmbedTextClassName" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="PodNet_EmbedTextIsConst" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="PodNet_EmbedTextIdentifier" />
<CompilerVisibleItemMetadata Include="AdditionalFiles" MetadataName="PodNet_EmbedTextCommentContentLines" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
namespace PodNet.EmbeddedTexts.IntegrationTests;
using PodNet.Analyzers.Testing.CSharp;

namespace PodNet.EmbeddedTexts.IntegrationTests;

/// <summary>
/// This tests that, given the provided files are correctly added to <c>AdditionaFiles</c>, the correct
/// classes are generated in the correct namespaces with the correct API surface (property), and it returns
/// the file content as the result (unless ignored). These "tests" won't even compile if the correct
/// the file content as the result (unless ignored). Most of these "tests" won't even compile if the correct
/// APIs are not generated.
/// </summary>
/// <remarks>
/// Note that testing comments in integrated scenarios is not possible, due to the comments
/// only being available to the IDE and not the runtime. Unit tests can be used to test them.
/// </remarks>
[TestClass]
public class EmbeddedTextsGeneratorIntegrationTest
{
Expand All @@ -24,17 +30,17 @@ public void IgnoredContentIsNotEmbedded()
// Given that the Files\Ignored\** pattern is excluded by setting "PodNet_EmbedText" to "false", the following shouldn't compile:
// Files.Ignored.Ignored_txt.Content;
var undefined = "PodNet.EmbeddedTexts.IntegrationTests.Files.Ignored.Ignored_txt";
var compilation = Analyzers.Testing.CSharp.PodCSharpCompilation.Create([$$"""
var compilation = PodCSharpCompilation.Create([$$"""
class ShouldError
{
string WontCompile() => {{undefined}}.Content;
};
"""]);
var diagnostics = compilation.CurrentCompilation.GetDiagnostics();
var diagnostics = compilation.GetDiagnostics();
Assert.IsTrue(diagnostics.Any(d => d is
{
Severity: Microsoft.CodeAnalysis.DiagnosticSeverity.Error,
Id: "CS0234", // {The type or namespace name '{0}' does not exist in the namespace '{1}' (are you missing an assembly reference?)}
Id: "CS0234", // The type or namespace name '{0}' does not exist in the namespace '{1}' (are you missing an assembly reference?)
Location: { SourceSpan: var span, SourceTree: var tree }
} && tree?.GetText().GetSubText(span).ToString() == undefined));
}
Expand All @@ -52,4 +58,14 @@ public void CustomClassAndNamespaceNamesCanBeSupplied()
Assert.IsNotNull(TestNamespace.TestSubNamespace.CustomNamespace_txt.Content);
Assert.IsNotNull(TestNamespace.TestSubNamespace.TestClass.Content);
}

[TestMethod]
public void ConstAndPropertyCanBeCustomized()
{
// The following won't compile if the symbol is a property instead of a const.
const string? content = Files.Const_txt.Content;
Assert.IsNotNull(content);

Assert.IsNotNull(Files.CustomPropertyName_txt.TestProperty);
}
}
10 changes: 10 additions & 0 deletions tests/EmbeddedTexts.IntegrationTests/Files/10Lines.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
1 change: 1 addition & 0 deletions tests/EmbeddedTexts.IntegrationTests/Files/Const.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Const
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CustomPropertyName
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
</PropertyGroup>

<ItemGroup>

<PackageReference Include="MSTest" Version="3.4.3" />

<PackageReference Include="PodNet.Analyzers.Testing" Version="1.0.3" />

<PackageReference Include="PodNet.Analyzers.Testing" Version="1.0.7" />
<PackageReference Include="MSTest" Version="3.5.0" />

<!-- Take note that the NuGet package has to be packed before the integration tests are executed. -->
<!-- Take care to empty the package from the local and global caches as needed. -->
Expand All @@ -20,12 +19,17 @@
<AdditionalFiles Include="Files\**" />

<AdditionalFiles Update="Files\Ignored\**" PodNet_EmbedText="false" />
<AdditionalFiles Update="Files\Ignored\Unignored.txt" PodNet_EmbedText="true" />

<AdditionalFiles Update="Files\CustomClass.txt" PodNet_EmbedTextClassName="TestClass" />
<AdditionalFiles Update="Files\CustomClassAndNamespace.txt" PodNet_EmbedTextNamespace="TestNamespace.TestSubNamespace" />
<AdditionalFiles Update="Files\CustomNamespace.txt" PodNet_EmbedTextNamespace="TestNamespace.TestSubNamespace" />
<AdditionalFiles Update="Files\CustomClassAndNamespace.txt" PodNet_EmbedTextNamespace="TestNamespace.TestSubNamespace" PodNet_EmbedTextClassName="TestClass" />
<AdditionalFiles Update="Files\Ignored\Unignored.txt" PodNet_EmbedText="true" />

<AdditionalFiles Update="Files\CustomPropertyName.txt" PodNet_EmbedTextIdentifier="TestProperty" />
<AdditionalFiles Update="Files\Const.txt" PodNet_EmbedTextIsConst="true" />

<AdditionalFiles Update="Files\10Lines.txt" PodNet_EmbedTextCommentContentLines="5" />
</ItemGroup>

</Project>
Loading

0 comments on commit 518974e

Please sign in to comment.