public void IfValueIsMinDateTime_ReturnEmptySting()
        {
            object parameter = string.Empty;
            var value = DateTime.MinValue;
            var converter = new DateTimeFormatConverter();

            var result = converter.Convert(value, typeof(object), parameter, CultureInfo.InvariantCulture);

            Assert.AreEqual(result, string.Empty);
        }
        public void IfValueIsNull_ReturnNull()
        {
            object parameter = string.Empty;
            object value = null;
            var converter = new DateTimeFormatConverter();

            var result = converter.Convert(value, typeof(object), parameter, CultureInfo.InvariantCulture);

            Assert.AreEqual(result, null);
        }
        public void Convert()
        {
            var converter = new DateTimeFormatConverter();

            var date = converter.Convert(null, null, null, null);
            Assert.IsNull(date);

            date = converter.Convert(DateTime.MinValue, null, null, null);
            Assert.AreEqual(string.Empty, date);

            var expectedDate = DateTime.Today;

            date = converter.Convert(expectedDate, null, DateTimeFormat.Date.ToString(), null);
            Assert.AreEqual(expectedDate.ToShortDateString(), date);

            date = converter.Convert(expectedDate, null, DateTimeFormat.Time.ToString(), CultureInfo.CurrentCulture);
            Assert.AreEqual(expectedDate.ToShortTimeString(), date);

            date = converter.Convert(expectedDate, null, DateTimeFormat.DateTime.ToString(), null);
            Assert.AreEqual(expectedDate.ToString(CultureInfo.CurrentCulture), date);
        }
        public void ConvertTest()
        {
            const int Year = 2012;
            const int Month = 1;
            const int Day = 2;
            const int Hour = 3;
            const int Minute = 4;
            const int Sec = 5;

            object parameter = DateTimeFormat.Date.ToString();
            var value = new DateTime(Year, Month, Day, Hour, Minute, Sec);
            var converter = new DateTimeFormatConverter();

            var result = converter.Convert(value, typeof(object), parameter, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, value.ToShortDateString());
            
            parameter = DateTimeFormat.Time.ToString();
            result = converter.Convert(value, typeof(object), parameter, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, value.ToShortTimeString());

            parameter = DateTimeFormat.DateTime.ToString();
            result = converter.Convert(value, typeof(object), parameter, CultureInfo.InvariantCulture);
            Assert.AreEqual(result, value.ToString(CultureInfo.InvariantCulture));
        }
예제 #5
0
        private static ConverterInfo GetConverterInfo(IColumnItem column)
        {
            var localProperty = column.Property;
            IValueConverter converterObject = null;
            object converterParameter = null;
            var converterString = string.Empty;
            PropertyInfo sourceProperty = null;

            if (ReferenceFieldsHelper.IsReferenceProperty(localProperty))
            {
                // Field type specific attributes should be taken from source.
                var refInfo = ReferenceFieldsHelper.GetCrossRefInfo(localProperty);
                sourceProperty = new ReferenceFieldsHelper().GetSourcePropertyInfo(refInfo.Item1, refInfo.Item2);
            }

            var prop = sourceProperty ?? localProperty;

            if (PropertyIsDateTime(localProperty, column))
            {
                var datetimeFormat = ReferenceFieldsHelper.GetDateTimeFormat(prop);

                if (!string.IsNullOrEmpty(datetimeFormat))
                {
                    converterObject = new DateTimeFormatConverter();
                    converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource DateTimeFormatConverter}}, ConverterParameter={0}", datetimeFormat);
                }
            }
            else if (localProperty.PropertyType == typeof(string))
            {
                var approvalAttribute = (ApprovalDefinitionAttribute)(sourceProperty ?? prop).GetCustomAttributes(typeof(ApprovalDefinitionAttribute), false).FirstOrDefault();

                if (approvalAttribute != null)
                {
                    converterObject = new ApprovalStateToDisplayNameConverter();
                    converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource ApprovalStateToDisplayNameConverter}}");
                }
            }
            else if (prop.PropertyType == typeof(bool?))
            {
                var checkboxSettings = ReferenceFieldsHelper.GetCheckboxSettings(prop);

                if (checkboxSettings != null && checkboxSettings.IsSwitchToggle)
                {
                    var param = new[]
                                            {
                                                checkboxSettings.UndefinedLabel,
                                                checkboxSettings.FalseLabel,
                                                checkboxSettings.TrueLabel
                                            };

                    converterObject = new CheckboxToCustomValueConverter();
                    converterParameter = string.Join("~", param).Replace(' ', '_').Replace('\'', '^');
                    converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource checkboxToCustomValueConverter}}, ConverterParameter={0}", converterParameter);
                }
            }

            var numericAttribute = (NumericAttribute)prop.GetCustomAttributes(typeof(NumericAttribute), false).FirstOrDefault();

            if (numericAttribute != null)
            {
                switch (numericAttribute.NumericType)
                {
                    case NumericTypes.Percentage:
                        converterObject = new NumberToPercentConverter();
                        converterString = string.Format(CultureInfo.InvariantCulture, "Converter={{StaticResource NumberToPercentConverter}}");
                        break;
                }
            }

            return new ConverterInfo(converterObject, converterParameter, converterString);
        }
 public void ConvertBack()
 {
     var converter = new DateTimeFormatConverter();
     var obj = converter.ConvertBack(new object(), GetType(), 1, CultureInfo.CurrentCulture);
     Assert.IsNull(obj);
 }