var test = require("unit.js"),
    assert = test.assert,
    common = require("./common.js");

describe("jQuery.fn.testPlugin", function () {
    var window = common.getWindow(),
        jQuery = common.getjQuery(window),
        unique = jQuery("#unique"),
        nonUnique = jQuery(".non-unique"),
        constructor = function () {
            this.constructorCalled = true;
        },
        defaults = {
            testNumber: 123,
            testBoolean: true,
            testString: "A string"
        },
        members = {
            constructorCalled: false,
            _setOption: function (option, value) {
                this.options[option] = value;
            },
            _getOption: function (option) {
                return this.options[option];
            },
            setTestNumber: function (value) {
                if (typeof value == "number") this._setOption("testNumber", value);
            },
            getTestNumber: function () {
                return this._getOption("testNumber");
            },
            setTestBoolean: function (value) {
                if (typeof value == "boolean") this._setOption("testBoolean", value);
            },
            getTestBoolean: function () {
                return this._getOption("testBoolean");
            },
            setTestString: function (value) {
                if (typeof value == "string") this._setOption("testString", value);
            },
            getTestString: function () {
                return this._getOption("testString");
            }
        },
        extendedMembers = {
            setTestNumber: function (value) {
                if (typeof value == "number") this._setOption("testNumber", -value);
            },
            setTestBoolean: function (value) {
                if (typeof value == "boolean") this._setOption("testBoolean", !value);
            },
            setTestString: function (value) {
                this.prototype.setTestString(value + value);
            }
        };

    describe("Pre-flight checks", function () {
        it("jQuery.addPlugin should be a function", function() {
            test.function(jQuery.addPlugin);
        });

        it("jQuery('#unique') should contain a single item", function () {
            test.number(unique.length).is(1);
        });

        it("jQuery('.non-unique') should contain 4 items", function () {
            test.number(nonUnique.length).is(4);
        });

        it("jQuery.addPlugin('testPlugin', constructor, defaults, members) should create jQuery.fn.testPlugin", function () {
            jQuery.addPlugin("testPlugin", constructor, defaults, members);
            test.function(jQuery.fn.testPlugin)
                .object(jQuery.fn.testPlugin.defaults)
                    .is(defaults)
                .function(jQuery.fn.testPlugin.updateDefaultsWith)
                .function(jQuery.fn.testPlugin.extendMembersWith)
                .function(jQuery.fn.testPlugin.cloneTo)
                .function(jQuery.fn.testPlugin.extendTo);
        });
    });

    describe("jQuery('#unique').testPlugin()", function () {
        it("should instantiate testPlugin on #unique", function () {
            unique.testPlugin();
            test.object(unique.data("jquery-plugincreator-testPlugin"));
        });
        it("should copy the contents of `defaults` into the `options` member on the testPlugin instance", function () {
            test.object(unique.data("jquery-plugincreator-testPlugin").options).is(defaults);
        });
        it("should call `constructor` during instantiation and set the `constructorCalled` member to true", function () {
            test.bool(unique.data("jquery-plugincreator-testPlugin").constructorCalled, true);
        });
    });

    describe("jQuery.fn.testPlugin.cloneTo('cloneOfTestPlugin')", function () {
        it("should create jQuery.fn.cloneOfTestPlugin", function () {
            jQuery.fn.testPlugin.cloneTo("cloneOfTestPlugin");
            test.function(jQuery.fn.cloneOfTestPlugin)
                .object(jQuery.fn.cloneOfTestPlugin.defaults)
                    .is(defaults)
                .function(jQuery.fn.cloneOfTestPlugin.updateDefaultsWith)
                .function(jQuery.fn.cloneOfTestPlugin.extendMembersWith)
                .function(jQuery.fn.cloneOfTestPlugin.cloneTo)
                .function(jQuery.fn.cloneOfTestPlugin.extendTo);
        });

        describe("jQuery('#unique').cloneOfTestPlugin()", function () {
            it("should instantiate cloneOfTestPlugin on #unique", function () {
                unique.cloneOfTestPlugin();
                test.object(unique.data("jquery-plugincreator-cloneOfTestPlugin"));
            });
            it("should copy the contents of `defaults` into the `options` member on the cloneOfTestPlugin instance", function () {
                test.object(unique.data("jquery-plugincreator-cloneOfTestPlugin").options).is(defaults);
            });
            it("should call `constructor` during instantiation and set the `constructorCalled` member to true", function () {
                test.bool(unique.data("jquery-plugincreator-cloneOfTestPlugin").constructorCalled, true);
            });
        });
    });

    describe("jQuery.fn.testPlugin.extendTo('childOfTestPlugin', extendedMembers)", function () {
        it("should create jQuery.fn.childOfTestPlugin", function () {
            jQuery.fn.testPlugin.extendTo("childOfTestPlugin", extendedMembers);
            test.function(jQuery.fn.childOfTestPlugin)
                .object(jQuery.fn.childOfTestPlugin.defaults)
                    .is(defaults)
                .function(jQuery.fn.childOfTestPlugin.updateDefaultsWith)
                .function(jQuery.fn.childOfTestPlugin.extendMembersWith)
                .function(jQuery.fn.childOfTestPlugin.cloneTo)
                .function(jQuery.fn.childOfTestPlugin.extendTo);
        });
    });

    after(function () {
        window.close();
    });
});

@@ -934,118 +934,8 @@
  %7D,%0A
-            getTestNumber: function () %7B%0A                return this._getOption(%22testNumber%22);%0A            %7D,%0A
     
@@ -1082,120 +1082,8 @@
  %7D,%0A
-            getTestBoolean: function () %7B%0A                return this._getOption(%22testBoolean%22);%0A            %7D,%0A
     
@@ -1227,117 +1227,8 @@
  %7D,%0A
-            getTestString: function () %7B%0A                return this._getOption(%22testString%22);%0A            %7D%0A
     
@@ -2909,35 +2909,39 @@
            t
-est.object(
+his.instance = 
 unique.data(
@@ -2966,32 +2966,71 @@
 tor-testPlugin%22)
+;%0A            test.object(this.instance
 );%0A        %7D);%0A 
@@ -3176,54 +3176,21 @@
 ect(
-unique.data(%22jquery-plugincreator-testPlugin%22)
+this.instance
 .opt
@@ -3369,54 +3369,21 @@
 ool(
-unique.data(%22jquery-plugincreator-testPlugin%22)
+this.instance
 .con
@@ -4287,35 +4287,39 @@
            t
-est.object(
+his.instance = 
 unique.data(
@@ -4359,16 +4359,59 @@
 Plugin%22)
+;%0A                test.object(this.instance
 );%0A     
@@ -4584,61 +4584,21 @@
 ect(
-unique.data(%22jquery-plugincreator-cloneOfTestPlugin%22)
+this.instance
 .opt
@@ -4789,61 +4789,21 @@
 ool(
-unique.data(%22jquery-plugincreator-cloneOfTestPlugin%22)
+this.instance
 .con

Use shared this to reference plugin instance.