Exemple #1
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));
        }
Exemple #2
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);
        }
Exemple #3
0
        public void Configuration_Null_Configurator_Throws_ArgumentNullException()
        {
            // Arrange

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

            // Assert
            Assert.That(test, Throws.ArgumentNullException);
        }
Exemple #4
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);
        }
Exemple #5
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!"));
        }
Exemple #6
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);
        }
Exemple #7
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);
        }
Exemple #8
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));
        }
Exemple #9
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"));
        }
Exemple #10
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
        }
Exemple #11
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
        }
Exemple #12
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
        }
Exemple #13
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
        }
Exemple #14
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);
        }
Exemple #15
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);
        }
Exemple #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>());
        }
Exemple #17
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>());
        }
Exemple #18
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>());
        }
Exemple #19
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>());
        }
Exemple #20
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>());
        }
Exemple #21
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>());
        }
Exemple #22
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!"));
        }
Exemple #23
0
        public void Configuration_Rejects_Mix_Of_Default_And_Func()
        {
            // Arrange

            // Act
            TestDelegate test = () =>
            {
                ParserContainer.Create(config =>
                {
                    config.UseDefault <int>();
                    config.UseFunc(
                        s => 0,
                        (string s, out int r) =>
                    {
                        r = 0;
                        return(true);
                    });
                });
            };

            // Assert
            Assert.That(test, Throws.TypeOf <OsmoticConfigurationException>());
        }