summaryrefslogtreecommitdiffstats
path: root/QtTest/TestCaseBase.qml
blob: 5eaef462becc57237d06daebf635e6a602903089 (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
import Qt 4.7

Item {
    id: testCase
    visible: false
    property string name
    property variant tests: []

    property string currentTestCase
    property int numPassed
    property int numFailed
    property int numSkipped
    property bool expectingFail
    property string expectFailMsg

    function fail(msg) {
        if (!msg)
            msg = "";
        if (expectingFail) {
            if (expectFailMsg)
                print("XFAIL  : " + currentTestCase + " " + expectFailMsg + " " + msg)
            else
                print("XFAIL  : " + currentTestCase + " " + msg)
            throw new Error("QtTest::expect_fail")
        } else {
            print("FAIL!  : " + currentTestCase + " " + msg)
            throw new Error("QtTest::fail")
        }
    }

    function fail2(msg, msg2) {
        if (msg)
            fail(msg + ": " + msg2)
        else
            fail(msg2)
    }

    function verify(cond, msg) {
        if (!cond)
            fail(msg)
    }

    function compare(actual, expected, msg) {
        if (actual != expected)
            fail2(msg, "actual: " + actual + ", expected: " + expected)
    }

    function skip(msg) {
        print("SKIP   : " + currentTestCase + " " + msg)
        throw new Error("QtTest::skip")
    }

    function expectFail(msg) {
        expectingFail = true
        expectFailMsg = msg
    }

    property variant testCaseResult

    function runInternal(prop, dataDriven, arg, tag) {
        var prefix;
        if (name)
            prefix = name + "::"
        if (dataDriven && tag)
            currentTestCase = prefix + prop + "() [" + tag + "]"
        else
            currentTestCase = prefix + prop + "()"
        expectingFail = false
        var success = true
        try {
            testCaseResult = testCase[prop](arg)
            if (expectingFail) {
                ++numFailed
                success = false
                print("XPASS  : " + currentTestCase)
            } else if (!dataDriven) {
                print("PASS   : " + currentTestCase)
            }
        } catch (e) {
            testCaseResult = []
            if (e.message == "QtTest::fail") {
                ++numFailed
                success = false
            } else if (e.message == "QtTest::skip") {
                ++numSkipped
            } else if (e.message == "QtTest::expect_fail") {
                ++numFailed
            }
        }
        return success
    }

    function run() {
        var success = true
        numPassed = 0
        numFailed = 0
        numSkipped = 0
        for (var prop in testCase) {
            if (prop.indexOf("test_") != 0)
                continue
            var tail = prop.lastIndexOf("_data");
            if (tail != -1 && tail == (prop.length - 5))
                continue
            var datafunc = prop + "_data"
            if (datafunc in testCase) {
                if (runInternal(datafunc, true)) {
                    var table = testCaseResult
                    var successThis = true
                    var haveData = false
                    for (var index in table) {
                        haveData = true
                        var row = table[index]
                        if (!runInternal(prop, true, row, row.tag))
                            successThis = false
                    }
                    if (!haveData)
                        print("WARNING: no data supplied for " + prop + "() by " + datafunc + "()")
                    if (successThis) {
                        var prefix;
                        if (name)
                            prefix = name + "::"
                        currentTestCase = prefix + prop + "()"
                        print("PASS   : " + currentTestCase)
                        ++numPassed
                    } else {
                        success = false
                    }
                } else {
                    success = false
                }
            } else {
                if (runInternal(prop, false))
                    ++numPassed
                else
                    success = false
            }
        }
        currentTestCase = ""
        return success;
    }
}