Permalink
Browse files

Initial commit

  • Loading branch information...
1 parent 5bbdfd9 commit 4ba7581b0556f5da2485c9da0824e7ee279686da @krzysztof-o committed Apr 14, 2013
Showing with 133 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +7 −0 Makefile
  3. +45 −0 index.js
  4. +15 −0 test/A.js
  5. +11 −0 test/B.js
  6. +54 −0 test/tests.js
View
@@ -0,0 +1 @@
+node_modules
View
@@ -0,0 +1,7 @@
+REPORTER = dot
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+
+.PHONY: test
View
@@ -0,0 +1,45 @@
+var Module = require('module');
+delete require.cache[module.id];
+
+exports.class = function(path) {
+ return function() {
+ var _module = getUniqueModule(path);
+
+ //call constructor
+ if (_module.hasOwnProperty('constructor')) {
+ _module.constructor.apply(null, argumentsToArray(arguments));
+ }
+ return _module;
+ };
+}
+
+exports.extends = function(path) {
+ var baseClassObject = module.parent.exports;
+ var _module = getUniqueModule(path);
+
+ for (var property in _module) {
+ if (!baseClassObject.hasOwnProperty(property)) {
+ baseClassObject[property] = _module[property];
+ }
+ }
+ return _module;
+}
+
+function getUniqueModule(path) {
+ var parent = module.parent;
+ var filename = Module._resolveFilename(path, parent);
+
+ delete Module._cache[filename];
+ return Module._load(filename, parent);
+}
+
+function argumentsToArray(arg) {
+ var i = 0;
+ var arr = [];
+ while(arg.hasOwnProperty(i)) {
+ arr.push(arg[i]);
+ i++;
+ }
+ return arr;
+}
+
View
@@ -0,0 +1,15 @@
+var oop = require('../index.js');
+var _super = oop.extends('./B');
+
+exports.constructor = function() {
+ _super.constructor();
+}
+
+exports.methodA = function() {
+ return "A::methodA " + _super.methodA();
+}
+
+exports.methodB = function() {
+ return "A::methodB";
+}
+
View
@@ -0,0 +1,11 @@
+exports.constructor = function() {
+
+}
+
+exports.methodA = function() {
+ return "B::methodA";
+}
+
+exports.methodC = function() {
+ return "B::methodC"
+}
View
@@ -0,0 +1,54 @@
+var assert = require('assert');
+var oop = require('..');
+
+describe('class A', function() {
+ before(function() {
+ A = oop.class('./A');
+ });
+ after(function() {
+ A = null;
+ });
+
+ it('should not be null', function(){
+ assert.notEqual(A, null);
+ });
+});
+
+describe('instance of class A', function() {
+ before(function() {
+ A = oop.class('./A');
+ a = new A();
+ });
+ after(function() {
+ A = null;
+ a = null;
+ });
+
+ it('should be not null', function() {
+ assert.notEqual(a, null);
+ });
+ //it('should be instanceof', function(){
+ //assert.ok(animal instanceof Animal);
+ //});
+ it('should be able to have access to methods', function(){
+ assert.notEqual(a.methodA, null);
+ assert.notEqual(a.methodB, null);
+ });
+ it('should be able to have access to methods inherited from class B', function(){
+ assert.notEqual(a.methodC, null);
+ });
+ it('should be able to call overriden method', function(){
+ assert.equal(a.methodA(), 'A::methodA B::methodA');
+ });
+ it('should be able to call method', function(){
+ assert.equal(a.methodB(), 'A::methodB');
+ });
+ it('should be able to call inherited method', function(){
+ assert.equal(a.methodC(), 'B::methodC');
+ });
+});
+
+
+
+
+

0 comments on commit 4ba7581

Please sign in to comment.