aspnetcore/src/Components/Analyzers/test/ComponentParameterCaptureUn...

69 lines
2.5 KiB
C#

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using TestHelper;
using Xunit;
namespace Microsoft.AspNetCore.Components.Analyzers.Test
{
public class ComponentParameterCaptureUnmatchedValuesMustBeUniqueTest : DiagnosticVerifier
{
[Fact]
public void IgnoresPropertiesWithCaptureUnmatchedValuesFalse()
{
var test = $@"
namespace ConsoleApplication1
{{
using System.Collections.Generic;
using {typeof(ParameterAttribute).Namespace};
class TypeName
{{
[Parameter(CaptureUnmatchedValues = false)] string MyProperty {{ get; set; }}
[Parameter(CaptureUnmatchedValues = true)] Dictionary<string, object> MyOtherProperty {{ get; set; }}
}}
}}" + ComponentsTestDeclarations.Source;
VerifyCSharpDiagnostic(test);
}
[Fact]
public void AddsDiagnosticForMultipleCaptureUnmatchedValuesProperties()
{
var test = $@"
namespace ConsoleApplication1
{{
using System.Collections.Generic;
using {typeof(ParameterAttribute).Namespace};
class TypeName
{{
[Parameter(CaptureUnmatchedValues = true)] Dictionary<string, object> MyProperty {{ get; set; }}
[Parameter(CaptureUnmatchedValues = true)] Dictionary<string, object> MyOtherProperty {{ get; set; }}
}}
}}" + ComponentsTestDeclarations.Source;
var message = @"Component type 'ConsoleApplication1.TypeName' defines properties multiple parameters with CaptureUnmatchedValues. Properties:
ConsoleApplication1.TypeName.MyOtherProperty
ConsoleApplication1.TypeName.MyProperty";
VerifyCSharpDiagnostic(test,
new DiagnosticResult
{
Id = DiagnosticDescriptors.ComponentParameterCaptureUnmatchedValuesMustBeUnique.Id,
Message = message,
Severity = DiagnosticSeverity.Warning,
Locations = new[]
{
new DiagnosticResultLocation("Test0.cs", 6, 15)
}
});
}
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
{
return new ComponentParameterAnalyzer();
}
}
}