Skip to content
This repository
Browse code

Added more tests

  • Loading branch information...
commit ad88661f3cdf0465a3966f8ca4fe0edacaca43c7 1 parent 4f01ae1
Kirollos Risk authored August 21, 2012

Showing 1 changed file with 164 additions and 17 deletions. Show diff stats Hide diff stats

  1. 181  test/fiber-test.js
181  test/fiber-test.js
@@ -2,37 +2,184 @@ var assert = require('assert'),
2 2
     vows = require('vows'),
3 3
     Fiber = require('../src/fiber');
4 4
 
5  
-var SuperClass = Fiber.extend(function() {
  5
+// Stub Fiber class definition
  6
+var Definition = Fiber.extend(function( base ) {
6 7
   return {
7 8
     init: function(name) {
8 9
       this.name = name;
  10
+    },
  11
+    getBase: function() {
  12
+      return base;
  13
+    },
  14
+    method1: function() {
  15
+      return true;
  16
+    },
  17
+    method2: function() {
  18
+      return true;
9 19
     }
10  
-  }
  20
+  };
11 21
 });
12 22
 
13  
-var SubClass = SuperClass.extend(function( base ) {
14  
-  return {
15  
-    init: function(name) {
16  
-      base.init.call(this, name);
  23
+// BASIC TESTS
  24
+
  25
+vows.describe('Basic Instantiation').addBatch({
  26
+  'When initializing a Fiber class via <Fiber.extend>': {
  27
+    topic: function() {
  28
+      return new Definition('name');
  29
+    },
  30
+    'the <init> function should execute': function (result) {
  31
+      assert.equal(result.name, 'name');
  32
+    },
  33
+    'and then be destroyed.': function (result) {
  34
+      assert.isUndefined(result.init);
  35
+    }
  36
+  },
  37
+  'A Fiber class definition':  {
  38
+    topic: function() {
  39
+      return Definition;
  40
+    },
  41
+    'Should have the proper constructor': function (result) {
  42
+      assert.equal(result.prototype.constructor, Definition);
  43
+    },
  44
+    'Should be extendable': function (result) {
  45
+      assert.isFunction(result.extend);
  46
+    }
  47
+  },
  48
+  'When calling a method in the class': {
  49
+    topic: function() {
  50
+      return new Definition();
  51
+    },
  52
+    'the method should execute': function(result) {
  53
+      assert.isFunction(result.method1);
  54
+      assert.isTrue(result.method1());
  55
+    }
  56
+  },
  57
+  'Every instance of a class': {
  58
+    topic: function() {
  59
+      return new Definition();
  60
+    },
  61
+    'should have access to the super\'s prototype': function (result) {
  62
+      assert.isTrue(result.getBase().constructor === Fiber)
17 63
     }
18 64
   }
19  
-});
  65
+}).export(module);
20 66
 
21  
-vows.describe('Basic Instantiation').addBatch({
22  
-  'When initializing a base class': {
  67
+// INHERITANCE
  68
+
  69
+vows.describe('Inheritance').addBatch({
  70
+  'When initializing a sub class which doesn\'t override <init>': {
  71
+    topic: function() {
  72
+      var Sub = Definition.extend(function( base ) {
  73
+        return {
  74
+          getBase: function() {
  75
+            return base;
  76
+          }
  77
+        };
  78
+      });
  79
+      var t = new Sub('name');
  80
+      return t;
  81
+    },
  82
+    'the parent <init> function should execute': function (result) {
  83
+      assert.equal(result.name, 'name');
  84
+    },
  85
+    'and then be destroyed.': function (result) {
  86
+      assert.isUndefined(result.init);
  87
+    },
  88
+    'The subclass instance should have access to the super\'s prototype': function (result) {
  89
+      assert.isTrue(result.getBase().constructor === Definition)
  90
+    }
  91
+  },
  92
+  'When initializing a sub class': {
  93
+    topic: function() {
  94
+      var Sub = Definition.extend(function( base ) {
  95
+        return {
  96
+          method2: function() {
  97
+            return true;
  98
+          },
  99
+        };
  100
+      });
  101
+      var t = new Sub('name');
  102
+      return t;
  103
+    },
  104
+    'A method that only exists in the super class should stills execute': function (result) {
  105
+      assert.isFunction(result.method1);
  106
+      assert.isTrue(result.method1());
  107
+    },
  108
+    'A method that is overridden should execute': function (result) {
  109
+      assert.isFunction(result.method2);
  110
+      assert.isTrue(result.method2());
  111
+    }
  112
+  },
  113
+  'When initializing a super and sub class': {
  114
+    topic: function() {
  115
+      // Super class
  116
+      var Super = Fiber.extend(function( base ) {
  117
+        return {
  118
+          init: function(num) {
  119
+            this.num = num;
  120
+          },
  121
+          increment: function() {
  122
+            return this.num + 1;
  123
+          }
  124
+        };
  125
+      });
  126
+      // Sub Class
  127
+      var Sub = Super.extend(function( base ) {
  128
+        return {
  129
+          init: function(num) {
  130
+            base.init.call(this, num);
  131
+          },
  132
+          increment: function() {
  133
+            return base.increment.call(this);
  134
+          }
  135
+        };
  136
+      });
  137
+      return new Sub(0);
  138
+    },
  139
+    'The subclass init constructor should call the super init constructor': function (result) {
  140
+      assert.equal(result.num, 0);
  141
+    },
  142
+    'An overridden method which calls the super\'s method should execute': function (result) {
  143
+      assert.isFunction(result.increment);
  144
+      assert.equal(result.increment(), 1);
  145
+    }
  146
+  }
  147
+}).export(module);
  148
+
  149
+// DECORATION
  150
+
  151
+vows.describe('Decoration').addBatch({
  152
+  'Ensure we can decorate': {
23 153
     topic: function() {
24  
-      return new SuperClass('super');
  154
+      return Fiber;
25 155
     },
26  
-    'result should have a name': function (result) {
27  
-      assert.equal(result.name, 'super');
  156
+    'Check <Fiber.decorate> exists': function(result) {
  157
+      assert.isFunction(result.decorate);
28 158
     }
29 159
   },
30  
-  'When initializing a subclass': {
  160
+  'When decorating a <Fiber> instance': {
31 161
     topic: function() {
32  
-      return new SubClass('sub');
  162
+      var Sub = Definition.extend(function( base ) {
  163
+        return {};
  164
+      });
  165
+      var t = new Sub('name');
  166
+      Fiber.decorate(t, function(base) {
  167
+        this.method3 = function() {
  168
+          return true;
  169
+        }
  170
+      });
  171
+      return t;
33 172
     },
34  
-    'result should have a name': function (result) {
35  
-      assert.equal(result.name, 'sub');
  173
+    'The method added via the decorator should exist': function(result) {
  174
+      assert.isFunction(result.method3);
  175
+      assert.isTrue(result.method3());
36 176
     }
37 177
   }
38  
-}).export(module);
  178
+}).export(module);
  179
+
  180
+// MIXIN
  181
+
  182
+// PROXY
  183
+
  184
+// NO-CONFLICT
  185
+

0 notes on commit ad88661

Please sign in to comment.
Something went wrong with that request. Please try again.