I’m back fiddling with Codea after a long time plowing other fields. Thought I would again try to build a little unit testing framework. I was planning to emulate the java and c# ones, where you subclass e.g. TestCase, and the the framework runs all the test methods in your class.
So one question is, how can I get all the classes that are subclasses of TestCase?
Another question, as always, is whether I’m on the wrong track and should approach it another way.
I just finished writing the code, when I noticed I might have misunderstood your question. Anyways, here’s some code to find all the functions in a class:
--# Main
function setup()
local subclasses = {}
for i,v in pairs(TestCase) do
if type(v) == "function" then
table.insert(subclasses,{i,v})
end
end
for i,v in ipairs(subclasses) do
print("Function "..i..": "..v[1])
end
end
--# TestCase
TestCase = class()
function TestCase:init()
end
function TestCase:draw()
end
function TestCase:func1()
end
function TestCase:func2()
end
function TestCase:func3()
end
If you meant something else, please let me know.
Thanks!
Edit: I did misunderstand your question. Let me see if I can find a way.
function setup()
class = TestCase()
end
function draw()
class:init()
class:draw()
class:function1()
end
TestCase = class()
function TestCase:init()
end
function TestCase:draw()
end
function TestCase:function1()
end
@Luismi - What he means is he wants to find all the classes the inherit a specific class. You might try giving the parent class a unique identifier that the children inherit, then you go through each class and see if it has that unique identifier. Other than that, I can’t find a clear way.
Thanks!
-- SubClasses
-- Use this function to perform your initial setup
function setup()
s = subclasses(Animal)
print("subclasses of class Animal:\
")
for k, v in pairs(s) do print(k .. " says " .. v:say()) end
end
-- This function gets called once every frame
function draw()
end
function subclasses(c)
local t = {}
for k, v in pairs(_G) do
if type(v) == "table" and v._base and v._base == c then
t[k] = v
end
end
return t
end
Animal = class()
function Animal:say()
return self.sound
end
Dog = class(Animal)
Dog.sound = "Woof"
Cat = class(Animal)
Cat.sound = "Meow"
Fox = class(Animal)
Fox.sound = "???"
Zoyt, your example shows me that what I’m doing to find the test methods may not be far off.
Jordan, the v._base thing looks to be just what I’m looking for. It seems to find immediate subclasses, which is fine, prob forever. Otherwise I guess I could search recursively, but I see no real need to go there.
Luismi, I apologize that I wasn’t clear. Thanks, all.