Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1533 from cdowding-sl/fix/1432
Browse files Browse the repository at this point in the history
Fix #1432
  • Loading branch information
Tom-hayden authored Nov 13, 2019
2 parents d12be61 + 1e4cb3e commit facefdb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;


Expand All @@ -35,14 +37,28 @@ public DataBag(Map<Field, DataBagValue> 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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,36 @@ Feature: User can specify that a value is so formatted
| <expected> |
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" |
| -1.0 | "%f" | "-1.000000" |
| 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" |
Expand Down

0 comments on commit facefdb

Please sign in to comment.