From 366b167e8063a434b85b41c2dbc28fb39c271c26 Mon Sep 17 00:00:00 2001 From: Charlton Dowding Date: Mon, 11 Nov 2019 14:23:20 +0000 Subject: [PATCH] fix(#1432): Fixed data bag get formatted value --- .../generation/databags/DataBag.java | 26 ++++++++--- .../operators/general/FormattedAs.feature | 43 +++++++++---------- 2 files changed, 41 insertions(+), 28 deletions(-) diff --git a/generator/src/main/java/com/scottlogic/deg/generator/generation/databags/DataBag.java b/generator/src/main/java/com/scottlogic/deg/generator/generation/databags/DataBag.java index cc7bcd2b3..54753419c 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/generation/databags/DataBag.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/generation/databags/DataBag.java @@ -16,10 +16,12 @@ package com.scottlogic.deg.generator.generation.databags; +import com.scottlogic.deg.common.output.GeneratedObject; import com.scottlogic.deg.common.profile.Field; +import com.scottlogic.deg.common.profile.FieldType; import com.scottlogic.deg.common.util.FlatMappingSpliterator; -import com.scottlogic.deg.common.output.GeneratedObject; +import java.math.BigDecimal; import java.util.*; @@ -35,14 +37,28 @@ public DataBag(Map fieldToValue) { @Override public Object getFormattedValue(Field field) { Object value = getDataBagValue(field).getValue(); + String formatting = field.getFormatting(); - if (field.getFormatting() == null || value == null) { + if (formatting == null || value == null) { return value; } - try { - return String.format(field.getFormatting(), value); - } catch (IllegalFormatException e){ + try + { + if (field.getType() == FieldType.NUMERIC && (formatting.contains("d") || formatting.contains("x") || formatting.contains("o"))) + { + long l = ((BigDecimal) value).longValueExact(); + return String.format(formatting, l); + } + if (field.getType() == FieldType.NUMERIC && (formatting.contains("a"))) + { + double d = ((BigDecimal) value).doubleValue(); + return String.format(formatting, d); + } + return String.format(formatting, value); + } + catch (IllegalFormatException e) + { return value; } } diff --git a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/features/operators/general/FormattedAs.feature b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/features/operators/general/FormattedAs.feature index 53c67db4c..85d06faec 100644 --- a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/features/operators/general/FormattedAs.feature +++ b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/features/operators/general/FormattedAs.feature @@ -14,22 +14,21 @@ Feature: User can specify that a value is so formatted | | Examples: | input | format | expected | -# | 1.0 | "%a" | "0x1.0p0" | no way to specify float over double -# | 1.5 | "%a" | "0x1.8p0" | no way to specify float over double - | 1 | "%b" | "true" | -# | 32 | "%c" | " " | -# | 0 | "%d" | "0" | - # | 1 | "%d" | "1" | -# | 1 | "%20d" | " 1" | -# | 1 | "%-20d" | "1 " | -# | 1 | "%020d" | "00000000000000000001" | - # | 1 | "\|%+20d\|" | "\| +1\|" | - # | -1 | "\|%+20d\|" | "\| -1\|" | - # | 1 | "% d" | " 1" | - # | 1 | "%,d" | "1" | - # | 1111111111111111111 | "%,d" | "1,111,111,111,111,111,111" | - # | 1 | "%(d" | "1" | - # | -1 | "%(d" | "(1)" | + | 1.0 | "%a" | "0x1.0p0" | + | 1.5 | "%a" | "0x1.8p0" | + | 1 | "%b" | "true" | + | 0 | "%d" | "0" | + | 1 | "%d" | "1" | + | 1 | "%20d" | " 1" | + | 1 | "%-20d" | "1 " | + | 1 | "%020d" | "00000000000000000001" | + | 1 | "\|%+20d\|" | "\| +1\|" | + | -1 | "\|%+20d\|" | "\| -1\|" | + | 1 | "% d" | " 1" | + | 1 | "%,d" | "1" | + | 1111111111111111111 | "%,d" | "1,111,111,111,111,111,111" | + | 1 | "%(d" | "1" | + | -1 | "%(d" | "(1)" | | 1.0 | "%e" | "1.000000e+00" | | 123456789.123456789 | "%e" | "1.234568e+08" | | 1.0 | "%f" | "1.000000" | @@ -37,16 +36,14 @@ Feature: User can specify that a value is so formatted | 123456789.123456789 | "%f" | "123456789.123457" | | 1.0 | "%g" | "1.00000" | | 123456789.123456789 | "%g" | "1.23457e+08" | -# | 0 | "%o" | "0" | - # | 123456789 | "%o" | "726746425" | - # | -123456789 | "%o" | "37051031353" | + | 0 | "%o" | "0" | + | 123456789 | "%o" | "726746425" | | 1 | "%s" | "1" | | 1 | "%10s" | " 1" | | 1 | "%-10s" | "1 " | -# | 0 | "%x" | "0" | - # | 1 | "%x" | "1" | -# | 123456789 | "%x" | "75bcd15" | -# | -123456789 | "%x" | "f8a432eb" | + | 0 | "%x" | "0" | + | 1 | "%x" | "1" | + | 123456789 | "%x" | "75bcd15" | | 11111111111 | "%-10s" | "11111111111" | | 1 | "%.5s" | "1" | | 11111111111 | "%.5s" | "11111" |