diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 579dda03..13fb8379 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -455,7 +455,8 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) std::string name = p->GetString(); CBotClass* pOld = CBotClass::Find(name); - if ( pOld != nullptr && pOld->m_IsDef ) + if ( (pOld != nullptr && pOld->m_IsDef) || /* public class exists in different program */ + pStack->GetProgram()->ClassExists(name)) /* class exists in this program */ { pStack->SetError( CBotErrRedefClass, p ); return nullptr; @@ -489,14 +490,13 @@ CBotClass* CBotClass::Compile1(CBotToken* &p, CBotCStack* pStack) } int level = 1; - do // skip over the definition + while (level > 0 && p != nullptr) { int type = p->GetType(); p = p->GetNext(); if (type == ID_OPBLK) level++; if (type == ID_CLBLK) level--; } - while (level > 0 && p != nullptr); if (level > 0) pStack->SetError(CBotErrCloseBlock, classe->m_pOpenblk); diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index dce62198..32813de1 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -281,6 +281,16 @@ const std::list& CBotProgram::GetFunctions() return m_functions; } +bool CBotProgram::ClassExists(std::string name) +{ + for (CBotClass* p : m_classes) + { + if ( p->GetName() == name ) return true; + } + + return false; +} + //////////////////////////////////////////////////////////////////////////////// CBotTypResult cSizeOf( CBotVar* &pVar, void* pUser ) { diff --git a/src/CBot/CBotProgram.h b/src/CBot/CBotProgram.h index d90fb0ef..4a64b562 100644 --- a/src/CBot/CBotProgram.h +++ b/src/CBot/CBotProgram.h @@ -333,6 +333,12 @@ public: */ const std::list& GetFunctions(); + /** + * \brief Check if class with that name was created in this program + * \return True if class was defined in this program, otherwise, false + */ + bool ClassExists(std::string name); + /** * \brief Returns static list of all registered external calls */ diff --git a/test/unit/CBot/CBot_test.cpp b/test/unit/CBot/CBot_test.cpp index 0dae25e0..6cf28413 100644 --- a/test/unit/CBot/CBot_test.cpp +++ b/test/unit/CBot/CBot_test.cpp @@ -932,8 +932,19 @@ TEST_F(CBotUT, ClassMethodRedefined) ); } -// TODO: Not only doesn't work but segfaults -TEST_F(CBotUT, DISABLED_ClassRedefined) +TEST_F(CBotUT, ClassRedefinedInDifferentPrograms) +{ + auto publicProgram = ExecuteTest( + "public class TestClass {}\n" + ); + + ExecuteTest( + "public class TestClass {}\n", + CBotErrRedefClass + ); +} + +TEST_F(CBotUT, ClassRedefinedInOneProgram) { ExecuteTest( "public class TestClass {}\n" @@ -942,6 +953,15 @@ TEST_F(CBotUT, DISABLED_ClassRedefined) ); } +TEST_F(CBotUT, ClassMissingCloseBlock) +{ + ExecuteTest( + "public class Something\n" + "{\n", + CBotErrCloseBlock + ); +} + // TODO: NOOOOOO!!! Nononononono :/ TEST_F(CBotUT, DISABLED_PublicClasses) {