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

feat: Add Support for DS within return node #217

Merged
merged 1 commit into from
Mar 31, 2020
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
23 changes: 15 additions & 8 deletions lib/Deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,25 @@ class iPgm {

/**
* @description adds a return element to the program XML
* @param {string} data
* @param {string} value
* @param {string} type
* @param {object} [options]
*/
addReturn(data, type = '1024a', options) {
if (!type) {
iPgmDeprecate('defaulting return type to 1024a has been deprecated. You should specify a type instead.');
// eslint-disable-next-line no-param-reassign
type = '1024a';
addReturn(value, type = '1024a', options) {
const data = {};
data.type = type;
data.value = value;
if (!data.type) {
iPgmDeprecate('defaulting return data type to 1024a has been deprecated. You should specify a type instead.');
data.type = '1024a';
}
iPgmDeprecate('As of v1.0, \'iPgm.addReturn()\' is deprecated. Please use \'ProgramCall.addParam()\' instead.');
return this.ProgramCall.addReturn(data, type, options);

if (typeof options === 'object') { // <data> options
Object.keys(options).forEach((key) => {
data[key] = options[key];
});
}
return this.ProgramCall.addReturn(data);
}

/**
Expand Down
26 changes: 10 additions & 16 deletions lib/ProgramCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,25 +92,19 @@ class ProgramCall {

/**
* @description adds a return element to the program XML
* @param {string} data
* @param {string} type
* @param {object} [options]
* @param {object} data
*/
addReturn(data, type, options = null) {
if (!type) {
throw new Error('Specifying the return type is required.');
addReturn(data = {}) {
if (typeof data !== 'object') {
throw new Error('Expected the first parameter to be an object');
}
this.xml += '<return>';

if (options && typeof options === 'object') {
this.xml += `<data type='${type}'`;
Object.keys(options).forEach((key) => {
this.xml += ` ${key}='${options[key]}'`;
});
} else {
this.xml += `<data type='${type}'`;
if (data.type === undefined) {
throw new Error('Expected \'type\' key on the data object');
}
this.xml += `>${data}</data></return>`;

this.xml += '<return>';
this.__addData__(data);
this.xml += '</return>';
}

/**
Expand Down
30 changes: 25 additions & 5 deletions test/unit/ProgamCallUnit.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ describe('ProgramCall Class Unit Tests', () => {
const pgm = new ProgramCall('MYPGM', { lib: 'MYLIB', func: 'MY_PROCEDURE' });

pgm.addParam({ value: '', type: '1A', by: 'val' });
pgm.addReturn('', '2A', 'output');
pgm.addReturn({ name: 'output', type: '2A', value: '' });

const lookAtXML = pgm.toXML();
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
Expand All @@ -156,7 +156,7 @@ describe('ProgramCall Class Unit Tests', () => {
pgm.addParam({
name: 'inds', type: 'ds', by: 'val', fields: params,
});
pgm.addReturn('', '2A', { name: 'output' });
pgm.addReturn({ name: 'output', type: '2A', value: '' });

const lookAtXML = pgm.toXML();
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
Expand All @@ -168,7 +168,7 @@ describe('ProgramCall Class Unit Tests', () => {
pgm.addParam({
value: '', type: '1A', by: 'val', io: 'both',
});
pgm.addReturn('', '2A', { name: 'output' });
pgm.addReturn({ name: 'output', type: '2A', value: '' });

const lookAtXML = pgm.toXML();
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
Expand All @@ -186,7 +186,7 @@ describe('ProgramCall Class Unit Tests', () => {
pgm.addParam({
name: 'inds', type: 'ds', by: 'val', io: 'both', fields: params,
});
pgm.addReturn('', '2A', { name: 'output' });
pgm.addReturn({ name: 'output', type: '2A', value: '' });

const lookAtXML = pgm.toXML();
expect(lookAtXML).to.match(/<parm .*by='val'.*>/);
Expand All @@ -204,12 +204,32 @@ describe('ProgramCall Class Unit Tests', () => {
error: 'fast',
});

pgm.addReturn('0', '20A');
pgm.addReturn({ type: '20A', value: '0' });

const expectedXML = '<pgm name=\'QTOCNETSTS\' lib=\'QSYS\' func=\'QtoRtvTCPA\' '
+ 'error=\'fast\'><return><data type=\'20A\'>0</data></return></pgm>';

expect(pgm.toXML()).to.equal(expectedXML);
});

it('appends return with ds to pgm xml', () => {
const pgm = new ProgramCall('TEST');

const ds = {
name: 'test_ds',
type: 'ds',
io: 'out',
fields: [
{ type: '10i0', value: 0 },
{ type: '10A', value: '' },
],
};
pgm.addReturn(ds);

const expectedXML = "<pgm name='TEST' error='fast'><return><ds name='test_ds'>"
+ "<data type='10i0'>0</data><data type='10A'></data></ds></return></pgm>";

expect(pgm.toXML()).to.equal(expectedXML);
});
});
});