Skip to content

Commit

Permalink
fix: input number description with unit object
Browse files Browse the repository at this point in the history
  • Loading branch information
nsenave committed Dec 13, 2024
1 parent ae7d59f commit 01228ce
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public class LunaticInputNumberDescription implements ProcessingStep<Questionnai
EnoParameters.Language.FR, "Format attendu : un nombre%s entre %s et %s",
EnoParameters.Language.EN, "Expected format: a number%s between %s and %s");
private static final Map<EnoParameters.Language, String> UNIT_DESCRIPTION_PREFIX = Map.of(
EnoParameters.Language.FR, " en ",
EnoParameters.Language.EN, " in ");
EnoParameters.Language.FR, "en",
EnoParameters.Language.EN, "in");

private final EnoParameters.Language language;
private final Locale locale;
Expand Down Expand Up @@ -54,21 +54,30 @@ private void generateInputNumberDescription(List<ComponentType> lunaticComponent

private void generateInputNumberDescription(InputNumber inputNumber) {
//
String unit = inputNumber.getUnit();
LabelType unitLabel = inputNumber.getUnitLabel();
boolean noUnit = unitLabel == null;
int decimals = inputNumber.getDecimals() != null ? inputNumber.getDecimals().intValue() : 0;
DecimalFormat decimalFormat = (DecimalFormat) NumberFormat.getNumberInstance(locale);
decimalFormat.setMinimumFractionDigits(decimals);
String minDescription = decimalFormat.format(inputNumber.getMin());
String maxDescription = decimalFormat.format(inputNumber.getMax());
//
String unitDescription = unit != null ? UNIT_DESCRIPTION_PREFIX.get(language) + unit : "";
String unitDescription = generateUnitDescription(unitLabel);
String generatedDescription = String.format(INPUT_NUMBER_DESCRIPTION_CANVAS.get(language),
unitDescription, minDescription, maxDescription);
if (!noUnit)
generatedDescription = "\"" + generatedDescription + "\"";
//
LabelType description = new LabelType();
description.setValue(generatedDescription);
description.setType(LabelTypeEnum.TXT);
description.setType(noUnit ? LabelTypeEnum.TXT : LabelTypeEnum.VTL_MD);
inputNumber.setDescription(description);
}

private String generateUnitDescription(LabelType unitLabel) {
if (unitLabel == null)
return "";
return " " + UNIT_DESCRIPTION_PREFIX.get(language) + " \" || " + unitLabel.getValue() + " || \"";
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package fr.insee.eno.core.processing.out.steps.lunatic;

import fr.insee.eno.core.DDIToEno;
import fr.insee.eno.core.exceptions.business.DDIParsingException;
import fr.insee.eno.core.mappers.LunaticMapper;
import fr.insee.eno.core.model.EnoQuestionnaire;
import fr.insee.eno.core.parameter.EnoParameters;
import fr.insee.eno.core.processing.out.steps.lunatic.table.LunaticTableProcessing;
import fr.insee.lunatic.model.flat.InputNumber;
import fr.insee.lunatic.model.flat.LabelType;
import fr.insee.lunatic.model.flat.LabelTypeEnum;
Expand Down Expand Up @@ -39,15 +44,17 @@ void withUnitAndDecimal() {
inputNumber.setMin(0d);
inputNumber.setMax(10d);
inputNumber.setDecimals(BigInteger.ONE);
inputNumber.setUnit("€");
inputNumber.setUnit(new LabelType());
inputNumber.getUnitLabel().setValue("\"\"");
lunaticQuestionnaire.getComponents().add(inputNumber);
//
LunaticInputNumberDescription processing = new LunaticInputNumberDescription(EnoParameters.Language.FR);
processing.apply(lunaticQuestionnaire);
//
LabelType description = lunaticQuestionnaire.getComponents().getFirst().getDescription();
assertEquals("Format attendu : un nombre en € entre 0,0 et 10,0", description.getValue());
assertEquals(LabelTypeEnum.TXT, description.getType());
assertEquals("\"Format attendu : un nombre en \" || \"\" || \" entre 0,0 et 10,0\"",
description.getValue());
assertEquals(LabelTypeEnum.VTL_MD, description.getType());
}

@Test
Expand All @@ -58,15 +65,17 @@ void withUnitNoDecimal() {
inputNumber.setMin(20d);
inputNumber.setMax(1000d);
inputNumber.setDecimals(BigInteger.ZERO);
inputNumber.setUnit("k€");
inputNumber.setUnit(new LabelType());
inputNumber.getUnitLabel().setValue("\"k€\"");
lunaticQuestionnaire.getComponents().add(inputNumber);
//
LunaticInputNumberDescription processing = new LunaticInputNumberDescription(EnoParameters.Language.FR);
processing.apply(lunaticQuestionnaire);
//
LabelType description = lunaticQuestionnaire.getComponents().getFirst().getDescription();
assertEquals("Format attendu : un nombre en k€ entre 20 et 1 000", description.getValue());
assertEquals(LabelTypeEnum.TXT, description.getType());
assertEquals("\"Format attendu : un nombre en \" || \"k€\" || \" entre 20 et 1 000\"",
description.getValue());
assertEquals(LabelTypeEnum.VTL_MD, description.getType());
}

@Test
Expand Down Expand Up @@ -95,16 +104,42 @@ void highBoundValue() {
inputNumber.setMin(0d);
inputNumber.setMax(10_000_000_000d);
inputNumber.setDecimals(BigInteger.ZERO);
inputNumber.setUnit("€");
inputNumber.setUnit(new LabelType());
inputNumber.getUnitLabel().setValue("\"\"");
lunaticQuestionnaire.getComponents().add(inputNumber);
//
LunaticInputNumberDescription processing = new LunaticInputNumberDescription(EnoParameters.Language.FR);
processing.apply(lunaticQuestionnaire);
//
LabelType description = lunaticQuestionnaire.getComponents().getFirst().getDescription();
assertEquals("Format attendu : un nombre en entre 0 et 10 000 000 000",
assertEquals("\"Format attendu : un nombre en \" || \"\" || \" entre 0 et 10 000 000 000\"",
description.getValue());
assertEquals(LabelTypeEnum.TXT, description.getType());
assertEquals(LabelTypeEnum.VTL_MD, description.getType());
}

@Test
void integrationTest() throws DDIParsingException {
// Given
EnoQuestionnaire enoQuestionnaire = DDIToEno.transform(
this.getClass().getClassLoader().getResourceAsStream("integration/ddi/ddi-dynamic-unit.xml"),
EnoParameters.of(EnoParameters.Context.DEFAULT, EnoParameters.ModeParameter.CAWI));
Questionnaire lunaticQuestionnaire = new Questionnaire();
new LunaticMapper().mapQuestionnaire(enoQuestionnaire, lunaticQuestionnaire);
new LunaticSortComponents(enoQuestionnaire).apply(lunaticQuestionnaire);
new LunaticTableProcessing(enoQuestionnaire).apply(lunaticQuestionnaire);

// When
new LunaticInputNumberDescription(EnoParameters.Language.FR).apply(lunaticQuestionnaire);

// Then
InputNumber inputNumber1 = (InputNumber) lunaticQuestionnaire.getComponents().get(1);
InputNumber inputNumber2 = (InputNumber) lunaticQuestionnaire.getComponents().get(3);
assertEquals("\"Format attendu : un nombre en \" || \"\" || \" entre 1 et 10\"",
inputNumber1.getDescription().getValue());
assertEquals("\"Format attendu : un nombre en \" || WHICH_UNIT || \" entre 1 et 10\"",
inputNumber2.getDescription().getValue());
assertEquals(LabelTypeEnum.VTL_MD, inputNumber1.getDescription().getType());
assertEquals(LabelTypeEnum.VTL_MD, inputNumber2.getDescription().getType());
}

}

0 comments on commit 01228ce

Please sign in to comment.