jstdtestcaseadapter.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright 2015 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Conditionally add "adapter" methods to allow JSTD test cases
  16. * to run under the Closure Test Runner. The goal is to allow tests
  17. * to function regardless of the environment they are running under to allow
  18. * them to transition to the Closure test runner and allow JSTD runner to be
  19. * deprecated.
  20. */
  21. goog.setTestOnly('goog.testing.JsTdTestCaseAdapter');
  22. goog.provide('goog.testing.JsTdTestCaseAdapter');
  23. goog.require('goog.async.run');
  24. goog.require('goog.functions');
  25. goog.require('goog.testing.JsTdAsyncWrapper');
  26. goog.require('goog.testing.TestCase');
  27. goog.require('goog.testing.jsunit');
  28. /**
  29. * @param {string} testCaseName The name of the test case.
  30. * @param {function(): boolean} condition A condition to determine whether to
  31. * run the tests.
  32. * @param {?=} opt_proto An optional prototype object for the test case.
  33. * @param {boolean=} opt_isAsync Whether this test is an async test using the
  34. * JSTD testing queue.
  35. * @return {!Function}
  36. * @private
  37. */
  38. goog.testing.JsTdTestCaseAdapter.TestCaseFactory_ = function(
  39. testCaseName, condition, opt_proto, opt_isAsync) {
  40. /** @constructor */
  41. var T = function() {};
  42. if (opt_proto) T.prototype = opt_proto;
  43. T.displayName = testCaseName;
  44. goog.async.run(function() {
  45. var t = new T();
  46. if (opt_isAsync) {
  47. t = goog.testing.JsTdAsyncWrapper.convertToAsyncTestObj(t);
  48. }
  49. var testCase = new goog.testing.TestCase(testCaseName);
  50. testCase.shouldRunTests = condition;
  51. testCase.setTestObj(t);
  52. goog.testing.TestCase.initializeTestRunner(testCase);
  53. });
  54. return T;
  55. };
  56. /**
  57. * @param {string} testCaseName The name of the test case.
  58. * @param {?=} opt_proto An optional prototype object for the test case.
  59. * @return {!Function}
  60. * @private
  61. */
  62. goog.testing.JsTdTestCaseAdapter.TestCase_ = function(testCaseName, opt_proto) {
  63. return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
  64. testCaseName, goog.functions.TRUE, opt_proto);
  65. };
  66. /**
  67. * @param {string} testCaseName The name of the test case.
  68. * @param {function(): boolean} condition A condition to determine whether to
  69. * run the tests.
  70. * @param {?=} opt_proto An optional prototype object for the test case.
  71. * @return {!Function}
  72. * @private
  73. */
  74. goog.testing.JsTdTestCaseAdapter.ConditionalTestCase_ = function(
  75. testCaseName, condition, opt_proto) {
  76. return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
  77. testCaseName, condition, opt_proto);
  78. };
  79. /**
  80. * @param {string} testCaseName The name of the test case.
  81. * @param {?=} opt_proto An optional prototype object for the test case.
  82. * @return {!Function}
  83. * @private
  84. */
  85. goog.testing.JsTdTestCaseAdapter.AsyncTestCase_ = function(
  86. testCaseName, opt_proto) {
  87. return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
  88. testCaseName, goog.functions.TRUE, opt_proto, true);
  89. };
  90. /**
  91. * @param {string} testCaseName The name of the test case.
  92. * @param {function(): boolean} condition A condition to determine whether to
  93. * run the tests.
  94. * @param {?=} opt_proto An optional prototype object for the test case.
  95. * @return {!Function}
  96. * @private
  97. */
  98. goog.testing.JsTdTestCaseAdapter.AsyncConditionalTestCase_ = function(
  99. testCaseName, condition, opt_proto) {
  100. return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
  101. testCaseName, condition, opt_proto, true);
  102. };
  103. // --- conditionally add polyfills for the basic JSTD API ---
  104. /** @suppress {duplicate} */
  105. var TestCase = TestCase || goog.testing.JsTdTestCaseAdapter.TestCase_;
  106. /** @suppress {duplicate} */
  107. var ConditionalTestCase = ConditionalTestCase ||
  108. goog.testing.JsTdTestCaseAdapter.ConditionalTestCase_;
  109. /** @suppress {duplicate} */
  110. var AsyncTestCase =
  111. AsyncTestCase || goog.testing.JsTdTestCaseAdapter.AsyncTestCase_;
  112. /** @suppress {duplicate} */
  113. var AsyncConditionalTestCase = AsyncConditionalTestCase ||
  114. goog.testing.JsTdTestCaseAdapter.AsyncConditionalTestCase_;
  115. /** @suppress {duplicate} */
  116. var ConditionalAsyncTestCase = ConditionalAsyncTestCase ||
  117. goog.testing.JsTdTestCaseAdapter.AsyncConditionalTestCase_;
  118. // The API is also available under the jstestdriver namespace.
  119. /** @suppress {duplicate} */
  120. var jstestdriver = jstestdriver || {};
  121. if (!jstestdriver.testCaseManager) {
  122. /** A jstestdriver API polyfill. */
  123. jstestdriver.testCaseManager = {
  124. TestCase: TestCase,
  125. ConditionalTestCase: ConditionalTestCase,
  126. AsyncTestCase: AsyncTestCase,
  127. AsyncConditionalTestCase: AsyncConditionalTestCase,
  128. ConditionalAsyncTestCase: ConditionalAsyncTestCase
  129. };
  130. }