summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/testing/assert_additions.js
blob: 2811966fe7fe29b4adc6e3fb0ae00bc688858105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * Asserts that a given argument's value is undefined.
 * @param {object} a The argument to check.
 */
function assertUndefined(a) {
  if (a !== undefined) {
    throw new Error('Assertion failed: expected undefined');
  }
}

/**
 * Asserts that the argument is neither null nor undefined.
 * @param {object} obj The argument to check.
 * @param {string=} opt_message Error message if the condition is not met.
 */
function assertNotNullNorUndefined(obj, opt_message) {
  if (obj === undefined || obj === null) {
    throw new Error('Can\'t be null or undefined: ' + (opt_message || '') +
        '\n' + 'Actual: ' + a);
  }
}

/**
 * Asserts that a given function call throws an exception.
 * @param {string} msg Message to print if exception not thrown.
 * @param {Function} fn The function to call.
 * @param {string} error The name of the exception we expect {@code fn} to
 *     throw.
 */
function assertException(msg, fn, error) {
  try {
    fn();
  } catch (e) {
    if (error && e.name != error) {
      throw new Error('Expected to throw ' + error + ' but threw ' + e.name +
          ' - ' + msg);
    }
    return;
  }

  throw new Error('Expected to throw exception ' + error + ' - ' + msg);
}

/**
 * Asserts that two arrays of strings are equal.
 * @param {Array<string>} array1 The expected array.
 * @param {Array<string>} array2 The test array.
 */
function assertEqualStringArrays(array1, array2) {
  var same = true;
  if (array1.length != array2.length) {
    same = false;
  }
  for (var i = 0; i < Math.min(array1.length, array2.length); i++) {
    if (array1[i].trim() != array2[i].trim()) {
      same = false;
    }
  }
  if (!same) {
    throw new Error('Expected ' + JSON.stringify(array1) +
                    ', got ' + JSON.stringify(array2));
  }
}

/**
 * Asserts that two objects have the same JSON serialization.
 * @param {Object} expected The expected object.
 * @param {Object} actual The actual object.
 * @param {string=} opt_message Message used for errors.
 */
function assertEqualsJSON(expected, actual, opt_message) {
  if (JSON.stringify(actual) !== JSON.stringify(expected)) {
    throw new Error((opt_message ? opt_message + '\n' : '') +
        'Expected ' + JSON.stringify(expected) + '\n' +
        'Got      ' + JSON.stringify(actual));
  }
}

assertSame = assertEquals;
assertNotSame = assertNotEquals;