File: shell.js

package info (click to toggle)
kjs 5.103.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,020 kB
  • sloc: cpp: 36,704; javascript: 5,079; yacc: 790; perl: 191; sh: 52; makefile: 7
file content (111 lines) | stat: -rw-r--r-- 2,360 bytes parent folder | download | duplicates (2)
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
// Utility functions for JS test suite
// (c) 2001 Harri Porten <porten@kde.org>
// (c) 2020 froglogic GmbH <contact@froglogic.com>

function testPassed(msg)
{
  debug("PASS. " + msg);
}

function testFailed(msg)
{
  debug("FAIL. " + msg);
  if (this.regtest)
    regtest.reportResult(false, msg);  
}

function description(s)
{
    debug(s);
}

function evalError(arg, ex)
{
    //return "eval('" + arg + "') => " + ex.toString();
    return ex.toString() + " [eval('" + arg + "')]";
}

function shouldBe(_a, _b)
{
  if (typeof _a != "string" || typeof _b != "string")
    debug("WARN: shouldBe() expects string arguments");
  var _av, _bv;
  try {
    _av = eval(_a);
  } catch (e) {
    _av = evalError(_a, e);
  }
  try {
    _bv = eval(_b);
  } catch (e) {
    _bv = evalError(_b, e);
  }

  if (_av === _bv)
    testPassed(_a + " is " + _b);
  else
    testFailed(_a + " should be " + _bv + ". Was " + _av);
}

function shouldBeTrue(_a) { shouldBe(_a, "true"); }

function shouldBeFalse(_a) { shouldBe(_a, "false"); }

function shouldBeUndefined(_a)
{
  var _av;
  try {
    _av = eval(_a);
  } catch (e) {
    _av = evalError(_a, e);
  }

  if (typeof _av == "undefined")
    testPassed(_a + " is undefined.");
  else
    testFailed(_a + " should be undefined. Was " + _av);
}

function shouldThrow(_a, _e)
{
  var exception;
  var _av;
  try {
     _av = eval(_a);
  } catch (e) {
     exception = e;
  }

  var _ev;
  if (_e)
      _ev =  eval(_e);

  if (exception) {
    if (typeof _e == "undefined" || exception == _ev)
      testPassed(_a + " threw exception " + exception + ".");
    else
      testFailed(_a + " should throw exception " + _ev + ". Threw exception " + exception + ".");
  } else if (typeof _av == "undefined")
    testFailed(_a + " should throw exception " + _e + ". Was undefined.");
  else
    testFailed(_a + " should throw exception " + _e + ". Was " + _av + ".");
}

function shouldExcept(error, func, msg)
{
    var exception;
    try {
        func();
    } catch (e) {
        exception = e;
    }
    if (exception) {
        if (exception instanceof error) {
            test.pass(msg + ": got expected exception " + error.name);
        } else {
            test.fail(msg + ": got wrong exception. Name: " + error.name);
        }
    } else {
        test.fail(msg + ": function did not throw an exception");
    }
}