[qunit] providing unique this for setup/teardown and the tests [patch included]

[qunit] providing unique this for setup/teardown and the tests [patch included]


Hi there,
I've just stumbled about the problem that there is no easy way to a)
not pollute the global namespace and b) have setup and teardown
methods that actually provide objects to the tests.
Initially I just tried to use this in setup/teardown and the tests to
do this, but I soon found out that this doesn't work.
So I resolved to using global vars to hold this data - which I don't
like because then I can't use ?noglobals in the url to detect global
leakage.
So I made the this thing work and I am proposing to add this to qunit,
as it's damn useful and you also get the cleanup of these vars for
free (so you only need the teardown for global variables you changed
explicitly).
-- snip --
Index: testrunner.js
===================================================================
--- testrunner.js    (Revision 301)
+++ testrunner.js    (Revision 302)
@@ -371,20 +371,22 @@
    if ( !validTest(name) )
        return;
+    var testEnvironment = {};
+
    synchronize(function() {
        config.assertions = [];
        config.expected = null;
        try {
            if( !pollution )
                saveGlobal();
-            lifecycle.setup();
+            lifecycle.setup.call(testEnvironment);
        } catch(e) {
            QUnit.ok( false, "Setup failed on " + name + ": " + e.message );
        }
    });
    synchronize(function() {
        try {
-            callback();
+            callback.call(testEnvironment);
        } catch(e) {
            if( typeof console != "undefined" && console.error &&
console.warn ) {
                console.error("Test " + name + " died, exception and test
follows");
@@ -399,11 +401,11 @@
    synchronize(function() {
        try {
            checkPollution();
-            lifecycle.teardown();
+            lifecycle.teardown.call(testEnvironment);
        } catch(e) {
            QUnit.ok( false, "Teardown failed on " + name + ": " +
e.message );
        }
    });
    synchronize(function() {
        try {
            reset();
-- snap --
Regards,
Martin
p.s.: Since I won't read the mailinglist, please also send replies to
my address.