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 base.require('base.unittest.assertions');
8 base.require('base.quad');
9 base.require('base.rect');
11 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/common.js');
12 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/vec2.js');
13 base.requireRawScript('../third_party/gl-matrix/src/gl-matrix/vec3.js');
15 base.unittest.testSuite('base.unittest.assertions', function() {
17 global.rawAssertThrows = function(fn) {
21 if (e instanceof base.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);
38 global.rawAssertThrows = undefined;
39 global.rawAssertNotThrows = undefined;
42 test('assertTrue', function() {
43 rawAssertThrows(function() {
46 rawAssertNotThrows(function() {
51 test('assertFalse', function() {
52 rawAssertThrows(function() {
55 rawAssertNotThrows(function() {
60 test('assertUndefined', function() {
61 rawAssertThrows(function() {
64 rawAssertNotThrows(function() {
65 assertUndefined(undefined);
69 test('assertNotUndefined', function() {
70 rawAssertThrows(function() {
71 assertNotUndefined(undefined);
73 rawAssertNotThrows(function() {
74 assertNotUndefined('');
78 test('assertNull', function() {
79 rawAssertThrows(function() {
82 rawAssertNotThrows(function() {
87 test('assertNotNull', function() {
88 rawAssertThrows(function() {
91 rawAssertNotThrows(function() {
96 test('assertEquals', function() {
97 rawAssertThrows(function() {
100 rawAssertNotThrows(function() {
108 throw new base.unittest.TestError('Failed to throw');
110 assertNotEquals('Converting circular structure to JSON', e.message);
117 throw new base.unittest.TestError('Failed to throw');
119 assertNotEquals('Converting circular structure to JSON', e.message);
123 test('assertNotEquals', function() {
124 rawAssertThrows(function() {
125 assertNotEquals(1, 1);
127 rawAssertNotThrows(function() {
128 assertNotEquals(1, 2);
132 test('assertArrayEquals', function() {
133 rawAssertThrows(function() {
134 assertArrayEquals([2, 3], [2, 4]);
136 rawAssertThrows(function() {
137 assertArrayEquals([1], [1, 2]);
139 rawAssertNotThrows(function() {
140 assertArrayEquals(['a', 'b'], ['a', 'b']);
144 test('assertArrayEqualsShallow', function() {
145 rawAssertThrows(function() {
146 assertArrayShallowEquals([2, 3], [2, 4]);
148 rawAssertThrows(function() {
149 assertArrayShallowEquals([1], [1, 2]);
151 rawAssertNotThrows(function() {
152 assertArrayShallowEquals(['a', 'b'], ['a', 'b']);
156 test('assertAlmostEquals', function() {
157 rawAssertThrows(function() {
158 assertAlmostEquals(1, 0);
160 rawAssertThrows(function() {
161 assertAlmostEquals(1, 1.000011);
164 rawAssertNotThrows(function() {
165 assertAlmostEquals(1, 1);
167 rawAssertNotThrows(function() {
168 assertAlmostEquals(1, 1.000001);
170 rawAssertNotThrows(function() {
171 assertAlmostEquals(1, 1 - 0.000001);
175 test('assertVec2Equals', function() {
176 rawAssertThrows(function() {
177 assertVec2Equals(vec2.fromValues(0, 1), vec2.fromValues(0, 2));
179 rawAssertThrows(function() {
180 assertVec2Equals(vec2.fromValues(1, 2), vec2.fromValues(2, 2));
182 rawAssertNotThrows(function() {
183 assertVec2Equals(vec2.fromValues(1, 1), vec2.fromValues(1, 1));
187 test('assertVec3Equals', function() {
188 rawAssertThrows(function() {
189 assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(0, 1, 3));
191 rawAssertThrows(function() {
192 assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(0, 3, 2));
194 rawAssertThrows(function() {
195 assertVec3Equals(vec3.fromValues(0, 1, 2), vec3.fromValues(3, 1, 2));
197 rawAssertNotThrows(function() {
198 assertVec3Equals(vec3.fromValues(1, 2, 3), vec3.fromValues(1, 2, 3));
202 test('assertQuadEquals', function() {
203 rawAssertThrows(function() {
205 base.Quad.fromXYWH(1, 1, 2, 2), base.Quad.fromXYWH(1, 1, 2, 3));
207 rawAssertNotThrows(function() {
209 base.Quad.fromXYWH(1, 1, 2, 2), base.Quad.fromXYWH(1, 1, 2, 2));
213 test('assertRectEquals', function() {
214 rawAssertThrows(function() {
216 base.Rect.fromXYWH(1, 1, 2, 2), base.Rect.fromXYWH(1, 1, 2, 3));
218 rawAssertNotThrows(function() {
220 base.Rect.fromXYWH(1, 1, 2, 2), base.Rect.fromXYWH(1, 1, 2, 2));
224 test('assertObjectEquals', function() {
225 rawAssertThrows(function() {
226 assertObjectEquals({a: 1}, {a: 2});
228 rawAssertThrows(function() {
229 assertObjectEquals({a: 1}, []);
231 rawAssertThrows(function() {
232 assertObjectEquals({a: 1, b: {}}, {a: 1, c: {}, b: {}});
234 rawAssertNotThrows(function() {
235 assertObjectEquals({}, {});
237 rawAssertNotThrows(function() {
238 assertObjectEquals({a: 1}, {a: 1});
242 test('assertThrows', function() {
243 rawAssertThrows(function() {
244 assertThrows(function() {
247 rawAssertNotThrows(function() {
248 assertThrows(function() {
249 throw new Error('expected_error');
254 test('assertDoesNotThrow', function() {
255 rawAssertThrows(function() {
256 assertDoesNotThrow(function() {
257 throw new Error('expected_error');
260 rawAssertNotThrows(function() {
261 assertDoesNotThrow(function() {
266 test('assertApproxEquals', function() {
267 rawAssertThrows(function() {
268 assertApproxEquals(1, 5, 0.5);
270 rawAssertNotThrows(function() {
271 assertApproxEquals(1, 2, 1);
275 test('assertVisible', function() {
276 rawAssertThrows(function() {
279 rawAssertThrows(function() {
280 assertVisible({offsetHeight: 0});
282 rawAssertThrows(function() {
283 assertVisible({offsetWidth: 0});
285 rawAssertNotThrows(function() {
286 assertVisible({offsetWidth: 1, offsetHeight: 1});