Ejemplo n.º 1
0
        protected static int Generate(GenerateOptions options)
        {
            System.Console.WriteLine($"Generating file {options.Destination} based on {options.Source}.");
            //Parse the model
            var container = new ParserContainer();

            container.Initialize(Path.GetExtension(options.Source));

            var modelFactory = new ModelFactory();
            var collection   = modelFactory.Instantiate(options.Source, container);

            var types = collection.Select(o => o.GetType()).Distinct();

            //Render the template
            foreach (var type in types)
            {
                var templateContainer = new TemplateContainer();
                templateContainer.Initialize();
                var templateFactory = templateContainer.Retrieve(type);
                var engine          = templateFactory.Instantiate(string.Empty, false, options.Template);

                var objects = collection.Where(o => o.GetType() == type);
                var text    = engine.Execute(objects);
                File.WriteAllText(options.Destination, text);
            }

            return(0);
        }
Ejemplo n.º 2
0
        public void Parse_TryParse_String_ParserObject_Can_Replace_Default(string input, string expectedParseResult, bool expectedTryParseResult)
        {
            // Arrange
            Mock <IParser <string> > stringParserMock = new Mock <IParser <string> >();

            stringParserMock.Setup(m => m.Parse(It.IsAny <string>()))
            .Returns(new ParseReturns <string>(s => s + "..."));

            stringParserMock.Setup(m => m.TryParse(It.IsAny <string>(), out It.Ref <string> .IsAny))
            .Callback(new TryParseCallback <string>((string s, out string result) => result = s + "..."))
            .Returns(new TryParseReturns <string>((string s, ref string result) => expectedTryParseResult));

            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.UseParserObject(stringParserMock.Object);
            });

            // Act
            string parseResult    = parser.For <string>().Parse(input);
            bool   tryParseResult = parser.For <string>().TryParse(input, out string tryParseOutput);

            // Assert
            Assert.That(parseResult, Is.EqualTo(expectedParseResult));
            Assert.That(tryParseResult, Is.EqualTo(expectedTryParseResult));
            Assert.That(tryParseOutput, Is.EqualTo(expectedParseResult));
        }
Ejemplo n.º 3
0
        public void Uri_ParseFunc_And_TryParseFunc_Proxy_Constructor_And_TryCreate()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                // Show that this option can be applied on top of custom delegates.
                config.ReferenceTypesParseNullToNull = true;

                // Proxy Uri::.ctor and Uri::TryCreate and use UriKind.RelativeOrAbsolute with both.
                config.UseFunc(
                    s => new Uri(s, UriKind.RelativeOrAbsolute),
                    (string s, out Uri result) =>
                {
                    return(Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out result));
                });
            });
            IParser <Uri> uriParser = parser.For <Uri>();

            // Act
            Uri  uri1    = uriParser.Parse("https://www.google.com/");
            Uri  uri2    = uriParser.Parse(null);
            bool result1 = uriParser.TryParse("https://www.google.com/", out Uri output1);
            bool result2 = uriParser.TryParse(null, out Uri output2);
            bool result3 = uriParser.TryParse("http://", out Uri output3);

            // Assert
            Assert.That(uri1, Is.Not.Null);
            Assert.That(uri2, Is.Null);
            Assert.That(result1, Is.True);
            Assert.That(result2, Is.True);
            Assert.That(result3, Is.False);
        }
Ejemplo n.º 4
0
        public void Configuration_Null_Configurator_Throws_ArgumentNullException()
        {
            // Arrange

            // Act
            TestDelegate test = () => ParserContainer.Create(null);

            // Assert
            Assert.That(test, Throws.ArgumentNullException);
        }
Ejemplo n.º 5
0
        public void ReferenceTypesParseNullToNull_Option()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.ReferenceTypesParseNullToNull = true;
            });

            // Act
            Version result = parser.For <Version>().Parse(null);

            // Assert
            Assert.That(result, Is.Null);
        }
Ejemplo n.º 6
0
        public void NullableValueTypesParseEmptyStringToNull_Option()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.NullableValueTypesParseEmptyStringToNull = true;
            });

            // Act
            int?result = parser.For <int?>().Parse("");

            // Assert
            Assert.That(result, Is.Null);
        }
Ejemplo n.º 7
0
        public void Parse_Uses_Configured_Type_Converter(string input)
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.UseTypeConverter <TestClassWithTypeConverter>();
            });

            // Act
            TestClassWithTypeConverter obj = parser.For <TestClassWithTypeConverter>().Parse(input);

            // Assert
            Assert.That(obj.Value, Is.EqualTo(input));
        }
Ejemplo n.º 8
0
        public void MissingTryParse_ReturnFalse_Option()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.MissingTryParse = MissingTryParseHandling.ReturnFalse;
            });

            // Act
            bool result = parser.For <TestClassWithoutTryParse>().TryParse("anything", out var disregard);

            // Assert
            Assert.That(result, Is.False);
        }
Ejemplo n.º 9
0
        public void Parse_String_Func_Can_Replace_Default()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.UseFunc(s => s.ToLower());
            });

            // Act
            string result = parser.For <string>().Parse("Hello!");

            // Assert
            Assert.That(result, Is.EqualTo("hello!"));
        }
Ejemplo n.º 10
0
        public void Configuration_UseTypeConverter_Throws_If_Configuration_Locked()
        {
            // Arrange
            ParserConfiguration outerConfig = null;

            ParserContainer.Create(config =>
            {
                outerConfig = config;
            });

            // Act
            TestDelegate test = () => outerConfig.UseTypeConverter <string>();

            // Assert
        }
Ejemplo n.º 11
0
        public void MissingTryParse_WrapParseInTryCatch_Option()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.MissingTryParse = MissingTryParseHandling.WrapParseInTryCatch;
            });

            // Act
            bool result = parser.For <TestClassWithoutTryParse>().TryParse("anything", out var output);

            // Assert
            Assert.That(result, Is.True);
            Assert.That(output.Value, Is.EqualTo("anything"));
        }
Ejemplo n.º 12
0
        public void Configuration_NullableValueTypesParseEmptyStringToNull_Throws_If_Configuration_Locked()
        {
            // Arrange
            ParserConfiguration outerConfig = null;

            ParserContainer.Create(config =>
            {
                outerConfig = config;
            });

            // Act
            TestDelegate test = () => outerConfig.NullableValueTypesParseEmptyStringToNull = true;

            // Assert
        }
Ejemplo n.º 13
0
        public void Configuration_ReferenceTypesParseNullToNull_Throws_If_Configuration_Locked()
        {
            // Arrange
            ParserConfiguration outerConfig = null;

            ParserContainer.Create(config =>
            {
                outerConfig = config;
            });

            // Act
            TestDelegate test = () => outerConfig.ReferenceTypesParseNullToNull = true;

            // Assert
        }
Ejemplo n.º 14
0
        public void Configuration_MissingTryParse_Throws_If_Configuration_Locked()
        {
            // Arrange
            ParserConfiguration outerConfig = null;

            ParserContainer.Create(config =>
            {
                outerConfig = config;
            });

            // Act
            TestDelegate test = () => outerConfig.MissingTryParse = MissingTryParseHandling.ReturnFalse;

            // Assert
        }
Ejemplo n.º 15
0
        public void Configuration_Rejects_ParseFunc_For_Delegate()
        {
            // Arrange

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseFunc <Func <int> >(s => null);
                });
            };

            // Assert
            Assert.That(test, Throws.InstanceOf <OsmoticConfigurationException>());
        }
Ejemplo n.º 16
0
        public void Configuration_UseFunc_Throws_If_Configuration_Locked()
        {
            // Arrange
            ParserConfiguration outerConfig = null;

            ParserContainer.Create(config =>
            {
                outerConfig = config;
            });

            // Act
            TestDelegate test = () => outerConfig.UseFunc(s => "");

            // Assert
            Assert.That(test, Throws.TypeOf <OsmoticConfigurationException>());
        }
Ejemplo n.º 17
0
        public void Configuration_Null_TryParseFunc_Throws_ArgumentNullException()
        {
            // Arrange

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseFunc(s => 0, null);
                });
            };

            // Assert
            Assert.That(test, Throws.ArgumentNullException);
        }
Ejemplo n.º 18
0
        public void Configuration_Null_ParserObject_Throws_ArgumentNullException()
        {
            // Arrange

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseParserObject <int>(null);
                });
            };

            // Assert
            Assert.That(test, Throws.ArgumentNullException);
        }
Ejemplo n.º 19
0
        public IEnumerable <object> Instantiate(string filename, ParserContainer container)
        {
            if (!File.Exists(filename))
            {
                throw new ArgumentException(string.Format("No file has been found at the location '{0}'.", filename));
            }
            var collection = new List <object>();

            foreach (var rootParser in container.RootParsers)
            {
                using (var stream = File.OpenRead(filename))
                    collection.AddRange(((IRootParser)rootParser).Parse(stream));
            }

            return(collection);
        }
Ejemplo n.º 20
0
        public void Configuration_Rejects_Mix_Of_TypeConverter_And_Func()
        {
            // Arrange

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseTypeConverter <int>();
                    config.UseFunc(s => 0);
                });
            };

            // Assert
            Assert.That(test, Throws.TypeOf <OsmoticConfigurationException>());
        }
Ejemplo n.º 21
0
        public void Parse_ValidXml_CorrectlyParsedAccount(string extension)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream($"Idunn.FileShare.Testing.Unit.Parser.Resources.Sample{extension}"))
            {
                var register  = new FileShare.Parser.ParserRegister();
                var container = new ParserContainer();
                container.Initialize(register, extension);

                var parser   = register.GetRootParser();
                var accounts = parser.Parse(stream);
                Assert.That(accounts, Is.Not.Null);
                Assert.That(accounts, Is.AssignableTo <IEnumerable <Account> >());
                Assert.That(accounts, Has.Count.EqualTo(1));
            }
        }
Ejemplo n.º 22
0
        public void Configuration_Rejects_ParserObject_For_Interface()
        {
            // Arrange
            Mock <IParser <IFormattable> > parserObjectMock = new Mock <IParser <IFormattable> >();
            IParser <IFormattable>         parserObject     = parserObjectMock.Object;

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseParserObject(parserObject);
                });
            };

            // Assert
            Assert.That(test, Throws.InstanceOf <OsmoticConfigurationException>());
        }
Ejemplo n.º 23
0
        public void Configuration_UseParserObject_Throws_If_Configuration_Locked()
        {
            // Arrange
            ParserConfiguration outerConfig = null;

            ParserContainer.Create(config =>
            {
                outerConfig = config;
            });
            Mock <IParser <string> > parserObjectMock = new Mock <IParser <string> >();
            IParser <string>         parserObject     = parserObjectMock.Object;

            // Act
            TestDelegate test = () => outerConfig.UseParserObject(parserObject);

            // Assert
            Assert.That(test, Throws.TypeOf <OsmoticConfigurationException>());
        }
Ejemplo n.º 24
0
        public void Configuration_Rejects_Mix_Of_TypeConverter_And_ParserObject()
        {
            // Arrange
            Mock <IParser <int> > parserObjectMock = new Mock <IParser <int> >();
            IParser <int>         parserObject     = parserObjectMock.Object;

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseTypeConverter <int>();
                    config.UseDefault <int>();
                });
            };

            // Assert
            Assert.That(test, Throws.TypeOf <OsmoticConfigurationException>());
        }
Ejemplo n.º 25
0
        public void Parse_Valid_CorrectlyParsedDatabasePermissions(string extension)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream($"Idunn.SqlServer.Testing.Unit.Parser.Resources.Sample{extension}"))
            {
                var register  = new ParserRegister();
                var container = new ParserContainer();
                container.Initialize(register, extension);

                var parser    = register.GetRootParser();
                var principal = (parser.Parse(stream).ElementAt(0) as Principal);

                var db = principal.Databases.Single(d => d.Name == "db-001");
                Assert.That(db.Permissions, Is.Not.Null.And.Not.Empty);
                Assert.That(db.Permissions, Has.Count.EqualTo(1));
                Assert.That(db.Permissions.First().Name, Is.EqualTo("CONNECT"));
            }
        }
Ejemplo n.º 26
0
        public void Parse_ValidXml_CorrectlyParsedFolderPermissions(string extension)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream($"Idunn.FileShare.Testing.Unit.Parser.Resources.Sample{extension}"))
            {
                var register  = new FileShare.Parser.ParserRegister();
                var container = new ParserContainer();
                container.Initialize(register, extension);

                var parser  = register.GetRootParser();
                var account = (parser.Parse(stream).ElementAt(0) as Account);

                var folder = account.Folders.Single(f => f.Path == "c:\\folder-001");
                Assert.That(folder.Permissions, Is.Not.Null.And.Not.Empty);
                Assert.That(folder.Permissions, Has.Count.EqualTo(1));
                Assert.That(folder.Permissions.First().Name, Is.EqualTo("LIST"));
            }
        }
Ejemplo n.º 27
0
        //[TestCase(".yml")]
        public void Parse_ValidWithMultiplePrincipals_CorrectlyPrincipals(string extension)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream($"Idunn.SqlServer.Testing.Unit.Parser.Resources.Multiple{extension}"))
            {
                var register  = new ParserRegister();
                var container = new ParserContainer();
                container.Initialize(register, extension);

                var parser     = register.GetRootParser();
                var principals = (parser.Parse(stream) as IEnumerable <Principal>);

                Assert.That(principals, Is.Not.Null);
                Assert.That(principals, Has.Count.EqualTo(2));
                Assert.That(principals.Any(p => p.Name == "ExecuteDwh"));
                Assert.That(principals.Any(p => p.Name == "Logger"));
                Assert.That(principals.All(p => p.Databases.Count >= 1));
            }
        }
Ejemplo n.º 28
0
        //[TestCase(".yml")]
        public void Parse_ValidXmlWithMultiplePrincipals_CorrectlyPrincipals(string extension)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream($"Idunn.FileShare.Testing.Unit.Parser.Resources.Multiple{extension}"))
            {
                var register  = new FileShare.Parser.ParserRegister();
                var container = new ParserContainer();
                container.Initialize(register, extension);

                var parser   = register.GetRootParser();
                var accounts = (parser.Parse(stream) as IEnumerable <Account>);

                Assert.That(accounts, Is.Not.Null);
                Assert.That(accounts, Has.Count.EqualTo(2));
                Assert.That(accounts.Any(a => a.Name == "domain\\account-001"));
                Assert.That(accounts.Any(a => a.Name == "domain\\account-002"));
                Assert.That(accounts.All(a => a.Folders.Count >= 1));
            }
        }
Ejemplo n.º 29
0
        public void Parse_Valid_CorrectlyParsedSecurables(string extension)
        {
            var assembly = Assembly.GetExecutingAssembly();

            using (var stream = assembly.GetManifestResourceStream($"Idunn.SqlServer.Testing.Unit.Parser.Resources.Sample{extension}"))
            {
                var register  = new ParserRegister();
                var container = new ParserContainer();
                container.Initialize(register, extension);

                var parser    = register.GetRootParser();
                var principal = (parser.Parse(stream).ElementAt(0) as Principal);

                var db = principal.Databases.Single(d => d.Name == "db-001");
                Assert.That(db.Securables, Is.Not.Null.And.Not.Empty);
                Assert.That(db.Securables, Has.Count.EqualTo(2));
                Assert.That(db.Securables.All(s => s.Type == "schema"));
                Assert.That(db.Securables.Any(s => s.Name == "dbo"));
                Assert.That(db.Securables.Any(s => s.Name == "admin"));
            }
        }
Ejemplo n.º 30
0
        public void TryParse_String_Func_Can_Replace_Default()
        {
            // Arrange
            ParserContainer parser = ParserContainer.Create(config =>
            {
                config.UseFunc(
                    s => s.ToLower(),
                    (string s, out string r) =>
                {
                    r = s.ToLower();
                    return(true);
                });
            });

            // Act
            bool result = parser.For <string>().TryParse("Hello!", out string output);

            // Assert
            Assert.That(result, Is.True);
            Assert.That(output, Is.EqualTo("hello!"));
        }