summaryrefslogtreecommitdiff
path: root/modules/script/jsUnit.js
blob: 369eb2007254b0e65f71b8e06012d3ecd6b477b1 (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// SPDX-License-Identifier: MPL-1.1 OR GPL-2.0-or-later OR LGPL-2.1-or-later
// SPDX-FileCopyrightText: 2003 Edward Hieatt, edward@jsunit.net
// SPDX-FileCopyrightText: 2001-4 Edward Hieatt, edward@jsunit.net
// SPDX-FileCopyrightText: 2008 litl, LLC
// SPDX-FileContributor: @author Edward Hieatt, edward@jsunit.net

/*
 - JsUnit
*/

var JSUNIT_UNDEFINED_VALUE;
var JSUNIT_VERSION="2.1";
var isTestPageLoaded = false;

// GJS: introduce implicit variable to avoid exceptions
var top = null;

//hack for NS62 bug
function jsUnitFixTop() {
  var tempTop = top;
  if (!tempTop) {
    tempTop = window;
    while (typeof tempTop.parent !== 'undefined') {
      tempTop = tempTop.parent;
      if (tempTop.top && tempTop.top.jsUnitTestSuite) {
        tempTop = tempTop.top;
        break;
      }
    }
  }
  top = tempTop;
}

jsUnitFixTop();

function _displayStringForValue(aVar) {
  if (aVar === null)
    return 'null';

  if (aVar === top.JSUNIT_UNDEFINED_VALUE)
    return 'undefined';

  return aVar;
}

function fail(failureMessage) {
    throw new JsUnitException(null, failureMessage);
}

function error(errorMessage) {
    throw new Error(errorMessage);
}

function argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) {
  return args.length == expectedNumberOfNonCommentArgs + 1;
}

function commentArg(expectedNumberOfNonCommentArgs, args) {
  if (argumentsIncludeComments(expectedNumberOfNonCommentArgs, args))
    return args[0];

  return null;
}

function nonCommentArg(desiredNonCommentArgIndex, expectedNumberOfNonCommentArgs, args) {
  return argumentsIncludeComments(expectedNumberOfNonCommentArgs, args) ?
    args[desiredNonCommentArgIndex] :
    args[desiredNonCommentArgIndex - 1];
}

function _validateArguments(expectedNumberOfNonCommentArgs, args) {
  if (!( args.length == expectedNumberOfNonCommentArgs ||
        (args.length == expectedNumberOfNonCommentArgs + 1 && typeof(args[0]) == 'string') ))
    error('Incorrect arguments passed to assert function');
}

function _assert(comment, booleanValue, failureMessage) {
  if (!booleanValue)
    throw new JsUnitException(comment, failureMessage);
}

function assert() {
  _validateArguments(1, arguments);
  var booleanValue=nonCommentArg(1, 1, arguments);

  if (typeof(booleanValue) != 'boolean')
    error('Bad argument to assert(boolean)');

  _assert(commentArg(1, arguments), booleanValue === true, 'Call to assert(boolean) with false');
}

function assertTrue() {
  _validateArguments(1, arguments);
  var booleanValue=nonCommentArg(1, 1, arguments);

  if (typeof(booleanValue) != 'boolean')
    error('Bad argument to assertTrue(boolean)');

  _assert(commentArg(1, arguments), booleanValue === true, 'Call to assertTrue(boolean) with false');
}

function assertFalse() {
  _validateArguments(1, arguments);
  var booleanValue=nonCommentArg(1, 1, arguments);

  if (typeof(booleanValue) != 'boolean')
    error('Bad argument to assertFalse(boolean)');

  _assert(commentArg(1, arguments), booleanValue === false, 'Call to assertFalse(boolean) with true');
}

function assertEquals() {
  _validateArguments(2, arguments);
  var var1=nonCommentArg(1, 2, arguments);
  var var2=nonCommentArg(2, 2, arguments);
  _assert(commentArg(2, arguments), var1 === var2, 'Expected ' + var1 + ' (' + typeof(var1) + ') but was ' + _displayStringForValue(var2) + ' (' + typeof(var2) + ')');
}

function assertNotEquals() {
  _validateArguments(2, arguments);
  var var1=nonCommentArg(1, 2, arguments);
  var var2=nonCommentArg(2, 2, arguments);
  _assert(commentArg(2, arguments), var1 !== var2, 'Expected not to be ' + _displayStringForValue(var2));
}

function assertNull() {
  _validateArguments(1, arguments);
  var aVar=nonCommentArg(1, 1, arguments);
  _assert(commentArg(1, arguments), aVar === null, 'Expected null but was ' + _displayStringForValue(aVar));
}

function assertNotNull() {
  _validateArguments(1, arguments);
  var aVar=nonCommentArg(1, 1, arguments);
  _assert(commentArg(1, arguments), aVar !== null, 'Expected not to be null');
}

function assertUndefined() {
  _validateArguments(1, arguments);
  var aVar=nonCommentArg(1, 1, arguments);
  _assert(commentArg(1, arguments), aVar === top.JSUNIT_UNDEFINED_VALUE, 'Expected undefined but was ' + _displayStringForValue(aVar));
}

function assertNotUndefined() {
  _validateArguments(1, arguments);
  var aVar=nonCommentArg(1, 1, arguments);
  _assert(commentArg(1, arguments), aVar !== top.JSUNIT_UNDEFINED_VALUE, 'Expected not to be undefined');
}

function assertNaN() {
  _validateArguments(1, arguments);
  var aVar=nonCommentArg(1, 1, arguments);
  _assert(commentArg(1, arguments), isNaN(aVar), 'Expected NaN');
}

function assertNotNaN() {
  _validateArguments(1, arguments);
  var aVar=nonCommentArg(1, 1, arguments);
  _assert(commentArg(1, arguments), !isNaN(aVar), 'Expected not NaN');
}

// GJS: assertRaises(function)
function assertRaises() {
    _validateArguments(1, arguments);
    var fun=nonCommentArg(1, 1, arguments);
    var exception;

    if (typeof(fun) != 'function')
        error("Bad argument to assertRaises(function)");

    var retval;
    try {
        retval = fun();
    } catch (e) {
        exception = e;
    }

    _assert(commentArg(1, arguments), exception !== top.JSUNIT_UNDEFINED_VALUE, "Call to assertRaises(function) did not raise an exception. Return value was " + _displayStringForValue(retval) + ' (' + typeof(retval) + ')');
}

function isLoaded() {
  return isTestPageLoaded;
}

function setUp() {
}

function tearDown() {
}

function getFunctionName(aFunction) {
  var name = aFunction.toString().match(/function (\w*)/)[1];

  if ((name == null) || (name.length == 0))
    name = 'anonymous';

  return name;
}

function parseErrorStack(excp)
{
  var stack = [];
  var name;

  if (!excp || !excp.stack)
  {
    return stack;
  }

  var stacklist = excp.stack.split('\n');

  for (var i = 0; i < stacklist.length - 1; i++)
  {
    var framedata = stacklist[i];

    name = framedata.match(/^(\w*)/)[1];
    if (!name) {
      name = 'anonymous';
    }

    var line = framedata.match(/(:\d+)$/)[1];
    if (line) {
        name += line;
    }

    stack[stack.length] = name;
  }
  // remove top level anonymous functions to match IE

  while (stack.length && stack[stack.length - 1] == 'anonymous')
  {
    stack.length = stack.length - 1;
  }
  return stack;
}

function JsUnitException(comment, message) {
  this.isJsUnitException = true;
  this.comment           = comment;
  this.message           = message;
  this.stack             = (new Error()).stack;
}

JsUnitException.prototype = Object.create(Error.prototype, {});

function warn() {
  if (top.tracer != null)
    top.tracer.warn(arguments[0], arguments[1]);
}

function inform() {
  if (top.tracer != null)
    top.tracer.inform(arguments[0], arguments[1]);
}

function info() {
  inform(arguments[0], arguments[1]);
}

function debug() {
  if (top.tracer != null)
    top.tracer.debug(arguments[0], arguments[1]);
}

function setjsUnitTracer(ajsUnitTracer) {
  top.tracer=ajsUnitTracer;
}

function trim(str) {
  if (str == null)
    return null;

  var startingIndex = 0;
  var endingIndex   = str.length-1;

  while (str.substring(startingIndex, startingIndex+1) == ' ')
    startingIndex++;

  while (str.substring(endingIndex, endingIndex+1) == ' ')
    endingIndex--;

  if (endingIndex < startingIndex)
    return '';

  return str.substring(startingIndex, endingIndex+1);
}

function isBlank(str) {
  return trim(str) == '';
}

// the functions push(anArray, anObject) and pop(anArray)
// exist because the JavaScript Array.push(anObject) and Array.pop()
// functions are not available in IE 5.0

function push(anArray, anObject) {
  anArray[anArray.length]=anObject;
}
function pop(anArray) {
  if (anArray.length>=1) {
    delete anArray[anArray.length - 1];
    anArray.length--;
  }
}

// safe, strict access to jsUnitParmHash
function jsUnitGetParm(name)
{
  if (typeof(top.jsUnitParmHash[name]) != 'undefined')
  {
    return top.jsUnitParmHash[name];
  }
  return null;
}

if (top && typeof(top.xbDEBUG) != 'undefined' && top.xbDEBUG.on && top.testManager)
{
  top.xbDebugTraceObject('top.testManager.containerTestFrame', 'JSUnitException');
  // asserts
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', '_displayStringForValue');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'error');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'argumentsIncludeComments');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'commentArg');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'nonCommentArg');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', '_validateArguments');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', '_assert');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assert');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertTrue');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertEquals');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertNotEquals');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertNull');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertNotNull');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertUndefined');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertNotUndefined');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertNaN');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'assertNotNaN');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'isLoaded');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'setUp');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'tearDown');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'getFunctionName');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'warn');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'inform');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'debug');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'setjsUnitTracer');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'trim');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'isBlank');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'newOnLoadEvent');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'push');
  top.xbDebugTraceFunction('top.testManager.containerTestFrame', 'pop');
}

function newOnLoadEvent() {
  isTestPageLoaded = true;
}

function jsUnitSetOnLoad(windowRef, onloadHandler)
{
  var isKonqueror = navigator.userAgent.indexOf('Konqueror/') != -1 ||
                    navigator.userAgent.indexOf('Safari/')    != -1;

  if (typeof(windowRef.attachEvent) != 'undefined') {
    // Internet Explorer, Opera
    windowRef.attachEvent("onload", onloadHandler);
  } else if (typeof(windowRef.addEventListener) != 'undefined' && !isKonqueror){
    // Mozilla, Konqueror
    // exclude Konqueror due to load issues
    windowRef.addEventListener("load", onloadHandler, false);
  } else if (typeof(windowRef.document.addEventListener) != 'undefined' && !isKonqueror) {
    // DOM 2 Events
    // exclude Mozilla, Konqueror due to load issues
    windowRef.document.addEventListener("load", onloadHandler, false);
  } else if (typeof(windowRef.onload) != 'undefined' && windowRef.onload) {
    windowRef.jsunit_original_onload = windowRef.onload;
    windowRef.onload = function() { windowRef.jsunit_original_onload(); onloadHandler(); };
  } else {
    // browsers that do not support windowRef.attachEvent or
    // windowRef.addEventListener will override a page's own onload event
    windowRef.onload=onloadHandler;
  }
}

// GJS: comment out as isLoaded() isn't terribly useful for us
//jsUnitSetOnLoad(window, newOnLoadEvent);

// GJS: entry point to run all functions named as test*, surrounded by
// calls to setUp() and tearDown()
function gjstestRun(window_, setUp, tearDown) {
  var propName;
  var rv = 0;
  var failures = [];
  if (!window_) window_ = window;
  if (!setUp) setUp = window_.setUp;
  if (!tearDown) tearDown = window_.tearDown;

  for (propName in window_) {
    if (!propName.match(/^test\w+/))
      continue;

    var testFunction = window_[propName];
    if (typeof(testFunction) != 'function')
      continue;

    log("running test " + propName);

    setUp();
    try {
      testFunction();
    } catch (e) {
      var result = null;
      if (typeof(e.isJsUnitException) != 'undefined' && e.isJsUnitException) {
        result = '';
        if (e.comment != null)
          result += ('"' + e.comment + '"\n');

        result += e.message;

        if (e.stack)
          result += '\n\nStack trace follows:\n' + e.stack;

        // assertion failure, kind of expected so just log it and flag the
        // whole test as failed
        log(result);
        rv = 1;
        failures.push(propName);
      }
      else {
        // unexpected error, let the shell handle it
        throw e;
      }
    }
    tearDown();
  }

  if (failures.length > 0) {
    log(failures.length + " tests failed in this file");
    log("Failures were: " + failures.join(", "));
  }

  // if gjstestRun() is the last call in a file, this becomes the
  // exit code of the test program, so 0 = success, 1 = failed
  return rv;
}