~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/app/javascript/lazr/testing/testing.js

[r=deryck][bug=803954] Bring lazr-js source into lp tree and package
        yui as a dependency

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2009, Canonical Ltd. All rights reserved. */
 
2
 
 
3
YUI.add("lazr.testing.runner", function(Y) {
 
4
 
 
5
/**
 
6
 * Testing utilities.
 
7
 *
 
8
 * @module lazr.testing
 
9
 * @namespace lazr
 
10
 */
 
11
 
 
12
Runner = Y.namespace("lazr.testing.Runner");
 
13
 
 
14
Runner.add = function(suite) {
 
15
    if ((typeof jstestdriver === "undefined")) {
 
16
        // If we are not running under JsTestDriver, then
 
17
        // register the suite with Y.Test.Runner and run it.
 
18
        Y.Test.Runner.add(suite);
 
19
    } else {
 
20
        // If ``jstestdriver`` is defined, that means we are
 
21
        // running under JsTestDriver, so instead register each
 
22
        // test case from the suite as a separate TestCase() with
 
23
        // JsTestDriver.
 
24
        var tests = [];
 
25
 
 
26
        Y.each(suite.items, function(testCase, idx) {
 
27
            var suiteName = suite.name;
 
28
            var testCaseName = testCase.name;
 
29
 
 
30
            var clone = {};
 
31
            for (var prop in testCase){
 
32
                // Clone everything that is not a test method.
 
33
                if (prop.indexOf("test") === -1){
 
34
                    clone[prop] = testCase[prop];
 
35
                }
 
36
            }
 
37
 
 
38
            // Now for each test method, create a JsTestDriver
 
39
            // TestCase that wraps a single YUI TestSuite, that wraps
 
40
            // a clone of the original TestCase but with only the
 
41
            // single test method that we are interested in.
 
42
            Y.each(testCase, function(property, name) {
 
43
                if (name.indexOf("test") === 0 &&
 
44
                    Y.Lang.isFunction(property)){
 
45
                    tests.push({"suiteName": suiteName,
 
46
                                "caseName": testCaseName,
 
47
                                "case": clone,
 
48
                                "methodName": name,
 
49
                                "method": property});
 
50
                }
 
51
            });
 
52
        });
 
53
 
 
54
        Y.each(tests, function(testObject, i) {
 
55
            testObject = tests[i];
 
56
 
 
57
            var fakeTestCase = {
 
58
                "setUp": Y.bind(function(testObject){
 
59
                    var testSuite = new Y.Test.Suite(testObject.suiteName);
 
60
                    var testCase = new Y.Test.Case(testObject['case']);
 
61
                    testCase[testObject.methodName] = testObject.method;
 
62
                    testSuite.add(testCase);
 
63
                    Y.Test.Runner.clear();
 
64
                    Y.Test.Runner.add(testSuite);
 
65
                }, this, testObject),
 
66
                "tearDown": function(){
 
67
                    Y.Test.Runner.clear();
 
68
                }
 
69
            };
 
70
 
 
71
            fakeTestCase[testObject.methodName] = Y.bind(function (testObject) {
 
72
                var results = [];
 
73
 
 
74
                var onComplete = function (methodName, results, e) {
 
75
                    Y.Test.Runner.unsubscribe("testcasecomplete");
 
76
                    results.push(e.results[methodName]);
 
77
                };
 
78
 
 
79
                Y.Test.Runner.subscribe(
 
80
                    "testcasecomplete",
 
81
                    Y.bind(onComplete, this, testObject.methodName, results),
 
82
                    Y.Test.Runner);
 
83
 
 
84
                Clock.reset();
 
85
                Y.Test.Runner.run();
 
86
                var i = 100;
 
87
                while (i--) {
 
88
                    if (!Y.Test.Runner.isRunning()){
 
89
                        break;
 
90
                    }
 
91
                    Clock.tick(100);
 
92
                }
 
93
 
 
94
                var result = results.pop();
 
95
                if (result === undefined) {
 
96
                    fail("Test did not finish after 100 iterations.");
 
97
                } else {
 
98
                    if (result.result == "fail") {
 
99
                        fail(result.message);
 
100
                    }
 
101
                }
 
102
 
 
103
            }, this, testObject);
 
104
 
 
105
            // JSLint will complain if the constructur is used without `new`
 
106
            // and if the result of `new` is not used. The TestCase class is
 
107
            // defined globally by jstestdriver and automatically registers
 
108
            // itself, so it is not necessary to return this object.
 
109
            var ignored = new TestCase(
 
110
                testObject.caseName + "." + testObject.methodName,
 
111
                fakeTestCase);
 
112
        });
 
113
    }
 
114
};
 
115
 
 
116
Runner.run = function(suite) {
 
117
    Y.on("domready", function() {
 
118
        if ((typeof jstestdriver === "undefined")) {
 
119
            // If we are not running under JsTestDriver, then run all
 
120
            // the registered test suites with Y.Test.Runner.
 
121
            var yconsole = new Y.Console({
 
122
                newestOnTop: false,
 
123
                useBrowserConsole: true
 
124
            });
 
125
            yconsole.render("#log");
 
126
            Y.Test.Runner.run();
 
127
        }
 
128
    });
 
129
};
 
130
 
 
131
}, "0.1", {"requires": ["oop", "test", "console"]});