summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/mime/jsmime/test/unit/head_xpcshell_glue.js
blob: 6f7fca89f179a4c97f2b7b9e2507bd09200445e4 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// This file needs to contain glue to rephrase the Mocha testsuite framework in
// a way that the xpcshell test suite can understand.

var { Assert } = ChromeUtils.importESModule(
  "resource://testing-common/Assert.sys.mjs"
);
var requireCache = new Map();

// Preload an assert module
var assert = new Assert();
assert.doesNotThrow = function (block, message) {
  message = message ? " " + message : ".";
  try {
    block();
  } catch (e) {
    this.report(true, e, null, "Got unwanted exception" + message);
  }
};
requireCache.set("assert", assert);

// Preload an fs module
var fs = {
  readFile(filename, options, callback) {
    if (callback === undefined) {
      callback = options;
      options = {};
    }

    // Convert according to encoding. For the moment, we don't support this
    // node.js feature in the shim since we don't need to.
    var translator = contents => contents;
    if (options !== undefined && "encoding" in options) {
      translator = function () {
        throw new Error("I can't do this!");
      };
    }

    Promise.resolve(filename)
      .then(do_get_file)
      .then(file => IOUtils.read(file.path))
      .then(translator)
      .then(contents => callback(undefined, contents), callback);
  },
};
requireCache.set("fs", fs);
var { jsmime } = ChromeUtils.import("resource:///modules/jsmime.jsm");
requireCache.set("jsmime", jsmime);

function require(path) {
  if (requireCache.has(path)) {
    return requireCache.get(path);
  }

  let file;
  if (path.startsWith("test/")) {
    let name = path.substring("test/".length);
    file = "resource://testing-common/jsmime/" + name + ".js";
  } else {
    file = "resource:///modules/jsmime/" + path + ".js";
  }

  var globalObject = {
    define: innerDefine.bind(this, path),
  };
  Services.scriptloader.loadSubScript(file, globalObject);
  return requireCache.get(path);
}

function innerDefine(moduleName, dfn) {
  if (typeof dfn !== "function") {
    throw new Error("What is going on here?");
  }
  function resolvingRequire(path) {
    if (path.startsWith("./")) {
      path = path.substring(2);
    }
    return require(path);
  }
  var result = dfn(resolvingRequire);
  requireCache.set(moduleName, result);
}

var define = innerDefine.bind(this, "xpcshell-test");

// Mocha TDD UI Bindings
// ---------------------

/**
 * A block of tests, from the suite class.
 */
function MochaSuite(name) {
  this.name = name;
  this.setup = [];
  this.tests = [];
  this.teardown = [];
  this.suites = [];
}

// The real code for running a suite of tests, written as async function.
MochaSuite.prototype._runSuite = async function () {
  info("Running suite " + this.name);
  for (let setup_ of this.setup) {
    await runFunction(setup_);
  }
  for (let test_ of this.tests) {
    info("Running test " + test_.name);
    await runFunction(test_.test);
  }
  for (let suite_ of this.suites) {
    await suite_.runSuite();
  }
  for (let fn of this.teardown) {
    await runFunction(fn);
  }
  info("Finished suite " + this.name);
};

// The outer call to run a test suite, which returns a promise of completion.
MochaSuite.prototype.runSuite = function () {
  return this._runSuite();
};

// Run the given function, returning a promise of when the test will complete.
function runFunction(fn) {
  let completed = new Promise(function (resolve, reject) {
    function onEnd(error) {
      if (error !== undefined) {
        reject(error);
      } else {
        resolve();
      }
    }
    // If the function is expecting an argument, that argument is the callback
    // above. If it's not, then it may be returning a promise.
    if (fn.length == 1) {
      fn(onEnd);
    } else {
      // Promise.resolve nicely handles both promises and not-promise values for
      // us.
      resolve(fn());
    }
  });
  return completed;
}

var currentSuite = new MochaSuite("");
function suite(name, tests) {
  name = name.toString();
  if (/[\x80-]/.exec(name)) {
    name = "<unprintable name>";
  }
  let suiteParent = currentSuite;
  currentSuite = new MochaSuite(name);
  suiteParent.suites.push(currentSuite);
  tests();
  currentSuite = suiteParent;
}
function test(name, block) {
  name = name.toString();
  if (/[\x80-]/.exec(name)) {
    name = "<unprintable name>";
  }
  currentSuite.tests.push({ name, test: block });
}
function setup(block) {
  currentSuite.setup.push(block);
}
function teardown(block) {
  currentSuite.teardown.push(block);
}

// The actual binding xpcshell needs to do its work.
function run_test() {
  add_task(() => currentSuite.runSuite());
  run_next_test();
}