Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<RootNamespace>Fluidic.Sample</RootNamespace>
<RootNamespace>Cutout.Sample</RootNamespace>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Fluidic\Fluidic.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
<ProjectReference Include="..\Cutout\Cutout.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions Cutout.Sample/Examples.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Text;

namespace Cutout.Sample;

public static partial class Examples
{
[Template("This is a very simple example")]
public static partial void Test(this StringBuilder builder);
}
18 changes: 18 additions & 0 deletions Cutout.Tests/CallStatementTests.Case1a#Test.Test.g.verified.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//HintName: Test.Test.g.cs
// <auto-generated/>
#nullable enable

using Cutout;
using System.Diagnostics.CodeAnalysis;
using System;

namespace <global namespace>;

public static partial class Test
{
public static partial void Test(this StringBuilder builder, String product)
{
builder.Append(@"Some text before ");
Case2(builder,product);
}
}
37 changes: 37 additions & 0 deletions Cutout.Tests/CallStatementTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Text;
using Cutout.Tests.Extensions;

namespace Cutout.Tests;

public static partial class CallTemplates
{
public sealed record Product(string Title);

private const string CallExample1 = "Some text before {{ call Case2(product.Title) }}";

[Template(CallExample1)]
public static partial void Case1(this StringBuilder builder, Product product);

private const string CallExample2 = "The product title is {{ title }}";

[Template(CallExample2)]
public static partial void Case2(this StringBuilder builder, string title);
}

public sealed class CallStatementTests
{
[Fact(DisplayName = "A call statement can used")]
public void Case1()
{
var builder = new StringBuilder();
builder.Case1(new CallTemplates.Product("Awesome Shoes"));
Assert.Equal("Some text before The product title is Awesome Shoes", builder.ToString());
}

[Fact(DisplayName = "Case1 produces the expected source")]
public Task Case1a() =>
"""
[Template("Some text before {{ call Case2(product) }}")]
public static partial void Test(this StringBuilder builder, string product);
""".VerifyTemplate();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
<AdditionalFiles Include="*.liquid"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing"/>
<PackageReference Include="Verify.SourceGenerators"/>
Expand All @@ -22,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Fluidic\Fluidic.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true"/>
<ProjectReference Include="..\Cutout\Cutout.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Text;

namespace Fluidic.Tests.Extensions;
namespace Cutout.Tests.Extensions;

public sealed class TestAdditionalFile(string path, SourceText text) : AdditionalText
{
Expand All @@ -28,23 +28,23 @@ ImmutableArray<AdditionalText> additionalTexts
source != null ? [CSharpSyntaxTree.ParseText(source)] : [],
[
MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
MetadataReference.CreateFromFile(typeof(StringTemplateAttribute).Assembly.Location),
MetadataReference.CreateFromFile(typeof(TemplateAttribute).Assembly.Location),
],
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
);
var generator = new StringTemplateSourceGenerator();
var generator = new TemplateSourceGenerator();
var driver = CSharpGeneratorDriver.Create(generator).AddAdditionalTexts(additionalTexts);
return driver.RunGenerators(compilation);
}

internal static Task VerifyStringTemplate(
internal static Task VerifyTemplate(
this string? source,
[CallerFilePath] string sourceFile = ""
)
{
var driver = $$"""
using System;
using Fluidic;
using Cutout;

internal static class Test
{
Expand All @@ -54,28 +54,8 @@ internal static class Test
return Verify(driver, sourceFile: sourceFile).IgnoreStandardSupportCode();
}

internal static Task VerifyFileTemplate(
this string? source,
ImmutableArray<AdditionalText> additionalTexts,
[CallerFilePath] string sourceFile = ""
)
{
var driver = $$"""
using System;
using Fluidic;

internal static class Test
{
{{source}}
}
""".BuildDriver(additionalTexts);
return Verify(driver, sourceFile: sourceFile).IgnoreStandardSupportCode();
}

internal static SettingsTask IgnoreStandardSupportCode(this SettingsTask settings)
{
return settings.IgnoreGeneratedResult(x =>
x.HintName is "StringTemplateAttribute.g.cs" or "FileTemplateAttribute.g.cs"
);
return settings.IgnoreGeneratedResult(x => x.HintName is "TemplateAttribute.g.cs");
}
}
23 changes: 23 additions & 0 deletions Cutout.Tests/ForStatementTests.Case1a#Test.Test.g.verified.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//HintName: Test.Test.g.cs
// <auto-generated/>
#nullable enable

using Cutout;
using System.Diagnostics.CodeAnalysis;
using System;

namespace <global namespace>;

public static partial class Test
{
public static partial void Test(this StringBuilder builder, String product)
{
builder.Append(@"This is a test for tags [");
foreach (var tag in product.Tags)
{
builder.Append(tag);
builder.Append(@"; ");
}
Comment thread
bmazzarol marked this conversation as resolved.
builder.Append(@"] which is cool.");
}
}
24 changes: 24 additions & 0 deletions Cutout.Tests/ForStatementTests.Case2a#Test.Test.g.verified.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//HintName: Test.Test.g.cs
// <auto-generated/>
#nullable enable

using Cutout;
using System.Diagnostics.CodeAnalysis;
using System;

namespace <global namespace>;

public static partial class Test
{
public static partial void Test(this StringBuilder builder, String product)
{
builder.Append(@"This is a test for tags [");
while (i < product.Tags.Length)
{
builder.Append(i + 1);
builder.Append(@". ");
builder.Append(product.Tags[i++]);
}
Comment thread
bmazzarol marked this conversation as resolved.
builder.Append(@"] which is cool.");
}
}
32 changes: 32 additions & 0 deletions Cutout.Tests/ForStatementTests.Case3a#Test.Test.g.verified.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//HintName: Test.Test.g.cs
// <auto-generated/>
#nullable enable

using Cutout;
using System.Diagnostics.CodeAnalysis;
using System;

namespace <global namespace>;

public static partial class Test
{
public static partial void Test(this StringBuilder builder, String product)
{
builder.Append(@"This is a test for tags [");
for (var i = 0; i < product.Tags.Length; i++)
{
if (product.Tags[i] == "awesome")
{
continue;
}
else if (product.Tags[i] == "shoes")
{
break;
}
Comment thread
bmazzarol marked this conversation as resolved.
builder.Append(i + 1);
builder.Append(@". ");
builder.Append(product.Tags[i]);
}
Comment thread
bmazzarol marked this conversation as resolved.
builder.Append(@"] which is cool.");
}
}
Loading