Skip to content

Commit

Permalink
Merge pull request #84 from jonsagara/feature/attrs
Browse files Browse the repository at this point in the history
Add properties to cell and column attributes to expose constructor arguments
  • Loading branch information
sveinungf authored Nov 19, 2024
2 parents bb12192 + 80fe5b0 commit d302f75
Show file tree
Hide file tree
Showing 18 changed files with 409 additions and 10 deletions.
34 changes: 34 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellFormatTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.CellFormat;
using SpreadCheetah.Styling;
using SpreadCheetah.TestHelpers.Assertions;
Expand Down Expand Up @@ -96,4 +98,36 @@ public async Task CellFormat_ClassWithMultipleCellFormats()
Assert.Equal(StandardNumberFormat.LongDate, sheet["B1"].Style.NumberFormat.StandardFormat);
Assert.Equal("#.00", sheet["C1"].Style.NumberFormat.CustomFormat);
}

[Fact]
public void CellFormat_ClassWithCellCustomFormat_CanReadCustomFormat()
{
// Arrange
var property = typeof(ClassWithCellCustomFormat).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellCustomFormat.Price), StringComparison.Ordinal));

// Act
var cellFormatAttr = property?.GetCustomAttribute<CellFormatAttribute>();

// Assert
Assert.NotNull(property);
Assert.NotNull(cellFormatAttr);
Assert.Equal("#.0#", cellFormatAttr.CustomFormat);
}

[Fact]
public void CellFormat_ClassWithCellStandardFormat_CanReadFormat()
{
// Arrange
var property = typeof(ClassWithCellStandardFormat).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellStandardFormat.Price), StringComparison.Ordinal));

// Act
var cellFormatAttr = property?.GetCustomAttribute<CellFormatAttribute>();

// Assert
Assert.NotNull(property);
Assert.NotNull(cellFormatAttr);
Assert.Equal(StandardNumberFormat.TwoDecimalPlaces, cellFormatAttr.Format);
}
}
18 changes: 18 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellStyleTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.CellStyle;
using SpreadCheetah.Styling;
using SpreadCheetah.TestHelpers.Assertions;
Expand Down Expand Up @@ -212,4 +214,20 @@ public async Task CellStyle_WorksheetRowDependencyInfoCreatedOnlyOnce()
var subsequentDependencyInfo = spreadsheet.GetOrCreateWorksheetRowDependencyInfo(typeInfo);
Assert.Same(initialDependencyInfo, subsequentDependencyInfo);
}

[Fact]
public void CellStyle_ClassWithCellStyle_CanReadStyleName()
{
// Arrange
var property = typeof(ClassWithCellStyle).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellStyle.Price), StringComparison.Ordinal));

// Act
var cellStyleAttr = property?.GetCustomAttribute<CellStyleAttribute>();

// Assert
Assert.NotNull(property);
Assert.NotNull(cellStyleAttr);
Assert.Equal("Price style", cellStyleAttr.StyleName);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Globalization;
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.CellValueConverter;
using SpreadCheetah.Styling;
using SpreadCheetah.TestHelpers.Assertions;
using System.Globalization;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;
Expand Down Expand Up @@ -107,7 +109,7 @@ public async Task CellValueConverter_ClassWithConverterAndCellStyle()
Assert.Equal("ABC123", sheet["A1"].StringValue);
Assert.True(sheet["A1"].Style.Font.Bold);
}

[Fact]
public async Task CellValueConverter_ClassWithConverterOnCustomType()
{
Expand All @@ -119,7 +121,9 @@ public async Task CellValueConverter_ClassWithConverterOnCustomType()
spreadsheet.AddStyle(style, "PercentType");
var obj = new ClassWithCellValueConverterOnCustomType
{
Property = "Abc123", ComplexProperty = null, PercentType = new Percent(123)
Property = "Abc123",
ComplexProperty = null,
PercentType = new Percent(123)
};

// Act
Expand All @@ -129,10 +133,26 @@ public async Task CellValueConverter_ClassWithConverterOnCustomType()
// Assert
using var sheet = SpreadsheetAssert.SingleSheet(stream);
Assert.Equal("Abc123", sheet["A1"].StringValue);

Assert.Equal("-", sheet["B1"].StringValue);

Assert.Equal(123, sheet["C1"].DecimalValue);
Assert.True(sheet["C1"].Style.Font.Bold);
}

[Fact]
public void CellFormat_ClassWithCellValueConverter_CanReadConverterType()
{
// Arrange
var property = typeof(ClassWithCellValueConverter).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellValueConverter.Name), StringComparison.Ordinal));

// Act
var cellValueConverterAttr = property?.GetCustomAttribute<CellValueConverterAttribute>();

// Assert
Assert.NotNull(property);
Assert.NotNull(cellValueConverterAttr);
Assert.Equal(typeof(UpperCaseValueConverter), cellValueConverterAttr.ConverterType);
}
}
25 changes: 25 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellValueTruncateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.CellValueTruncation;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;

public class CellValueTruncateTests
{
[Fact]
public void CellValueTruncate_ClassWithSingleAccessProperty_CanReadLength()
{
// Arrange
var property = typeof(ClassWithSingleAccessProperty).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSingleAccessProperty.Value), StringComparison.Ordinal));

// Act
var cellValueTruncateAttr = property?.GetCustomAttribute<CellValueTruncateAttribute>();

// Assert
Assert.NotNull(property);
Assert.NotNull(cellValueTruncateAttr);
Assert.Equal(1, cellValueTruncateAttr.Length);
}
}
126 changes: 126 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/ColumnHeaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.ColumnHeader;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;

public class ColumnHeaderTests
{
[Fact]
public void ColumnHeader_ClassWithPropertyReferenceColumnHeaders_CanReadTypeAndPropertyName()
{
// Arrange
var publicProperties = typeof(ClassWithPropertyReferenceColumnHeaders).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var firstNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithPropertyReferenceColumnHeaders.FirstName), StringComparison.Ordinal));
var lastNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithPropertyReferenceColumnHeaders.LastName), StringComparison.Ordinal));
var nationalityProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithPropertyReferenceColumnHeaders.Nationality), StringComparison.Ordinal));
var addressLine1Property = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithPropertyReferenceColumnHeaders.AddressLine1), StringComparison.Ordinal));
var addressLine2Property = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithPropertyReferenceColumnHeaders.AddressLine2), StringComparison.Ordinal));
var ageProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithPropertyReferenceColumnHeaders.Age), StringComparison.Ordinal));

// Act
var firstNameColHeaderAttr = firstNameProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var lastNameColHeaderAttr = lastNameProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var nationalityColHeaderAttr = nationalityProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var addressLine1ColHeaderAttr = addressLine1Property?.GetCustomAttribute<ColumnHeaderAttribute>();
var addressLine2ColHeaderAttr = addressLine2Property?.GetCustomAttribute<ColumnHeaderAttribute>();
var ageColHeaderAttr = ageProperty?.GetCustomAttribute<ColumnHeaderAttribute>();

// Assert
Assert.NotNull(firstNameProperty);
Assert.NotNull(firstNameColHeaderAttr);
Assert.Equal(typeof(ColumnHeaderResources), firstNameColHeaderAttr.Type);
Assert.Equal(nameof(ColumnHeaderResources.Header_FirstName), firstNameColHeaderAttr.PropertyName);

Assert.NotNull(lastNameProperty);
Assert.NotNull(lastNameColHeaderAttr);
Assert.Equal(typeof(ColumnHeaderResources), lastNameColHeaderAttr.Type);
Assert.Equal(nameof(ColumnHeaderResources.Header_LastName), lastNameColHeaderAttr.PropertyName);

Assert.NotNull(nationalityProperty);
Assert.NotNull(nationalityColHeaderAttr);
Assert.Equal(typeof(ColumnHeaders), nationalityColHeaderAttr.Type);
Assert.Equal(nameof(ColumnHeaders.HeaderNationality), nationalityColHeaderAttr.PropertyName);

Assert.NotNull(addressLine1Property);
Assert.NotNull(addressLine1ColHeaderAttr);
Assert.Equal(typeof(ColumnHeaders), addressLine1ColHeaderAttr.Type);
Assert.Equal(nameof(ColumnHeaders.HeaderAddressLine1), addressLine1ColHeaderAttr.PropertyName);

Assert.NotNull(addressLine2Property);
Assert.NotNull(addressLine2ColHeaderAttr);
Assert.Equal(typeof(ColumnHeaders), addressLine2ColHeaderAttr.Type);
Assert.Equal(nameof(ColumnHeaders.HeaderAddressLine2), addressLine2ColHeaderAttr.PropertyName);

Assert.NotNull(ageProperty);
Assert.NotNull(ageColHeaderAttr);
Assert.Equal(typeof(ColumnHeaders), ageColHeaderAttr.Type);
Assert.Equal(nameof(ColumnHeaders.HeaderAge), ageColHeaderAttr.PropertyName);
}

[Fact]
public void ColumnHeader_ClassWithSpecialCharacterColumnHeaders_CanReadName()

Check warning on line 63 in SpreadCheetah.SourceGenerator.Test/Tests/ColumnHeaderTests.cs

View workflow job for this annotation

GitHub Actions / build

Method is too long (61 lines; maximum allowed: 60) (https://github.com/meziantou/Meziantou.Analyzer/blob/main/docs/Rules/MA0051.md)
{
// Arrange
var publicProperties = typeof(ClassWithSpecialCharacterColumnHeaders).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var firstNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.FirstName), StringComparison.Ordinal));
var lastNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.LastName), StringComparison.Ordinal));
var nationalityProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.Nationality), StringComparison.Ordinal));
var addressLine1Property = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.AddressLine1), StringComparison.Ordinal));
var addressLine2Property = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.AddressLine2), StringComparison.Ordinal));
var ageProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.Age), StringComparison.Ordinal));
var noteProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.Note), StringComparison.Ordinal));
var note2Property = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithSpecialCharacterColumnHeaders.Note2), StringComparison.Ordinal));

// Act
var firstNameColHeaderAttr = firstNameProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var lastNameColHeaderAttr = lastNameProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var nationalityColHeaderAttr = nationalityProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var addressLine1ColHeaderAttr = addressLine1Property?.GetCustomAttribute<ColumnHeaderAttribute>();
var addressLine2ColHeaderAttr = addressLine2Property?.GetCustomAttribute<ColumnHeaderAttribute>();
var ageColHeaderAttr = ageProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var noteColHeaderAttr = noteProperty?.GetCustomAttribute<ColumnHeaderAttribute>();
var note2ColHeaderAttr = note2Property?.GetCustomAttribute<ColumnHeaderAttribute>();

// Assert
Assert.NotNull(firstNameProperty);
Assert.NotNull(firstNameColHeaderAttr);
Assert.Equal("First name", firstNameColHeaderAttr.Name);

Assert.NotNull(lastNameProperty);
Assert.NotNull(lastNameColHeaderAttr);
Assert.Equal("", lastNameColHeaderAttr.Name);

Assert.NotNull(nationalityProperty);
Assert.NotNull(nationalityColHeaderAttr);
Assert.Equal("Nationality (escaped characters \", \', \\)", nationalityColHeaderAttr.Name);

Assert.NotNull(addressLine1Property);
Assert.NotNull(addressLine1ColHeaderAttr);
Assert.Equal("Address line 1 (escaped characters \r\n, \t)", addressLine1ColHeaderAttr.Name);

Assert.NotNull(addressLine2Property);
Assert.NotNull(addressLine2ColHeaderAttr);
Assert.Equal(@"Address line 2 (verbatim
string: "", \)", addressLine2ColHeaderAttr.Name);

Assert.NotNull(ageProperty);
Assert.NotNull(ageColHeaderAttr);
Assert.Equal("""
Age (
raw
string
literal
)
""", ageColHeaderAttr.Name);

Assert.NotNull(noteProperty);
Assert.NotNull(noteColHeaderAttr);
Assert.Equal("Note (unicode escape sequence 🌉, \ud83d\udc4d, \xE7)", noteColHeaderAttr.Name);

Assert.NotNull(note2Property);
Assert.NotNull(note2ColHeaderAttr);
Assert.Equal($"Note 2 (constant interpolated string: This is a constant)", note2ColHeaderAttr.Name);
}
}
42 changes: 42 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/ColumnOrderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.ColumnOrdering;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;

public class ColumnOrderTests
{
[Fact]
public void ColumnOrder_ClassWithPropertyReferenceColumnHeaders_CanReadOrder()
{
// Arrange
var publicProperties = typeof(ClassWithColumnOrdering).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var firstNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithColumnOrdering.FirstName), StringComparison.Ordinal));
var lastNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithColumnOrdering.LastName), StringComparison.Ordinal));
var gpaProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithColumnOrdering.Gpa), StringComparison.Ordinal));
var ageProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithColumnOrdering.Age), StringComparison.Ordinal));

// Act
var firstNameColOrderAttr = firstNameProperty?.GetCustomAttribute<ColumnOrderAttribute>();
var lastNameColOrderAttr = lastNameProperty?.GetCustomAttribute<ColumnOrderAttribute>();
var gpaColOrderAttr = gpaProperty?.GetCustomAttribute<ColumnOrderAttribute>();
var ageColOrderAttr = ageProperty?.GetCustomAttribute<ColumnOrderAttribute>();

// Assert
Assert.NotNull(firstNameProperty);
Assert.NotNull(firstNameColOrderAttr);
Assert.Equal(2, firstNameColOrderAttr.Order);

Assert.NotNull(lastNameProperty);
Assert.NotNull(lastNameColOrderAttr);
Assert.Equal(1, lastNameColOrderAttr.Order);

Assert.NotNull(gpaProperty);
Assert.Null(gpaColOrderAttr);

Assert.NotNull(ageProperty);
Assert.NotNull(ageColOrderAttr);
Assert.Equal(3, ageColOrderAttr.Order);
}
}
25 changes: 25 additions & 0 deletions SpreadCheetah.SourceGenerator.Test/Tests/ColumnWidthTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Reflection;
using SpreadCheetah.SourceGeneration;
using SpreadCheetah.SourceGenerator.Test.Models.ColumnWidth;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;

public class ColumnWidthTests
{
[Fact]
public void ColumnWidth_ClassWithColumnWidth_CanReadOrder()
{
// Arrange
var nameProperty = typeof(ClassWithColumnWidth).GetProperties(BindingFlags.Public | BindingFlags.Instance)
.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithColumnWidth.Name), StringComparison.Ordinal));

// Act
var nameColWidthAttr = nameProperty?.GetCustomAttribute<ColumnWidthAttribute>();

// Assert
Assert.NotNull(nameProperty);
Assert.NotNull(nameColWidthAttr);
Assert.Equal(20, nameColWidthAttr.Width);
}
}
Loading

0 comments on commit d302f75

Please sign in to comment.