-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from d1820/add-data-member
Add data member
- Loading branch information
Showing
53 changed files
with
1,000 additions
and
205 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
27 changes: 27 additions & 0 deletions
27
ProtoAttributor.Tests/DataContracts/AttributeReaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
158
ProtoAttributor.Tests/DataContracts/DataAttributeAdderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
ProtoAttributor.Tests/DataContracts/DataAttributeRemoverTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
95
ProtoAttributor.Tests/DataContracts/DataAttributeRewriterTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
Oops, something went wrong.