Esempio n. 1
0
        public async Task RoslynWith_EFScenarioBad()
        {
            // Arrange
            var projectFilePath = @"..\..\..\EFScenario.Bad\EFScenario.Bad.csproj";

            var compilation = await LoadProjectAndGetCompilationAsync(projectFilePath);

            INamedTypeSymbol dbContextTypeSymbol;
            var entityTypeSymbols = SymbolHelper.GetAllEntityTypesFromDbContext(compilation, out dbContextTypeSymbol);

            // Act
            var efRoslynTheorem        = new EFRoslynTheorem();
            ObjectTheoremResult solved = efRoslynTheorem.Solve(entityTypeSymbols);

            // Assert
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Unsatisfiable, solved.Status);

            foreach (var assumes in efRoslynTheorem.ClassAssumptions)
            {
                efRoslynTheorem.RemoveAssumption(assumes.Assumption);
            }

            foreach (var assumes in efRoslynTheorem.PropertyAssumptions)
            {
                efRoslynTheorem.RemoveAssumption(assumes.Assumption);
            }

            solved = efRoslynTheorem.ReSolve();
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }
        private void AnalyzeCompilation(CompilationAnalysisContext context)
        {
            Trace.WriteLine("AnalyzeCompilation");

            try
            {
                INamedTypeSymbol dbContextTypeSymbol;
                IEnumerable <INamedTypeSymbol> entityTypeSymbols = SymbolHelper.GetAllEntityTypesFromDbContext(context.Compilation, out dbContextTypeSymbol);
                if (!entityTypeSymbols.Any())
                {
                    return;
                }

                var allTypeSymbols   = context.Compilation.GetSymbolsWithName(s => !s.EndsWith("DbContext"), SymbolFilter.Type).Cast <INamedTypeSymbol>();
                var allMemberSymbols = allTypeSymbols.SelectMany(t => t.GetMembers().Where(m => m.Kind == SymbolKind.Property));
                Trace.WriteLine("Class count: " + allTypeSymbols.Count());
                Trace.WriteLine("Property count: " + allMemberSymbols.Count());

                var efRoslynTheorem = new EFRoslynTheorem();
                var result          = efRoslynTheorem.Solve(entityTypeSymbols);
                if (result.Status == Status.Unsatisfiable)
                {
                    var classAssumptions    = efRoslynTheorem.ClassAssumptions.ToList();
                    var propertyAssumptions = efRoslynTheorem.PropertyAssumptions.OrderBy(pa => pa.Rank).ToList();

                    do
                    {
                        result = TryToRemoveWrongAssumption(efRoslynTheorem, result, classAssumptions, propertyAssumptions);
                    } while (result != null && result.Status != Status.Satisfiable);

                    if (result == null || result.Status != Status.Satisfiable)
                    {
                        var diagnostic2 = Diagnostic.Create(UnsatisfiableRule, dbContextTypeSymbol.Locations[0], dbContextTypeSymbol.Name);
                        Trace.WriteLine("ReportDiagnostic " + diagnostic2.Descriptor.Id);
                        context.ReportDiagnostic(diagnostic2);
                        return;
                    }

                    var cacheId = EFRoslynTheoremCache.Add(context.Compilation, efRoslynTheorem, result);

                    var props = ImmutableDictionary.Create <string, string>();
                    props = props.Add("CacheId", cacheId);

                    var diagnostic = Diagnostic.Create(SatisfiableRule, dbContextTypeSymbol.Locations[0], props, dbContextTypeSymbol.Name);
                    Trace.WriteLine("ReportDiagnostic " + diagnostic.Descriptor.Id);
                    context.ReportDiagnostic(diagnostic);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
                throw;
            }
        }
Esempio n. 3
0
        public async Task RoslynWith_EFScenarioGood()
        {
            // Arrange
            var projectFilePath = @"..\..\..\EFScenario.Good\EFScenario.Good.csproj";

            var compilation = await LoadProjectAndGetCompilationAsync(projectFilePath);

            INamedTypeSymbol dbContextTypeSymbol;
            var entityTypeSymbols = SymbolHelper.GetAllEntityTypesFromDbContext(compilation, out dbContextTypeSymbol);

            // Act
            var efRoslynTheorem        = new EFRoslynTheorem();
            ObjectTheoremResult solved = efRoslynTheorem.Solve(entityTypeSymbols);

            // Assert
            Assert.IsNotNull(solved);
            Assert.AreEqual(Status.Satisfiable, solved.Status);
        }