Skip to content

Commit

Permalink
bug fix for file namespaces .net 6 (#8)
Browse files Browse the repository at this point in the history
Co-authored-by: DanTurco <[email protected]>
  • Loading branch information
d1820 and dturco-kbx authored Jan 15, 2022
1 parent d46223b commit f18df25
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Manifests/vs2022/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="ProtoAttributor.2022.fc16f3d2-4cc4-42ca-a6ca-2e7fb9cb3e5f" Version="1.0" Language="en-US" Publisher="Dan Turco" />
<Identity Id="ProtoAttributor.2022.fc16f3d2-4cc4-42ca-a6ca-2e7fb9cb3e5f" Version="1.1" Language="en-US" Publisher="Dan Turco" />
<DisplayName>ProtoAttributor 2022</DisplayName>
<Description xml:space="preserve">ProtoAttributor is an open source Visual Studio extension that can manage the appropriate attributes on a class to support ProtoBuf. It currently supports ProtoContract, ProtoMember, ProtoIgnore, DataContract, DataMember, IgnoreDataMemeber attributes.</Description>
<MoreInfo>https://github.com/d1820/proto-attributor</MoreInfo>
Expand Down
1 change: 1 addition & 0 deletions ProtoAttributor.Tests/Mocks/TestClassNoUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ public class TestClassWithNoUsings
public int MyProperty11 { get; set; }
}
}

13 changes: 13 additions & 0 deletions ProtoAttributor.Tests/Mocks/TestClassWithNoBracketNamespace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ProtoAttributor.Tests.Mocks;
/// <summary> TestClass </summary>
public class TestClassWithNoBracketNamespace
{
/// <summary> Gets or sets my property. </summary>
/// <value> My property. </value>
public int MyProperty { get; set; }

/// <summary> Gets or sets my property11. </summary>
/// <value> My property11. </value>
public int MyProperty11 { get; set; }
}

7 changes: 5 additions & 2 deletions ProtoAttributor.Tests/ProtoAttributor.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="FluentAssertions" Version="6.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="protobuf-net" Version="3.0.73" />
Expand All @@ -27,6 +27,9 @@
</ItemGroup>

<ItemGroup>
<Compile Update="Mocks\TestClassWithNoBracketNamespace.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
<Compile Update="Mocks\TestCodeWithDataMemberAttributes.cs">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Compile>
Expand Down
41 changes: 41 additions & 0 deletions ProtoAttributor.Tests/ProtoContracts/ProtoAttributeAdderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,47 @@ public ProtoAttributeAdderTests(TestFixure fixture)
_fixture = fixture;
}

[Fact]
public void AddsAttributesForProtBuf_WhenClassWithNoBracketNamespace()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestClassWithNoBracketNamespace.cs"));
var rewriter = new ProtoAttributeAdder();

var rewrittenRoot = rewriter.Visit(tree.GetRoot());

var output = rewrittenRoot.GetText().ToString();

var source = output.Split(new string[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

output.Should().Contain("using ProtoBuf;");
output.Should().Contain("[ProtoContract]");
output.Should().Contain("[ProtoMember(1)]");
output.Should().Contain("[ProtoMember(2)]");

_fixture.AssertOutputContainsCount(source, "ProtoMember", 2);
}


[Fact]
public void AddsAttributesForProtBuf_WhenClassWithNoUsings()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestClassNoUsings.cs"));
var rewriter = new ProtoAttributeAdder();

var rewrittenRoot = rewriter.Visit(tree.GetRoot());

var output = rewrittenRoot.GetText().ToString();

var source = output.Split(new string[] { " ", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

output.Should().Contain("using ProtoBuf;");
output.Should().Contain("[ProtoContract]");
output.Should().Contain("[ProtoMember(1)]");
output.Should().Contain("[ProtoMember(2)]");

_fixture.AssertOutputContainsCount(source, "ProtoMember", 2);
}

[Fact]
public void AddsAttributesForProtBuf()
{
Expand Down
4 changes: 4 additions & 0 deletions ProtoAttributor/Parsers/TriviaMaintainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public static ClassDeclarationSyntax Apply(ClassDeclarationSyntax node, Func<Cla
node = node.WithoutTrailingTrivia();

var wp = leadingTrivia.FirstOrDefault(w => w.Kind() == SyntaxKind.WhitespaceTrivia);
if (wp == default)
{
wp = SyntaxFactory.Whitespace(String.Empty);
}

node = builder?.Invoke(node, wp);

Expand Down
2 changes: 1 addition & 1 deletion ProtoAttributor/ProtoAttributor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
<PackageReference Include="Microsoft.VisualStudio.SDK" Version="17.0.31902.203" ExcludeAssets="runtime">
<IncludeAssets>compile; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.0.5232">
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.0.5234">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down

0 comments on commit f18df25

Please sign in to comment.