Browse files

even more testing

  • Loading branch information...
1 parent 9a457fc commit 4e7f110fbcfe3e9610acb95401d2c17b041b79ef @Jakobo committed Apr 26, 2011
Showing with 130 additions and 25 deletions.
  1. +3 −0 Makefile.js
  2. +35 −12 src/sslac.js
  3. +92 −13 tests/sslac.test.js
View
3 Makefile.js
@@ -3,6 +3,9 @@ var VERSION = "0.0.2",
fs = require("fs"),
qunit = require("qunit");
+// qunit settings
+qunit.options.coverage = false;
+
// test, yo
qunit.run({
code: "./src/sslac.js",
View
47 src/sslac.js
@@ -9,7 +9,7 @@
* Features:
* - auto extension: Objects can be set to extend by default, allowing for
* easy inheritance of this.* properties as well as prototyped methods
- * - static and instance functionality: Can create both statuc and instance
+ * - static and instance functionality: Can create both static and instance
* level objects
* - monkeypatching: objects can be modified on the fly to ease code upgrade paths
* @class Sslac
@@ -103,6 +103,17 @@
SslacRegistry[ns] = new ObjectRef(ns);
return SslacRegistry[ns];
}
+
+ /**
+ * @method createStatic
+ * @private
+ * @for Sslac
+ * @see Class
+ */
+ function createStatic(ns) {
+ SslacRegistry[ns] = new ObjectRef(ns, true);
+ return SslacRegistry[ns];
+ }
/**
* @method createFunction
@@ -182,13 +193,14 @@
* @consructor
* @for Sslac
*/
- function ObjectRef(ns) {
+ function ObjectRef(ns, isStatic) {
var parent = null,
parentNS = "",
localConstructor = function () {},
privilegedMethods = {},
- placeNS = namespaceOf(ns);
-
+ placeNS = namespaceOf(ns),
+ staticObj = {};
+
/**
* Builds privlieged methods
* @for ObjectRef
@@ -293,11 +305,15 @@
this.Implements = function () {
var thisModule = this;
- function implementsMethod() {}
+ function createImplementsMethod(name) {
+ return function() {
+ throw new Error("The interface defined requires "+name);
+ };
+ }
for (var i = 0, len = arguments.length; i < len; i++) {
// isArray
- if (globalWindow.Function.toString.call(arguments[i]).slice(8, -1).toLowerCase() === "array") {
+ if (Object.prototype.toString.call(arguments[i]).slice(8, -1).toLowerCase() === "array") {
for (var j = 0, j_len = arguments[i].length; j < j_len; j++) {
thisModule.Implements(arguments[i][j]);
}
@@ -309,7 +325,7 @@
}
else {
if (!this.getMethod(arguments[i])) {
- this.Method(arguments[i], implementsMethod);
+ this.Method(arguments[i], createImplementsMethod(arguments[i]));
}
}
}
@@ -374,6 +390,7 @@
*/
this.Static = function (name, fn) {
F[name] = fn;
+ staticObj[name] = fn;
return this;
};
@@ -419,7 +436,12 @@
// extend default class
this.Extends(Class);
- placeNS[nameOf(ns)] = F;
+ if (isStatic) {
+ placeNS[nameOf(ns)] = staticObj;
+ }
+ else {
+ placeNS[nameOf(ns)] = F;
+ }
}
// assign outward
@@ -433,19 +455,19 @@
* @for Sslac
* @see createObject
* @param ns {String} the namespace to store the new object into
- * @return {Object} the created object (F)
+ * @return {Object} the created object reference
*/
externalInterface.Class = createObject;
/**
* Turns ObjectRef into F by instantiating the ObjectRef
* @method Static
* @for Sslac
- * @see createObject
+ * @see createStatic
* @param ns {String} the namespace to store the new object into
- * @return {Object} the created object (F)
+ * @return {Object} the created object reference
*/
- externalInterface.Static = createObject;
+ externalInterface.Static = createStatic;
/**
* Creates a function
@@ -454,6 +476,7 @@
* @see createFunction
* @param ns {String} the namespace to store the function in
* @param fn {Function} the function to store
+ * @return {Object} the created object reference
*/
externalInterface.Function = createFunction;
View
105 tests/sslac.test.js
@@ -9,7 +9,15 @@ if (module) {
// FRAMING
////////////////////////////////////////////////////////////////////////////////
-// objects used in this test collection
+var INTERFACE_TEST = ["foo", "bar", "baz", "quux"],
+ GENERIC_NAMESPACE = "SslacTest.NS.A",
+ GENERIC_DEFINITION = "SslacTest.DefinitionObject",
+ SEED_VALUE_ONE = 3,
+ SEED_VALUE_TWO = 5,
+ SEED_VALUE_THREE = 7,
+ SEED_VALUE_FOUR = 9;
+
+// basic namespaced object
Sslac.Class("SslacTest.BasicNamespacedObject")
.Constructor(function Constructor(input) {
this.input = input;
@@ -18,6 +26,7 @@ Sslac.Class("SslacTest.BasicNamespacedObject")
return this.input;
});
+// basic extension object
Sslac.Class("SslacTest.BasicExtensionObject").Extends("SslacTest.BasicNamespacedObject")
.Constructor(function Constructor(input) {
this.Parent(input);
@@ -26,6 +35,7 @@ Sslac.Class("SslacTest.BasicExtensionObject").Extends("SslacTest.BasicNamespaced
return this.input;
});
+// an extension object relying on this.Parent
Sslac.Class("SslacTest.ChainedExtensionObject").Extends("SslacTest.BasicNamespacedObject")
.Constructor(function Constructor(input, alsoInput) {
this.Parent(input);
@@ -35,37 +45,53 @@ Sslac.Class("SslacTest.ChainedExtensionObject").Extends("SslacTest.BasicNamespac
return this.Parent() + this.alsoInput;
});
+// a static object
+Sslac.Static("SslacTest.BasicStaticObject")
+.Static("staticMethod", function staticMethod(input) {
+ return input;
+});
+
+Sslac.Class("SslacTest.BasicImplementsObject").Implements(INTERFACE_TEST)
+.Method("foo", function foo() {});
+
+Sslac.Class("SslacTest.BasicImplementsExtendsObject")
+.Extends("SslacTest.BasicImplementsObject")
+.Implements(INTERFACE_TEST);
+
+Sslac.Class("SslacTest.RedefinableObject")
+.Method("foo", function foo() {
+ return SEED_VALUE_ONE;
+});
+
////////////////////////////////////////////////////////////////////////////////
// TESTS
////////////////////////////////////////////////////////////////////////////////
module("Namespacing");
test("Namespace Set and Retrieve", function() {
- var testName = "SslacTest.NS.A";
- Sslac.Define(testName);
+ Sslac.Define(GENERIC_NAMESPACE);
ok(typeof SslacTest.NS.A == "object", "defines successfully");
- ok(Sslac.nameOf(testName) == "A", "nameOf gets last item in chain");
- strictEqual(Sslac.namespaceOf(testName), SslacTest.NS, "namespaceOf retrieves parent NS object");
- strictEqual(Sslac.valueOf(testName), SslacTest.NS.A, "valueOf returns object at given NS");
+ ok(Sslac.nameOf(GENERIC_NAMESPACE) == "A", "nameOf gets last item in chain");
+ strictEqual(Sslac.namespaceOf(GENERIC_NAMESPACE), SslacTest.NS, "namespaceOf retrieves parent NS object");
+ strictEqual(Sslac.valueOf(GENERIC_NAMESPACE), SslacTest.NS.A, "valueOf returns object at given NS");
});
////////////////////////////////////////////////////////////////////////////////
module("Definition Management");
test("getDefinition", function() {
- var testName = "SslacTest.DefinitionObject",
- def = Sslac.Class(testName);
- strictEqual(Sslac.definitionOf(testName), def, "definitionOf returns Sslac definition object");
+ var def = Sslac.Class(GENERIC_DEFINITION);
+ strictEqual(Sslac.definitionOf(GENERIC_DEFINITION), def, "definitionOf returns Sslac definition object");
});
////////////////////////////////////////////////////////////////////////////////
module("Instance Class", {
setup: function() {
- this.seedValue = 7;
+ this.seedValue = SEED_VALUE_ONE;
this.baseObject = new SslacTest.BasicNamespacedObject(this.seedValue);
},
teardown: function() {
@@ -86,7 +112,7 @@ test("Constructor Tests", function() {
module("Inherited Class", {
setup: function() {
- this.seedValue = 9;
+ this.seedValue = SEED_VALUE_ONE;
this.extendObject = new SslacTest.BasicExtensionObject(this.seedValue);
},
teardown: function() {
@@ -112,8 +138,8 @@ test("New Methods", function() {
module("Inherited & Chained Class", {
setup: function() {
- this.seedValue = 5;
- this.seedValueTwo = 7;
+ this.seedValue = SEED_VALUE_ONE;
+ this.seedValueTwo = SEED_VALUE_TWO;
this.extendObject = new SslacTest.ChainedExtensionObject(this.seedValue, this.seedValueTwo);
},
teardown: function() {
@@ -133,3 +159,56 @@ test("Parent Chaining", function() {
////////////////////////////////////////////////////////////////////////////////
+module("Static Class");
+
+test("Type Checks", function() {
+ ok(typeof SslacTest.BasicStaticObject == "object", "Static object is still an object");
+});
+
+test("Invocation", function() {
+ strictEqual(SslacTest.BasicStaticObject.staticMethod(SEED_VALUE_ONE), SEED_VALUE_ONE, "static invocation");
+});
+
+////////////////////////////////////////////////////////////////////////////////
+
+module("Interfaces", {
+ setup: function() {
+ this.ifaceObj = new SslacTest.BasicImplementsObject();
+ this.extIfaceObj = new SslacTest.BasicImplementsExtendsObject();
+ },
+ teardown: function() {
+ delete this.ifaceObj;
+ delete this.extIfaceObj;
+ }
+});
+
+test("Interface Defined", function() {
+ expect(INTERFACE_TEST.length);
+ for (var i = 0, len = INTERFACE_TEST.length; i < len; i++) {
+ ok(typeof this.ifaceObj[INTERFACE_TEST[i]] === "function", INTERFACE_TEST[i]+" is a function");
+ }
+});
+
+test("Error on Non Defined Method", function() {
+ expect(INTERFACE_TEST.length - 1);
+ for (var i = 0, len = INTERFACE_TEST.length; i < len; i++) {
+ try {
+ this.ifaceObj[INTERFACE_TEST[i]]();
+ }
+ catch(e) {
+ ok(true, "Caught undefined "+INTERFACE_TEST[i]);
+ }
+ }
+});
+
+test("Extended Objects inherit defined methods", function() {
+ // foo should work
+ expect(1);
+ try {
+ this.extIfaceObj.foo();
+ ok(true, "foo is valid");
+ }
+ catch(e) {}
+});
+
+

0 comments on commit 4e7f110

Please sign in to comment.