summaryrefslogtreecommitdiffstats
path: root/tests/test.js
blob: 6c4f6de4e4e48ea27ba8be68542865fa3df8b82e (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
var TEST_JS_LOADED // Purposfully left undefined - this is to avoid the global variables being loaded more than once
if (TEST_JS_LOADED == undefined) {
    TEST_JS_LOADED = true

    // Set to true to print out every single comparison
    var verbose = false

    // Set to true to "break" on the first error
    var haltOnError = false

    var errorCount = 0

    function error(message) {
        console.log("+++ " + message)
        printBackTrace()
        errorCount++
        if ( haltOnError )
            throw "error"
    }

    function printBackTrace() {
        var bt = utils.backtrace()
        // We start at 3, so we don't show backtrakce(), printBackTrace() and error()
        for ( var i=3;i<bt.length;i++) {
            console.log("\t" + bt[i])
        }
    }

    function logCompare(act,exp,message) {
        if ( verbose ) {
            if ( message != undefined )
                console.log("comparing(" + message + ") " + act + " with " + exp)
            else
                console.log("comparing " + act + " with " + exp)
        }
    }

    function failCompare(act,exp,message) {
        if ( message != undefined )
            error("Compare failed (" + message +"): actual=" + act + " expected=" + exp)
        else
            error("Compare failed: actual=" + act + " expected=" + exp)
    }

    function compareArrays(act, exp, message) {
        logCompare(act,exp,message)
        if (act.length != exp.length)
            failCompare(act,exp,message)
        else {
            for (var i=0; i<act.length; ++i) {
                if (act[i] != exp[i]) {
                    failCompare(act,exp,message)
                    break
                }
            }
        }
    }

    function compare(act, exp, message) {
        logCompare(act,exp,message)
        if (act != exp)
            failCompare(act,exp,message)
    }

    function comparePositions(act, exp, message) {
        compare(act.line, exp.line, message)
        compare(act.column, exp.column, message)
    }

    function verifyPosition(line,column, message) {
        compare(editor.currentLine, line, message)
        compare(editor.currentColumn, column, message)
    }

    function assert(cond, message) {
        if ( verbose ) {
            if ( message != undefined )
                console.log("assert(" + message + "): " + cond)
            else
                console.log("assert: " + cond)
        }
        if ( !cond ) {
            if ( message != undefined )
                error("Assertion failed: " + message)
            else
                error("Assertion failed")
        }
    }
}