Skip to content

Commit

Permalink
Reduced the overlap in tests for the new column/cell attribute proper…
Browse files Browse the repository at this point in the history
…ties.
  • Loading branch information
jonsagara committed Nov 18, 2024
1 parent 85c007d commit 80fe5b0
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 420 deletions.
44 changes: 0 additions & 44 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellFormatTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,48 +130,4 @@ public void CellFormat_ClassWithCellStandardFormat_CanReadFormat()
Assert.NotNull(cellFormatAttr);
Assert.Equal(StandardNumberFormat.TwoDecimalPlaces, cellFormatAttr.Format);
}

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

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

// Assert
Assert.NotNull(property);
Assert.NotNull(cellFormatAttr);
Assert.Equal(StandardNumberFormat.DateAndTime, cellFormatAttr.Format);
}

[Fact]
public void CellFormat_ClassWithMultipleCellFormats_CanReadFormat()
{
// Arrange
var publicProperties = typeof(ClassWithMultipleCellFormats).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var idProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellFormats.Id), StringComparison.Ordinal));
var fromDateProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellFormats.FromDate), StringComparison.Ordinal));
var priceProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellFormats.Price), StringComparison.Ordinal));

// Act
var idCellFormatAttr = idProperty?.GetCustomAttribute<CellFormatAttribute>();
var fromDateCellFormatAttr = fromDateProperty?.GetCustomAttribute<CellFormatAttribute>();
var priceCellFormatAttr = priceProperty?.GetCustomAttribute<CellFormatAttribute>();

// Assert
Assert.NotNull(idProperty);
Assert.NotNull(idCellFormatAttr);
Assert.Equal("#.00", idCellFormatAttr.CustomFormat);

Assert.NotNull(fromDateProperty);
Assert.NotNull(fromDateCellFormatAttr);
Assert.Equal(StandardNumberFormat.LongDate, fromDateCellFormatAttr.Format);

Assert.NotNull(priceProperty);
Assert.NotNull(priceCellFormatAttr);
Assert.Equal("#.00", priceCellFormatAttr.CustomFormat);
}
}
81 changes: 0 additions & 81 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellStyleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,85 +230,4 @@ public void CellStyle_ClassWithCellStyle_CanReadStyleName()
Assert.NotNull(cellStyleAttr);
Assert.Equal("Price style", cellStyleAttr.StyleName);
}

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

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

// Assert
Assert.NotNull(property);
Assert.NotNull(cellStyleAttr);
Assert.Equal("Created style", cellStyleAttr.StyleName);
}

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

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

// Assert
Assert.NotNull(property);
Assert.NotNull(cellStyleAttr);
Assert.Equal("Name style", cellStyleAttr.StyleName);
}

[Fact]
public void CellStyle_ClassWithMultipleCellStyles_CanReadStyleName()
{
// Arrange
var publicProperties = typeof(ClassWithMultipleCellStyles).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var firstNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellStyles.FirstName), StringComparison.Ordinal));
var middleNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellStyles.MiddleName), StringComparison.Ordinal));
var lastNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellStyles.LastName), StringComparison.Ordinal));
var ageProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithMultipleCellStyles.Age), StringComparison.Ordinal));

// Act
var firstNameCellStyleAttr = firstNameProperty?.GetCustomAttribute<CellStyleAttribute>();
var middleNameCellStyleAttr = middleNameProperty?.GetCustomAttribute<CellStyleAttribute>();
var lastNameCellStyleAttr = lastNameProperty?.GetCustomAttribute<CellStyleAttribute>();
var ageCellStyleAttr = ageProperty?.GetCustomAttribute<CellStyleAttribute>();

// Assert
Assert.NotNull(firstNameProperty);
Assert.NotNull(firstNameCellStyleAttr);
Assert.Equal("Name", firstNameCellStyleAttr.StyleName);

Assert.NotNull(middleNameProperty);
Assert.Null(middleNameCellStyleAttr);

Assert.NotNull(lastNameProperty);
Assert.NotNull(lastNameCellStyleAttr);
Assert.Equal("Name", lastNameCellStyleAttr.StyleName);

Assert.NotNull(ageProperty);
Assert.NotNull(ageCellStyleAttr);
Assert.Equal("Age", ageCellStyleAttr.StyleName);
}

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

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

// Assert
Assert.NotNull(property);
Assert.NotNull(cellStyleAttr);
Assert.Equal("Name style", cellStyleAttr.StyleName);
}
}
121 changes: 8 additions & 113 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellValueConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +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 System.Reflection;
using Xunit;

namespace SpreadCheetah.SourceGenerator.Test.Tests;
Expand Down Expand Up @@ -109,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 @@ -121,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 @@ -131,9 +133,9 @@ 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);
}
Expand All @@ -153,111 +155,4 @@ public void CellFormat_ClassWithCellValueConverter_CanReadConverterType()
Assert.NotNull(cellValueConverterAttr);
Assert.Equal(typeof(UpperCaseValueConverter), cellValueConverterAttr.ConverterType);
}

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

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

// Assert
Assert.NotNull(property);
Assert.NotNull(cellValueConverterAttr);
Assert.Equal(typeof(UpperCaseValueConverter), cellValueConverterAttr.ConverterType);
}

[Fact]
public void CellFormat_ClassWithCellValueConverterOnCustomType_CanReadConverterType()
{
// Arrange
var publicProperties = typeof(ClassWithCellValueConverterOnCustomType).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var property = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellValueConverterOnCustomType.Property), StringComparison.Ordinal));
var complexProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellValueConverterOnCustomType.ComplexProperty), StringComparison.Ordinal));
var percentTypeProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithCellValueConverterOnCustomType.PercentType), StringComparison.Ordinal));

// Act
var propertyCellValueConverterAttr = property?.GetCustomAttribute<CellValueConverterAttribute>();
var complexPropertyCellValueConverterAttr = complexProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var percentTypePropertyCellValueConverterAttr = percentTypeProperty?.GetCustomAttribute<CellValueConverterAttribute>();

// Assert
Assert.NotNull(property);
Assert.Null(propertyCellValueConverterAttr);

Assert.NotNull(complexProperty);
Assert.NotNull(complexPropertyCellValueConverterAttr);
Assert.Equal(typeof(NullToDashValueConverter<object?>), complexPropertyCellValueConverterAttr.ConverterType);

Assert.NotNull(property);
Assert.NotNull(percentTypePropertyCellValueConverterAttr);
Assert.Equal(typeof(PercentToNumberConverter), percentTypePropertyCellValueConverterAttr.ConverterType);
}

[Fact]
public void CellFormat_ClassWithGenericConverter_CanReadConverterType()
{
// Arrange
var publicProperties = typeof(ClassWithGenericConverter).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var firstNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithGenericConverter.FirstName), StringComparison.Ordinal));
var middleNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithGenericConverter.MiddleName), StringComparison.Ordinal));
var lastNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithGenericConverter.LastName), StringComparison.Ordinal));
var gpaProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithGenericConverter.Gpa), StringComparison.Ordinal));

// Act
var firstNamePropertyCellValueConverterAttr = firstNameProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var middleNamePropertyCellValueConverterAttr = middleNameProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var lastNamePropertyCellValueConverterAttr = lastNameProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var gpaCellValueConverterAttr = gpaProperty?.GetCustomAttribute<CellValueConverterAttribute>();

// Assert
Assert.NotNull(firstNameProperty);
Assert.Null(firstNamePropertyCellValueConverterAttr);

Assert.NotNull(middleNameProperty);
Assert.NotNull(middleNamePropertyCellValueConverterAttr);
Assert.Equal(typeof(NullToDashValueConverter<string>), middleNamePropertyCellValueConverterAttr.ConverterType);

Assert.NotNull(lastNameProperty);
Assert.Null(lastNamePropertyCellValueConverterAttr);

Assert.NotNull(gpaProperty);
Assert.NotNull(gpaCellValueConverterAttr);
Assert.Equal(typeof(NullToDashValueConverter<decimal?>), gpaCellValueConverterAttr.ConverterType);
}

[Fact]
public void CellFormat_ClassWithReusedConverter_CanReadConverterType()
{
// Arrange
var publicProperties = typeof(ClassWithReusedConverter).GetProperties(BindingFlags.Public | BindingFlags.Instance);
var firstNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithReusedConverter.FirstName), StringComparison.Ordinal));
var middleNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithReusedConverter.MiddleName), StringComparison.Ordinal));
var lastNameProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithReusedConverter.LastName), StringComparison.Ordinal));
var gpaProperty = publicProperties.SingleOrDefault(p => string.Equals(p.Name, nameof(ClassWithReusedConverter.Gpa), StringComparison.Ordinal));

// Act
var firstNamePropertyCellValueConverterAttr = firstNameProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var middleNamePropertyCellValueConverterAttr = middleNameProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var lastNamePropertyCellValueConverterAttr = lastNameProperty?.GetCustomAttribute<CellValueConverterAttribute>();
var gpaCellValueConverterAttr = gpaProperty?.GetCustomAttribute<CellValueConverterAttribute>();

// Assert
Assert.NotNull(firstNameProperty);
Assert.NotNull(firstNamePropertyCellValueConverterAttr);
Assert.Equal(typeof(UpperCaseValueConverter), firstNamePropertyCellValueConverterAttr.ConverterType);

Assert.NotNull(middleNameProperty);
Assert.Null(middleNamePropertyCellValueConverterAttr);

Assert.NotNull(lastNameProperty);
Assert.NotNull(lastNamePropertyCellValueConverterAttr);
Assert.Equal(typeof(UpperCaseValueConverter), lastNamePropertyCellValueConverterAttr.ConverterType);

Assert.NotNull(gpaProperty);
Assert.Null(gpaCellValueConverterAttr);
}
}
16 changes: 0 additions & 16 deletions SpreadCheetah.SourceGenerator.Test/Tests/CellValueTruncateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,4 @@ public void CellValueTruncate_ClassWithSingleAccessProperty_CanReadLength()
Assert.NotNull(cellValueTruncateAttr);
Assert.Equal(1, cellValueTruncateAttr.Length);
}

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

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

// Assert
Assert.NotNull(property);
Assert.NotNull(cellValueTruncateAttr);
Assert.Equal(15, cellValueTruncateAttr.Length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void ColumnHeader_ClassWithPropertyReferenceColumnHeaders_CanReadTypeAndP
}

[Fact]
public void ColumnHeader_ClassWithSpecialCharacterColumnHeaders_CanReadTypeAndPropertyName()
public void ColumnHeader_ClassWithSpecialCharacterColumnHeaders_CanReadName()
{
// Arrange
var publicProperties = typeof(ClassWithSpecialCharacterColumnHeaders).GetProperties(BindingFlags.Public | BindingFlags.Instance);
Expand Down
Loading

0 comments on commit 80fe5b0

Please sign in to comment.