diff --git a/CSV Metrics Logger.sln b/CSV Metrics Logger.sln index fbc8414..ef920b5 100644 --- a/CSV Metrics Logger.sln +++ b/CSV Metrics Logger.sln @@ -4,6 +4,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSV Metrics Logger", "CSV M EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CSV Metrics Logger Generator", "CSV Metrics Logger Generator\CSV Metrics Logger Generator.csproj", "{559505B4-5322-4CFD-ABB9-D835C2A7EC09}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{871708D2-633C-4D78-86EF-A1B72043B652}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU diff --git a/Tests/TestDataOneLine.cs b/Tests/TestDataOneLine.cs new file mode 100644 index 0000000..76465a2 --- /dev/null +++ b/Tests/TestDataOneLine.cs @@ -0,0 +1,6 @@ +using CSV_Metrics_Logger; + +namespace Tests; + +[CSVRecord] +public readonly partial record struct TestDataOneLine(string Name, sbyte Age); \ No newline at end of file diff --git a/Tests/TestDataRecordStructMixed.cs b/Tests/TestDataRecordStructMixed.cs new file mode 100644 index 0000000..141a36b --- /dev/null +++ b/Tests/TestDataRecordStructMixed.cs @@ -0,0 +1,9 @@ +using CSV_Metrics_Logger; + +namespace Tests; + +[CSVRecord] +public readonly partial record struct TestDataRecordStructMixed(string Area, int Step, float Value, int Z) +{ + public bool State { get; init; } +} \ No newline at end of file diff --git a/Tests/TestDataRegularStruct.cs b/Tests/TestDataRegularStruct.cs new file mode 100644 index 0000000..3920200 --- /dev/null +++ b/Tests/TestDataRegularStruct.cs @@ -0,0 +1,13 @@ +using CSV_Metrics_Logger; + +namespace Tests; + +[CSVRecord] +public partial struct TestDataRegularStruct +{ + public string City { get; set; } + + public uint Age { get; set; } + + public double Size { get; set; } +} \ No newline at end of file diff --git a/Tests/Tests.cs b/Tests/Tests.cs new file mode 100644 index 0000000..ce84396 --- /dev/null +++ b/Tests/Tests.cs @@ -0,0 +1,84 @@ +using System.Diagnostics.CodeAnalysis; + +namespace Tests; + +[ExcludeFromCodeCoverage] +public sealed class Tests +{ + [Test] + public void TestBasicImplementation() + { + var testData = new TestDataRecordStructMixed("Area1", 1, 1.1f, 7); + Assert.Multiple(() => + { + Assert.That(typeof(TestDataRecordStructMixed).GetInterface("IConvertToCSV"), Is.Not.Null); + Assert.That(typeof(TestDataRecordStructMixed).GetProperties(), Has.Length.EqualTo(5)); + + var numberColumns = testData.GetCSVColumnCount(); + Assert.That(numberColumns, Is.EqualTo(5)); + + var header = testData.GetCSVHeaders(); + Assert.That(header, Is.EquivalentTo(new[] { "Area", "Step", "Value", "Z", "State" })); + + var dataLine = testData.ConvertToCSVDataLine(); + Assert.That(dataLine, Is.EquivalentTo(new[] { "Area1", "1", "1.1", "7", "False" })); + }); + } + + [Test] + public void TestLongText() + { + var text = """ + This is a long text that should be split into multiple lines + and should be able to be used as a multiline string + ... + and on and on... + """; + var testData = new TestDataRecordStructMixed(text, 500_000, 0.00057f, -6_087); + Assert.Multiple(() => + { + var dataLine = testData.ConvertToCSVDataLine(); + Assert.That(dataLine, Is.EquivalentTo(new[] { text, "500000", "0.00057", "-6087", "False" })); + }); + } + + [Test] + public void TestOneLineStructure() + { + var testData = new TestDataOneLine("Bob", 120); + Assert.Multiple(() => + { + var numberColumns = testData.GetCSVColumnCount(); + Assert.That(numberColumns, Is.EqualTo(2)); + + var header = testData.GetCSVHeaders(); + Assert.That(header, Is.EquivalentTo(new[] { "Name", "Age" })); + + var dataLine = testData.ConvertToCSVDataLine(); + Assert.That(dataLine, Is.EquivalentTo(new[] { "Bob", "120" })); + }); + } + + [Test] + public void TestRegularStruct() + { + var testData = new TestDataRegularStruct + { + City = "New York", + Age = 35, + Size = 1.85, + }; + + Assert.Multiple(() => + { + var numberColumns = testData.GetCSVColumnCount(); + Assert.That(numberColumns, Is.EqualTo(3)); + + var header = testData.GetCSVHeaders(); + Assert.That(header, Is.EquivalentTo(new[] { "City", "Age", "Size" })); + + var dataLine = testData.ConvertToCSVDataLine(); + Assert.That(dataLine, Is.EquivalentTo(new[] { "New York", "35", "1.85" })); + }); + } +} \ No newline at end of file diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj new file mode 100644 index 0000000..5925039 --- /dev/null +++ b/Tests/Tests.csproj @@ -0,0 +1,30 @@ + + + + net8.0 + enable + enable + CS8600;CS8602;CS8603 + + false + true + + + + + + + + + + + + + + + + + + + +