File: walnut.js

package info (click to toggle)
conkeror 1.0.3%2Bgit170123-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,988 kB
  • sloc: ansic: 280; sh: 255; xml: 173; makefile: 69
file content (93 lines) | stat: -rw-r--r-- 2,286 bytes parent folder | download
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
/**
 * (C) Copyright 2008 John J. Foerch
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

function assert (got) {
    if (! got)
        throw new Error("expected a true value, got <"+got+">.");
    return true;
}

function assert_equals (got, expect) {
    if (got != expect)
        throw new Error("expected <"+expect+">, got <"+got+">.");
    return true;
}

function assert_error (fn) {
    try {
        fn();
    } catch (e) {
        return true;
    }
    throw new Error("expected an error calling <"+fn+">.");
}

function assert_null (got) {
    if (got !== null)
        throw new Error("expected null, got <"+got+">.");
    return true;
}

function assert_not (got) {
    if (got)
        throw new Error("expected a false value, got <"+got+">.");
    return true;
}

function assert_objects_equal (got, expect) {
    if (typeof(got) != "object" || typeof(expect) != "object" ||
        got === null || expect === null)
    {
        return assert_equals(got, expect);
    }
    if (got.constructor !== expect.constructor)
        throw new Error("objects are of different type");
    var expectkeys = object_keys(expect);
    var gotkeys = object_keys(got);
    if (gotkeys.length != expectkeys.length)
        throw new Error("objects have different property counts");
    for (var i in expectkeys) {
        assert_objects_equal(got[i], expect[i]);
    }
    return true;
}


function walnut_results () {
    this.run = 0;
    this.failed = 0;
}

function walnut_run (suite) {
    var results = new walnut_results();
    if (suite.suite_setup)
        suite.suite_setup();
    for (var k in suite) {
        if (k.substr(0,5) == 'test_') {
            if (suite.setup)
                suite.setup();
            results.run++;
            dump(k+'..');
            try {
                suite[k]();
                dumpln('ok');
            } catch (e) {
                results.failed++;
                dumpln('failed');
                dump_error(e);
            }
            if (suite.teardown)
                suite.teardown();
        }
    }
    if (suite.suite_teardown)
        suite.suite_teardown();
    dumpln(results.run+" run, "+results.failed+" failed");
    return results;
}

provide("walnut");