Ejemplo n.º 1
0
        public void TryEditStartupForNewContext_Adds_Context_Registration_To_ConfigureServices(string beforeStartupResource, string afterStartupResource, string dbContextResource)
        {
            string resourcePrefix = "compiler/resources/";

            var beforeStartupText = ResourceUtilities.GetEmbeddedResourceFileContent(resourcePrefix + beforeStartupResource);
            var afterStartupText  = ResourceUtilities.GetEmbeddedResourceFileContent(resourcePrefix + afterStartupResource);
            var dbContextText     = ResourceUtilities.GetEmbeddedResourceFileContent(resourcePrefix + dbContextResource);
            var startupTree       = CSharpSyntaxTree.ParseText(beforeStartupText);
            var contextTree       = CSharpSyntaxTree.ParseText(dbContextText);
            var testAssembly      =
                Assembly.Load(new AssemblyName("Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.Test"));
            var configAssembly =
                Assembly.Load(new AssemblyName("Microsoft.Extensions.Configuration.Abstractions"));
            var efReference = MetadataReference.CreateFromFile(testAssembly.Location);

            var configReference = MetadataReference.CreateFromFile(configAssembly.Location);


            var compilation = CSharpCompilation.Create("DoesNotMatter", new[] { startupTree, contextTree }, new[] { efReference, configReference });

            DbContextEditorServices testObj = GetTestObject();

            var types       = RoslynUtilities.GetDirectTypesInCompilation(compilation);
            var startupType = ModelType.FromITypeSymbol(types.Where(ts => ts.Name == "Startup").First());
            var contextType = ModelType.FromITypeSymbol(types.Where(ts => ts.Name == "MyContext").First());

            var result = testObj.EditStartupForNewContext(startupType, "MyContext", "ContextNamespace", "MyContext-NewGuid");

            Assert.True(result.Edited);
            Assert.Equal(afterStartupText, result.NewTree.GetText().ToString(), ignoreCase: false, ignoreLineEndingDifferences: true);
        }
Ejemplo n.º 2
0
        public void AddModelToContext_Adds_Model_From_Same_Project_To_Context(string beforeContextResource, string modelResource, string afterContextResource)
        {
            string resourcePrefix = "compiler/resources/";

            var beforeDbContextText = ResourceUtilities.GetEmbeddedResourceFileContent(resourcePrefix + beforeContextResource);
            var modelText           = ResourceUtilities.GetEmbeddedResourceFileContent(resourcePrefix + modelResource);
            var afterDbContextText  = ResourceUtilities.GetEmbeddedResourceFileContent(resourcePrefix + afterContextResource);

            var contextTree = CSharpSyntaxTree.ParseText(beforeDbContextText);
            var modelTree   = CSharpSyntaxTree.ParseText(modelText);
            var efReference = MetadataReference.CreateFromFile(Assembly.GetEntryAssembly().Location);

            var compilation = CSharpCompilation.Create("DoesNotMatter", new[] { contextTree, modelTree }, new[] { efReference });

            DbContextEditorServices testObj = GetTestObject();

            var types       = RoslynUtilities.GetDirectTypesInCompilation(compilation);
            var modelType   = ModelType.FromITypeSymbol(types.Where(ts => ts.Name == "MyModel").First());
            var contextType = ModelType.FromITypeSymbol(types.Where(ts => ts.Name == "MyContext").First());

            var result = testObj.AddModelToContext(contextType, modelType);

            Assert.True(result.Edited);

            Assert.Equal(afterDbContextText, result.NewTree.GetText().ToString(), ignoreCase: false, ignoreLineEndingDifferences: true);
        }
Ejemplo n.º 3
0
 public IEnumerable <ModelType> GetAllTypes()
 {
     return(_projectWorkspace.CurrentSolution.Projects
            .Select(project => project.GetCompilationAsync().Result)
            .Select(comp => RoslynUtilities.GetDirectTypesInCompilation(comp))
            .Aggregate((col1, col2) => col1.Concat(col2).ToList())
            .Select(ts => ModelType.FromITypeSymbol(ts)));
 }