Exemple #1
0
        public static void SetVariableValue(string name, Type type, object value)
        {
            using (DBVariantDemoDataContext context = new DBVariantDemoDataContext())
            {
                Variant variant = context.Variants.Where(v => v.Name == name).SingleOrDefault();

                if (variant == null)
                {
                    context.Variants.Add(variant = new Variant()
                    {
                        Name = name
                    });
                }

                if (!_supportedTypesTypeToId.TryGetValue(type, out int typeId))
                {
                    throw new Exception("Unsupported type.");
                }

                variant.TypeId = typeId;
                variant.Value  = _formatter.For(type).Format(value);

                context.SaveChanges();
            }
        }
Exemple #2
0
        public void NullableValueTypesFormatNull_ThrowArgumentNullException_Option()
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.NullableValueTypesFormatNull = NullFormatHandling.ThrowArgumentNullException;
            });

            // Act
            TestDelegate test = () => formatter.For <int?>().Format(null);

            // Assert
            Assert.That(test, Throws.TypeOf <ArgumentNullException>());
        }
Exemple #3
0
        public void FormatSpecific_String_Func_Can_Replace_Default(string input, string expected)
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.UseFunc <string>(s => s.ToUpper(), (s, f) => s.ToUpper());
            });

            // Act
            string result = formatter.For <string>().Format("hello?");

            // Assert
            Assert.That(result, Is.EqualTo("HELLO?"));
        }
Exemple #4
0
        public void ReferenceTypesFormatNullToNull_Option()
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.ReferenceTypesFormatNullToNull = true;
            });

            // Act
            string result = formatter.For <TestClass>().Format(null);

            // Assert
            Assert.That(result, Is.Null);
        }
Exemple #5
0
        public void NullableValueTypesFormatNull_ReturnEmptyString_Option()
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.NullableValueTypesFormatNull = NullFormatHandling.ReturnEmptyString;
            });

            // Act
            string result = formatter.For <int?>().Format(null);

            // Assert
            Assert.That(result, Is.EqualTo(""));
        }
Exemple #6
0
        public void MissingFormatSpecific_UseToString_Option()
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.MissingFormatSpecific = MissingFormatSpecificHandling.UseToString;
            });
            var input = new TestClassWithoutFormatSpecific("anything");

            // Act
            string result = formatter.For <TestClassWithoutFormatSpecific>().Format(input, null);

            // Assert
            Assert.That(result, Is.EqualTo("anything"));
        }
Exemple #7
0
        public void Format_Uses_Configured_Type_Converter(string input)
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.UseTypeConverter <TestClassWithTypeConverter>();
            });
            TestClassWithTypeConverter obj = new TestClassWithTypeConverter(input);

            // Act
            string result = formatter.For <TestClassWithTypeConverter>().Format(obj);

            // Assert
            Assert.That(result, Is.EqualTo(input));
        }
Exemple #8
0
        public void Format_FormatSpecific_String_FormatterObject_Can_Replace_Default(string input, string expected)
        {
            // Arrange
            Mock <IFormatter <string> > stringFormatterMock = new Mock <IFormatter <string> >();

            stringFormatterMock.Setup(m => m.Format(It.IsAny <string>()))
            .Returns(new FormatReturns <string>(s => s.ToUpper()));

            stringFormatterMock.Setup(m => m.Format(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(new FormatSpecificReturns <string>((s, f) => s.ToUpper()));

            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.UseFormatterObject(stringFormatterMock.Object);
            });

            // Act
            string result1 = formatter.For <string>().Format(input);
            string result2 = formatter.For <string>().Format(input, null);

            // Assert
            Assert.That(result1, Is.EqualTo(expected));
            Assert.That(result2, Is.EqualTo(expected));
        }
Exemple #9
0
        public void MissingFormatSpecific_ThrowNotSupportedException_Option()
        {
            // Arrange
            FormatterContainer formatter = FormatterContainer.Create(config =>
            {
                config.MissingFormatSpecific = MissingFormatSpecificHandling.ThrowNotSupportedException;
            });
            var input = new TestClassWithoutFormatSpecific("anything");

            // Act
            TestDelegate test = () => formatter.For <TestClassWithoutFormatSpecific>().Format(input, null);

            // Assert
            Assert.That(test, Throws.TypeOf <NotSupportedException>());
        }
Exemple #10
0
 public static IFormatter <T> For <T>() => _formatter.For <T>();
        public void Format_String_Is_Idempotent(string input)
        {
            // Arrange

            // Act
            string result = DefaultFormatter.For <string>().Format(input);

            // Assert
            Assert.That(result, Is.EqualTo(input));
        }