Skip to content

Commit

Permalink
Make Numeric conversion more robust (ex : 'EUR 123 123.5 €' will be c…
Browse files Browse the repository at this point in the history
…onverted to 123123.5 before performing the control)
  • Loading branch information
vertigo17 committed Apr 3, 2024
1 parent 296926c commit 0d02b81
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ private MessageEvent evaluateControlIfNumericXXX(String control, String controlV
} catch (NumberFormatException nfe) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC);
mes.resolveDescription("COND", control);
mes.resolveDescription("STRINGVALUE", newControlValue1);
mes.resolveDescription("NEWSTRING", newControlValue1);
mes.resolveDescription("STRINGVALUE", controlValue1);
return mes;
}

Expand All @@ -579,7 +580,8 @@ private MessageEvent evaluateControlIfNumericXXX(String control, String controlV
} catch (NumberFormatException nfe) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC);
mes.resolveDescription("COND", control);
mes.resolveDescription("STRINGVALUE", newControlValue2);
mes.resolveDescription("NEWSTRING", newControlValue2);
mes.resolveDescription("STRINGVALUE", controlValue2);
return mes;
}

Expand Down Expand Up @@ -1204,22 +1206,26 @@ private MessageEvent switchControl(String control, String path, String actual, S
case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOR:
case TestCaseStepActionControl.CONTROL_VERIFYELEMENTNUMERICMINOROREQUAL:
double value1;
String actualCleaned = StringUtil.prepareToNumeric(actual);
try {
value1 = Double.parseDouble(actual);
value1 = Double.parseDouble(actualCleaned);
} catch (NumberFormatException nfe) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC);
mes.resolveDescription("COND", control);
mes.resolveDescription("NEWSTRING", actualCleaned);
mes.resolveDescription("STRINGVALUE", actual);
return mes;
}

// We try to convert the strings value2 to numeric.
double value2;
String expectedCleaned = StringUtil.prepareToNumeric(expected);
try {
value2 = Double.parseDouble(expected);
value2 = Double.parseDouble(expectedCleaned);
} catch (NumberFormatException nfe) {
mes = new MessageEvent(MessageEventEnum.CONTROL_FAILED_VALUES_NOTNUMERIC);
mes.resolveDescription("COND", control);
mes.resolveDescription("NEWSTRING", expectedCleaned);
mes.resolveDescription("STRINGVALUE", expected);
return mes;
}
Expand Down
20 changes: 17 additions & 3 deletions source/src/main/java/org/cerberus/core/util/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,24 @@ public static boolean isBoolean(String str) {
* get converted to float. For ex, it replace , with .
*/
public static String prepareToNumeric(String str) {
if (str.contains(",")) {
return str.replace(",", ".");
String result = str.replaceAll("[^0-9.,]", "");
if (result.contains(",")) {
result = result.replace(",", ".");
}
return str;
int i = 0;
while (nbChars(result, ".") > 1 && i++ < 100) {
result = result.replaceFirst("\\.", "");
LOG.debug("replaced " + result);
}
LOG.debug("Cleaned string from {} to {}", str, result);

return result;
}

public static int nbChars(String str, String substr) {
LOG.debug(str.length() - str.replace(substr, "").length());
return str.length() - str.replace(substr, "").length();

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* Allow multiple extraParameters on robot chrome (space seperator).
* Added %system.ROBOTSESSIONID% and %system.ROBOTPROVIDERSESSIONID% system variable.
* Allow to have optional subdata and feed cerberus_testdatalib_subdataDefaultValue parameter to defined its default value (Thanks to Promod team ;-)).
* Make Numeric conversion more robust (ex : 'EUR 123 123.5 €' will be converted to 123123.5 before performing the control)

*Warning to be considered before applying the version (deprecated features)*
[square]
Expand Down

0 comments on commit 0d02b81

Please sign in to comment.