-- $LastChangedDate: 2010-05-25 21:26:05 -0400 (Tue, 25 May 2010) $
-- LEGAL: COPYRIGHT (C) 2010 JIM E. BROOKS WWW.PALOMINO3D.ORG
-- This tests ability to inherit and override a base method.

dofile( "pre.lua" )
LoadScript( "lib.lua" )

Object = { }

function Object:New( name )
    -- If base class was instantiated directly, the base class will be passed as self.
    -- Or a derivative was instantiated which can chain ctors by passing its instance as self.
    if ( self == Object ) then
        self = lib.NewInstance( Object )  -- not chained, need new instance
    end
    self.name = name
    return self
end

function Object:GetClassName()
    return "Object"
end

function Object:GetName()
    return self.name
end

Dyna = lib.DerivedClass( Object )

function Dyna:New( name )
    self = lib.NewInstance( Dyna )
    Object.New( self, name )
  --self.name assigned by base ctor
    self.className = "Dyna"
    return self
end

function Dyna:GetClassName()
    return "Dyna"
end

-- Test.
object = Object:New( "Object" )
print( "object:GetClassName() = '" .. object:GetClassName() .. "'" )
assert( object:GetClassName() == "Object" )
print( "object:GetName()      = '" .. object:GetName() .. "'" )
assert( object:GetName() == "Object" )

dyna = Dyna:New( "Dyna" )
print( "dyna:GetClassName()   = '" .. dyna:GetClassName() .. "'" )
assert( dyna:GetClassName() == "Dyna" )
print( "dyna:GetName()        = '" .. dyna:GetName() .. "'" )
assert( dyna:GetName() == "Dyna" )

-- Success.
os.exit( 0 )
