Debian Codegraph @

closure-library/closure/goog/testing/jstdtestcaseadapter.js

raw
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.setTestOnly('goog.testing.JsTdTestCaseAdapter');
23 goog.provide('goog.testing.JsTdTestCaseAdapter');
24
25 goog.require('goog.async.run');
26 goog.require('goog.functions');
27 goog.require('goog.testing.JsTdAsyncWrapper');
28 goog.require('goog.testing.TestCase');
29 goog.require('goog.testing.jsunit');
30
31
32 /**
33 * @param {string} testCaseName The name of the test case.
34 * @param {function(): boolean} condition A condition to determine whether to
35 * run the tests.
36 * @param {?=} opt_proto An optional prototype object for the test case.
37 * @param {boolean=} opt_isAsync Whether this test is an async test using the
38 * JSTD testing queue.
39 * @return {!Function}
40 * @private
41 */
42 goog.testing.JsTdTestCaseAdapter.TestCaseFactory_ = function(
43 testCaseName, condition, opt_proto, opt_isAsync) {
44 /** @constructor */
45 var T = function() {};
46 if (opt_proto) T.prototype = opt_proto;
47 T.displayName = testCaseName;
48
49 goog.async.run(function() {
50 var t = new T();
51 if (opt_isAsync) {
52 t = goog.testing.JsTdAsyncWrapper.convertToAsyncTestObj(t);
53 }
54 var testCase = new goog.testing.TestCase(testCaseName);
55 testCase.shouldRunTests = condition;
56 testCase.setTestObj(t);
57 goog.testing.TestCase.initializeTestRunner(testCase);
58 });
59
60 return T;
61 };
62
63
64 /**
65 * @param {string} testCaseName The name of the test case.
66 * @param {?=} opt_proto An optional prototype object for the test case.
67 * @return {!Function}
68 * @private
69 */
70 goog.testing.JsTdTestCaseAdapter.TestCase_ = function(testCaseName, opt_proto) {
71 return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
72 testCaseName, goog.functions.TRUE, opt_proto);
73 };
74
75
76 /**
77 * @param {string} testCaseName The name of the test case.
78 * @param {function(): boolean} condition A condition to determine whether to
79 * run the tests.
80 * @param {?=} opt_proto An optional prototype object for the test case.
81 * @return {!Function}
82 * @private
83 */
84 goog.testing.JsTdTestCaseAdapter.ConditionalTestCase_ = function(
85 testCaseName, condition, opt_proto) {
86 return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
87 testCaseName, condition, opt_proto);
88 };
89
90
91 /**
92 * @param {string} testCaseName The name of the test case.
93 * @param {?=} opt_proto An optional prototype object for the test case.
94 * @return {!Function}
95 * @private
96 */
97 goog.testing.JsTdTestCaseAdapter.AsyncTestCase_ = function(
98 testCaseName, opt_proto) {
99 return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
100 testCaseName, goog.functions.TRUE, opt_proto, true);
101 };
102
103
104 /**
105 * @param {string} testCaseName The name of the test case.
106 * @param {function(): boolean} condition A condition to determine whether to
107 * run the tests.
108 * @param {?=} opt_proto An optional prototype object for the test case.
109 * @return {!Function}
110 * @private
111 */
112 goog.testing.JsTdTestCaseAdapter.AsyncConditionalTestCase_ = function(
113 testCaseName, condition, opt_proto) {
114 return goog.testing.JsTdTestCaseAdapter.TestCaseFactory_(
115 testCaseName, condition, opt_proto, true);
116 };
117
118
119 // --- conditionally add polyfills for the basic JSTD API ---
120
121
122 /** @suppress {duplicate} */
123 var TestCase = TestCase || goog.testing.JsTdTestCaseAdapter.TestCase_;
124
125
126 /** @suppress {duplicate} */
127 var ConditionalTestCase = ConditionalTestCase ||
128 goog.testing.JsTdTestCaseAdapter.ConditionalTestCase_;
129
130
131 /** @suppress {duplicate} */
132 var AsyncTestCase =
133 AsyncTestCase || goog.testing.JsTdTestCaseAdapter.AsyncTestCase_;
134
135
136 /** @suppress {duplicate} */
137 var AsyncConditionalTestCase = AsyncConditionalTestCase ||
138 goog.testing.JsTdTestCaseAdapter.AsyncConditionalTestCase_;
139
140
141 /** @suppress {duplicate} */
142 var ConditionalAsyncTestCase = ConditionalAsyncTestCase ||
143 goog.testing.JsTdTestCaseAdapter.AsyncConditionalTestCase_;
144
145
146 // The API is also available under the jstestdriver namespace.
147
148 /** @suppress {duplicate} */
149 var jstestdriver = jstestdriver || {};
150 if (!jstestdriver.testCaseManager) {
151 /** A jstestdriver API polyfill. */
152 jstestdriver.testCaseManager = {
153 TestCase: TestCase,
154 ConditionalTestCase: ConditionalTestCase,
155 AsyncTestCase: AsyncTestCase,
156 AsyncConditionalTestCase: AsyncConditionalTestCase,
157 ConditionalAsyncTestCase: ConditionalAsyncTestCase
158 };
159 }