public void TestSeparateFullKeyProviderBothPrefixes()
        {
            var directory    = Path.Combine("output", "FileOutputWriterTests", "SeparateFullKeyProviderBothPrefixes");
            var outputWriter = new FileWatcherOutputWriter(directory);

            Compiler.Generate(
                "SeparateFullKeyProviderBothPrefixes",
                PrefixPattern,
                new Generator
            {
                OutputWriter      = outputWriter,
                EnableDataBinding = true,
                SeparateClasses   = true,
                NamespacePrefix   = "Generator.Prefix",
                NamespaceProvider = new Dictionary <NamespaceKey, string>
                {
                    { new NamespaceKey("http://tempuri.org/PurchaseOrderSchema.xsd"), "NamedNamespace" },
                }.ToNamespaceProvider(new GeneratorConfiguration {
                    NamespacePrefix = "GeneratorConfiguration.Prefix"
                }.NamespaceProvider.GenerateNamespace),
            });

            SharedTestFunctions.TestSamples(_output, "SeparateFullKeyProviderBothPrefixes", PrefixPattern);
            Assert.Equal(2, outputWriter.Files.Count());
            Assert.Equal(Path.Combine(directory, "NamedNamespace", "PurchaseOrderType.cs"), outputWriter.Files.First());
        }
        public void TestEmptyKeyProviderBothPrefixes()
        {
            var directory    = Path.Combine("output", "FileOutputWriterTests", "EmptyKeyProviderBothPrefixes");
            var outputWriter = new FileWatcherOutputWriter(directory);

            Compiler.Generate(
                "EmptyKeyProviderBothPrefixes",
                PrefixPattern,
                new Generator
            {
                OutputWriter      = outputWriter,
                EnableDataBinding = true,
                NamespacePrefix   = "Generator.Prefix",
                NamespaceProvider = new Dictionary <NamespaceKey, string>
                {
                    { new NamespaceKey(String.Empty), "NamedNamespace" },
                }.ToNamespaceProvider(new GeneratorConfiguration {
                    NamespacePrefix = "GeneratorConfiguration.Prefix"
                }.NamespaceProvider.GenerateNamespace),
            });

            SharedTestFunctions.TestSamples(_output, "EmptyKeyProviderBothPrefixes", PrefixPattern);
            Assert.Single(outputWriter.Files);
            Assert.Equal(Path.Combine(directory, "NamedNamespace.cs"), outputWriter.Files.First());
        }
Example #3
0
        public static Assembly GenerateFiles(string name, IEnumerable <string> files, Generator generatorPrototype = null)
        {
            if (Assemblies.ContainsKey(name))
            {
                return(Assemblies[name]);
            }

            generatorPrototype ??= new Generator
            {
                GenerateNullables  = true,
                IntegerDataType    = typeof(int),
                DataAnnotationMode = DataAnnotationMode.All,
                GenerateDesignerCategoryAttribute  = false,
                GenerateComplexTypesForCollections = true,
                EntityFramework              = false,
                GenerateInterfaces           = true,
                NamespacePrefix              = name,
                GenerateDescriptionAttribute = true,
                TextValuePropertyName        = "Value"
            };

            var output = new FileWatcherOutputWriter(Path.Combine("output", name));

            var gen = new Generator
            {
                OutputWriter                       = output,
                NamespaceProvider                  = generatorPrototype.NamespaceProvider,
                GenerateNullables                  = generatorPrototype.GenerateNullables,
                UseShouldSerializePattern          = generatorPrototype.UseShouldSerializePattern,
                IntegerDataType                    = generatorPrototype.IntegerDataType,
                DataAnnotationMode                 = generatorPrototype.DataAnnotationMode,
                GenerateDesignerCategoryAttribute  = generatorPrototype.GenerateDesignerCategoryAttribute,
                GenerateComplexTypesForCollections = generatorPrototype.GenerateComplexTypesForCollections,
                EntityFramework                    = generatorPrototype.EntityFramework,
                GenerateInterfaces                 = generatorPrototype.GenerateInterfaces,
                MemberVisitor                      = generatorPrototype.MemberVisitor,
                GenerateDescriptionAttribute       = generatorPrototype.GenerateDescriptionAttribute,
                CodeTypeReferenceOptions           = generatorPrototype.CodeTypeReferenceOptions,
                TextValuePropertyName              = generatorPrototype.TextValuePropertyName,
                EmitOrder = generatorPrototype.EmitOrder
            };

            gen.Generate(files);

            return(CompileFiles(name, output.Files));
        }
        public void TestDefaultProvider_ThrowsArgumentException()
        {
            var outputWriter = new FileWatcherOutputWriter(Path.Combine("output", "FileOutputWriterTests", "DefaultProvider"));

            void Action() => Compiler.Generate(
                "DefaultProvider",
                PrefixPattern,
                new Generator
            {
                OutputWriter      = outputWriter,
                EnableDataBinding = true,
            });

            ArgumentException ex = Assert.Throws <ArgumentException>(Action);

            Assert.Equal("Namespace http://tempuri.org/PurchaseOrderSchema.xsd not provided through map or generator.", ex.Message);
        }
        public void TestDefaultProviderGeneratorPrefix()
        {
            var directory    = Path.Combine("output", "FileOutputWriterTests", "DefaultProviderGeneratorPrefix");
            var outputWriter = new FileWatcherOutputWriter(directory);

            Compiler.Generate(
                "DefaultProviderGeneratorPrefix",
                PrefixPattern,
                new Generator
            {
                OutputWriter      = outputWriter,
                EnableDataBinding = true,
                NamespacePrefix   = "Generator.Prefix",
            });

            SharedTestFunctions.TestSamples(_output, "DefaultProviderGeneratorPrefix", PrefixPattern);
            Assert.Single(outputWriter.Files);
            Assert.Equal(Path.Combine(directory, "Generator.Prefix.cs"), outputWriter.Files.First());
        }
Example #6
0
        public static Assembly GenerateFiles(string name, IEnumerable <string> files, Generator generatorPrototype = null)
        {
            if (Assemblies.ContainsKey(name))
            {
                return(Assemblies[name]);
            }

            generatorPrototype = generatorPrototype ?? new Generator
            {
                GenerateNullables  = true,
                IntegerDataType    = typeof(int),
                DataAnnotationMode = DataAnnotationMode.All,
                GenerateDesignerCategoryAttribute = false,
                EntityFramework              = false,
                GenerateInterfaces           = true,
                NamespacePrefix              = name,
                GenerateDescriptionAttribute = true
            };

            var output = new FileWatcherOutputWriter(Path.Combine("output", name));

            var gen = new Generator
            {
                OutputWriter       = output,
                NamespaceProvider  = generatorPrototype.NamespaceProvider,
                GenerateNullables  = generatorPrototype.GenerateNullables,
                IntegerDataType    = generatorPrototype.IntegerDataType,
                DataAnnotationMode = generatorPrototype.DataAnnotationMode,
                GenerateDesignerCategoryAttribute = generatorPrototype.GenerateDesignerCategoryAttribute,
                EntityFramework              = generatorPrototype.EntityFramework,
                GenerateInterfaces           = generatorPrototype.GenerateInterfaces,
                MemberVisitor                = generatorPrototype.MemberVisitor,
                GenerateDescriptionAttribute = generatorPrototype.GenerateDescriptionAttribute
            };

            gen.Generate(files);

            return(CompileFiles(name, output.Files));
        }
        public void TestFullKeyProvider()
        {
            var directory    = Path.Combine("output", "FileOutputWriterTests", "FullKeyProvider");
            var outputWriter = new FileWatcherOutputWriter(directory);

            Compiler.Generate(
                "FullKeyProvider",
                PrefixPattern,
                new Generator
            {
                OutputWriter      = outputWriter,
                EnableDataBinding = true,
                NamespaceProvider = new Dictionary <NamespaceKey, string>
                {
                    { new NamespaceKey("http://tempuri.org/PurchaseOrderSchema.xsd"), "NamedNamespace" },
                }.ToNamespaceProvider(),
            });

            SharedTestFunctions.TestSamples(_output, "FullKeyProvider", PrefixPattern);
            Assert.Single(outputWriter.Files);
            Assert.Equal(Path.Combine(directory, "NamedNamespace.cs"), outputWriter.Files.First());
        }
        public void TestSeparateEmptyKeyProvider()
        {
            var directory    = Path.Combine("output", "FileOutputWriterTests", "SeparateEmptyKeyProvider");
            var outputWriter = new FileWatcherOutputWriter(directory);

            Compiler.Generate(
                "SeparateEmptyKeyProvider",
                PrefixPattern,
                new Generator
            {
                OutputWriter      = outputWriter,
                EnableDataBinding = true,
                SeparateClasses   = true,
                NamespaceProvider = new Dictionary <NamespaceKey, string>
                {
                    { new NamespaceKey(String.Empty), "NamedNamespace" },
                }.ToNamespaceProvider(),
            });

            SharedTestFunctions.TestSamples(_output, "SeparateEmptyKeyProvider", PrefixPattern);
            Assert.Equal(2, outputWriter.Files.Count());
            Assert.Equal(Path.Combine(directory, "NamedNamespace", "PurchaseOrderType.cs"), outputWriter.Files.First());
        }
        private Assembly CompileFiles(string name, IEnumerable <string> files, Generator generatorPrototype = null)
        {
            if (Assemblies.ContainsKey(name))
            {
                return(Assemblies[name]);
            }

            generatorPrototype = generatorPrototype ?? new Generator
            {
                GenerateNullables  = true,
                IntegerDataType    = typeof(int),
                DataAnnotationMode = DataAnnotationMode.All,
                GenerateDesignerCategoryAttribute = false,
                EntityFramework    = false,
                GenerateInterfaces = true,
                NamespacePrefix    = name
            };

            var output = new FileWatcherOutputWriter(Path.Combine("output", name));

            var gen = new Generator
            {
                OutputWriter       = output,
                NamespaceProvider  = generatorPrototype.NamespaceProvider,
                GenerateNullables  = generatorPrototype.GenerateNullables,
                IntegerDataType    = generatorPrototype.IntegerDataType,
                DataAnnotationMode = generatorPrototype.DataAnnotationMode,
                GenerateDesignerCategoryAttribute = generatorPrototype.GenerateDesignerCategoryAttribute,
                EntityFramework    = generatorPrototype.EntityFramework,
                GenerateInterfaces = generatorPrototype.GenerateInterfaces,
                MemberVisitor      = generatorPrototype.MemberVisitor,
                TimeDataType       = generatorPrototype.TimeDataType
            };

            gen.Generate(files);

            var provider   = CodeDomProvider.CreateProvider("CSharp");
            var assemblies = new[]
            {
                "System.dll",
                "System.Core.dll",
                "System.Xml.dll",
                "System.Xml.Linq.dll",
                "System.Xml.Serialization.dll",
                "System.ServiceModel.dll",
                "System.ComponentModel.DataAnnotations.dll",
            };

            var binFolder = Path.Combine(output.OutputDirectory, "bin");

            Directory.CreateDirectory(binFolder);
            var results = provider.CompileAssemblyFromFile(new CompilerParameters(assemblies, Path.Combine(binFolder, name + ".dll")), output.Files.ToArray());

            Assert.False(results.Errors.HasErrors, string.Join("\n", results.Output.Cast <string>()));
            Assert.False(results.Errors.HasWarnings, string.Join("\n", results.Output.Cast <string>()));
            Assert.NotNull(results.CompiledAssembly);

            var assembly = Assembly.Load(results.CompiledAssembly.GetName());

            Assemblies[name] = assembly;

            return(assembly);
        }