Пример #1
0
        public async Task GetName_ReturnsValueFromFirstAttributeWithValue()
        {
            var methodName  = nameof(GetNameTests.SingleAttribute);
            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("testModelName", name);
        }
Пример #2
0
        public async Task GetName_ReturnsFirstName_IfMultipleAttributesAreSpecified()
        {
            var methodName  = nameof(GetNameTests.MultipleAttributes);
            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("name1", name);
        }
Пример #3
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];

            var symbolCache = new TopLevelParameterNameAnalyzer.SymbolCache(compilation);

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

            return(result);
        }
Пример #4
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();

            var symbolCache = new TopLevelParameterNameAnalyzer.SymbolCache(compilation);

            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);
        }