1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 tvcm.require('tvcm.unittest.assertions');
8 tvcm.require('tvcm.quad');
9 tvcm.require('tvcm.rect');
11 tvcm.requireRawScript('gl-matrix/common.js');
12 tvcm.requireRawScript('gl-matrix/vec2.js');
13 tvcm.requireRawScript('gl-matrix/vec3.js');
15 tvcm.unittest.testSuite('tvcm.unittest.assertions_test', function() {
16 function assertionTestSetup() {
17 global.rawAssertThrows = function(fn) {
21 if (e instanceof tvcm.unittest.TestError)
23 throw new Error('Unexpected error from <' + fn + '>: ' + e);
25 throw new Error('Expected <' + fn + '> to throw');
28 global.rawAssertNotThrows = function(fn) {
32 throw new Error('Expected <' + fn + '> to not throw: ' + e.message);
37 function assertionTestTeardown() {
38 global.rawAssertThrows = undefined;
39 global.rawAssertNotThrows = undefined;
42 function assertionTest(name, testFn) {
44 setUp: assertionTestSetup,
45 tearDown: assertionTestTeardown
49 assertionTest('assertTrue', function() {
50 rawAssertThrows(function() {
53 rawAssertNotThrows(function() {
58 assertionTest('assertFalse', function() {
59 rawAssertThrows(function() {
62 rawAssertNotThrows(function() {
67 assertionTest('assertUndefined', function() {
68 rawAssertThrows(function() {
71 rawAssertNotThrows(function() {
72 assertUndefined(undefined);
76 assertionTest('assertNotUndefined', function() {
77 rawAssertThrows(function() {
78 assertNotUndefined(undefined);
80 rawAssertNotThrows(function() {
81 assertNotUndefined('');
85 assertionTest('assertNull', function() {
86 rawAssertThrows(function() {
89 rawAssertNotThrows(function() {
94 assertionTest('assertNotNull', function() {
95 rawAssertThrows(function() {
98 rawAssertNotThrows(function() {
103 assertionTest('assertEquals', function() {
104 rawAssertThrows(function() {
107 rawAssertNotThrows(function() {
115 throw new tvcm.unittest.TestError('Failed to throw');
117 assertNotEquals('Converting circular structure to JSON', e.message);
124 throw new tvcm.unittest.TestError('Failed to throw');
126 assertNotEquals('Converting circular structure to JSON', e.message);
130 assertionTest('assertNotEquals', function() {
131 rawAssertThrows(function() {
132 assertNotEquals(1, 1);
134 rawAssertNotThrows(function() {
135 assertNotEquals(1, 2);
139 assertionTest('assertArrayEquals', function() {
140 rawAssertThrows(function() {
141 assertArrayEquals([2, 3], [2, 4]);
143 rawAssertThrows(function() {
144 assertArrayEquals([1], [1, 2]);
146 rawAssertNotThrows(function() {
147 assertArrayEquals(['a', 'b'], ['a', 'b']);
151 assertionTest('assertArrayEqualsShallow', function() {
152 rawAssertThrows(function() {
153 assertArrayShallowEquals([2, 3], [2, 4]);
155 rawAssertThrows(function() {
156 assertArrayShallowEquals([1], [1, 2]);
158 rawAssertNotThrows(function() {
159 assertArrayShallowEquals(['a', 'b'], ['a', 'b']);
163 assertionTest('assertAlmostEquals', function() {
164 rawAssertThrows(function() {
165 assertAlmostEquals(1, 0);
167 rawAssertThrows(function() {
168 assertAlmostEquals(1, 1.000011);
171 rawAssertNotThrows(function() {
172 assertAlmostEquals(1, 1);
174 rawAssertNotThrows(function() {
175 assertAlmostEquals(1, 1.000001);
177 rawAssertNotThrows(function() {
178 assertAlmostEquals(1, 1 - 0.000001);
182 assertionTest('assertVec2Equals', function() {
183 rawAssertThrows(function() {
184 assertVec2Equals(vec2.fromValues(0, 1), vec2.fromValues(0, 2));
186 rawAssertThrows(function() {
187 assertVec2Equals(vec2.fromValues(1, 2), vec2.fromValues(2, 2));
189 rawAssertNotThrows(function() {
190 assertVec2Equals(vec2.fromValues(1, 1), vec2.fromValues(1, 1));
194 assertionTest('assertVec3Equals', function() {
195 rawAssertThrows(function() {
196 assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(0, 1, 3));
198 rawAssertThrows(function() {
199 assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(0, 3, 2));
201 rawAssertThrows(function() {
202 assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(3, 1, 2));
204 rawAssertNotThrows(function() {
205 assertVec3Equals(vec3.fromValues(1, 2, 3), vec3.fromValues(1, 2, 3));
209 assertionTest('assertQuadEquals', function() {
210 rawAssertThrows(function() {
212 tvcm.Quad.fromXYWH(1, 1, 2, 2), tvcm.Quad.fromXYWH(1, 1, 2, 3));
214 rawAssertNotThrows(function() {
216 tvcm.Quad.fromXYWH(1, 1, 2, 2), tvcm.Quad.fromXYWH(1, 1, 2, 2));
220 assertionTest('assertRectEquals', function() {
221 rawAssertThrows(function() {
223 tvcm.Rect.fromXYWH(1, 1, 2, 2), tvcm.Rect.fromXYWH(1, 1, 2, 3));
225 rawAssertNotThrows(function() {
227 tvcm.Rect.fromXYWH(1, 1, 2, 2), tvcm.Rect.fromXYWH(1, 1, 2, 2));
231 assertionTest('assertObjectEquals', function() {
232 rawAssertThrows(function() {
233 assertObjectEquals({a: 1}, {a: 2});
235 rawAssertThrows(function() {
236 assertObjectEquals({a: 1}, []);
238 rawAssertThrows(function() {
239 assertObjectEquals({a: 1, b: {}}, {a: 1, c: {}, b: {}});
241 rawAssertNotThrows(function() {
242 assertObjectEquals({}, {});
244 rawAssertNotThrows(function() {
245 assertObjectEquals({a: 1}, {a: 1});
249 assertionTest('assertThrows', function() {
250 rawAssertThrows(function() {
251 assertThrows(function() {
254 rawAssertNotThrows(function() {
255 assertThrows(function() {
256 throw new Error('expected_error');
261 assertionTest('assertDoesNotThrow', function() {
262 rawAssertThrows(function() {
263 assertDoesNotThrow(function() {
264 throw new Error('expected_error');
267 rawAssertNotThrows(function() {
268 assertDoesNotThrow(function() {
273 assertionTest('assertApproxEquals', function() {
274 rawAssertThrows(function() {
275 assertApproxEquals(1, 5, 0.5);
277 rawAssertNotThrows(function() {
278 assertApproxEquals(1, 2, 1);
282 assertionTest('assertVisible', function() {
283 rawAssertThrows(function() {
286 rawAssertThrows(function() {
287 assertVisible({offsetHeight: 0});
289 rawAssertThrows(function() {
290 assertVisible({offsetWidth: 0});
292 rawAssertNotThrows(function() {
293 assertVisible({offsetWidth: 1, offsetHeight: 1});