Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jr:count value should always be coerced to number #789

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ public static int answerDataToInt(IAnswerData countData) {
return (int) Math.floor((Double) count);
} else if (count instanceof Long) {
return (int) ((Long) count).longValue();
} else if (count instanceof String) {
try {
double value = Double.parseDouble((String) count);
return (int) Math.floor(value);
} catch (NumberFormatException e) {
return 0;
}
} else {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void setOtherThanIntegerValueOnRepeatWithCount_convertsValueToInteger() t
while (!scenario.atTheEndOfForm()) {
scenario.next();
}
assertThat(scenario.countRepeatInstancesOf("/data/my-jr-count-repeat"), is(0));
assertThat(scenario.countRepeatInstancesOf("/data/my-jr-count-repeat"), is(1));

// Decimal
scenario.jumpToBeginningOfForm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,64 @@
public class AnswerDataUtilTest {

@Test
public void NumericalDataReturnsProperValueWhenConvertedToInt() {
public void answerDataToInt_returnsCorrectValueForIntegerData() {
assertEquals(1, AnswerDataUtil.answerDataToInt(new IntegerData(1)));
assertEquals(5, AnswerDataUtil.answerDataToInt(new IntegerData(5)));
assertEquals(20, AnswerDataUtil.answerDataToInt(new IntegerData(20)));
}

assertEquals(7, AnswerDataUtil.answerDataToInt(new DecimalData(7.35)));

assertEquals(3, AnswerDataUtil.answerDataToInt(new LongData(3L)));
@Test
public void answerDataToInt_returnsZeroForIntegerDataIfThereIsNoValue() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new IntegerData()));
}

@Test
public void NonNumericalDataReturnsZeroWhenConvertedToInt() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new BooleanData(true)));
assertEquals(0, AnswerDataUtil.answerDataToInt(new SelectOneData(new Selection("Selection1"))));
assertEquals(0, AnswerDataUtil.answerDataToInt(new DateData(new Date())));
assertEquals(0, AnswerDataUtil.answerDataToInt(new StringData("blah")));
public void answerDataToInt_returnsCorrectValueForDecimalData() {
assertEquals(2, AnswerDataUtil.answerDataToInt(new DecimalData(2)));
assertEquals(7, AnswerDataUtil.answerDataToInt(new DecimalData(7.5)));
assertEquals(41, AnswerDataUtil.answerDataToInt(new DecimalData(41)));
}

@Test
public void IfDataTypeHasNoAnswerReturnsZeroWhenConvertedToInt() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new IntegerData()));
public void answerDataToInt_returnsZeroForDecimalDataIfThereIsNoValue() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new DecimalData()));
}

@Test
public void answerDataToInt_returnsCorrectValueForLongData() {
assertEquals(4, AnswerDataUtil.answerDataToInt(new LongData(4L)));
assertEquals(15, AnswerDataUtil.answerDataToInt(new LongData(15L)));
assertEquals(120, AnswerDataUtil.answerDataToInt(new LongData(120L)));
}

@Test
public void answerDataToInt_returnsZeroForLongDataIfThereIsNoValue() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new LongData()));
assertEquals(0, AnswerDataUtil.answerDataToInt(new BooleanData()));
}

@Test
public void answerDataToInt_returnsCorrectValueForStringData() {
assertEquals(6, AnswerDataUtil.answerDataToInt(new StringData("6")));
assertEquals(9, AnswerDataUtil.answerDataToInt(new StringData("9.0")));
assertEquals(17, AnswerDataUtil.answerDataToInt(new StringData("17")));
assertEquals(21, AnswerDataUtil.answerDataToInt(new StringData("21.5")));
assertEquals(53, AnswerDataUtil.answerDataToInt(new StringData("53")));
}

@Test
public void answerDataToInt_returnsZeroForStringDataIfThereIsNoValue() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new StringData()));
}

@Test
public void answerDataToInt_returnsZeroForStringDataIfTheValueCanNotBeConvertedToInteger() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new StringData("blah")));
}

@Test
public void answerDataToInt_returnsZeroForNotSupportedDataTypes() {
assertEquals(0, AnswerDataUtil.answerDataToInt(new BooleanData(true)));
assertEquals(0, AnswerDataUtil.answerDataToInt(new SelectOneData(new Selection("Selection1"))));
assertEquals(0, AnswerDataUtil.answerDataToInt(new DateData(new Date())));
}
}