Skip to content
This repository was archived by the owner on Sep 6, 2021. It is now read-only.

Commit 3c12006

Browse files
authored
Implement apiBody and apiQuery tags
Merge pull request #104 from SWheeler17/dev
2 parents 6f25fac + 869c55a commit 3c12006

File tree

7 files changed

+334
-0
lines changed

7 files changed

+334
-0
lines changed

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ var app = {
6161
},
6262
parsers: {
6363
api : './parsers/api.js',
64+
apibody : './parsers/api_body.js',
65+
apiquery : './parsers/api_query.js',
6466
apidefine : './parsers/api_define.js',
6567
apidescription : './parsers/api_description.js',
6668
apierror : './parsers/api_error.js',

lib/parsers/api_body.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Same as @apiParam
2+
var apiParser = require('./api_param.js');
3+
4+
function parse(content, source) {
5+
return apiParser.parse(content, source, 'Body');
6+
}
7+
8+
/**
9+
* Exports
10+
*/
11+
module.exports = {
12+
parse : parse,
13+
path : 'local.body',
14+
method : apiParser.method,
15+
markdownFields: [ 'description' ]
16+
};

lib/parsers/api_query.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Same as @apiParam
2+
var apiParser = require('./api_param.js');
3+
4+
function parse(content, source) {
5+
return apiParser.parse(content, source, 'Query');
6+
}
7+
8+
/**
9+
* Exports
10+
*/
11+
module.exports = {
12+
parse : parse,
13+
path : 'local.query',
14+
method : apiParser.method,
15+
markdownFields: [ 'description' ]
16+
};

lib/workers/api_body_title.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var apiWorker = require('./api_param_title.js');
2+
3+
// Additional information for error log
4+
var _messages = {
5+
common: {
6+
element: 'apiBody',
7+
usage : '@apiBody (group) varname',
8+
example: '@apiDefine MyValidParamGroup Some title\n@apiBody (MyValidParamGroup) username'
9+
}
10+
};
11+
12+
/**
13+
* PreProcess
14+
*
15+
* @param {Object[]} parsedFiles
16+
* @param {String[]} filenames
17+
* @param {Object} packageInfos
18+
* @returns {Object}
19+
*/
20+
function preProcess(parsedFiles, filenames, packageInfos) {
21+
return apiWorker.preProcess(parsedFiles, filenames, packageInfos, 'defineBodyTitle');
22+
}
23+
24+
/**
25+
* PostProcess
26+
*
27+
* @param {Object[]} parsedFiles
28+
* @param {String[]} filenames
29+
* @param {Object[]} preProcess
30+
* @param {Object} packageInfos
31+
*/
32+
function postProcess(parsedFiles, filenames, preProcess, packageInfos) {
33+
apiWorker.postProcess(parsedFiles, filenames, preProcess, packageInfos, 'defineBodyTitle', 'body', _messages);
34+
}
35+
36+
/**
37+
* Exports
38+
*/
39+
module.exports = {
40+
preProcess : preProcess,
41+
postProcess: postProcess
42+
};

lib/workers/api_query_title.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var apiWorker = require('./api_param_title.js');
2+
3+
// Additional information for error log
4+
var _messages = {
5+
common: {
6+
element: 'apiQuery',
7+
usage : '@apiQuery (group) varname',
8+
example: '@apiDefine MyValidParamGroup Some title\n@apiQuery (MyValidParamGroup) username'
9+
}
10+
};
11+
12+
/**
13+
* PreProcess
14+
*
15+
* @param {Object[]} parsedFiles
16+
* @param {String[]} filenames
17+
* @param {Object} packageInfos
18+
* @returns {Object}
19+
*/
20+
function preProcess(parsedFiles, filenames, packageInfos) {
21+
return apiWorker.preProcess(parsedFiles, filenames, packageInfos, 'defineQueryTitle');
22+
}
23+
24+
/**
25+
* PostProcess
26+
*
27+
* @param {Object[]} parsedFiles
28+
* @param {String[]} filenames
29+
* @param {Object[]} preProcess
30+
* @param {Object} packageInfos
31+
*/
32+
function postProcess(parsedFiles, filenames, preProcess, packageInfos) {
33+
apiWorker.postProcess(parsedFiles, filenames, preProcess, packageInfos, 'defineQueryTitle', 'query', _messages);
34+
}
35+
36+
/**
37+
* Exports
38+
*/
39+
module.exports = {
40+
preProcess : preProcess,
41+
postProcess: postProcess
42+
};

test/parser_api_body_test.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*jshint unused:false*/
2+
3+
/**
4+
* Test: Parser apiParam
5+
*/
6+
7+
// node modules
8+
var should = require('should');
9+
10+
// lib modules
11+
var parser = require('../lib/parsers/api_body');
12+
13+
describe('Parser: apiBody', function() {
14+
15+
// TODO: Add 1.000 more possible cases ;-)
16+
var testCases = [
17+
{
18+
title: 'Simple fieldname only',
19+
content: 'simple',
20+
expected: {
21+
group: 'Body',
22+
type: undefined,
23+
size: undefined,
24+
allowedValues: undefined,
25+
optional: false,
26+
field: 'simple',
27+
defaultValue: undefined,
28+
description: ''
29+
}
30+
},
31+
{
32+
title: 'Type, Fieldname, Description',
33+
content: '{String} name The users name.',
34+
expected: {
35+
group: 'Body',
36+
type: 'String',
37+
size: undefined,
38+
allowedValues: undefined,
39+
optional: false,
40+
field: 'name',
41+
defaultValue: undefined,
42+
description: 'The users name.'
43+
}
44+
},
45+
{
46+
title: 'Type, Fieldname, Description',
47+
content: '{String|String[]} name The users name.',
48+
expected: {
49+
group: 'Body',
50+
type: 'String|String[]',
51+
size: undefined,
52+
allowedValues: undefined,
53+
optional: false,
54+
field: 'name',
55+
defaultValue: undefined,
56+
description: 'The users name.'
57+
}
58+
},
59+
{
60+
title: '$Simple fieldname only',
61+
content: '$simple',
62+
expected: {
63+
group: 'Body',
64+
type: undefined,
65+
size: undefined,
66+
allowedValues: undefined,
67+
optional: false,
68+
field: '$simple',
69+
defaultValue: undefined,
70+
description: ''
71+
}
72+
},
73+
{
74+
title: 'All options, with optional defaultValue',
75+
content: ' ( MyGroup ) { \\Object\\String.uni-code_char[] { 1..10 } = \'abc\', \'def\' } ' +
76+
'[ \\MyClass\\field.user_first-name = \'John Doe\' ] Some description.',
77+
expected: {
78+
group: 'MyGroup',
79+
type: '\\Object\\String.uni-code_char[]',
80+
size: '1..10',
81+
allowedValues: [ '\'abc\'', '\'def\'' ],
82+
optional: true,
83+
field: '\\MyClass\\field.user_first-name',
84+
defaultValue: 'John Doe',
85+
description: 'Some description.'
86+
}
87+
},
88+
{
89+
title: 'All options, without optional-marker',
90+
content: ' ( MyGroup ) { \\Object\\String.uni-code_char[] { 1..10 } = \'abc\', \'def\' } ' +
91+
'\\MyClass\\field.user_first-name = \'John Doe\' Some description.',
92+
expected: {
93+
group: 'MyGroup',
94+
type: '\\Object\\String.uni-code_char[]',
95+
size: '1..10',
96+
allowedValues: [ '\'abc\'', '\'def\'' ],
97+
optional: false,
98+
field: '\\MyClass\\field.user_first-name',
99+
defaultValue: 'John Doe',
100+
description: 'Some description.'
101+
}
102+
},
103+
{
104+
title: 'All options, without optional-marker, without default value quotes',
105+
content: ' ( MyGroup ) { \\Object\\String.uni-code_char[] { 1..10 } = \'abc\', \'def\' } ' +
106+
'\\MyClass\\field.user_first-name = John_Doe Some description.',
107+
expected: {
108+
group: 'MyGroup',
109+
type: '\\Object\\String.uni-code_char[]',
110+
size: '1..10',
111+
allowedValues: [ '\'abc\'', '\'def\'' ],
112+
optional: false,
113+
field: '\\MyClass\\field.user_first-name',
114+
defaultValue: 'John_Doe',
115+
description: 'Some description.'
116+
}
117+
},
118+
];
119+
120+
// create
121+
it('case 1: should pass all regexp test cases', function(done) {
122+
testCases.forEach(function(testCase) {
123+
var parsed = parser.parse(testCase.content);
124+
(parsed !== null).should.equal(true, 'Title: ' + testCase.title + ', Source: ' + testCase.content);
125+
parsed.should.eql(testCase.expected);
126+
});
127+
done();
128+
});
129+
130+
});

test/parser_api_query_test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*jshint unused:false*/
2+
3+
/**
4+
* Test: Parser apiParam
5+
*/
6+
7+
// node modules
8+
var should = require('should');
9+
10+
// lib modules
11+
var parser = require('../lib/parsers/api_query');
12+
13+
describe('Parser: apiQuery', function() {
14+
15+
// TODO: Add 1.000 more possible cases ;-)
16+
var testCases = [
17+
{
18+
title: 'Type, Fieldname, Description',
19+
content: '{String} name The users name.',
20+
expected: {
21+
group: 'Query',
22+
type: 'String',
23+
size: undefined,
24+
allowedValues: undefined,
25+
optional: false,
26+
field: 'name',
27+
defaultValue: undefined,
28+
description: 'The users name.'
29+
}
30+
},
31+
{
32+
title: 'Type, Fieldname, Description',
33+
content: '{String|String[]} name The users name.',
34+
expected: {
35+
group: 'Query',
36+
type: 'String|String[]',
37+
size: undefined,
38+
allowedValues: undefined,
39+
optional: false,
40+
field: 'name',
41+
defaultValue: undefined,
42+
description: 'The users name.'
43+
}
44+
},
45+
{
46+
title: '$Simple fieldname only',
47+
content: '$simple',
48+
expected: {
49+
group: 'Query',
50+
type: undefined,
51+
size: undefined,
52+
allowedValues: undefined,
53+
optional: false,
54+
field: '$simple',
55+
defaultValue: undefined,
56+
description: ''
57+
}
58+
},
59+
{
60+
title: 'All options, with optional defaultValue',
61+
content: ' ( MyGroup ) { \\Object\\String.uni-code_char[] { 1..10 } = \'abc\', \'def\' } ' +
62+
'[ \\MyClass\\field.user_first-name = \'John Doe\' ] Some description.',
63+
expected: {
64+
group: 'MyGroup',
65+
type: '\\Object\\String.uni-code_char[]',
66+
size: '1..10',
67+
allowedValues: [ '\'abc\'', '\'def\'' ],
68+
optional: true,
69+
field: '\\MyClass\\field.user_first-name',
70+
defaultValue: 'John Doe',
71+
description: 'Some description.'
72+
}
73+
},
74+
];
75+
76+
// create
77+
it('case 1: should pass all regexp test cases', function(done) {
78+
testCases.forEach(function(testCase) {
79+
var parsed = parser.parse(testCase.content);
80+
(parsed !== null).should.equal(true, 'Title: ' + testCase.title + ', Source: ' + testCase.content);
81+
parsed.should.eql(testCase.expected);
82+
});
83+
done();
84+
});
85+
86+
});

0 commit comments

Comments
 (0)