-
Notifications
You must be signed in to change notification settings - Fork 10
Testing with odk-xpath lib #87
base: master
Are you sure you want to change the base?
Changes from 1 commit
346f20b
5c98ac8
e66d56b
4d97b50
2ed27a9
05338e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -366,8 +366,11 @@ describe( 'Custom "OpenRosa" functions', () => { | |
} ); | ||
|
||
it( 'uuid()', () => { | ||
const result = g.doc.evaluate( 'uuid()', g.doc, null, g.win.XPathResult.STRING_TYPE ); | ||
let result = g.doc.evaluate( 'uuid()', g.doc, null, g.win.XPathResult.STRING_TYPE ); | ||
expect( result.stringValue ).to.have.length( 36 ); | ||
|
||
result = g.doc.evaluate( 'uuid(6)', g.doc, null, g.win.XPathResult.STRING_TYPE ); | ||
expect( result.stringValue ).to.have.length( 6 ); | ||
} ); | ||
|
||
it( 'int()', () => { | ||
|
@@ -1021,7 +1024,7 @@ describe( 'Custom "OpenRosa" functions', () => { | |
g.win.XPathJS.customXPathFunction.remove( 'comment-status' ); | ||
} ); | ||
|
||
xit( 'can be added', () => { | ||
it( 'can be added', () => { | ||
const obj = { | ||
status: 'good' | ||
}; | ||
|
@@ -1035,23 +1038,17 @@ describe( 'Custom "OpenRosa" functions', () => { | |
expect( test1 ).to.throw( /Failed to execute/ ); | ||
|
||
// Add custom function | ||
g.win.XPathJS.customXPathFunction.add( 'comment-status', { | ||
fn( a ) { | ||
const curValue = a.toString(); | ||
let status = ''; | ||
|
||
try { | ||
status = JSON.parse( curValue ).status; | ||
} catch ( e ) { | ||
console.error( 'Could not parse JSON from', curValue ); | ||
} | ||
|
||
return new g.win.XPathJS.customXPathFunction.type.StringType( status ); | ||
}, | ||
args: [ { | ||
t: 'string' | ||
} ], | ||
ret: 'string' | ||
g.win.XPathJS.customXPathFunction.add( 'comment-status', function( a ) { | ||
if(arguments.length !== 1) throw new g.win.Error('Invalid args'); | ||
const curValue = a.v[0]; // {t: 'arr', v: [{'status': 'good'}]} | ||
let status = ''; | ||
try { | ||
status = JSON.parse( curValue ).status; | ||
} catch ( e ) { | ||
console.error( 'Could not parse JSON from', curValue ); | ||
} | ||
|
||
return new g.win.XPathJS.customXPathFunction.type.StringType( status ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That looks good. Feel free to change more if e.g. |
||
} ); | ||
|
||
// Check functioning: | ||
|
@@ -1068,4 +1065,36 @@ describe( 'Custom "OpenRosa" functions', () => { | |
|
||
} ); | ||
|
||
describe('digest', () => { | ||
it( 'digest', () => { | ||
[ | ||
["digest('abc', 'SHA-1', 'hex')", 'a9993e364706816aba3e25717850c26c9cd0d89d'], | ||
["digest('abc', 'SHA-256', 'hex')", 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad'], | ||
["digest('abc', 'SHA-256')", 'ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0='], | ||
["digest('abc', 'SHA-256', 'base64')", 'ungWv48Bz+pBQUDeXa4iI7ADYaOWF3qctBD/YfIAFa0='] | ||
].forEach( async ([expr, expected]) => { | ||
var result = g.doc.evaluate(expr, g.doc, null, g.win.XPathResult.STRING_TYPE, null); | ||
|
||
// The web crypto api only supports async functions and does not support md5 | ||
// https://www.w3.org/TR/WebCryptoAPI/#SubtleCrypto-method-digest | ||
// https://www.w3.org/2012/webcrypto/track/issues/24 | ||
// Is this a weird promise? | ||
expect(await (result.stringValue)).to.equal(expected); | ||
}); | ||
|
||
const invalidAlgoTest = () => { | ||
g.doc.evaluate("digest('abc', 'XYZ', 'hex')", g.doc, null, g.win.XPathResult.STRING_TYPE, null); | ||
} | ||
|
||
expect(invalidAlgoTest).to.throw(); | ||
|
||
const invalidEncodingTest = () => { | ||
g.doc.evaluate("digest('abc', 'SHA-1', 'x')", g.doc, null, g.win.XPathResult.STRING_TYPE, null); | ||
} | ||
|
||
expect(invalidEncodingTest).to.throw(); | ||
} ); | ||
} ); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately, I don't think we can do asynchronous functions in XPath. I am also not very enthusiastic about adding a large library to do this synchronously, so I'm wondering if we should just forget about supporting We do include a library in enketo-express called |
||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no particular format requirement, so this should be good! Fyi, this is the ODK Collect code: https://github.com/opendatakit/javarosa/blob/59611cc0755dfbaf6631866ff15d6a9a516df704/src/main/java/org/javarosa/core/util/PropertyUtils.java#L54