assert_additions.js revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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. 4 5/** 6 * Asserts that a given argument's value is undefined. 7 * @param {object} a The argument to check. 8 */ 9function assertUndefined(a) { 10 if (a !== undefined) { 11 throw new Error('Assertion failed: expected undefined'); 12 } 13} 14 15/** 16 * Asserts that a given function call throws an exception. 17 * @param {string} msg Message to print if exception not thrown. 18 * @param {Function} fn The function to call. 19 * @param {string} error The name of the exception we expect {@code fn} to 20 * throw. 21 */ 22function assertException(msg, fn, error) { 23 try { 24 fn(); 25 } catch (e) { 26 if (error && e.name != error) { 27 throw new Error('Expected to throw ' + error + ' but threw ' + e.name + 28 ' - ' + msg); 29 } 30 return; 31 } 32 33 throw new Error('Expected to throw exception ' + error + ' - ' + msg); 34} 35 36/** 37 * Asserts that two arrays of strings are equal. 38 * @param {Array.<string>} array1 The expected array. 39 * @param {Array.<string>} array2 The test array. 40 */ 41function assertEqualStringArrays(array1, array2) { 42 var same = true; 43 if (array1.length != array2.length) { 44 same = false; 45 } 46 for (var i = 0; i < Math.min(array1.length, array2.length); i++) { 47 if (array1[i].trim() != array2[i].trim()) { 48 same = false; 49 } 50 } 51 if (!same) { 52 throw new Error('Expected ' + JSON.stringify(array1) + 53 ', got ' + JSON.stringify(array2)); 54 } 55} 56 57/** 58 * Asserts that two objects have the same JSON serialization. 59 * @param {Object} obj1 The expected object. 60 * @param {Object} obj2 The actual object. 61 */ 62function assertEqualsJSON(obj1, obj2) { 63 if (!eqJSON(obj1, obj2)) { 64 throw new Error('Expected ' + JSON.stringify(obj1) + 65 ', got ' + JSON.stringify(obj2)); 66 } 67} 68 69assertSame = assertEquals; 70assertNotSame = assertNotEquals; 71