Permalink
Browse files

Basic mixin / extend support

  • Loading branch information...
felixge committed Apr 2, 2011
1 parent 6315ee9 commit c26b18cd8d647afae4742da767cebdef1f018ea0
View
@@ -0,0 +1,4 @@
+SHELL := /bin/bash
+
+test-integration:
+ @find test/integration/test-*.js | xargs -n 1 -t node
View
@@ -0,0 +1,11 @@
+# oop
+
+## Purpose
+
+This library tries to bring basic oop features to JavaScript while being as
+light-weight and simple as possible.
+
+
+## Todo
+
+* Documentation

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,58 @@
+var oop = require('oop');
+var assert = require('assert');
+
+function Friendly() {
+ // Properties from the mixed in class should be copied
+ this.friendly = true;
+ this.overWriteMe = 'Friendly';
+
+ // Methods of the mixed in class should be callable from the constructor
+ this.greet();
+}
+
+Friendly.prototype.greet = function() {
+};
+
+function Tall() {
+ this.tall = true;
+ this.overWriteMe = 'Tall';
+
+ this.slamdunk();
+}
+
+Tall.prototype.slamdunk = function() {
+}
+
+function Person() {
+ // The properties of the mixin target should be kept
+ this.person = true;
+ // Unless they are overwritten by one of the mixins
+ this.overWriteMe = 'Person';
+
+ // Mixins are runtime created
+ oop.forceMixin(this, Friendly, Tall);
+}
+
+Person.prototype.whimper = function() {
+}
+
+var person = new Person();
+// Make sure all properties got copied
+assert.ok(person.person);
+assert.ok(person.friendly);
+assert.ok(person.tall);
+
+// Make sure overWriteMe got overwritten by the last mixin
+assert.strictEqual(person.overWriteMe, 'Tall');
+
+// Make sure all methods got copied
+person.whimper();
+person.greet();
+person.slamdunk();
+
+// Make sure only the properties are enumerable
+var properties = Object.keys(person);
+assert.deepEqual(properties, ['person', 'overWriteMe', 'friendly', 'tall']);
+
+// Make sure mixin is an alias of forceMixin
+assert.strictEqual(oop.mixin, oop.forceMixin);
@@ -0,0 +1,26 @@
+var oop = require('oop');
+var assert = require('assert');
+
+function A() {
+ this.a = true;
+}
+A.methodA = function() {};
+
+function B() {
+ A.call(this);
+
+ this.b = true;
+}
+oop.extend(B, A);
+
+B.methodB = function() {};
+
+var b = new B();
+assert.ok(b instanceof B);
+assert.ok(b instanceof A);
+
+assert.ok(b.a);
+assert.ok(b.b);
+
+B.methodA();
+B.methodB();
@@ -0,0 +1,35 @@
+var oop = require('oop');
+var assert = require('assert');
+
+function A() {
+ this.propertyA = true;
+ this.sharedProperty = 'A';
+}
+A.prototype.methodA = function() {}
+A.prototype.sharedMethod = function() {}
+
+function B() {
+ this.propertyB = true;
+ this.sharedProperty = 'B';
+
+ oop.softMixin(this, A);
+}
+B.prototype.methodB = function() {}
+B.prototype.sharedMethod = function() {}
+
+var b = new B();
+// Make sure individual properties got copied
+assert.ok(b.propertyA);
+assert.ok(b.propertyB);
+
+// Make sure that mixin A didn't overwrite shared property B
+assert.strictEqual(b.sharedProperty, 'B');
+
+// Make sure all methods got copied and are callable
+b.methodA();
+b.methodB();
+b.sharedMethod();
+
+// Check that our sharedMethod comes from B and wasn't overwritten by mixin A
+assert.strictEqual(b.sharedMethod, B.prototype.sharedMethod);
+assert.notStrictEqual(b.sharedMethod, A.prototype.sharedMethod);
@@ -0,0 +1,18 @@
+var oop = require('oop');
+var assert = require('assert');
+
+function A() {
+ this.myProperty = 'foo';
+}
+
+function B() {
+ this.myProperty = 'bar';
+}
+
+function C() {
+ oop.strictMixin(this, A, B);
+}
+
+assert.throws(function() {
+ var c = new C();
+}, /class "B".+property "myProperty"/i);
@@ -0,0 +1,16 @@
+var oop = require('oop');
+var assert = require('assert');
+
+function A() {}
+A.prototype.myMethod = function() {};
+
+function B() {}
+B.prototype.myMethod = function() {};
+
+function C() {
+ oop.strictMixin(this, A, B);
+}
+
+assert.throws(function() {
+ var c = new C();
+}, /class "B".+prototype property "myMethod"/i);

0 comments on commit c26b18c

Please sign in to comment.