Beispiel #1
0
        public void GenerateCodeTests()
        {
            var codeGenerator = new TypeProviderCodeGenerator();

            codeGenerator.AddContentType("article", "Article");
            codeGenerator.AddContentType("office", "Office");

            var executingPath = AppContext.BaseDirectory;
            var expectedCode  = File.ReadAllText(executingPath + "/Assets/CustomTypeProvider_CompiledCode.txt");

            var compiledCode = codeGenerator.GenerateCode();

            Assert.Equal(expectedCode, compiledCode, ignoreWhiteSpaceDifferences: true, ignoreLineEndingDifferences: true);
        }
Beispiel #2
0
        public void IntegrationTest_GeneratedCodeCompilesWithoutErrors()
        {
            var codeGenerator = new TypeProviderCodeGenerator();

            codeGenerator.AddContentType("article", "Article");
            codeGenerator.AddContentType("office", "Office");

            var compiledCode = codeGenerator.GenerateCode();

            // Dummy implementation of Article and Office class to make compilation work
            var dummyClasses = "public class Article {} public class Office {}";

            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName: Path.GetRandomFileName(),
                syntaxTrees: new[] {
                CSharpSyntaxTree.ParseText(compiledCode),
                CSharpSyntaxTree.ParseText(dummyClasses)
            },
                references: new[] {
                MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location),
                MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("mscorlib")).Location),
                MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Runtime")).Location),
                MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("System.Linq")).Location),
                MetadataReference.CreateFromFile(typeof(Object).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Kentico.Kontent.Delivery.DeliveryClientBuilder).GetTypeInfo().Assembly.Location)
            },
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                EmitResult result            = compilation.Emit(ms);
                var        compilationErrors = $"Compilation errors:{Environment.NewLine}";

                if (!result.Success)
                {
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);

                    foreach (Diagnostic diagnostic in failures)
                    {
                        compilationErrors += String.Format($"{diagnostic.Id}: {diagnostic.GetMessage()}{Environment.NewLine}");
                    }
                }

                Assert.True(result.Success, compilationErrors);
            }
        }
Beispiel #3
0
        public void GenerateCodeTests()
        {
            var codeGenerator = new TypeProviderCodeGenerator();

            codeGenerator.AddContentType("article", "Article");
            codeGenerator.AddContentType("office", "Office");

            string compiledCode = codeGenerator.GenerateCode();

            string executingPath = AppContext.BaseDirectory;
            string expectedCode  = File.ReadAllText(executingPath + "/Assets/CustomTypeProvider_CompiledCode.txt");

            // Ignore white space
            expectedCode = Regex.Replace(expectedCode, @"\s+", "");
            compiledCode = Regex.Replace(compiledCode, @"\s+", "");

            Assert.Equal(expectedCode, compiledCode);
        }
Beispiel #4
0
        private async Task GenerateTypeProvider()
        {
            var classCodeGenerators = await GetClassCodeGenerators();

            if (!classCodeGenerators.Any())
            {
                Console.WriteLine(NoContentTypeAvailableMessage);
                return;
            }

            var typeProviderCodeGenerator = new TypeProviderCodeGenerator(Options.Namespace);

            foreach (var codeGenerator in classCodeGenerators)
            {
                typeProviderCodeGenerator.AddContentType(codeGenerator.ClassDefinition.Codename, codeGenerator.ClassDefinition.ClassName);
            }

            var typeProviderCode = typeProviderCodeGenerator.GenerateCode();

            WriteToOutputProvider(typeProviderCode, TypeProviderCodeGenerator.ClassName, true);
        }