Skip to content

Commit

Permalink
Merge pull request #2 from d1820/add-data-member
Browse files Browse the repository at this point in the history
Add data member
  • Loading branch information
d1820 authored Feb 14, 2021
2 parents 8957216 + 36d0f6e commit d3b1255
Show file tree
Hide file tree
Showing 53 changed files with 1,000 additions and 205 deletions.
25 changes: 0 additions & 25 deletions .github/workflows/buildandtest.yml

This file was deleted.

27 changes: 27 additions & 0 deletions ProtoAttributor.Tests/DataContracts/AttributeReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.IO;
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp;
using ProtoAttributor.Parsers.DataContracts;
using Xunit;

namespace ProtoAttributor.Tests.DataContracts
{
public class AttributeReaderTests
{
public string LoadTestFile(string relativePath)
{
return File.ReadAllText(relativePath);
}

[Fact]
public void GetsNextIdForDataMember()
{
var tree = CSharpSyntaxTree.ParseText(LoadTestFile(@"./Mocks/TestCodeWithDataMemberAttributes.cs"));
var protoReader = new DataAttributeReader();

var output = protoReader.GetDataMemberNextId(tree.GetRoot());

output.Should().Be(3);
}
}
}
158 changes: 158 additions & 0 deletions ProtoAttributor.Tests/DataContracts/DataAttributeAdderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
using System;
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp;
using ProtoAttributor.Parsers.DataContracts;
using Xunit;

namespace ProtoAttributor.Tests.DataContracts
{
public class DataAttributeAdderTests: IClassFixture<TestFixure>
{
private readonly TestFixure _fixture;

public DataAttributeAdderTests(TestFixure fixture)
{
_fixture = fixture;
}

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

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

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

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[Required]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");

_fixture.AssertOutputContainsCount(source, "DataMember", 2);
_fixture.AssertOutputContainsCount(source, "Required", 1);
}

[Fact]
public void AddsAttributesForProtBufWherePropDontHaveIgnoreDataMember()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestCodeWithAttributesAndDataMemberIgnore.cs"));
var rewriter = new DataAttributeAdder();
var rewrittenRoot = rewriter.Visit(tree.GetRoot());

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

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[Required]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
output.Should().Contain("[DataMember(Order = 3)]");

_fixture.AssertOutputContainsCount(source, "[DataMember", 3);
_fixture.AssertOutputContainsCount(source, "[Required]", 1);
_fixture.AssertOutputContainsCount(source, "[IgnoreDataMember]", 1);
}

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

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

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");

output.Should().Contain(" /// </value>");
output.Should().Contain(" [DataMember(Order = 1)]");
output.Should().Contain(" public int MyProperty { get; set; }");

//This verifies spacing is correct
output.Should().Contain(@"
/// <summary> Comments not wrapped </summary>
/// <value> My property. </value>
[DataMember(Order = 1)]
public int MyProperty { get; set; }");
}

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

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

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
}

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

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

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
output.Should().Contain("[DataMember(Order = 3)]");
output.Should().Contain("[DataMember(Name = \"Test\")]");
}

[Fact]
public void AddsAttributesWithCorrectOrderWhenFileHasWierdFormatting()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestWierdFormatting.cs"));
var rewriter = new DataAttributeAdder();
var rewrittenRoot = rewriter.Visit(tree.GetRoot());

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
}

[Fact]
public void AddsAttributesWithCorrectOrderWhenFileHasProtoIgnores()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestIgnoreDataMember.cs"));
var rewriter = new DataAttributeAdder();
var rewrittenRoot = rewriter.Visit(tree.GetRoot());

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
output.Should().Contain("[DataMember(Order = 14)]");
output.Should().Contain("[DataMember(Order = 16)]");
_fixture.AssertOutputContainsCount(source, "[IgnoreDataMember]", 2);
}
}
}
40 changes: 40 additions & 0 deletions ProtoAttributor.Tests/DataContracts/DataAttributeRemoverTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp;
using ProtoAttributor.Parsers.DataContracts;
using Xunit;

namespace ProtoAttributor.Tests.DataContracts
{
public class DataAttributeRemoverTests: IClassFixture<TestFixure>
{
private readonly TestFixure _fixture;

public DataAttributeRemoverTests(TestFixure fixture)
{
_fixture = fixture;
}

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

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

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

output.Should().NotContain("System.Runtime.Serialization");
output.Should().NotContain("[DataContract]");
output.Should().NotContain("[KnownType");
output.Should().NotContain("[IgnoreDataMember]");
output.Should().NotContain(@"[DataMember(Order = 1, Name=""Test"")]");
output.Should().NotContain("[DataMember(Order = 2)]");
output.Should().NotContain(@"DataMember(Name = ""test12"")");

output.Should().Contain("[Required]");

output.Should().Contain("[Serializable]");
}
}
}
95 changes: 95 additions & 0 deletions ProtoAttributor.Tests/DataContracts/DataAttributeRewriterTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp;
using ProtoAttributor.Parsers.DataContracts;
using Xunit;

namespace ProtoAttributor.Tests.DataContracts
{
public class DataAttributeRewriterTests: IClassFixture<TestFixure>
{
private readonly TestFixure _fixture;

public DataAttributeRewriterTests(TestFixure fixture)
{
_fixture = fixture;
}

private readonly string _codeWithAttributes = @"
using System;
using Xunit;
namespace ProtoAttributor.Tests
{
[Required]
public class Test
{
[Required]
[DataMember(Order = 10, Name=""Test"")]
public int MyProperty { get; set; }
[DataMember(Order = 20)]
public int MyProperty2 { get; set; }
[Required]
public int MyProperty3 { get; set; }
public int MyProperty4 { get; set; }
}
}
";

[Fact]
public void RewritesAttributesWithCorrectOrderWhenAttributesAlreadyExists()
{
var tree = CSharpSyntaxTree.ParseText(_codeWithAttributes);
var rewriter = new DataAttributeRewriter();

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

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain(@"[DataMember(Order = 1, Name=""Test"")]");
output.Should().Contain("[DataMember(Order = 2)]");
output.Should().Contain("[DataMember(Order = 3)]");
output.Should().Contain("[DataMember(Order = 4)]");
}

[Fact]
public void RewritesAttributesWithCorrectOrderWhenFileHasProtoIgnores()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestDataIgnore.cs"));
var rewriter = new DataAttributeRewriter();
var rewrittenRoot = rewriter.Visit(tree.GetRoot());

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
output.Should().Contain("[DataMember(Order = 3)]");
output.Should().Contain("[DataMember(Order = 4)]");
_fixture.AssertOutputContainsCount(source, "[IgnoreDataMember]", 2);
}

[Fact]
public void RewritesAttributesWithCorrectOrderWhenAttributeExistsWithoutOrderProperty()
{
var tree = CSharpSyntaxTree.ParseText(_fixture.LoadTestFile(@"./Mocks/TestMissingOrderPropertyAndWeirdSpacing.cs"));
var rewriter = new DataAttributeRewriter();
var rewrittenRoot = rewriter.Visit(tree.GetRoot());

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

output.Should().Contain("System.Runtime.Serialization");
output.Should().Contain("[DataContract]");
output.Should().Contain("[DataMember(Order = 1)]");
output.Should().Contain("[DataMember(Order = 2)]");
output.Should().Contain(@"[DataMember(Name =""test"",Order = 3)]");
output.Should().Contain("[DataMember(Order = 4)]");
_fixture.AssertOutputContainsCount(source, "[IgnoreDataMember]", 1);
}
}
}
22 changes: 5 additions & 17 deletions ProtoAttributor.Tests/Mocks/TestClassNoUsings.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,14 @@

namespace ProtoAttributor.Tests.Mocks
{
/// <summary>
/// TestClass
/// </summary>
/// <summary> TestClass </summary>
public class TestClassWithNoUsings
{
/// <summary>
/// Gets or sets my property.
/// </summary>
/// <value>
/// My property.
/// </value>
/// <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>
/// <summary> Gets or sets my property11. </summary>
/// <value> My property11. </value>
public int MyProperty11 { get; set; }

}
}
5 changes: 1 addition & 4 deletions ProtoAttributor.Tests/Mocks/TestClassPlain.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using FluentAssertions;
using Microsoft.CodeAnalysis.CSharp;
using System.ComponentModel.DataAnnotations;
using System;
using Xunit;

namespace ProtoAttributor.Tests.Mocks
{
public class TestClassPlain
{
[Required]
public int MyProperty { get; set; }

public int MyProperty2 { get; set; }
}
}
Loading

0 comments on commit d3b1255

Please sign in to comment.