Get all subclasses?

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.

Thanks,

Ron

Let me write up a quick example.

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

I do not know if you mean that

@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!

Ops @Zoyt . My apologies , really is that I did not understand the question very well

@Luismi - No worries. I didn’t either at first (I had to Google it).

@RonJeffries is this what you are looking for?

-- 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 = "???"

All, thanks!

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.

R

Ok, that nearly worked. I think it’s “base”, not “_base”, and I have some other issues, but am an inch further along. Thanks again…

R

@Jordan - =)) I see what you did there.

Am I missing something with the base / _base thing? The tables for classes sure seem to me to say base, but the docs all say _base.

Thanks,

R

@Zoyt, @Jordan, What does the Fox say? That’s a classic :))

@RonJeffries - http://www.youtube.com/watch?v=jofNR_WkoCE
Here is most likely your reaction when you see it:
No sign of life

@Zoyt Yet again, I can only apologise on behalf of the Norwegians for releasing that to the world …

I Love That Song!!!

Foxes yip or bark or something, don’t they?

If they don’t yip or bark or something, I really wanna know, what does the fox say?

@SkyTheCoder We should probably get back on topic, but foxes make a variety of sounds, excluding yipping and barking.