worker_test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* ***** BEGIN LICENSE BLOCK *****
  2. * Distributed under the BSD license:
  3. *
  4. * Copyright (c) 2010, Ajax.org B.V.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Ajax.org B.V. nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ***** END LICENSE BLOCK ***** */
  30. // The loading of ace/worker/worker is complicated, because when we need to load
  31. // it via RequireJS, it needs a shim to make it into a AMD-module.
  32. // When using the AMD-loader it's just works, so we just it load the file now
  33. require("amd-loader");
  34. define(function(require, exports, module) {
  35. "use strict";
  36. var assert = require("../test/assertions");
  37. var worker = require("./worker")
  38. module.exports = {
  39. setUp : function() {
  40. // And define a few mock dependency modules
  41. worker.define("depA", [], function(require, exports, module) {
  42. module.exports = 'dependency A';
  43. })
  44. worker.define("depB", [], function(require, exports, module) {
  45. module.exports = 'dependency B';
  46. })
  47. },
  48. "test: define() with no dependencies, and CommonJS-compatability require()-calls" : function() {
  49. // We want to be able to call define without an id or deps, but
  50. // since we aren't loading an external file, we must explicitly give
  51. // it some kind of id, in this case 'test1'.
  52. worker.require.id = 'test1';
  53. // Now define out module
  54. worker.define(function(require, exports, module) {
  55. var depA = require("depA");
  56. var depB = require("depB");
  57. assert.equal("dependency A", depA);
  58. assert.equal("dependency B", depB);
  59. module.exports = 'test 1';
  60. })
  61. // And then try and require it
  62. var res = worker.require("test1")
  63. assert.equal("test 1", res)
  64. },
  65. "test: define() with dependencies" : function() {
  66. // We want to be able to call define without an id, but since we aren't
  67. // loading an external file, we must explicitly give it some kind of
  68. // id, in this case 'test2'.
  69. worker.require.id = 'test2';
  70. // Now define our module
  71. worker.define(['depA', 'depB'], function(depA, depB) {
  72. assert.equal("dependency A", depA);
  73. assert.equal("dependency B", depB);
  74. return 'test 2';
  75. })
  76. // And then try and require it
  77. var res = worker.require("test2");
  78. assert.equal("test 2", res);
  79. },
  80. "test: define() used require, exports and module as a dependency": function() {
  81. // We want to be able to call define without an id, but since we aren't
  82. // loading an external file, we must explicitly give it some kind of
  83. // id, in this case 'test3'.
  84. worker.require.id = 'test3';
  85. // Now define our module
  86. worker.define(['require', 'exports', 'module', 'depA', 'depB'], function(require, exports, module) {
  87. var depA = require("depA");
  88. var depB = require("depB");
  89. assert.equal("dependency A", depA);
  90. assert.equal("dependency B", depB);
  91. module.exports = 'test 3';
  92. })
  93. // And then try and require it
  94. var res = worker.require("test3");
  95. assert.equal("test 3", res);
  96. },
  97. "test: define() with a mix of require and actual dependecies": function() {
  98. // We want to be able to call define without an id, but since we aren't
  99. // loading an external file, we must explicitly give it some kind of
  100. // id, in this case 'test4'.
  101. worker.require.id = 'test4';
  102. // Now define our module
  103. worker.define(['depA', 'require'], function(depA, require) {
  104. var depB = require("depB");
  105. assert.equal("dependency A", depA);
  106. assert.equal("dependency B", depB);
  107. return 'test 4';
  108. })
  109. // And then try and require it
  110. var res = worker.require("test4");
  111. assert.equal("test 4", res);
  112. }
  113. }
  114. });
  115. if (typeof module !== "undefined" && module === require.main) {
  116. require("asyncjs").test.testcase(module.exports).exec()
  117. }