Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/yummy-meals-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@getodk/xpath': patch
---

Stringify NaN as an empty string according to the XForms specs.
2 changes: 1 addition & 1 deletion packages/scenario/test/instance-input.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ describe.each<InstanceRoundTripCase>([
readonly inner3: ComparableAnswer;
}

const NAN_ANSWER = stringAnswer(String(NaN));
const NAN_ANSWER = stringAnswer('');

interface MissingRepeatInstanceInputCase {
readonly detail: string;
Expand Down
6 changes: 3 additions & 3 deletions packages/xforms-engine/test/instance/instance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('Form instance state', () => {

it.each([
{ firstValue: '2', expected: '4' },
{ firstValue: '', expected: 'NaN' },
{ firstValue: '', expected: '' },
])(
'updates the calculation to $expected when its dependency value is updated to $firstValue',
({ firstValue, expected }) => {
Expand Down Expand Up @@ -331,7 +331,7 @@ describe('Form instance state', () => {
it.each([
{ firstValue: '2', expected: '6' },
{ firstValue: '0', expected: '0' },
{ firstValue: '', expected: 'NaN' },
{ firstValue: '', expected: '' },
])(
'updates the calculated value $expected while it is relevant',
({ firstValue, expected }) => {
Expand All @@ -350,7 +350,7 @@ describe('Form instance state', () => {
it.each([
{ firstValue: '2', expected: '6' },
{ firstValue: '0', expected: '0' },
{ firstValue: '', expected: 'NaN' },
{ firstValue: '', expected: '' },
])(
'updates the calculated value $expected when it becomes relevant after the calculated dependency has been updated',
({ firstValue, expected }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/xpath/src/evaluations/NumberEvaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ export class NumberEvaluation<T extends XPathNode> extends ValueEvaluation<T, 'N

this.booleanValue = value !== 0 && !Number.isNaN(value);
this.numberValue = value;
this.stringValue = String(value);
this.stringValue = Number.isNaN(value) ? '' : String(value);
}
}
8 changes: 8 additions & 0 deletions packages/xpath/test/xforms/coalesce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe('#coalesce()', () => {
testContext.assertStringValue('coalesce(/simple/xpath/to/node, "SECOND")', 'SECOND');
});

it('should return second value if first value is NaN', () => {
testContext.assertStringValue('coalesce(1 * /simple/xpath/to/node, "0")', '0');
testContext.assertStringValue(
'coalesce(/simple/xpath/to/node * /simple/xpath/to/node, "0")',
'0'
);
});

it('coalesce(self::*)', () => {
testContext = createXFormsTestContext(`
<div id="FunctionSelectedCase">
Expand Down
4 changes: 2 additions & 2 deletions packages/xpath/test/xforms/once.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe('once()', () => {
});
});

it('should set value to NaN', () => {
testContext.assertStringValue('once(. * 10)', 'NaN', {
it('should set value to empty string when arithmetic on empty node produces NaN', () => {
testContext.assertStringValue('once(. * 10)', '', {
contextNode,
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/xpath/test/xforms/randomize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('randomize()', () => {
{ seed: 0, expected: 'CBEAFD' },
{ seed: NaN, expected: 'CBEAFD' },
{ seed: Infinity, expected: 'CBEAFD' },
{ seed: -Infinity, expected: 'CFBEAD' },
{ seed: -Infinity, expected: 'CBEAFD' },
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The old test expected 'CFBEAD' because we were incorrectly stringifying NaN as "NaN" and hashing it. The fix correctly stringifies NaN as "", which maps to seed 0 → 'CBEAFD'.

{ seed: 'floor(1.1)', expected: 'BFEACD' },
{ seed: '//xhtml:div[@id="testFunctionNodeset2"]/xhtml:p', expected: 'BFEACD' },
{ seed: MIRROR_HASH_VALUE, expected: MIRROR_HASH_SORT_ORDER },
Expand Down