|
| 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 | +/** |
| 16 | + * @fileoverview Conditionally add "adapter" methods to allow JSTD test cases |
| 17 | + * to run under the Closure Test Runner. The goal is to allow tests |
| 18 | + * to function regardless of the environment they are running under to allow |
| 19 | + * them to transition to the Closure test runner and allow JSTD runner to be |
| 20 | + * deprecated. |
| 21 | + */ |
| 22 | +goog.provide('goog.testing.JsTdTestCaseAdapter'); |
| 23 | + |
| 24 | +goog.require('goog.async.run'); |
| 25 | +goog.require('goog.testing.TestCase'); |
| 26 | +goog.require('goog.testing.jsunit'); |
| 27 | + |
| 28 | + |
| 29 | +/** |
| 30 | + * @param {string} testCaseName The name of the test case. |
| 31 | + * @param {boolean} condition A condition to determine whether to run the tests. |
| 32 | + * @param {?=} opt_proto An optional prototype object for the test case. |
| 33 | + * @return {!Function} |
| 34 | + * @private |
| 35 | + */ |
| 36 | +goog.testing.JsTdTestCaseAdapter.TestCaseFactory_ = function( |
| 37 | + testCaseName, condition, opt_proto) { |
| 38 | + /** @constructor */ |
| 39 | + var T = function() {}; |
| 40 | + if (opt_proto) T.prototype = opt_proto; |
| 41 | + T.displayName = testCaseName; |
| 42 | + |
| 43 | + goog.async.run(function() { |
| 44 | + var t = condition ? new T() : {}; |
| 45 | + var testCase = new goog.testing.TestCase(testCaseName); |
| 46 | + testCase.shouldRunTests = function() { |
| 47 | + return condition; |
| 48 | + }; |
| 49 | + testCase.setTestObj(t); |
| 50 | + goog.testing.TestCase.initializeTestRunner(testCase); |
| 51 | + }); |
| 52 | + |
| 53 | + return T; |
| 54 | +}; |
| 55 | + |
| 56 | +// --- conditionally add polyfills for the basic JSTD API --- |
| 57 | + |
| 58 | + |
| 59 | +/** |
| 60 | + * @param {string} testCaseName The name of the test case. |
| 61 | + * @param {?=} opt_proto An optional prototype object for the test case. |
| 62 | + * @return {!Function} |
| 63 | + * @suppress {duplicate} |
| 64 | + */ |
| 65 | +var TestCase = TestCase || function(testCaseName, opt_proto) { |
| 66 | + return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_( |
| 67 | + testCaseName, true, opt_proto); |
| 68 | +}; |
| 69 | + |
| 70 | + |
| 71 | +/** |
| 72 | + * @param {string} testCaseName The name of the test case. |
| 73 | + * @param {boolean} condition A condition to determine whether to run the tests. |
| 74 | + * @param {?=} opt_proto An optional prototype object for the test case. |
| 75 | + * @return {!Function} |
| 76 | + * @suppress {duplicate} |
| 77 | + */ |
| 78 | +var ConditionalTestCase = ConditionalTestCase || function( |
| 79 | + testCaseName, condition, opt_proto) { |
| 80 | + return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_( |
| 81 | + testCaseName, condition, opt_proto); |
| 82 | +}; |
| 83 | + |
| 84 | + |
| 85 | +/** |
| 86 | + * @param {string} testCaseName The name of the test case. |
| 87 | + * @param {?=} opt_proto An optional prototype object for the test case. |
| 88 | + * @return {!Function} |
| 89 | + * @suppress {duplicate} |
| 90 | + */ |
| 91 | +var AsyncTestCase = AsyncTestCase || TestCase; |
| 92 | + |
| 93 | + |
| 94 | +/** |
| 95 | + * @param {string} testCaseName The name of the test case. |
| 96 | + * @param {boolean} condition A condition to determine whether to run the tests. |
| 97 | + * @param {?=} opt_proto An optional prototype object for the test case. |
| 98 | + * @return {!Function} |
| 99 | + * @suppress {duplicate} |
| 100 | + */ |
| 101 | +var AsyncConditionalTestCase = AsyncConditionalTestCase || ConditionalTestCase; |
| 102 | + |
| 103 | + |
| 104 | +// The API is also available under the jstestdriver namespace. |
| 105 | + |
| 106 | +var jstestdriver = jstestdriver || {}; |
| 107 | +if (!jstestdriver.testCaseManager) { |
| 108 | + /** A jstestdriver API polyfill. */ |
| 109 | + jstestdriver.testCaseManager = { |
| 110 | + TestCase: TestCase, |
| 111 | + ConditionalTestCase: ConditionalTestCase, |
| 112 | + AsyncTestCase: AsyncTestCase, |
| 113 | + AsyncConditionalTestCase: AsyncConditionalTestCase |
| 114 | + }; |
| 115 | +} |
| 116 | + |
0 commit comments