test-modules.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. exports.testDefine = function(assert) {
  5. let tiger = require('./modules/tiger');
  6. assert.equal(tiger.name, 'tiger', 'name proprety was exported properly');
  7. assert.equal(tiger.type, 'cat', 'property form other module exported');
  8. };
  9. exports.testDefineInoresNonFactory = function(assert) {
  10. let mod = require('./modules/async2');
  11. assert.equal(mod.name, 'async2', 'name proprety was exported properly');
  12. assert.ok(mod.traditional2Name !== 'traditional2', '1st is ignored');
  13. };
  14. /* Disable test that require AMD specific functionality:
  15. // define() that exports a function as the module value,
  16. // specifying a module name.
  17. exports.testDefExport = function(assert) {
  18. var add = require('modules/add');
  19. assert.equal(add(1, 1), 2, 'Named define() exporting a function');
  20. };
  21. // define() that exports function as a value, but is anonymous
  22. exports.testAnonDefExport = function (assert) {
  23. var subtract = require('modules/subtract');
  24. assert.equal(subtract(4, 2), 2,
  25. 'Anonymous define() exporting a function');
  26. }
  27. // using require([], function () {}) to load modules.
  28. exports.testSimpleRequire = function (assert) {
  29. require(['modules/blue', 'modules/orange'], function (blue, orange) {
  30. assert.equal(blue.name, 'blue', 'Simple require for blue');
  31. assert.equal(orange.name, 'orange', 'Simple require for orange');
  32. assert.equal(orange.parentType, 'color',
  33. 'Simple require dependency check for orange');
  34. });
  35. }
  36. // using nested require([]) calls.
  37. exports.testSimpleRequireNested = function (assert) {
  38. require(['modules/blue', 'modules/orange', 'modules/green'],
  39. function (blue, orange, green) {
  40. require(['modules/orange', 'modules/red'], function (orange, red) {
  41. assert.equal(red.name, 'red', 'Simple require for red');
  42. assert.equal(red.parentType, 'color',
  43. 'Simple require dependency check for red');
  44. assert.equal(blue.name, 'blue', 'Simple require for blue');
  45. assert.equal(orange.name, 'orange', 'Simple require for orange');
  46. assert.equal(orange.parentType, 'color',
  47. 'Simple require dependency check for orange');
  48. assert.equal(green.name, 'green', 'Simple require for green');
  49. assert.equal(green.parentType, 'color',
  50. 'Simple require dependency check for green');
  51. });
  52. });
  53. }
  54. // requiring a traditional module, that uses async, that use traditional and
  55. // async, with a circular reference
  56. exports.testMixedCircular = function (assert) {
  57. var t = require('modules/traditional1');
  58. assert.equal(t.name, 'traditional1', 'Testing name');
  59. assert.equal(t.traditional2Name, 'traditional2',
  60. 'Testing dependent name');
  61. assert.equal(t.traditional1Name, 'traditional1', 'Testing circular name');
  62. assert.equal(t.async2Name, 'async2', 'Testing async2 name');
  63. assert.equal(t.async2Traditional2Name, 'traditional2',
  64. 'Testing nested traditional2 name');
  65. }
  66. // Testing define()(function(require) {}) with some that use exports,
  67. // some that use return.
  68. exports.testAnonExportsReturn = function (assert) {
  69. var lion = require('modules/lion');
  70. require(['modules/tiger', 'modules/cheetah'], function (tiger, cheetah) {
  71. assert.equal('lion', lion, 'Check lion name');
  72. assert.equal('tiger', tiger.name, 'Check tiger name');
  73. assert.equal('cat', tiger.type, 'Check tiger type');
  74. assert.equal('cheetah', cheetah(), 'Check cheetah name');
  75. });
  76. }
  77. // circular dependency
  78. exports.testCircular = function (assert) {
  79. var pollux = require('modules/pollux'),
  80. castor = require('modules/castor');
  81. assert.equal(pollux.name, 'pollux', 'Pollux\'s name');
  82. assert.equal(pollux.getCastorName(),
  83. 'castor', 'Castor\'s name from Pollux.');
  84. assert.equal(castor.name, 'castor', 'Castor\'s name');
  85. assert.equal(castor.getPolluxName(), 'pollux',
  86. 'Pollux\'s name from Castor.');
  87. }
  88. // test a bad module that asks for exports but also does a define() return
  89. exports.testBadExportAndReturn = function (assert) {
  90. var passed = false;
  91. try {
  92. var bad = require('modules/badExportAndReturn');
  93. } catch(e) {
  94. passed = /cannot use exports and also return/.test(e.toString());
  95. }
  96. assert.equal(passed, true, 'Make sure exports and return fail');
  97. }
  98. // test a bad circular dependency, where an exported value is needed, but
  99. // the return value happens too late, a module already asked for the exported
  100. // value.
  101. exports.testBadExportAndReturnCircular = function (assert) {
  102. var passed = false;
  103. try {
  104. var bad = require('modules/badFirst');
  105. } catch(e) {
  106. passed = /after another module has referenced its exported value/
  107. .test(e.toString());
  108. }
  109. assert.equal(passed, true, 'Make sure return after an exported ' +
  110. 'value is grabbed by another module fails.');
  111. }
  112. // only allow one define call per file.
  113. exports.testOneDefine = function (assert) {
  114. var passed = false;
  115. try {
  116. var dupe = require('modules/dupe');
  117. } catch(e) {
  118. passed = /Only one call to define/.test(e.toString());
  119. }
  120. assert.equal(passed, true, 'Only allow one define call per module');
  121. }
  122. // only allow one define call per file, testing a bad nested define call.
  123. exports.testOneDefineNested = function (assert) {
  124. var passed = false;
  125. try {
  126. var dupe = require('modules/dupeNested');
  127. } catch(e) {
  128. passed = /Only one call to define/.test(e.toString());
  129. }
  130. assert.equal(passed, true, 'Only allow one define call per module');
  131. }
  132. */
  133. require('sdk/test').run(exports);