File: assert.js

package info (click to toggle)
event-dance 0.2.0-8~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,096 kB
  • sloc: ansic: 29,033; javascript: 2,709; makefile: 434; xml: 247; sh: 30; python: 27
file content (147 lines) | stat: -rw-r--r-- 3,494 bytes parent folder | download | duplicates (5)
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
//
// Eduardo Lima Mitev <elima@igalia.com>
//
// Specification
//   http://wiki.commonjs.org/wiki/Unit_Testing/1.0
//
// Code pieces adapted from
//   http://github.com/280north/narwhal/blob/master/lib/assert.js
//

let assert = this;

assert.AssertionError = function (options) {
    if (typeof options == "string")
        options = {"message": options};
    this.name = "AssertionError";
    this.message = options.message;
    this.actual = options.actual;
    this.expected = options.expected;
    this.operator = options.operator;
};

assert.AssertionError.prototype = {
    __proto__: Error.prototype,

    toString: function () {
        if (this.message) {
            return [
                this.name + ":",
                this.message
            ].join (" ");
        } else {
            return [
                this.name + ":",
                this.expected,
                this.operator,
                this.actual
            ].join (" ");
        }
    },

    toSource: function () {
        return "new AssertionError " +
            Object.prototype.toSource.call (this) + "";
    }
};

assert.pass = function () {
};

assert.error = function () {
};

assert.fail = function (options) {
    throw (new assert.AssertionError (options));
};

assert.ok = function (value, message) {
    if (!!!value)
        (this.fail || assert.fail) ({
            "actual": value,
            "expected": true,
            "message": message,
            "operator": "=="
        });
    else
        (this.pass || assert.pass) (message);
};

assert.equal = function (actual, expected, message) {
    if (actual != expected)
        (this.fail || assert.fail) ({
            "actual": actual,
            "expected": expected,
            "message": message,
            "operator": "=="
        });
    else
        (this.pass || assert.pass) (message);
};

assert.notEqual = function (actual, expected, message) {
    if (actual == expected)
        (this.fail || assert.fail) ({
            "actual": actual,
            "expected": expected,
            "message": message,
            "operator": "!="
        });
    else
        (this.pass || assert.pass) (message);
};

assert.strictEqual = function (actual, expected, message) {
    if (actual !== expected)
        (this.fail || assert.fail) ({
            "actual": actual,
            "expected": expected,
            "message": message,
            "operator": "==="
        });
    else
        (this.pass || assert.pass) (message);
};

assert.notStrictEqual = function (actual, expected, message) {
    if (actual === expected)
        (this.fail || assert.fail) ({
            "actual": actual,
            "expected": expected,
            "message": message,
            "operator": "!=="
        });
    else
        (this.pass || assert.pass) (message);
};

assert["throws"] = function (block, Error, message) {
    let threw = false;
    let exception = null;

    if (typeof Error == "string") {
        message = Error;
        Error = undefined;
    }

    try {
        block ();
    } catch (e) {
        threw = true;
        exception = e;
    }

    if (! threw) {
        (this.fail || assert.fail) ({
            "message": message,
            "operator": "throw"
        });
    } else if (Error) {
        if (exception instanceof Error)
            (this.pass || assert.pass) (message);
        else
            throw exception;
    } else {
        (this.pass || assert.pass) (message);
    }
};