From f0b9776f43360c1c66d35db87e91f6f5a7cbe10f Mon Sep 17 00:00:00 2001 From: mathematicalcoffee Date: Thu, 7 Feb 2013 23:09:45 +1000 Subject: [PATCH 1/3] added tests for dictionary.js and dictionary/definitions.js --- test/specs/jsdoc/tag/dictionary.js | 107 +++++++++++++++++- .../specs/jsdoc/tag/dictionary/definitions.js | 15 +++ 2 files changed, 119 insertions(+), 3 deletions(-) create mode 100644 test/specs/jsdoc/tag/dictionary/definitions.js diff --git a/test/specs/jsdoc/tag/dictionary.js b/test/specs/jsdoc/tag/dictionary.js index af8f72d0..75e1bbc5 100644 --- a/test/specs/jsdoc/tag/dictionary.js +++ b/test/specs/jsdoc/tag/dictionary.js @@ -1,3 +1,104 @@ -describe("jsdoc/tag/dictionary", function() { - //TODO -}); \ No newline at end of file +describe('jsdoc/tag/dictionary', function() { + var dictionary = require('jsdoc/tag/dictionary'); + + it('should exist', function() { + expect(dictionary).toBeDefined(); + expect(typeof dictionary).toEqual('object'); + }); + + it('should export a defineTag function', function() { + expect(dictionary.defineTag).toBeDefined(); + expect(typeof dictionary.defineTag).toEqual('function'); + }); + + it('should export a lookUp function', function() { + expect(dictionary.lookUp).toBeDefined(); + expect(typeof dictionary.lookUp).toEqual('function'); + }); + + it('should export a isNamespace function', function() { + expect(dictionary.isNamespace).toBeDefined(); + expect(typeof dictionary.isNamespace).toEqual('function'); + }); + + it('should export a normalise function', function() { + expect(dictionary.normalise).toBeDefined(); + expect(typeof dictionary.normalise).toEqual('function'); + }); + + // TODO: should really remove this tag from the dictionary after, but how? + var tagOptions = { + canHaveValue: true, + isNamespace: true + }, + tagTitle = 'testTag', + tagSynonym = 'testTag2'; + var def = dictionary.defineTag(tagTitle, tagOptions).synonym(tagSynonym); + // Should really test TagDefinition but they are private. + // Instead, we'll just tests all the properties we expect of it. + describe("defineTag", function() { + + // Since TagDefinition is private, I'll just test for its properties here. + it("returns an object with 'title' property", function() { + expect(typeof def).toEqual('object'); + // how to test? + expect(def.title).toBeDefined(); + expect(typeof def.title).toEqual('string'); + expect(def.title).toEqual(dictionary.normalise(tagTitle)); + }); + + it("returned object has all the tag properties copied over", function() { + for (var prop in tagOptions) { + if (tagOptions.hasOwnProperty(prop)) { + expect(def[prop]).toEqual(tagOptions[prop]); + } + } + }); + }); + + xdescribe("lookUp", function() { + // TODO: BUG: tags are stored in the dictionary with their un-normalised name, + // so if your tag had capital letters then it would be stored as + // _definitions[with capital letters]. However, we *look tags up* + // under their *normalised* names, i.e. will be lower case. + // We should store in the _definitions table under the normalised name. + it("retrieves definition when using the tag's canonical name", function() { + expect(dictionary.lookUp(tagTitle)).toEqual(def); + }); + + it("retrieves definition when using a synonym", function() { + expect(dictionary.lookUp(tagSynonym)).toEqual(def); + }); + + it("returns FALSE when a tag is not found", function() { + expect(dictionary.lookUp('lkjas1l24jk')).toEqual(false); + }); + }); + + describe("isNamespace", function() { + it("returns whether a tag is a namespace when using its canonical name", function() { + expect(dictionary.isNamespace(tagTitle)).toEqual(true); + }); + + // TODO: BUG: isNamespace should use a normalised title. + xit("returns whether a tag is a namespace when using its synonym", function() { + expect(dictionary.isNamespace(tagSynonym)).toEqual(true); + }); + + it("non-existent tags or non-namespace tags should return false", function() { + expect(dictionary.isNamespace('see')).toEqual(false); + expect(dictionary.isNamespace('lkjasd90034')).toEqual(false); + }); + }); + + describe("normalise", function() { + it("should return the tag's title if it is not a synonym", function() { + expect(dictionary.normalise('FooBar')).toEqual('foobar'); + expect(dictionary.normalise(tagTitle)).toEqual(def.title); + }); + + it("should return the canonical name of a tag if the synonym is normalised", function() { + expect(dictionary.normalise(tagSynonym)).toEqual(def.title); + }); + }); +}); diff --git a/test/specs/jsdoc/tag/dictionary/definitions.js b/test/specs/jsdoc/tag/dictionary/definitions.js new file mode 100644 index 00000000..831ae626 --- /dev/null +++ b/test/specs/jsdoc/tag/dictionary/definitions.js @@ -0,0 +1,15 @@ +describe('jsdoc/tag/dictionary/definitions', function() { + var type = require('jsdoc/tag/dictionary/definitions'); + + it('should exist', function() { + expect(type).toBeDefined(); + expect(typeof type).toEqual('object'); + }); + + it('should export a defineTags function', function() { + expect(type.defineTags).toBeDefined(); + expect(typeof type.defineTags).toEqual('function'); + }); + // whole bit of dictionary.defineTags...but it just calls dictionary.defineTag + // and if I validate that then the rest is automatically validated? +}); From 5fadd37fb5e353b5b64b62ac7fbef10394f8be1e Mon Sep 17 00:00:00 2001 From: mathematicalcoffee Date: Fri, 8 Feb 2013 10:02:58 +1000 Subject: [PATCH 2/3] FIX: dictionary.defineTag should store tags under the normalised name. --- lib/jsdoc/tag/dictionary.js | 8 +++++--- test/specs/jsdoc/tag/dictionary.js | 11 +++-------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/jsdoc/tag/dictionary.js b/lib/jsdoc/tag/dictionary.js index 033e6134..c10f8f22 100644 --- a/lib/jsdoc/tag/dictionary.js +++ b/lib/jsdoc/tag/dictionary.js @@ -33,13 +33,15 @@ TagDefinition.prototype.synonym = function(synonymName) { dictionary = { /** @function */ defineTag: function(title, opts) { - _definitions[title] = new TagDefinition(title, opts); + var def = new TagDefinition(title, opts); + // all the other dictionary functions use normalised names; we should too. + _definitions[def.title] = def; if (opts.isNamespace) { - _namespaces.push(title); + _namespaces.push(def.title); } - return _definitions[title]; + return _definitions[def.title]; }, /** @function */ diff --git a/test/specs/jsdoc/tag/dictionary.js b/test/specs/jsdoc/tag/dictionary.js index 75e1bbc5..dfa37525 100644 --- a/test/specs/jsdoc/tag/dictionary.js +++ b/test/specs/jsdoc/tag/dictionary.js @@ -56,12 +56,7 @@ describe('jsdoc/tag/dictionary', function() { }); }); - xdescribe("lookUp", function() { - // TODO: BUG: tags are stored in the dictionary with their un-normalised name, - // so if your tag had capital letters then it would be stored as - // _definitions[with capital letters]. However, we *look tags up* - // under their *normalised* names, i.e. will be lower case. - // We should store in the _definitions table under the normalised name. + describe("lookUp", function() { it("retrieves definition when using the tag's canonical name", function() { expect(dictionary.lookUp(tagTitle)).toEqual(def); }); @@ -76,11 +71,11 @@ describe('jsdoc/tag/dictionary', function() { }); describe("isNamespace", function() { - it("returns whether a tag is a namespace when using its canonical name", function() { + // TODO: BUG: isNamespace should use a normalised title. + xit("returns whether a tag is a namespace when using its canonical name", function() { expect(dictionary.isNamespace(tagTitle)).toEqual(true); }); - // TODO: BUG: isNamespace should use a normalised title. xit("returns whether a tag is a namespace when using its synonym", function() { expect(dictionary.isNamespace(tagSynonym)).toEqual(true); }); From e384a06b455ae1a165bb9b8c22763aae376d2905 Mon Sep 17 00:00:00 2001 From: mathematicalcoffee Date: Fri, 8 Feb 2013 10:03:47 +1000 Subject: [PATCH 3/3] FIX: dictionary.isNamespace should do the lookup under the normalised name --- lib/jsdoc/tag/dictionary.js | 7 +++++-- test/specs/jsdoc/tag/dictionary.js | 5 ++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/jsdoc/tag/dictionary.js b/lib/jsdoc/tag/dictionary.js index c10f8f22..18190502 100644 --- a/lib/jsdoc/tag/dictionary.js +++ b/lib/jsdoc/tag/dictionary.js @@ -57,8 +57,11 @@ dictionary = { /** @function */ isNamespace: function(kind) { - if ( _namespaces.indexOf(kind) !== -1) { - return true; + if (kind) { + kind = dictionary.normalise(kind); + if ( _namespaces.indexOf(kind) !== -1) { + return true; + } } return false; diff --git a/test/specs/jsdoc/tag/dictionary.js b/test/specs/jsdoc/tag/dictionary.js index dfa37525..6c0768b9 100644 --- a/test/specs/jsdoc/tag/dictionary.js +++ b/test/specs/jsdoc/tag/dictionary.js @@ -71,12 +71,11 @@ describe('jsdoc/tag/dictionary', function() { }); describe("isNamespace", function() { - // TODO: BUG: isNamespace should use a normalised title. - xit("returns whether a tag is a namespace when using its canonical name", function() { + it("returns whether a tag is a namespace when using its canonical name", function() { expect(dictionary.isNamespace(tagTitle)).toEqual(true); }); - xit("returns whether a tag is a namespace when using its synonym", function() { + it("returns whether a tag is a namespace when using its synonym", function() { expect(dictionary.isNamespace(tagSynonym)).toEqual(true); });