summaryrefslogtreecommitdiffstats
path: root/dom/encoding/test/unit/head.js
blob: 6608f000c91de08855b8a23ecb4f44318b37dc21 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 *
 * This is a shim for the W3C testharness.js, mapping those of
 * its functions that we need to the testing/xpcshell/head.js API.
 * See <http://www.w3.org/2008/webapps/wiki/Harness> for documentation.
 * This shim does some tests a little differently than the W3C test
 * harness; equality comparisons, especially, are less precise.
 * The difference does not presently affect any test results.
 *
 * We use the lower-level do_report_result throughout this file,
 * rather than the high-level xpcshell/head.js API that has near
 * equivalents for the W3C assert_* functions, because only
 * do_report_result allows us to provide Components.stack.caller.
 */

function assert_equals(a, b, msg) {
  let text =
    msg +
    ": " +
    _wrap_with_quotes_if_necessary(a) +
    " == " +
    _wrap_with_quotes_if_necessary(b);
  do_report_result(a == b, text, Components.stack.caller, false);
}

function assert_not_equals(a, b, msg) {
  let text =
    msg +
    ": " +
    _wrap_with_quotes_if_necessary(a) +
    " != " +
    _wrap_with_quotes_if_necessary(b);
  do_report_result(a != b, text, Components.stack.caller, false);
}

function assert_array_equals(a, b, msg) {
  do_report_result(
    a.length == b.length,
    msg + ": (length) " + a.length + " == " + b.length,
    Components.stack.caller,
    false
  );
  for (let i = 0; i < a.length; ++i) {
    if (a[i] !== b[i]) {
      do_report_result(
        false,
        msg +
          ": [" +
          i +
          "] " +
          _wrap_with_quotes_if_necessary(a[i]) +
          " === " +
          _wrap_with_quotes_if_necessary(b[i]),
        Components.stack.caller,
        false
      );
    }
  }
  // If we get here, all array elements are equal.
  do_report_result(
    true,
    msg + ": all array elements equal",
    Components.stack.caller,
    false
  );
}

function assert_true(cond, msg) {
  do_report_result(
    !!cond,
    msg + ": " + uneval(cond),
    Components.stack.caller,
    false
  );
}

function assert_throws(ex, func) {
  if (!("name" in ex)) {
    do_throw(
      "first argument to assert_throws must be of the form " +
        "{'name': something}"
    );
  }

  let msg = "expected to catch an exception named " + ex.name;

  try {
    func();
  } catch (e) {
    if ("name" in e) {
      do_report_result(
        e.name == ex.name,
        msg + ", got " + e.name,
        Components.stack.caller,
        false
      );
    } else {
      do_report_result(
        false,
        msg + ", got " + legible_exception(ex),
        Components.stack.caller,
        false
      );
    }

    return;
  }

  // Call this here, not in the 'try' clause, so do_report_result's own
  // throw doesn't get caught by our 'catch' clause.
  do_report_result(
    false,
    msg + ", but returned normally",
    Components.stack.caller,
    false
  );
}

var tests = [];

function test(func, msg) {
  tests.push({
    msg,
    func,
    filename: Components.stack.caller.filename,
  });
}

function run_test() {
  tests.forEach(function (t) {
    info(`test group: ${t.msg}, {source_file: ${t.filename}}`);
    t.func();
  });
}