handle default values that are array literals (#604)

plus some test cleanup
This commit is contained in:
Jeff Williams 2014-03-18 10:16:41 -07:00
parent cd6c89ec10
commit 8128dc5335
6 changed files with 44 additions and 9 deletions

View File

@ -269,7 +269,7 @@ var DOCLET_SCHEMA = exports.DOCLET_SCHEMA = {
defaultvaluetype: {
type: STRING,
optional: true,
enum: [OBJECT]
enum: [OBJECT, ARRAY]
},
// is usage of this symbol deprecated?
deprecated: {

View File

@ -123,6 +123,21 @@ var nodeToString = exports.nodeToString = function(node) {
var str = '';
switch (node.type) {
case Syntax.ArrayExpression:
tempObject = [];
node.elements.forEach(function(el, i) {
// preserve literal values so that the JSON form shows the correct type
if (el.type === Syntax.Literal) {
tempObject[i] = el.value;
}
else {
tempObject[i] = nodeToString(el);
}
});
str = JSON.stringify(tempObject);
break;
case Syntax.AssignmentExpression:
str = nodeToString(node.left);
break;

View File

@ -301,6 +301,11 @@ exports.defineTags = function(dictionary) {
value = doclet.meta.code.value;
switch(type) {
case Syntax.ArrayExpression:
doclet.defaultvalue = astnode.nodeToString(doclet.meta.code.node);
doclet.defaultvaluetype = 'array';
break;
case Syntax.Literal:
doclet.defaultvalue = String(value);
break;

View File

@ -3,8 +3,8 @@ var data = obj;
var self = this;
var defaultObjectClass = '';
// Check if the default value is an object, if so, apply code highlighting
if (data.defaultvalue && data.defaultvaluetype === 'object') {
// Check if the default value is an object or array; if so, apply code highlighting
if (data.defaultvalue && (data.defaultvaluetype === 'object' || data.defaultvaluetype === 'array')) {
data.defaultvalue = "<pre class=\"prettyprint\"><code>" + data.defaultvalue + "</code></pre>";
defaultObjectClass = ' class="object-value"';
}

View File

@ -46,3 +46,6 @@ var multilineObject = {
valueB : false,
valueC : 7
};
/** @default */
var arr = ['foo', true, 19];

View File

@ -10,6 +10,7 @@ describe("@default tag", function() {
var header = (docSet.getByLongname('header') || [])[0];
var obj = docSet.getByLongname('obj')[0];
var multilineObject = docSet.getByLongname('multilineObject')[0];
var arr = docSet.getByLongname('arr')[0];
it('When symbol set to null has a @default tag with no text, the doclet\'s defaultValue property should be: null', function() {
expect(request.defaultvalue).toBe('null');
@ -39,14 +40,25 @@ describe("@default tag", function() {
expect(header.defaultvalue).toBeUndefined();
});
it('When symbol has a @default tag with an object, the doclet\'s defaultValue property should contain the stringified object', function() {
var expected_value = '{"valueA":"a","valueB":false,"valueC":7}';
expect(obj.defaultvalue).toEqual(expected_value);
it('When symbol has a @default tag with an object, the doclet should contain the stringified object', function() {
var testObj = { valueA: 'a', valueB: false, valueC: 7};
expect(obj.defaultvalue).toBe( JSON.stringify(testObj) );
expect(obj.defaultvaluetype).toBe('object');
});
it('When symbol has a @default tag with a multiline object, the doclet\'s defaultValue property should contain the properly stringified object', function() {
var expected_value = '{"valueA":"a","valueB":false,"valueC":7}';
expect(obj.defaultvalue).toEqual(expected_value);
it('When symbol has a @default tag with a multiline object, the doclet should contain the stringified object', function() {
var testObj = {
valueA: 'a',
valueB: false,
valueC: 7
};
expect(obj.defaultvalue).toBe( JSON.stringify(testObj) );
expect(obj.defaultvaluetype).toBe('object');
});
it('When symbol has a @default tag with an array, the doclet should contain the stringified array', function() {
var testArray = ['foo', true, 19];
expect(arr.defaultvalue).toBe( JSON.stringify(testArray) );
expect(arr.defaultvaluetype).toBe('array');
});
});