Пример #1
0
        public void Should_Fail_Transform_With_Invalid_Transform_File()
        {
            using (var tempXmlFile = new TempFile(".xml"))
            {
                string buffer = Encoding.UTF8.GetString(
                    ManifestResourceStream.Get(
                        Assembly.GetExecutingAssembly(), AssetsConstants.StandardDbMetadataFile));

                tempXmlFile.WriteAllText(buffer);

                var configuration = new Configuration()
                {
                    MetadataPath         = tempXmlFile.Filename,
                    XsltPath             = Guid.NewGuid().ToString() + ".xslt",
                    OutputDirectory      = string.Empty,
                    OutputFileExtension  = ".cs",
                    OutputFilename       = "Test",
                    OutputFileNameSuffix = string.Empty,
                    Parameters           = new List <Configuration.ParameterConfiguration>()
                };

                var transformer = new Transformer(new OutputWriter());
                var transform   = new Func <string>(() => transformer.Transform(configuration));

                Assert.Throws <InvalidOperationException>(transform);
            }
        }
Пример #2
0
        public void Should_Read_Test_Assembly()
        {
            byte[] buffer = ManifestResourceStream.Get(
                Assembly.GetExecutingAssembly(), AssetsConstants.TestAssembly);

            var assemblyReader = new DotnetAssemblyReader(new OutputWriter());
            var model          = assemblyReader.Read(buffer, string.Empty);

            model.Types.Count.Should().Be(2);

            var internalStaticClass = model.Types[0];

            internalStaticClass.Name.Should().Be("InternalStaticClass");
            internalStaticClass.Constructors.Count.Should().Be(0);
            internalStaticClass.Methods.Count.Should().Be(4);

            internalStaticClass.Methods[0].Should().BeEquals();
            internalStaticClass.Methods[1].Should().BeGetHashCode();
            internalStaticClass.Methods[2].Should().BeGetType();
            internalStaticClass.Methods[3].Should().BeToString();

            var publicGenericClass = model.Types[1];

            publicGenericClass.Name.Should().Be("PublicGenericClassT`1");
            publicGenericClass.Constructors.Count.Should().Be(1);
            publicGenericClass.Constructors[0].Name.Should().Be(".ctor");
            publicGenericClass.Constructors[0].Parameters.Count.Should().Be(1);
            publicGenericClass.Constructors[0].Parameters[0].Name.Should().Be("variable");
            publicGenericClass.Constructors[0].Parameters[0].CLRType.Should().Be("T");

            publicGenericClass.Properties.Count.Should().Be(1);
            publicGenericClass.Properties[0].Name.Should().Be("PublicInstanceVariable");
            publicGenericClass.Properties[0].CLRType.Should().Be("T");
            publicGenericClass.Properties[0].CanRead.Should().Be(true);
            publicGenericClass.Properties[0].CanWrite.Should().Be(true);

            publicGenericClass.Methods.Count.Should().Be(6);

            internalStaticClass.Methods[0].Should().BeEquals();
            internalStaticClass.Methods[1].Should().BeGetHashCode();
            internalStaticClass.Methods[2].Should().BeGetType();

            publicGenericClass.Methods[3].Name.Should().Be("PublicInstanceCalculatePoint");
            publicGenericClass.Methods[3].CLRType.Should().Be("System.Drawing.Point");
            publicGenericClass.Methods[3].Parameters.Count.Should().Be(2);
            publicGenericClass.Methods[3].Parameters[0].Name.Should().Be("x");
            publicGenericClass.Methods[3].Parameters[0].CLRType.Should().Be("System.Int32");
            publicGenericClass.Methods[3].Parameters[1].Name.Should().Be("y");
            publicGenericClass.Methods[3].Parameters[1].CLRType.Should().Be("System.Int32");

            publicGenericClass.Methods[4].Name.Should().Be("PublicInstanceDoNothing");
            publicGenericClass.Methods[4].CLRType.Should().Be("System.Void");
            publicGenericClass.Methods[4].Parameters.Count.Should().Be(0);

            publicGenericClass.Methods[5].Should().BeToString();
        }
Пример #3
0
        public void Should_Transform_To_CSharp_File()
        {
            using (var tempXmlFile = new TempFile(".xml"))
                using (var tempXslFile = new TempFile(".xslt"))
                {
                    string buffer = Encoding.UTF8.GetString(
                        ManifestResourceStream.Get(
                            Assembly.GetExecutingAssembly(), AssetsConstants.StandardDbMetadataFile));

                    tempXmlFile.WriteAllText(buffer);

                    buffer = Encoding.UTF8.GetString(
                        ManifestResourceStream.Get(
                            Assembly.GetExecutingAssembly(), AssetsConstants.TransformFile));

                    tempXslFile.WriteAllText(buffer);

                    var configuration = new Configuration()
                    {
                        MetadataPath         = tempXmlFile.Filename,
                        XsltPath             = tempXslFile.Filename,
                        OutputDirectory      = Path.GetDirectoryName(tempXmlFile.Filename),
                        OutputFileExtension  = ".cs",
                        OutputFilename       = "Products",
                        OutputFileNameSuffix = "Test",
                        Parameters           = new List <Configuration.ParameterConfiguration>()
                        {
                            new Configuration.ParameterConfiguration()
                            {
                                Name = "EntityName", Value = "Products"
                            },
                            new Configuration.ParameterConfiguration()
                            {
                                Name = "ApplicationName", Value = "Genx"
                            }
                        }
                    };

                    var      transformer = new Transformer(new OutputWriter());
                    string   outputPath  = transformer.Transform(configuration);
                    string[] output      = File.ReadAllLines(outputPath);
                    File.Delete(outputPath);

                    output[1].Should().Be("using System;");
                    output[3].Should().Be("namespace Genx.Test");
                    output[4].Should().Be("{");
                    output[5].Should().Be("    public class ProductsTest");
                    output[6].Should().Be("    {");
                    output[7].Should().Be("        public ProductsTest(string message)");
                    output[8].Should().Be("        {");
                    output[9].Should().Be("            Console.WriteLine(\"Test!\");");
                    output[10].Should().Be("        }");
                    output[11].Should().Be("    }");
                    output[12].Should().Be("}");
                }
        }
Пример #4
0
        public void Should_Read_Names_From_Db_Metadata_File()
        {
            using (var tempFile = new TempFile(".xml"))
            {
                string buffer = Encoding.UTF8.GetString(
                    ManifestResourceStream.Get(
                        Assembly.GetExecutingAssembly(), AssetsConstants.StandardDbMetadataFile));

                tempFile.WriteAllText(buffer);
                var reader = new ModelMetadataReader();
                var names  = reader.ReadNames(tempFile.Filename).ToList();

                Assert.True(names.Count == 2);

                names[0].Should().Be("Products");
                names[1].Should().Be("Categories");
            }
        }