Commits

Mark Vasilkov committed cd4b811

class_test.js and class_test_data.js

  • Participants
  • Parent commits a26100e

Comments (0)

Files changed (5)

 	baseUrl: '',
 	paths: {
 		'class': 'lib/misato/class',
-		class_test: 'lib/misato/class.test'
+		class_test: 'lib/misato/test/class_test',
+		class_test_data: 'lib/misato/test/class_test_data'
 	}
 })
 

File lib/misato/class.js

 
 						this._super = _super[name]
 						var retval = fn.apply(this, arguments)
-						this._super = saved
+						if (typeof saved == 'undefined')
+							delete this._super
+						else this._super = saved
 
 						return retval
 					}

File lib/misato/class.test.js

-define(['class'], function(Class) {
-	var Test1 = Class.extend({
-		_value: 0,
-
-		initialize: function() {
-			this._value = 9
-		}
-	}), test1 = new Test1
-
-	var Test2 = Test1.extend({
-		inc: function() {
-			++this._value
-			return this
-		},
-
-		dec: function() {
-			--this._value
-			return this
-		}
-	}), test2 = new Test2
-
-	module('Class')
-
-	test('Subclasses (level 1)', function() {
-		strictEqual(typeof Test1, 'function', 'Subclass is of type "function"')
-		ok(Test1 instanceof Function, 'Subclass is instance of Function')
-		strictEqual(Test1.constructor, Function, "Subclass' constructor is Function")
-
-		strictEqual(typeof test1, 'object', 'New subclass is of type "object"')
-		ok(test1 instanceof Test1, 'New subclass is instance of subclass')
-		ok(test1 instanceof Class, 'New subclass is instance of Class')
-		strictEqual(test1.constructor, Test1, "New subclass' constructor is ok")
-
-		strictEqual(test1._value, 9, 'Subclass#initialize is called')
-	})
-
-	test('Subclasses (level 2)', function() {
-		ok(test2 instanceof Test2, 'New subclass is instance of subclass')
-		ok(test2 instanceof Test1, 'New subclass is instance of parent subclass')
-		ok(test2 instanceof Class, 'New subclass is instance of Class')
-
-		strictEqual(test2._value, 9, 'Parent subclass#initialize is called')
-
-		test2.inc().inc().inc()
-		strictEqual(test2._value, 12, 'Methods pass sanity test (1)')
-
-		test2.dec().dec().dec().dec()
-		strictEqual(test2._value, 8, 'Methods pass sanity test (2)')
-	})
-})

File lib/misato/test/class_test.js

+define(['class', 'class_test_data'], function(Class, t) {
+	module('Class')
+
+	test('Subclasses (level 1)', function() {
+		strictEqual(typeof t.Lvl1, 'function', 'Subclass is of type "function"')
+		ok(t.Lvl1 instanceof Function, 'Subclass is instance of Function')
+		strictEqual(t.Lvl1.constructor, Function, "Subclass' constructor is Function")
+
+		strictEqual(typeof t.lvl1, 'object', 'New subclass is of type "object"')
+		ok(t.lvl1 instanceof t.Lvl1, 'New subclass is instance of subclass')
+		ok(t.lvl1 instanceof Class, 'New subclass is instance of Class')
+		strictEqual(t.lvl1.constructor, t.Lvl1, "New subclass' constructor is ok")
+
+		strictEqual(t.lvl1._value, 9, 'Subclass#initialize is called')
+	})
+
+	test('Subclasses (level 2)', function() {
+		ok(t.lvl2 instanceof t.Lvl2, 'New subclass is instance of subclass')
+		ok(t.lvl2 instanceof t.Lvl1, 'New subclass is instance of parent')
+		ok(t.lvl2 instanceof Class, 'New subclass is instance of Class')
+
+		strictEqual(t.lvl2._value, 9, 'Parent#initialize is called')
+		ok(!('_super' in t.lvl2), 'New subclass has no "_super" field')
+
+		t.lvl2.inc().inc().inc()
+		strictEqual(t.lvl2._value, 12, 'Methods pass sanity test (1)')
+
+		t.lvl2.dec().dec().dec().dec()
+		strictEqual(t.lvl2._value, 8, 'Methods pass sanity test (2)')
+	})
+
+	test('Subclasses (level 3)', function() {
+		strictEqual(t.lvl3._value, 69, 'Parent#initialize is called (_super)')
+		ok(!('_super' in t.lvl3), 'New subclass has no "_super" field')
+
+		t.lvl3.inc().inc().inc()
+		strictEqual(t.lvl3._value, 75, 'Methods pass sanity test (_super) (1)')
+
+		t.lvl3.dec().dec().dec()
+		strictEqual(t.lvl3._value, 69, 'Methods pass sanity test (_super) (2)')
+
+		raises(function() {t.lvl3.raise()}, Error, 'Methods can throw errors')
+	})
+})

File lib/misato/test/class_test_data.js

+define(['class'], function(Class) {
+	var Lvl1 = Class.extend({
+		_value: 0,
+
+		initialize: function() {
+			this._value = 9
+		}
+	})
+
+	var Lvl2 = Lvl1.extend({
+		inc: function() {
+			++this._value
+			return this
+		},
+
+		dec: function() {
+			--this._value
+			return this
+		}
+	})
+
+	var Lvl3 = Lvl2.extend({
+		initialize: function() {
+			this._super()
+			this._value += 60
+		},
+
+		inc: function() {
+			return this._super()._super()
+		},
+
+		dec: function() {
+			return this._super()._super()
+		},
+
+		raise: function() {
+			throw new Error
+		}
+	})
+
+	return {
+		Lvl1: Lvl1,
+		Lvl2: Lvl2,
+		Lvl3: Lvl3,
+
+		lvl1: new Lvl1,
+		lvl2: new Lvl2,
+		lvl3: new Lvl3
+	}
+})