public void Null__undefined_format_passes_data_through() { string input = "my input"; var expected = "my input"; var actual = DisplayFormatter.ApplyFormat(null, input); Assert.That(actual, Is.EqualTo(expected)); }
private static string?TryApplyDisplayFormat(DocumentBox box, string?str) { if (box.Definition.DisplayFormat == null) { return(str); } var transformed = DisplayFormatter.ApplyFormat(box.Definition.DisplayFormat, str); return(transformed ?? str); }
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)); }
[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)); }
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)); }
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); }
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)); }
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)); }
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); }
/// <summary> /// Gather the data available for a single template box. /// </summary> private static Result <DocumentBox?> PrepareBox(DataMapper mapper, TemplateBox box, Dictionary <string, decimal> runningTotals, int pageIndex) { try { if (box.MappingPath is null) { return(Result.Failure <DocumentBox?>("Box has no mapping path")); } var str = mapper.TryFindBoxData(box, pageIndex, runningTotals); if (DataMapper.IsSpecialValue(box, out var type)) { return(Result.Success <DocumentBox?>(new DocumentBox(box) { BoxType = type, RenderContent = str })); } if (str == null) { if (box.IsRequired) { return(Result.Failure <DocumentBox?>($"Required data was not found at [{string.Join(".",box.MappingPath)}]")); } return(Result.Success <DocumentBox?>(null)); // empty data is considered OK } if (box.DisplayFormat != null) { str = DisplayFormatter.ApplyFormat(box.DisplayFormat, str); if (str == null) { return(Result.Failure <DocumentBox?>($"Formatter failed: {box.DisplayFormat.Type} applied on {string.Join(".", box.MappingPath!)}")); } } return(Result.Success <DocumentBox?>(new DocumentBox(box, str))); } catch (Exception ex) { return(Result.Failure <DocumentBox>(ex) !); } }