~saviq/+junk/interface-tests

« back to all changes in this revision

Viewing changes to Util/Verifier.qml

  • Committer: Michał Sawicz
  • Date: 2013-04-22 16:36:42 UTC
  • Revision ID: michal.sawicz@canonical.com-20130422163642-7wbump445hn9moyj
refactor Util.TestCase into Util.Verifier

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import Util 0.1
4
4
 
5
5
TestCase {
6
 
    property var verifier
7
 
 
8
 
    verifier: QtObject {
9
 
        /* obj is the object on which the tests are carried out, name is used
10
 
           to identify the object in human-readable way */
11
 
        property var object
12
 
        property string name
13
 
 
14
 
        /* these are the default fail strings used during verification
15
 
           they can be overriden here or via optional arguments to the
16
 
           methods below */
17
 
        property string constantMsg: strings.constantMsg
18
 
        property string changedMsg: strings.changedMsg
19
 
        property string writableMsg: strings.writableMsg
20
 
        property string assignmentMsg: strings.assignmentMsg
21
 
        property string roleMsg: strings.roleMsg
22
 
 
23
 
        QtObject {
24
 
            id: strings
25
 
 
26
 
            readonly property string constantMsg: "%1.%2 should be a property of type %3"
27
 
            readonly property string changedMsg: "%1 should have an %2Changed signal"
28
 
            readonly property string writableMsg: "%1.%2 should be writable"
29
 
            readonly property string assignmentMsg: "assignment to %1.%2 did not work"
30
 
            readonly property string roleMsg: "%1 should expose a \"%2\" role of type %3"
31
 
        }
32
 
 
33
 
        /* verify that there's a $prop of $type */
34
 
        function constant(prop, type, msg) {
35
 
            verifyType(obj[prop], type, (msg || constantMsg).arg(name).arg(prop).arg(type));
36
 
        }
37
 
 
38
 
        /* verify that there's a $prop of $type and a corresponding $typeChanged signal */
39
 
        function property(prop, type, msg) {
40
 
            constant(prop, type);
41
 
            verifyType(obj[prop+"Changed"], "function", (msg || changedMsg).arg(name).arg(prop));
42
 
        }
43
 
 
44
 
        /* verify that there's a writable $prop of $type */
45
 
        function writable(prop, type, value, msg1, msg2) {
46
 
            property(prop, type);
47
 
            if (writable) {
48
 
               try {
49
 
                   obj[prop] = value;
50
 
               } catch(err) {
51
 
                   if (writable) fail((msg1 || writableMsg).arg(name).arg(prop));
52
 
               }
53
 
               compare(obj[prop], value, (msg2 || assignmentMsg).arg(name).arg(prop));
54
 
            }
55
 
        }
56
 
 
57
 
        /* verify that there's a $role of $type exposed */
58
 
        function role(role, type, msg) {
59
 
            constant(role, type, msg || roleMsg);
60
 
        }
61
 
 
62
 
        /* clear the properties */
63
 
        function clear() {
64
 
            obj = null;
65
 
            name = "";
66
 
 
67
 
            verifier.constantMsg = strings.constantMsg;
68
 
            verifier.changedMsg = strings.changedMsg;
69
 
            verifier.writableMsg = strings.writableMsg;
70
 
            verifier.assignmentMsg = strings.assignmentMsg;
71
 
            verifier.roleMsg = strings.roleMsg;
72
 
        }
 
6
    id: root
 
7
 
 
8
    /* obj is the object on which the tests are carried out, name is used
 
9
       to identify the object in human-readable way */
 
10
    property var object
 
11
    property string name
 
12
 
 
13
    /* these are the default fail strings used during verification
 
14
       they can be overriden here or via optional arguments to the
 
15
       methods below */
 
16
    property string constantMsg: strings.constantMsg
 
17
    property string changedMsg: strings.changedMsg
 
18
    property string writableMsg: strings.writableMsg
 
19
    property string assignmentMsg: strings.assignmentMsg
 
20
    property string roleMsg: strings.roleMsg
 
21
 
 
22
    QtObject {
 
23
        id: strings
 
24
 
 
25
        readonly property string constantMsg: "%1.%2 should be a property of type %3"
 
26
        readonly property string changedMsg: "%1 should have an %2Changed signal"
 
27
        readonly property string writableMsg: "%1.%2 should be writable"
 
28
        readonly property string assignmentMsg: "assignment to %1.%2 did not work"
 
29
        readonly property string roleMsg: "%1 should expose a \"%2\" role of type %3"
73
30
    }
74
31
 
75
32
    /* verify that $value is of $type, otherwise fail with $msg */
83
40
            verify(Util.isInstanceOf(value, type), msg);
84
41
        }
85
42
    }
 
43
 
 
44
    /* verify that there's a $prop of $type */
 
45
    function constant(prop, type, msg) {
 
46
        verifyType(object[prop], type, (msg || constantMsg).arg(name).arg(prop).arg(type));
 
47
    }
 
48
 
 
49
    /* verify that there's a $prop of $type and a corresponding $typeChanged signal */
 
50
    function property(prop, type, msg) {
 
51
        constant(prop, type);
 
52
        verifyType(object[prop+"Changed"], "function", (msg || changedMsg).arg(name).arg(prop));
 
53
    }
 
54
 
 
55
    /* verify that there's a writable $prop of $type */
 
56
    function writable(prop, type, value, msg1, msg2) {
 
57
        property(prop, type);
 
58
        if (writable) {
 
59
           try {
 
60
               object[prop] = value;
 
61
           } catch(err) {
 
62
               if (writable) fail((msg1 || writableMsg).arg(name).arg(prop));
 
63
           }
 
64
           compare(object[prop], value, (msg2 || assignmentMsg).arg(name).arg(prop));
 
65
        }
 
66
    }
 
67
 
 
68
    /* verify that there's a $role of $type exposed */
 
69
    function role(role, type, msg) {
 
70
        constant(role, type, msg || roleMsg);
 
71
    }
 
72
 
 
73
    /* clear the properties */
 
74
    function clear() {
 
75
        object = null;
 
76
        name = "";
 
77
 
 
78
        root.constantMsg = strings.constantMsg;
 
79
        root.changedMsg = strings.changedMsg;
 
80
        root.writableMsg = strings.writableMsg;
 
81
        root.assignmentMsg = strings.assignmentMsg;
 
82
        root.roleMsg = strings.roleMsg;
 
83
    }
86
84
}