Example #1
0
        public void ConvertToThrowsIfThereAreTwoEqualConverters()
        {
            var test = new StringFormatTest("3");
            var ex   = Assert.Throws <InvalidOperationException>(() =>
                                                                 ConvertFormat.To(typeof(short), test));

            Assert.AreEqual(
                "Multiple converters for: " +
                $"{typeof(StringFormatTest).FullName} -> {typeof(short).FullName}",
                ex.Message);
        }
Example #2
0
        public void ConvertWithThrowsIfFormatIsNull()
        {
            StringFormatTest format = null;

            Assert.That(
                () => ConvertFormat.With <HiddenConverter>(format),
                Throws.ArgumentNullException);
            Assert.That(
                () => ConvertFormat.With <HiddenConverter, int>(4, format),
                Throws.ArgumentNullException);

            var converter = new HiddenConverter();

            Assert.That(
                () => ConvertFormat.With(converter, format),
                Throws.ArgumentNullException);
        }
Example #3
0
        public void ConvertToThrowsIfConstructorFails()
        {
            using var test = new StringFormatTest { Value = "3" };
            var ex = Assert.Throws <Exception>(() =>
                                               ConvertFormat.To(typeof(ushort), test));

            Assert.AreEqual(
                "Exception of type 'System.Exception' was thrown.",
                ex.Message);

            // Just for coverage
            var converter = new FormatTestBadConstructor("2");

            Assert.That(
                converter.Convert(new StringFormatTest("3")),
                Is.EqualTo(3));
        }
Example #4
0
        public void ConvertWithInstanceThrowsExceptionIfInvalidConverter()
        {
            var    format = new StringFormatTest("3");
            string msg    = "Converter cannot convert from/to the type";

            Assert.That(
                () => ConvertFormat.With <SingleOuterConverterExample>(format),
                Throws.InvalidOperationException.With.Message.EqualTo(msg));
            Assert.That(
                () => ConvertFormat.With <ConverterAndOtherInterface, int>(4, format),
                Throws.InvalidOperationException.With.Message.EqualTo(msg));

            var converter = new SingleOuterConverterExample();

            Assert.That(
                () => ConvertFormat.With(converter, format),
                Throws.InvalidOperationException.With.Message.EqualTo(msg));
        }
Example #5
0
        public void ConvertWithThrowsExceptionIfNoImplementIConverter()
        {
            var    format = new StringFormatTest("3");
            string msg    = "Converter doesn't implement IConverter<,>";

            Assert.That(
                () => ConvertFormat.With <ConverterWithoutGenericInterface>(format),
                Throws.InvalidOperationException.With.Message.EqualTo(msg));
            Assert.That(
                () => ConvertFormat.With <ConverterWithoutGenericInterface, int>(4, format),
                Throws.InvalidOperationException.With.Message.EqualTo(msg));

            var converter = new ConverterWithoutGenericInterface();

            Assert.That(
                () => ConvertFormat.With(converter, format),
                Throws.InvalidOperationException.With.Message.EqualTo(msg));
        }
Example #6
0
        public void ConvertNeedsToBeHiddenIfNoPublicConstructor()
        {
            // With MEF we can't have an extension without a default constructor
            // because it will throw an exception in every general request.
            // So we need to hide those extensions.
            var test = new StringFormatTest("3");
            var ex   = Assert.Throws <InvalidOperationException>(() =>
                                                                 ConvertFormat.To(typeof(ulong), test));

            Assert.AreEqual(
                "Cannot find converter for: " +
                $"{typeof(StringFormatTest).FullName} -> {typeof(ulong).FullName}",
                ex.Message);

            // But we can use the With() of classes with Factory pattern.
            var converter = FormatTestPrivateConstructor.Create();

            Assert.AreEqual(
                ConvertFormat.With(converter, new StringFormatTest("1")),
                0);
        }
Example #7
0
        public void ConvertNeedsToBeHiddenIfConstructorsHaveArgs()
        {
            // With MEF we can't have an extension without a default constructor
            // because it will throw an exception in every general request.
            // So we need to hide those extensions.
            using var test = new StringFormatTest("3");
            var ex = Assert.Throws <InvalidOperationException>(() =>
                                                               ConvertFormat.To(typeof(long), test));

            Assert.AreEqual(
                "Cannot find converter for: " +
                $"{typeof(StringFormatTest).FullName} -> {typeof(long).FullName}",
                ex.Message);

            // But we can use the With()
            var converter = new FormatTestNoConstructor("3");

            Assert.AreEqual(
                0,
                ConvertFormat.With(converter, new StringFormatTest("1")));
        }