@@ -3250,6 +3250,158 @@ TEST_F(ClassTests, NonVirtualMethodInBaseClassCannotBeExposed)
32503250 ASSERT_EQ (1 , result<DerivedExampleClass>().virtualCFunctionConst_ );
32513251}
32523252
3253+ namespace {
3254+ struct FloatRectExample
3255+ {
3256+ float left = 0 .0f ;
3257+ float top = 0 .0f ;
3258+ float width = 0 .0f ;
3259+ float height = 0 .0f ;
3260+ };
3261+
3262+ class TransformableExample
3263+ {
3264+ public:
3265+ TransformableExample () = default ;
3266+ virtual ~TransformableExample () = default ;
3267+
3268+ void setPosition (float x, float y) { positionX_ = x; positionY_ = y; }
3269+ float getPositionX () const { return positionX_; }
3270+ float getPositionY () const { return positionY_; }
3271+
3272+ FloatRectExample getLocalBounds () const { return { 0 .0f , 0 .0f , 64 .0f , 32 .0f }; }
3273+
3274+ private:
3275+ float positionX_ = 0 .0f ;
3276+ float positionY_ = 0 .0f ;
3277+ };
3278+
3279+ class SpriteExample : public TransformableExample
3280+ {
3281+ public:
3282+ SpriteExample () = default ;
3283+
3284+ void setRotation (float rotation) { rotation_ = rotation; }
3285+ float getRotation () const { return rotation_; }
3286+
3287+ private:
3288+ float rotation_ = 0 .0f ;
3289+ };
3290+ } // namespace
3291+
3292+ TEST_F (ClassTests, BaseClassMethodsCanBeBoundOnDerivedClass)
3293+ {
3294+ luabridge::getGlobalNamespace (L)
3295+ .beginClass <FloatRectExample>(" FloatRect" )
3296+ .addProperty (" left" , &FloatRectExample::left)
3297+ .addProperty (" top" , &FloatRectExample::top)
3298+ .addProperty (" width" , &FloatRectExample::width)
3299+ .addProperty (" height" , &FloatRectExample::height)
3300+ .endClass ()
3301+ .beginClass <SpriteExample>(" Sprite" )
3302+ .addConstructor <void (*)()>()
3303+ .addFunction (" setPosition" , &SpriteExample::setPosition) // void (TransformableExample::*)(float, float)
3304+ .addFunction (" getPositionX" , &SpriteExample::getPositionX) // float (TransformableExample::*)() const
3305+ .addFunction (" getPositionY" , &SpriteExample::getPositionY)
3306+ .addFunction (" getLocalBounds" , &SpriteExample::getLocalBounds) // FloatRectExample (TransformableExample::*)() const
3307+ .addFunction (" setRotation" , &SpriteExample::setRotation)
3308+ .addFunction (" getRotation" , &SpriteExample::getRotation)
3309+ .endClass ();
3310+
3311+ runLua (R"(
3312+ local sprite = Sprite ()
3313+ sprite:setPosition (10.0, 20.0)
3314+ sprite:setRotation (45.0)
3315+ local bounds = sprite:getLocalBounds ()
3316+ result = {
3317+ x = sprite:getPositionX (),
3318+ y = sprite:getPositionY (),
3319+ rotation = sprite:getRotation (),
3320+ width = bounds.width,
3321+ height = bounds.height
3322+ }
3323+ )" );
3324+
3325+ ASSERT_TRUE (result ().isTable ());
3326+ ASSERT_EQ (10 .0f , result ()[" x" ].cast <float >());
3327+ ASSERT_EQ (20 .0f , result ()[" y" ].cast <float >());
3328+ ASSERT_EQ (45 .0f , result ()[" rotation" ].cast <float >());
3329+ ASSERT_EQ (64 .0f , result ()[" width" ].cast <float >());
3330+ ASSERT_EQ (32 .0f , result ()[" height" ].cast <float >());
3331+ }
3332+
3333+ TEST_F (ClassTests, BaseClassMethodsCanBeBoundOnDerivedClassViaStaticCast)
3334+ {
3335+ luabridge::getGlobalNamespace (L)
3336+ .beginClass <FloatRectExample>(" FloatRect" )
3337+ .addProperty (" width" , &FloatRectExample::width)
3338+ .addProperty (" height" , &FloatRectExample::height)
3339+ .endClass ()
3340+ .beginClass <SpriteExample>(" Sprite" )
3341+ .addConstructor <void (*)()>()
3342+ .addFunction (" setPosition" , static_cast <void (SpriteExample::*)(float , float )>(&SpriteExample::setPosition))
3343+ .addFunction (" getPositionX" , static_cast <float (SpriteExample::*)() const >(&SpriteExample::getPositionX))
3344+ .addFunction (" getLocalBounds" , static_cast <FloatRectExample (SpriteExample::*)() const >(&SpriteExample::getLocalBounds))
3345+ .endClass ();
3346+
3347+ runLua (R"(
3348+ local sprite = Sprite ()
3349+ sprite:setPosition (10.0, 20.0)
3350+ local bounds = sprite:getLocalBounds ()
3351+ result = {
3352+ x = sprite:getPositionX (),
3353+ width = bounds.width,
3354+ height = bounds.height
3355+ }
3356+ )" );
3357+
3358+ ASSERT_TRUE (result ().isTable ());
3359+ ASSERT_EQ (10 .0f , result ()[" x" ].cast <float >());
3360+ ASSERT_EQ (64 .0f , result ()[" width" ].cast <float >());
3361+ ASSERT_EQ (32 .0f , result ()[" height" ].cast <float >());
3362+ }
3363+
3364+ TEST_F (ClassTests, BaseClassMethodsAreInheritedThroughDeriveClass)
3365+ {
3366+ luabridge::getGlobalNamespace (L)
3367+ .beginClass <FloatRectExample>(" FloatRect" )
3368+ .addProperty (" width" , &FloatRectExample::width)
3369+ .addProperty (" height" , &FloatRectExample::height)
3370+ .endClass ()
3371+ .beginClass <TransformableExample>(" Transformable" )
3372+ .addFunction (" setPosition" , &TransformableExample::setPosition)
3373+ .addFunction (" getPositionX" , &TransformableExample::getPositionX)
3374+ .addFunction (" getPositionY" , &TransformableExample::getPositionY)
3375+ .addFunction (" getLocalBounds" , &TransformableExample::getLocalBounds)
3376+ .endClass ()
3377+ .deriveClass <SpriteExample, TransformableExample>(" Sprite" )
3378+ .addConstructor <void (*)()>()
3379+ .addFunction (" setRotation" , &SpriteExample::setRotation)
3380+ .addFunction (" getRotation" , &SpriteExample::getRotation)
3381+ .endClass ();
3382+
3383+ runLua (R"(
3384+ local sprite = Sprite ()
3385+ sprite:setPosition (10.0, 20.0)
3386+ sprite:setRotation (45.0)
3387+ local bounds = sprite:getLocalBounds ()
3388+ result = {
3389+ x = sprite:getPositionX (),
3390+ y = sprite:getPositionY (),
3391+ rotation = sprite:getRotation (),
3392+ width = bounds.width,
3393+ height = bounds.height
3394+ }
3395+ )" );
3396+
3397+ ASSERT_TRUE (result ().isTable ());
3398+ ASSERT_EQ (10 .0f , result ()[" x" ].cast <float >());
3399+ ASSERT_EQ (20 .0f , result ()[" y" ].cast <float >());
3400+ ASSERT_EQ (45 .0f , result ()[" rotation" ].cast <float >());
3401+ ASSERT_EQ (64 .0f , result ()[" width" ].cast <float >());
3402+ ASSERT_EQ (32 .0f , result ()[" height" ].cast <float >());
3403+ }
3404+
32533405TEST_F (ClassTests, NilCanBeConvertedToNullptrButNotToReference)
32543406{
32553407 struct X {};
0 commit comments