From af03b4f6d8a084d101872c2f78690fab795b3f40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Sat, 13 Aug 2011 03:12:21 +0200 Subject: [PATCH] fixed error in Object_spec that made other tests fail in some cases. Added some minor optimizations & fixes --- README.textile | 6 ++- middleclass.lua | 11 +++-- spec/Object_spec.lua | 99 ++++++++++++++++++++-------------------- spec/class_spec.lua | 6 ++- spec/classes_spec.lua | 41 ++++++++++------- spec/subclassOf_spec.lua | 2 + 6 files changed, 91 insertions(+), 74 deletions(-) diff --git a/README.textile b/README.textile index 7325a63..1eed3fa 100644 --- a/README.textile +++ b/README.textile @@ -86,5 +86,9 @@ If you are looking for @MindState@ (now called @Stateful@), it's over there, too h1. Specs -You may find the specs for this library in "middleclass-specs":https://github.com/kikito/middleclass-specs +This project uses "telescope":https://github.com/norman/telescope for its specs. If you want to run the specs, you will have to install telescope first. Then just execute the following from the root inspect folder: + +
+tsc -f spec/*
+
diff --git a/middleclass.lua b/middleclass.lua index 1822bc5..f136b7f 100644 --- a/middleclass.lua +++ b/middleclass.lua @@ -13,8 +13,9 @@ local function _setClassDictionariesMetatables(klass) dict.__index = dict if super then + local superStatic = super.static setmetatable(dict, super.__instanceDict) - setmetatable(klass.static, { __index = function(_,k) return dict[k] or super[k] end }) + setmetatable(klass.static, { __index = function(_,k) return dict[k] or superStatic[k] end }) else setmetatable(klass.static, { __index = function(_,k) return dict[k] end }) end @@ -25,7 +26,7 @@ local function _setClassMetatable(klass) __tostring = function() return "class " .. klass.name end, __index = klass.static, __newindex = klass.__instanceDict, - __call = function(_, ...) return klass:new(...) end + __call = function(self, ...) return self:new(...) end }) end @@ -53,9 +54,9 @@ local function _setClassMetamethods(klass) end end -local function _setDefaultInitializeMethod(klass) +local function _setDefaultInitializeMethod(klass, super) klass.initialize = function(instance, ...) - return klass.superclass.initialize(instance, ...) + return super.initialize(instance, ...) end end @@ -81,7 +82,7 @@ function Object.static:subclass(name) local subclass = _createClass(name, self) _setClassMetamethods(subclass) - _setDefaultInitializeMethod(subclass) + _setDefaultInitializeMethod(subclass, self) return subclass end diff --git a/spec/Object_spec.lua b/spec/Object_spec.lua index 8581be6..f34c761 100644 --- a/spec/Object_spec.lua +++ b/spec/Object_spec.lua @@ -2,6 +2,7 @@ require 'middleclass' context('Object', function() + context('name', function() test('is correctly set', function() assert_equal(Object.name, 'Object') @@ -29,7 +30,11 @@ context('Object', function() context('when given a class name', function() - local SubClass = Object:subclass('SubClass') + local SubClass + + before(function() + SubClass = Object:subclass('SubClass') + end) test('it returns a class with the correct name', function() assert_equal(SubClass.name, 'SubClass') @@ -51,70 +56,64 @@ context('Object', function() context('instance creation', function() local SubClass - local classes = { Object, SubClass } before(function() - SubClass = Object:subclass('SubClass') + SubClass = class('SubClass') function SubClass:initialize() self.mark=true end end) - for _,theClass in ipairs(classes) do - context(theClass.name, function() + context('allocate', function() - context('allocate', function() + test('allocates instances properly', function() + local instance = SubClass:allocate() + assert_equal(instance.class, SubClass) + assert_equal(tostring(instance), "instance of " .. tostring(SubClass)) + end) - test('allocates instances properly', function() - local instance = theClass:allocate() - assert_equal(instance.class, theClass) - assert_equal(tostring(instance), "instance of " .. tostring(theClass)) - end) + test('throws an error when used without the :', function() + assert_error(Object.allocate) + end) - test('throws an error when used without the :', function() - assert_error(Object.allocate) - end) + test('does not call the initializer', function() + local allocated = SubClass:allocate() + assert_nil(allocated.mark) + end) - test('does not call the initializer', function() - local allocated = theClass:allocate() - assert_nil(allocated.mark) - end) + test('can be overriden', function() - test('can be overriden', function() + local previousAllocate = SubClass.static.allocate - local previousAllocate = theClass.allocate + function SubClass.static:allocate() + local instance = previousAllocate(SubClass) + instance.mark = true + return instance + end - function theClass.static:allocate() - local instance = previousAllocate(theClass) - instance.mark = true - return instance - end - - local allocated = theClass:allocate() - assert_true(allocated.mark) - end) - - end) - - context('new', function() - - test('initializes instances properly', function() - local instance = theClass:new() - assert_equal(instance.class, theClass) - end) - - test('throws an error when used without the :', function() - assert_error(theClass.new) - end) - - test('calls the initializer', function() - local allocated = theClass:new() - assert_true(allocated.mark) - end) - - end) + local allocated = SubClass:allocate() + assert_true(allocated.mark) end) - end + end) + + context('new', function() + + test('initializes instances properly', function() + local instance = SubClass:new() + assert_equal(instance.class, SubClass) + end) + + test('throws an error when used without the :', function() + assert_error(SubClass.new) + end) + + test('calls the initializer', function() + local initialized = SubClass:new() + assert_true(initialized.mark) + end) + + end) + end) diff --git a/spec/class_spec.lua b/spec/class_spec.lua index fd63b46..877a9b6 100644 --- a/spec/class_spec.lua +++ b/spec/class_spec.lua @@ -9,7 +9,11 @@ context('class()', function() end) context('when given a name', function() - local TheClass = class('TheClass') + local TheClass + + before(function() + TheClass = class('TheClass') + end) test('the resulting class has the correct name', function() assert_equal(TheClass.name, 'TheClass') diff --git a/spec/classes_spec.lua b/spec/classes_spec.lua index 9e5b882..338965d 100644 --- a/spec/classes_spec.lua +++ b/spec/classes_spec.lua @@ -2,26 +2,33 @@ require 'middleclass' context('A Class', function() - context('name', function() - test('is correctly set', function() - local TheClass = class('TheClass') - assert_equal(TheClass.name, 'TheClass') - end) - end) + context('Default stuff', function() - context('tostring', function() - test('returns "class *name*"', function() - local TheClass = class('TheClass') - assert_equal(tostring(TheClass), 'class TheClass') - end) - end) + local AClass - context('()', function() - test('returns an object, like Class:new()', function() - local TheClass = class('TheClass') - local obj = TheClass() - assert_true(instanceOf(TheClass, obj)) + before(function() + AClass = class('AClass') end) + + context('name', function() + test('is correctly set', function() + assert_equal(AClass.name, 'AClass') + end) + end) + + context('tostring', function() + test('returns "class *name*"', function() + assert_equal(tostring(AClass), 'class AClass') + end) + end) + + context('()', function() + test('returns an object, like Class:new()', function() + local obj = AClass() + assert_equal(obj.class, AClass) + end) + end) + end) context('attributes', function() diff --git a/spec/subclassOf_spec.lua b/spec/subclassOf_spec.lua index 19e4d1a..a84264f 100644 --- a/spec/subclassOf_spec.lua +++ b/spec/subclassOf_spec.lua @@ -1,3 +1,5 @@ +require 'middleclass' + context('subclassOf', function() context('nils, integers, etc', function()