示例#1
0
        private static string?ReformatDate(DisplayFormatFilter format, string str)
        {
            var param = format.FormatParameters;

            var key = nameof(DateDisplayParams.FormatString);
            var fmt = (param.ContainsKey(key) ? param[key] : null) ?? "yyyy-MM-dd";

            try
            {
                // first, try the exact format that *should* be used
                if (DateTime.TryParseExact(str, "yyyy-MM-dd", null !, DateTimeStyles.AssumeUniversal | DateTimeStyles.AllowWhiteSpaces, out var dt))
                {
                    return(dt.ToString(fmt));
                }

                // try a more general search
                if (DateTime.TryParse(str, out dt))
                {
                    return(dt.ToString(fmt));
                }

                // abandon
                return(null);
            }
            catch
            {
                return(null);
            }
        }
示例#2
0
        private static string?ReformatNumberToFrac(DisplayFormatFilter format, string str)
        {
            var param = format.FormatParameters;

            if (!decimal.TryParse(str, out var value))
            {
                return(null);
            }

            var dpKey = nameof(FractionalDisplayParams.DecimalPlaces);
            var dpStr = param.ContainsKey(dpKey) ? param[dpKey] ?? "" : "2";

            if (!int.TryParse(dpStr, out var decimalPlaces))
            {
                decimalPlaces = 2;
            }
            if (decimalPlaces < 0 || decimalPlaces > 20)
            {
                decimalPlaces = 2;
            }

            var scale    = (decimal)Math.Pow(10.0, decimalPlaces);
            var intPart  = Math.Truncate(value);
            var fracPart = value - intPart;

            var final = Math.Round((1 + fracPart) * scale, 0);                   // add 1 to get leading zeros

            return(final.ToString(CultureInfo.InvariantCulture) !.Substring(1)); // remove the added 1
        }
示例#3
0
        public void Integral__truncates_numbers_with_no_rounding(string input, string expected)
        {
            var format = new DisplayFormatFilter {
                Type             = DisplayFormatType.Integral,
                FormatParameters = new Dictionary <string, string>()
            };

            var actual = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.EqualTo(expected));
        }
示例#4
0
        [Test] // Note: render image is used as a flag in the render phase. This format doesn't change the underlying data
        public void RenderImage__passes_data_through()
        {
            string input  = "my input";
            var    format = new DisplayFormatFilter {
                Type             = DisplayFormatType.RenderImage,
                FormatParameters = new Dictionary <string, string>()
            };

            var expected = "my input";
            var actual   = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.EqualTo(expected));
        }
示例#5
0
        public void Fractional__displays_the_fractional_part_with_exact_number_of_places_and_rounding(string input, string places, string expected)
        {
            var format = new DisplayFormatFilter {
                Type             = DisplayFormatType.Fractional,
                FormatParameters = new Dictionary <string, string> {
                    { nameof(FractionalDisplayParams.DecimalPlaces), places }
                }
            };

            var actual = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.EqualTo(expected));
        }
示例#6
0
        public void DateFormat__returns_null_for_invalid_inputs()
        {
            string input  = "ceci nest pas un date";
            var    format = new DisplayFormatFilter {
                Type             = DisplayFormatType.DateFormat,
                FormatParameters = new Dictionary <string, string> {
                    { nameof(DateDisplayParams.FormatString), "dddd d MMM yyyy" }
                }
            };

            var actual = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.Null);
        }
示例#7
0
        public void DateFormat__reformats_parsable_date_input()
        {
            string input  = "2019-04-11";
            var    format = new DisplayFormatFilter {
                Type             = DisplayFormatType.DateFormat,
                FormatParameters = new Dictionary <string, string> {
                    { nameof(DateDisplayParams.FormatString), "dddd d MMM yyyy" }
                }
            };

            var expected = "Thursday 11 Apr 2019";
            var actual   = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.EqualTo(expected));
        }
示例#8
0
        public void NumberFormat__reformats_numbers(string input, string expected)
        {
            var format = new DisplayFormatFilter {
                Type             = DisplayFormatType.NumberFormat,
                FormatParameters = new Dictionary <string, string> {
                    { nameof(NumberDisplayParams.Prefix), "=$" },
                    { nameof(NumberDisplayParams.Postfix), "=" },
                    { nameof(NumberDisplayParams.DecimalPlaces), "3" },
                    { nameof(NumberDisplayParams.DecimalSeparator), "." },
                    { nameof(NumberDisplayParams.ThousandsSeparator), "'" },
                }
            };

            var actual = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.EqualTo(expected));
        }
示例#9
0
        public void NumberFormat__returns_null_for_invalid_input()
        {
            var input  = "Little house on the prairie";
            var format = new DisplayFormatFilter {
                Type             = DisplayFormatType.NumberFormat,
                FormatParameters = new Dictionary <string, string> {
                    { nameof(NumberDisplayParams.Prefix), "=$" },
                    { nameof(NumberDisplayParams.Postfix), "=" },
                    { nameof(NumberDisplayParams.DecimalPlaces), "3" },
                    { nameof(NumberDisplayParams.DecimalSeparator), "." },
                    { nameof(NumberDisplayParams.ThousandsSeparator), "'" },
                }
            };

            var actual = DisplayFormatter.ApplyFormat(format, input);

            Assert.That(actual, Is.Null);
        }
示例#10
0
        private static string?ReformatNumber(DisplayFormatFilter format, string str)
        {
            var param = format.FormatParameters;

            if (!decimal.TryParse(str, out var value))
            {
                return(null);
            }

            var dpKey = nameof(NumberDisplayParams.DecimalPlaces);
            var dpStr = param.ContainsKey(dpKey) ? param[dpKey] ?? "" : "2";

            if (!int.TryParse(dpStr, out var decimalPlaces))
            {
                decimalPlaces = 2;
            }
            if (decimalPlaces < 0 || decimalPlaces > 20)
            {
                decimalPlaces = 2;
            }

            var tsKey     = nameof(NumberDisplayParams.ThousandsSeparator);
            var thousands = param.ContainsKey(tsKey) ? param[tsKey] ?? "" : "";

            var dcKey            = nameof(NumberDisplayParams.DecimalSeparator);
            var decimalSeparator = param.ContainsKey(dcKey) ? param[dcKey] ?? "" : ".";

            if (string.IsNullOrEmpty(decimalSeparator))
            {
                decimalSeparator = ".";
            }

            var preKey = nameof(NumberDisplayParams.Prefix);
            var prefix = param.ContainsKey(preKey) ? param[preKey] : "";

            var postKey = nameof(NumberDisplayParams.Postfix);
            var postfix = param.ContainsKey(postKey) ? param[postKey] : "";


            var result = FloatToString(value, decimalPlaces, decimalPlaces, decimalSeparator, thousands);


            return(prefix + result + postfix);
        }