jasmine-jsdoc.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /* global jasmine: true */
  2. 'use strict';
  3. var fs = require('jsdoc/fs');
  4. var path = require('jsdoc/path');
  5. var jsdoc = {
  6. augment: require('jsdoc/augment'),
  7. doclet: require('jsdoc/doclet'),
  8. env: require('jsdoc/env'),
  9. schema: require('jsdoc/schema'),
  10. src: {
  11. handlers: require('jsdoc/src/handlers'),
  12. parser: require('jsdoc/src/parser')
  13. },
  14. tag: {
  15. dictionary: require('jsdoc/tag/dictionary'),
  16. definitions: require('jsdoc/tag/dictionary/definitions')
  17. }
  18. };
  19. var originalDictionary = jsdoc.tag.dictionary;
  20. var jasmineAll = require('./lib/jasmine');
  21. var jasmine = jasmineAll.jasmine;
  22. var jasmineNode = ( require('./reporter') )(jasmine);
  23. var reporter = null;
  24. var parseResults = [];
  25. jasmine.addParseResults = function(filename, doclets) {
  26. parseResults.push({
  27. filename: filename,
  28. doclets: doclets
  29. });
  30. };
  31. jasmine.getParseResults = function() {
  32. return parseResults;
  33. };
  34. // use the requested parser, or default to the pure JS parser (on Node.js)
  35. jasmine.jsParser = (function() {
  36. var parser = 'js';
  37. if (jsdoc.env.opts.query && jsdoc.env.opts.query.parser) {
  38. parser = jsdoc.env.opts.query.parser;
  39. // remove this so the config tests don't complain
  40. delete jsdoc.env.opts.query;
  41. }
  42. return parser;
  43. })();
  44. jasmine.initialize = function(done) {
  45. var jasmineEnv = jasmine.getEnv();
  46. if (reporter !== null) {
  47. // If we've run before, we need to reset the runner
  48. jasmineEnv.currentRunner_ = new jasmine.Runner(jasmineEnv);
  49. // And clear the reporter
  50. jasmineEnv.reporter.subReporters_.splice(jasmineEnv.reporter.subReporters_.indexOf(reporter));
  51. }
  52. var reporterOpts = {
  53. color: !jsdoc.env.opts.nocolor,
  54. onComplete: done
  55. };
  56. reporter = jsdoc.env.opts.verbose ? new jasmineNode.TerminalVerboseReporter(reporterOpts) :
  57. new jasmineNode.TerminalReporter(reporterOpts);
  58. jasmineEnv.addReporter(reporter);
  59. // updateInterval is set to 0 because there were not-fully-understood
  60. // issues with asynchronous behavior in jasmine otherwise.
  61. jasmineEnv.updateInterval = 0;
  62. return jasmineEnv;
  63. };
  64. jasmine.createParser = function(type) {
  65. return jsdoc.src.parser.createParser(type || jasmine.jsParser);
  66. };
  67. /**
  68. * Execute the specs in the specified folder.
  69. *
  70. * @param {string} folder The folder in which the specs are to be found.
  71. * @param {function?} done Callback function to execute when finished.
  72. * @param {object} opts Options for executing the specs.
  73. * @param {boolean} opts.verbose Whether or not to output verbose results.
  74. * @param {RegExp} opts.matcher A regular expression to filter specs by. Only matching specs run.
  75. */
  76. jasmine.executeSpecsInFolder = function(folder, done, opts) {
  77. var specs = require('./spec-collection');
  78. var fileMatcher = opts.matcher || new RegExp('.(js)$', 'i');
  79. var jasmineEnv = jasmine.initialize(done, opts.verbose);
  80. // Load the specs
  81. specs.load(folder, fileMatcher, true, function() {
  82. var specsList = specs.getSpecs();
  83. var filename;
  84. // Add the specs to the context
  85. for (var i = 0, len = specsList.length; i < len; ++i) {
  86. filename = specsList[i];
  87. require(filename.path().replace(/\\/g, '/')
  88. .replace(new RegExp('^' + jsdoc.env.dirname + '/test'), './')
  89. .replace(/\.\w+$/, ''));
  90. }
  91. // Run Jasmine
  92. jasmineEnv.execute();
  93. });
  94. };
  95. function now() {
  96. return new Date().getTime();
  97. }
  98. jasmine.asyncSpecWait = function() {
  99. var wait = this.asyncSpecWait;
  100. wait.start = now();
  101. wait.done = false;
  102. (function innerWait() {
  103. waits(10);
  104. runs(function() {
  105. if (wait.start + wait.timeout < now()) {
  106. expect('timeout waiting for spec').toBeNull();
  107. } else if (wait.done) {
  108. wait.done = false;
  109. } else {
  110. innerWait();
  111. }
  112. });
  113. })();
  114. };
  115. jasmine.asyncSpecWait.timeout = 4 * 1000;
  116. jasmine.asyncSpecDone = function() {
  117. jasmine.asyncSpecWait.done = true;
  118. };
  119. jasmine.getDocSetFromFile = function(filename, parser, validate, augment) {
  120. var doclets;
  121. var sourceCode = fs.readFileSync( path.join(jsdoc.env.dirname, filename), 'utf8' );
  122. var testParser = parser || jasmine.createParser();
  123. jsdoc.src.handlers.attachTo(testParser);
  124. /* eslint-disable no-script-url */
  125. doclets = testParser.parse('javascript:' + sourceCode);
  126. /* eslint-enable no-script-url */
  127. if (augment !== false) {
  128. jsdoc.augment.augmentAll(doclets);
  129. }
  130. // test assume borrows have not yet been resolved
  131. // require('jsdoc/borrow').resolveBorrows(doclets);
  132. // store the parse results for later validation
  133. if (validate !== false) {
  134. jasmine.addParseResults(filename, doclets);
  135. }
  136. return {
  137. doclets: doclets,
  138. getByLongname: function(longname) {
  139. return doclets.filter(function(doclet) {
  140. return (doclet.longname || doclet.name) === longname;
  141. });
  142. }
  143. };
  144. };
  145. jasmine.replaceTagDictionary = function(dictionaryNames) {
  146. var dict = new jsdoc.tag.dictionary.Dictionary();
  147. var originalDictionaries = jsdoc.env.conf.tags.dictionaries.slice(0);
  148. if (!Array.isArray(dictionaryNames)) {
  149. dictionaryNames = [dictionaryNames];
  150. }
  151. jsdoc.env.conf.tags.dictionaries = dictionaryNames;
  152. jsdoc.tag.definitions.defineTags(dict);
  153. jsdoc.doclet._replaceDictionary(dict);
  154. jsdoc.env.conf.tags.dictionaries = originalDictionaries;
  155. };
  156. jasmine.restoreTagDictionary = function() {
  157. jsdoc.doclet._replaceDictionary(originalDictionary);
  158. };
  159. // set up jasmine's global functions
  160. Object.keys(jasmine).forEach(function(key) {
  161. exports[key] = global[key] = jasmine[key];
  162. });
  163. global.jasmine = jasmine;
  164. require('./async-callback');
  165. ['spyOn', 'it', 'xit', 'expect', 'runs', 'waitsFor', 'beforeEach', 'afterEach', 'describe',
  166. 'xdescribe'].forEach(function(item) {
  167. global[item] = jasmineAll[item];
  168. });