public void When_FoundMatchingPropertyNameWithIncorrectConverterType_ShouldReportError() { // Arrange var source = GetSourceText(new SourceGeneratorOptions( true, PropertyBuilder: builder => { builder .WriteLine("[IgnoreProperty]") .WriteLine("public long IgnoreMe { get; set; }") .WriteLine("[MapTypeConverter(typeof(Prop4Converter))]") .WriteLine("public long Prop4 { get; set; }"); }, SourcePropertyBuilder: builder => builder.WriteLine("public string Prop4 { get; set; }"))); source += @" namespace Test { using MapTo; public class Prop4Converter: ITypeConverter<string, int> { public int Convert(string source, object[] converterParameters) => int.Parse(source); } } "; // Act var(compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert var expectedError = DiagnosticsFactory.InvalidTypeConverterGenericTypesError(GetSourcePropertySymbol("Prop4", compilation), GetSourcePropertySymbol("Prop4", compilation, "Baz")); diagnostics.ShouldNotBeSuccessful(expectedError); }
public void When_MapToAttributeFoundWithoutMatchingProperties_Should_ReportError() { // Arrange const string source = @" using MapTo; namespace Test { [MapFrom(typeof(Baz))] public partial class Foo { } public class Baz { public int Prop1 { get; set; } } } "; // Act var(compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source); // Assert var fooType = compilation.GetTypeByMetadataName("Test.Foo"); fooType.ShouldNotBeNull(); var bazType = compilation.GetTypeByMetadataName("Test.Baz"); bazType.ShouldNotBeNull(); var expectedDiagnostic = DiagnosticsFactory.NoMatchingPropertyFoundError(fooType.Locations.Single(), fooType, bazType); var error = diagnostics.FirstOrDefault(d => d.Id == expectedDiagnostic.Id); error.ShouldNotBeNull(); }
public void When_FoundMatchingPropertyNameWithDifferentTypes_Should_ReportError() { // Arrange var source = GetSourceText(new SourceGeneratorOptions( true, PropertyBuilder: builder => { builder.WriteLine("public string Prop4 { get; set; }"); }, SourcePropertyBuilder: builder => builder.WriteLine("public int Prop4 { get; set; }"))); // Act var(compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions); // Assert var expectedError = DiagnosticsFactory.NoMatchingPropertyTypeFoundError(GetSourcePropertySymbol("Prop4", compilation)); diagnostics.ShouldNotBeSuccessful(expectedError); }
public void When_SecondaryConstructorExistsButDoNotReferencePrivateConstructor_Should_ReportError(string source, LanguageVersion languageVersion) { // Arrange source = source.Trim(); // Act var(compilation, diagnostics) = CSharpGenerator.GetOutputCompilation(source, analyzerConfigOptions: DefaultAnalyzerOptions, languageVersion: languageVersion); // Assert var constructorSyntax = compilation.SyntaxTrees .First() .GetRoot() .DescendantNodes() .OfType <ConstructorDeclarationSyntax>() .Single(); diagnostics.ShouldNotBeSuccessful(DiagnosticsFactory.MissingConstructorArgument(constructorSyntax)); }
internal static T GetBuildGlobalOption <T>(this GeneratorExecutionContext context, string propertyName, T defaultValue = default !) where T : notnull { if (!context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(GetBuildPropertyName(propertyName), out var optionValue) || string.IsNullOrWhiteSpace(optionValue)) { return(defaultValue); } var type = typeof(T); if (!type.IsEnum) { return((T)Convert.ChangeType(optionValue, type)); } try { return((T)Enum.Parse(type, optionValue, true)); } catch (Exception) { context.ReportDiagnostic(DiagnosticsFactory.ConfigurationParseError($"'{optionValue}' is not a valid value for {PropertyNameSuffix}{propertyName} property.")); return(defaultValue); } }