Skip to content

Commit

Permalink
Tests for diagnostics SPCH1008 and SPCH1009
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinungf committed Sep 7, 2024
1 parent c630ed1 commit 115ce1b
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Diagnostics: [
{
Id: SPCH1008,
Title: Attribute combination not supported,
Severity: Error,
WarningLevel: 0,
Location: : (7,19)-(7,27),
MessageFormat: Having both the {0} and the {1} attributes on a property is not supported,
Message: Having both the CellValueConverter and the CellValueTruncate attributes on a property is not supported,
Category: SpreadCheetah.SourceGenerator
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
Diagnostics: [
{
Id: SPCH1009,
Title: Type must have a public parameterless constructor,
Severity: Error,
WarningLevel: 0,
Location: : (5,5)-(5,52),
MessageFormat: Type '{0}' must have a public parameterless constructor,
Message: Type 'MyNamespace.FixedValueConverter' must have a public parameterless constructor,
Category: SpreadCheetah.SourceGenerator
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,59 @@ public partial class MyGenRowContext : WorksheetRowContext;
// Act & Assert
return TestHelper.CompileAndVerify<WorksheetRowGenerator>(source);
}

[Fact]
public Task CellValueConverter_ClassPropertyWithConverterAndCellValueTruncate()
{
// Arrange
const string source = """
using SpreadCheetah.SourceGeneration;

namespace MyNamespace;
public class ClassPropertyWithConverterAndCellValueTruncate
{
[CellValueConverter(typeof(StringValueConverter))]
[CellValueTruncate(20)]
public string? Property { get; set; }
}

internal class StringValueConverter : CellValueConverter<string>
{
public override DataCell ConvertToCell(string value) => new(value);
}

[WorksheetRow(typeof(ClassPropertyWithConverterAndCellValueTruncate))]
public partial class MyGenRowContext : WorksheetRowContext;
""";

// Act & Assert
return TestHelper.CompileAndVerify<WorksheetRowGenerator>(source, onlyDiagnostics: true);
}

[Fact]
public Task CellValueConverter_ClassWithoutParameterlessConstructor()
{
// Arrange
const string source = """
using SpreadCheetah.SourceGeneration;

namespace MyNamespace;
public class ClassWithoutParameterlessConstructor
{
[CellValueConverter(typeof(FixedValueConverter))]
public string? Value { get; set; }
}

internal class FixedValueConverter(string fixedValue) : CellValueConverter<string>
{
public override DataCell ConvertToCell(string value) => new(fixedValue);
}

[WorksheetRow(typeof(ClassWithoutParameterlessConstructor))]
public partial class MyGenRowContext : WorksheetRowContext;
""";

// Act & Assert
return TestHelper.CompileAndVerify<WorksheetRowGenerator>(source, onlyDiagnostics: true);
}
}
2 changes: 1 addition & 1 deletion SpreadCheetah.SourceGenerator/Diagnostics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static DiagnosticInfo AttributeCombinationNotSupported(LocationInfo? loca
private static readonly DiagnosticDescriptor AttributeCombinationNotSupportedDescriptor = new(
id: "SPCH1008",
title: "Attribute combination not supported",
messageFormat: "Having both {0} and {1} on a property is not supported",
messageFormat: "Having both the {0} and the {1} attributes on a property is not supported",
category: Category,
defaultSeverity: DiagnosticSeverity.Error,
isEnabledByDefault: true);
Expand Down
2 changes: 1 addition & 1 deletion SpreadCheetah.SourceGenerator/WorksheetRowGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private static RowType AnalyzeTypeProperties(ITypeSymbol classType, LocationInfo
if (data is { CellValueConverter: not null, CellValueTruncate: not null })
{
var location = property.Locations.FirstOrDefault()?.ToLocationInfo();
diagnosticInfos.Add(Diagnostics.AttributeCombinationNotSupported(location, "CellValueConverterAttribute", "CellValueTruncateAttribute"));
diagnosticInfos.Add(Diagnostics.AttributeCombinationNotSupported(location, "CellValueConverter", "CellValueTruncate"));
}

if (data.ColumnOrder is not { } order)
Expand Down

0 comments on commit 115ce1b

Please sign in to comment.