示例#1
0
        public async Task GetName_ReturnsFirstName_IfMultipleAttributesAreSpecified()
        {
            var methodName  = nameof(GetNameTests.MultipleAttributes);
            var compilation = await GetCompilationForGetName();

            Assert.True(TopLevelParameterNameAnalyzer.SymbolCache.TryCreate(compilation, out var symbolCache));

            var type   = compilation.GetTypeByMetadataName(typeof(GetNameTests).FullName);
            var method = (IMethodSymbol)type.GetMembers(methodName).First();

            var parameter = method.Parameters[0];
            var name      = TopLevelParameterNameAnalyzer.GetName(symbolCache, parameter);

            Assert.Equal("name1", name);
        }
示例#2
0
        public async Task GetName_ReturnsValueFromFirstAttributeWithValue()
        {
            var methodName  = nameof(GetNameTests.SingleAttribute);
            var compilation = await GetCompilationForGetName();

            Assert.True(TopLevelParameterNameAnalyzer.SymbolCache.TryCreate(compilation, out var symbolCache));

            var type   = compilation.GetTypeByMetadataName(typeof(GetNameTests).FullName);
            var method = (IMethodSymbol)type.GetMembers(methodName).First();

            var parameter = method.Parameters[0];
            var name      = TopLevelParameterNameAnalyzer.GetName(symbolCache, parameter);

            Assert.Equal("testModelName", name);
        }
示例#3
0
        public async Task GetName_ReturnsName_IfAttributeDoesNotSpecifyName()
        {
            var methodName  = nameof(GetNameTests.SingleAttributeWithoutName);
            var compilation = await GetCompilationForGetName();

            var symbolCache = new TopLevelParameterNameAnalyzer.SymbolCache(compilation);

            var type   = compilation.GetTypeByMetadataName(typeof(GetNameTests).FullName);
            var method = (IMethodSymbol)type.GetMembers(methodName).First();

            var parameter = method.Parameters[0];
            var name      = TopLevelParameterNameAnalyzer.GetName(symbolCache, parameter);

            Assert.Equal("param", name);
        }
示例#4
0
        private async Task <bool> IsProblematicParameterTest([CallerMemberName] string testMethod = "")
        {
            var testSource = MvcTestSource.Read(GetType().Name, testMethod);
            var project    = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source });

            var compilation = await project.GetCompilationAsync();

            var modelType = compilation.GetTypeByMetadataName($"Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzerTestFiles.{testMethod}");
            var method    = (IMethodSymbol)modelType.GetMembers("ActionMethod").First();
            var parameter = method.Parameters[0];

            Assert.True(TopLevelParameterNameAnalyzer.SymbolCache.TryCreate(compilation, out var symbolCache));

            var result = TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, parameter);

            return(result);
        }
示例#5
0
        public async Task SpecifiesModelType_ReturnsTrue_IfModelBinderSpecifiesTypeFromProperty()
        {
            var testMethod = nameof(SpecifiesModelType_ReturnsTrue_IfModelBinderSpecifiesTypeFromProperty);
            var testSource = MvcTestSource.Read(GetType().Name, "SpecifiesModelTypeTests");
            var project    = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source });

            var compilation = await project.GetCompilationAsync();

            Assert.True(TopLevelParameterNameAnalyzer.SymbolCache.TryCreate(compilation, out var symbolCache));

            var type   = compilation.GetTypeByMetadataName(typeof(SpecifiesModelTypeTests).FullName);
            var method = (IMethodSymbol)type.GetMembers(testMethod).First();

            var parameter = method.Parameters[0];
            var result    = TopLevelParameterNameAnalyzer.SpecifiesModelType(symbolCache, parameter);

            Assert.True(result);
        }
示例#6
0
        public async Task IsProblematicParameter_ReturnsFalse_ForSimpleTypes()
        {
            var testName   = nameof(IsProblematicParameter_ReturnsFalse_ForSimpleTypes);
            var testSource = MvcTestSource.Read(GetType().Name, testName);
            var project    = DiagnosticProject.Create(GetType().Assembly, new[] { testSource.Source });

            var compilation = await project.GetCompilationAsync();

            var modelType = compilation.GetTypeByMetadataName($"Microsoft.AspNetCore.Mvc.Analyzers.TopLevelParameterNameAnalyzerTestFiles.{testName}");
            var method    = (IMethodSymbol)modelType.GetMembers("ActionMethod").First();

            Assert.True(TopLevelParameterNameAnalyzer.SymbolCache.TryCreate(compilation, out var symbolCache));

            Assert.Collection(
                method.Parameters,
                p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)),
                p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)),
                p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)),
                p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)),
                p => Assert.False(TopLevelParameterNameAnalyzer.IsProblematicParameter(symbolCache, p)));
        }