-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial draft for improved source gen
- Loading branch information
1 parent
a62fbc4
commit 9272b88
Showing
20 changed files
with
1,474 additions
and
441 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
WoWsShipBuilder.Data.Generator.Test/DataElementGeneratorTest/DataElementGeneratorTest.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,55 @@ | ||
using System.Reflection; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Testing; | ||
using Microsoft.CodeAnalysis.Testing; | ||
using Microsoft.CodeAnalysis.Testing.Verifiers; | ||
using NUnit.Framework; | ||
using WoWsShipBuilder.Data.Generator.Attributes; | ||
using WoWsShipBuilder.DataElements.DataElements; | ||
|
||
namespace WoWsShipBuilder.Data.Generator.Test.DataElementGeneratorTest; | ||
|
||
[TestFixture] | ||
public partial class DataElementGeneratorTest | ||
{ | ||
private static CSharpSourceGeneratorTest<DataElementGenerator, NUnitVerifier> CreateTest(string source, string expected) | ||
{ | ||
const string baseClass = """ | ||
namespace WoWsShipBuilder.DataElements.DataElements; | ||
|
||
public abstract record DataContainerBase | ||
{ | ||
public global::System.Collections.Generic.List<IDataElement> DataElements { get; } = new(); | ||
|
||
protected static bool ShouldAdd(object? value) | ||
{ | ||
return value switch | ||
{ | ||
string strValue => !string.IsNullOrEmpty(strValue), | ||
decimal decValue => decValue != 0, | ||
(decimal min, decimal max) => min > 0 || max > 0, | ||
int intValue => intValue != 0, | ||
_ => false, | ||
}; | ||
} | ||
} | ||
"""; | ||
return new() | ||
{ | ||
TestState = | ||
{ | ||
Sources = { baseClass, source }, | ||
GeneratedSources = | ||
{ | ||
(typeof(DataElementGenerator), "DataElementTypes.g.cs", AttributeHelper.DataElementTypesEnum), | ||
(typeof(DataElementGenerator), "DataContainerAttribute.g.cs", AttributeHelper.DataContainerAttribute), | ||
(typeof(DataElementGenerator), "DataElementTypeAttribute.g.cs", AttributeHelper.DataElementTypeAttribute), | ||
(typeof(DataElementGenerator), "DataElementFilteringAttribute.g.cs", AttributeHelper.DataElementFilteringAttribute), | ||
(typeof(DataElementGenerator), "TestRecord.g.cs", expected), | ||
}, | ||
ReferenceAssemblies = ReferenceAssemblies.Net.Net70, | ||
AdditionalReferences = { MetadataReference.CreateFromFile(typeof(IDataElement).GetTypeInfo().Assembly.Location) }, | ||
}, | ||
}; | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
WoWsShipBuilder.Data.Generator.Test/DataElementGeneratorTest/GenerateCode.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,137 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Threading.Tasks; | ||
using NUnit.Framework; | ||
|
||
namespace WoWsShipBuilder.Data.Generator.Test.DataElementGeneratorTest; | ||
|
||
[SuppressMessage("Maintainability", "S2699", Justification = "false-positive since sonarlint does not recognize custom CreateTest method")] | ||
public partial class DataElementGeneratorTest | ||
{ | ||
[Test] | ||
public async Task GenerateCode_EmptyRecord_EmptyMethod() | ||
{ | ||
var source = """ | ||
using WoWsShipBuilder.DataElements.DataElementAttributes; | ||
using WoWsShipBuilder.DataElements.DataElements; | ||
|
||
namespace Test; | ||
|
||
[DataContainer] | ||
public partial record TestRecord : DataContainerBase | ||
{ | ||
} | ||
"""; | ||
|
||
var expected = """ | ||
// <auto-generated /> | ||
#nullable enable | ||
namespace Test | ||
{ | ||
public partial record TestRecord | ||
{ | ||
private void UpdateDataElements() | ||
{ | ||
this.DataElements.Clear(); | ||
} | ||
} | ||
} | ||
|
||
"""; | ||
|
||
await CreateTest(source, expected).RunAsync(); | ||
} | ||
|
||
[Test] | ||
public async Task GenerateCode_SingleKeyValueUnit_Success() | ||
{ | ||
var source = """ | ||
using WoWsShipBuilder.DataElements.DataElementAttributes; | ||
using WoWsShipBuilder.DataElements.DataElements; | ||
|
||
namespace Test; | ||
|
||
[DataContainer] | ||
public partial record TestRecord : DataContainerBase | ||
{ | ||
[DataElementType(DataElementTypes.KeyValueUnit, UnitKey = "Knots")] | ||
public decimal ManeuverabilityMaxSpeed { get; set; } | ||
} | ||
"""; | ||
|
||
var expected = """ | ||
// <auto-generated /> | ||
#nullable enable | ||
namespace Test | ||
{ | ||
public partial record TestRecord | ||
{ | ||
private void UpdateDataElements() | ||
{ | ||
this.DataElements.Clear(); | ||
if (global::WoWsShipBuilder.DataElements.DataElements.DataContainerBase.ShouldAdd(this.ManeuverabilityMaxSpeed)) | ||
{ | ||
this.DataElements.Add(new global::WoWsShipBuilder.DataElements.DataElements.KeyValueUnitDataElement("ShipStats_", this.ManeuverabilityMaxSpeed.ToString(), "Unit_Knots")); | ||
} | ||
} | ||
} | ||
} | ||
|
||
"""; | ||
|
||
await CreateTest(source, expected).RunAsync(); | ||
} | ||
|
||
[Test] | ||
public async Task GenerateCode_OneGroupTwoElements_Success() | ||
{ | ||
var source = """ | ||
using WoWsShipBuilder.DataElements.DataElementAttributes; | ||
using WoWsShipBuilder.DataElements.DataElements; | ||
|
||
namespace Test; | ||
|
||
[DataContainer] | ||
public partial record TestRecord : DataContainerBase | ||
{ | ||
[DataElementType(DataElementTypes.Grouped | DataElementTypes.KeyValue, GroupKey = "Loaders")] | ||
public string BowLoaders { get; set; } = default!; | ||
|
||
[DataElementType(DataElementTypes.Grouped | DataElementTypes.KeyValue, GroupKey = "Loaders")] | ||
public string SternLoaders { get; set; } = default!; | ||
} | ||
"""; | ||
|
||
var expected = """ | ||
// <auto-generated /> | ||
#nullable enable | ||
namespace Test | ||
{ | ||
public partial record TestRecord | ||
{ | ||
private void UpdateDataElements() | ||
{ | ||
this.DataElements.Clear(); | ||
var LoadersList = new global::System.Collections.Generic.List<global::WoWsShipBuilder.DataElements.DataElements.IDataElement>(); | ||
if (global::WoWsShipBuilder.DataElements.DataElements.DataContainerBase.ShouldAdd(this.BowLoaders)) | ||
{ | ||
LoadersList.Add(new global::WoWsShipBuilder.DataElements.DataElements.KeyValueDataElement("ShipStats_", this.BowLoaders, false, false)); | ||
} | ||
|
||
if (global::WoWsShipBuilder.DataElements.DataElements.DataContainerBase.ShouldAdd(this.SternLoaders)) | ||
{ | ||
LoadersList.Add(new global::WoWsShipBuilder.DataElements.DataElements.KeyValueDataElement("ShipStats_", this.SternLoaders, false, false)); | ||
} | ||
|
||
if (LoadersList.Count > 0) | ||
{ | ||
this.DataElements.Add(new global::WoWsShipBuilder.DataElements.DataElements.GroupedDataElement("ShipStats_Loaders", LoadersList)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
"""; | ||
|
||
await CreateTest(source, expected).RunAsync(); | ||
} | ||
} |
86 changes: 43 additions & 43 deletions
86
WoWsShipBuilder.Data.Generator.Test/SourceGeneratorResultTest.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 |
---|---|---|
@@ -1,43 +1,43 @@ | ||
using System.Linq; | ||
using FluentAssertions; | ||
using NUnit.Framework; | ||
using WoWsShipBuilder.Data.Generator.Test.TestStructures; | ||
using WoWsShipBuilder.DataElements.DataElements; | ||
|
||
namespace WoWsShipBuilder.Data.Generator.Test; | ||
|
||
public class SourceGeneratorResultTest | ||
{ | ||
[Test] | ||
public void SingleDataValue_DataElementsNotEmpty() | ||
{ | ||
const string testString = "1234test"; | ||
var testRecord = new TestDataUi1 | ||
{ | ||
TestValue = testString, | ||
}; | ||
|
||
testRecord.UpdateData(); | ||
|
||
testRecord.DataElements.Should().NotBeEmpty(); | ||
} | ||
|
||
[Test] | ||
public void GroupedValuesSet_DataElementHasGroup() | ||
{ | ||
const string testString = "1234test"; | ||
var testRecord = new TestDataUi1 | ||
{ | ||
TestGroup1 = testString, | ||
Test2Group1 = testString, | ||
}; | ||
|
||
testRecord.UpdateData(); | ||
|
||
testRecord.DataElements.Should().NotBeEmpty(); | ||
testRecord.DataElements.OfType<GroupedDataElement>().Should().HaveCount(1); | ||
var groupedData = testRecord.DataElements.OfType<GroupedDataElement>().Single(); | ||
groupedData.Key.Should().BeEquivalentTo("ShipStats_test1"); | ||
groupedData.Children.Should().HaveCount(2); | ||
} | ||
} | ||
// using System.Linq; | ||
// using FluentAssertions; | ||
// using NUnit.Framework; | ||
// using WoWsShipBuilder.Data.Generator.Test.TestStructures; | ||
// using WoWsShipBuilder.DataElements.DataElements; | ||
// | ||
// namespace WoWsShipBuilder.Data.Generator.Test; | ||
// | ||
// public class SourceGeneratorResultTest | ||
// { | ||
// [Test] | ||
// public void SingleDataValue_DataElementsNotEmpty() | ||
// { | ||
// const string testString = "1234test"; | ||
// var testRecord = new TestDataUi1 | ||
// { | ||
// TestValue = testString, | ||
// }; | ||
// | ||
// testRecord.UpdateData(); | ||
// | ||
// testRecord.DataElements.Should().NotBeEmpty(); | ||
// } | ||
// | ||
// [Test] | ||
// public void GroupedValuesSet_DataElementHasGroup() | ||
// { | ||
// const string testString = "1234test"; | ||
// var testRecord = new TestDataUi1 | ||
// { | ||
// TestGroup1 = testString, | ||
// Test2Group1 = testString, | ||
// }; | ||
// | ||
// testRecord.UpdateData(); | ||
// | ||
// testRecord.DataElements.Should().NotBeEmpty(); | ||
// testRecord.DataElements.OfType<GroupedDataElement>().Should().HaveCount(1); | ||
// var groupedData = testRecord.DataElements.OfType<GroupedDataElement>().Single(); | ||
// groupedData.Key.Should().BeEquivalentTo("ShipStats_test1"); | ||
// groupedData.Children.Should().HaveCount(2); | ||
// } | ||
// } |
Oops, something went wrong.