~launchpad-pqm/lazr-js/toolchain

81.2.1 by Sidnei da Silva
- Add skeleton for running lazr-js tests
1
/* Copyright (c) 2009, Canonical Ltd. All rights reserved. */
2
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
3
YUI.add("lazr.testing.runner", function(Y) {
81.2.1 by Sidnei da Silva
- Add skeleton for running lazr-js tests
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.
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
24
        var tests = [];
25
124.6.3 by Edwin Grubbs
Fixed test errors in Opera, WebKit, and IE7. Fixed lint errors.
26
        Y.each(suite.items, function(testCase, idx) {
81.2.4 by Sidnei da Silva
- A different approach at wrapping YUI tests, unfolding them into JsTestDriver TestCase objects
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.
152.2.14 by Edwin Grubbs
Addressed reviewer comments.
42
            Y.each(testCase, function(property, name) {
43
                if (name.indexOf("test") === 0 &&
44
                    Y.Lang.isFunction(property)){
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
45
                    tests.push({"suiteName": suiteName,
46
                                "caseName": testCaseName,
47
                                "case": clone,
152.2.14 by Edwin Grubbs
Addressed reviewer comments.
48
                                "methodName": name,
49
                                "method": property});
81.2.4 by Sidnei da Silva
- A different approach at wrapping YUI tests, unfolding them into JsTestDriver TestCase objects
50
                }
152.2.14 by Edwin Grubbs
Addressed reviewer comments.
51
            });
124.6.3 by Edwin Grubbs
Fixed test errors in Opera, WebKit, and IE7. Fixed lint errors.
52
        });
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
53
124.6.5 by Edwin Grubbs
Fixed lint errors.
54
        Y.each(tests, function(testObject, i) {
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
55
            testObject = tests[i];
56
57
            var fakeTestCase = {
58
                "setUp": Y.bind(function(testObject){
124.6.3 by Edwin Grubbs
Fixed test errors in Opera, WebKit, and IE7. Fixed lint errors.
59
                    var testSuite = new Y.Test.Suite(testObject.suiteName);
60
                    var testCase = new Y.Test.Case(testObject['case']);
61
                    testCase[testObject.methodName] = testObject.method;
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
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();
124.6.2 by Edwin Grubbs
Fixed explorer parse error.
68
                }
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
69
            };
70
124.6.3 by Edwin Grubbs
Fixed test errors in Opera, WebKit, and IE7. Fixed lint errors.
71
            fakeTestCase[testObject.methodName] = Y.bind(function (testObject) {
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
72
                var results = [];
73
173.2.8 by Sidnei da Silva
- Fix JsTestDriver integration by plugging to testcasecomplete instead.
74
                var onComplete = function (methodName, results, e) {
75
                    Y.Test.Runner.unsubscribe("testcasecomplete");
76
                    results.push(e.results[methodName]);
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
77
                };
78
79
                Y.Test.Runner.subscribe(
173.2.8 by Sidnei da Silva
- Fix JsTestDriver integration by plugging to testcasecomplete instead.
80
                    "testcasecomplete",
81
                    Y.bind(onComplete, this, testObject.methodName, results),
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
82
                    Y.Test.Runner);
83
84
                Clock.reset();
85
                Y.Test.Runner.run();
173.2.8 by Sidnei da Silva
- Fix JsTestDriver integration by plugging to testcasecomplete instead.
86
                var i = 100;
87
                while (i--) {
88
                    if (!Y.Test.Runner.isRunning()){
89
                        break;
90
                    }
81.2.11 by Sidnei da Silva
- Minor polish
91
                    Clock.tick(100);
81.2.6 by Sidnei da Silva
- Tests pass at last
92
                }
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
93
94
                var result = results.pop();
81.2.10 by Sidnei da Silva
- Move sample html into test for inline editor tests
95
                if (result === undefined) {
81.2.11 by Sidnei da Silva
- Minor polish
96
                    fail("Test did not finish after 100 iterations.");
81.2.10 by Sidnei da Silva
- Move sample html into test for inline editor tests
97
                } else {
124.6.3 by Edwin Grubbs
Fixed test errors in Opera, WebKit, and IE7. Fixed lint errors.
98
                    if (result.result == "fail") {
99
                        fail(result.message);
81.2.10 by Sidnei da Silva
- Move sample html into test for inline editor tests
100
                    }
81.2.5 by Sidnei da Silva
- Add jsUnitMockIO and use it to fake setTimeout so that the tests run synchronously.
101
                }
102
103
            }, this, testObject);
104
152.2.2 by Edwin Grubbs
Improved comment and fixed too long line.
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.
124.6.5 by Edwin Grubbs
Fixed lint errors.
109
            var ignored = new TestCase(
110
                testObject.caseName + "." + testObject.methodName,
111
                fakeTestCase);
112
        });
81.2.1 by Sidnei da Silva
- Add skeleton for running lazr-js tests
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({
174.1.1 by Sidnei da Silva
- Set default logger to useBrowserConsole
122
                newestOnTop: false,
123
                useBrowserConsole: true
81.2.1 by Sidnei da Silva
- Add skeleton for running lazr-js tests
124
            });
125
            yconsole.render("#log");
126
            Y.Test.Runner.run();
127
        }
128
    });
129
};
130
81.5.14 by Sidnei da Silva
- Make requirements parseable.
131
}, "0.1", {"requires": ["oop", "test", "console"]});