From 8a932fed3e5700af283017deab3cb1da11cb0dce Mon Sep 17 00:00:00 2001 From: Zaba999 Date: Thu, 11 Oct 2012 23:09:29 +0200 Subject: Added saving user scripts on game save. Fixed bug in writing script to file. --- src/ui/test/CMakeLists.txt | 33 +++++++++++++++ src/ui/test/edit_test.cpp | 73 +++++++++++++++++++++++++++++++++ src/ui/test/mocks/text_mock.h | 21 ++++++++++ src/ui/test/stubs/app_stub.cpp | 26 ++++++++++++ src/ui/test/stubs/engine_stub.cpp | 79 ++++++++++++++++++++++++++++++++++++ src/ui/test/stubs/restext_stub.cpp | 11 +++++ src/ui/test/stubs/robotmain_stub.cpp | 17 ++++++++ 7 files changed, 260 insertions(+) create mode 100644 src/ui/test/CMakeLists.txt create mode 100644 src/ui/test/edit_test.cpp create mode 100644 src/ui/test/mocks/text_mock.h create mode 100644 src/ui/test/stubs/app_stub.cpp create mode 100644 src/ui/test/stubs/engine_stub.cpp create mode 100644 src/ui/test/stubs/restext_stub.cpp create mode 100644 src/ui/test/stubs/robotmain_stub.cpp (limited to 'src/ui/test') diff --git a/src/ui/test/CMakeLists.txt b/src/ui/test/CMakeLists.txt new file mode 100644 index 0000000..e49e6af --- /dev/null +++ b/src/ui/test/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 2.8) + +set(CMAKE_BUILD_TYPE debug) +set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wold-style-cast -std=gnu++0x") + +include_directories( +. +../.. +../../.. +${GTEST_DIR}/include +) + + +add_executable(edit_test + ../../common/event.cpp + ../../common/logger.cpp + ../../common/misc.cpp + ../../common/iman.cpp + ../../common/stringutils.cpp + ../../graphics/engine/particle.cpp + ../../graphics/engine/text.cpp + ../button.cpp + ../control.cpp + ../edit.cpp + ../scroll.cpp + stubs/app_stub.cpp + stubs/engine_stub.cpp + stubs/restext_stub.cpp + stubs/robotmain_stub.cpp + edit_test.cpp) +target_link_libraries(edit_test gtest gmock ${SDL_LIBRARY} ${SDLTTF_LIBRARY}) + +add_test(edit_test ./edit_test) diff --git a/src/ui/test/edit_test.cpp b/src/ui/test/edit_test.cpp new file mode 100644 index 0000000..489b873 --- /dev/null +++ b/src/ui/test/edit_test.cpp @@ -0,0 +1,73 @@ +#include "../edit.h" +#include "../../app/app.h" +#include "mocks/text_mock.h" +#include +#include +#include + +class CEditTest : public testing::Test +{ +public: + CEditTest(){}; + + virtual void SetUp() + { + m_engine = new Gfx::CEngine(&m_iMan, NULL); + + m_iMan.AddInstance(CLASS_ENGINE, m_engine); + m_edit = new Ui::CEdit; + } + + virtual void TearDown() + { + m_iMan.DeleteInstance(CLASS_ENGINE, m_engine); + delete m_engine; + m_engine = NULL; + delete m_edit; + m_edit = NULL; + + } + virtual ~CEditTest() + { + + }; + +protected: + CInstanceManager m_iMan; + CApplication m_app; + Gfx::CEngine * m_engine; + Ui::CEdit * m_edit; + CLogger m_logger; +}; + +using ::testing::_; +using ::testing::Return; + +TEST_F(CEditTest, WriteTest) +{ + ASSERT_TRUE(true); + CTextMock * text = dynamic_cast(m_engine->GetText()); + EXPECT_CALL(*text, GetCharWidth(_, _, _, _)).WillRepeatedly(Return(1.0f)); + EXPECT_CALL(*text, GetStringWidth(_, _, _)).WillOnce(Return(1.0f)); + std::string filename = "test.file"; + m_edit->SetMaxChar(Ui::EDITSTUDIOMAX); + m_edit->SetAutoIndent(true); + std::string inputScript = "{\ntext1\ntext2\n\ntext3\n{\ntext4\n}\n}"; + std::string expectedScript = "{\r\n\ttext1\r\n\ttext2\r\n\t\r\n\ttext3\r\n\t{\r\n\t\ttext4\r\n\t}\r\n}"; + m_edit->SetText(inputScript.c_str(), true); + GetLogger()->Info("Writing text \n"); + m_edit->WriteText("script.txt"); + + std::fstream scriptFile; + + scriptFile.open("script.txt", std::ios_base::binary | std::ios_base::in); + std::string outputScript((std::istreambuf_iterator(scriptFile)), std::istreambuf_iterator()); + ASSERT_STREQ(expectedScript.c_str(), outputScript.c_str()); +} + +int main(int argc, char *argv[]) +{ + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} + diff --git a/src/ui/test/mocks/text_mock.h b/src/ui/test/mocks/text_mock.h new file mode 100644 index 0000000..59a6c48 --- /dev/null +++ b/src/ui/test/mocks/text_mock.h @@ -0,0 +1,21 @@ +#include "../../graphics/engine/text.h" +#include +#include "../../common/logger.h" + + +class CTextMock : public Gfx::CText +{ +public: + CTextMock(CInstanceManager *iMan, Gfx::CEngine* engine) : CText(iMan, engine) + { + } + + virtual ~CTextMock() + { + }; + + MOCK_METHOD4(GetCharWidth, float(Gfx::UTF8Char, Gfx::FontType, float, float)); + MOCK_METHOD3(GetStringWidth, float(const std::string &, Gfx::FontType, float)); + +}; + diff --git a/src/ui/test/stubs/app_stub.cpp b/src/ui/test/stubs/app_stub.cpp new file mode 100644 index 0000000..5dd79e4 --- /dev/null +++ b/src/ui/test/stubs/app_stub.cpp @@ -0,0 +1,26 @@ +#include "../../app/app.h" +#include "../../graphics/opengl/gldevice.h" + +template<> CApplication* CSingleton::mInstance = nullptr; + +namespace Gfx { + +GLDeviceConfig::GLDeviceConfig() +{ +} + +} /* Gfx */ +CApplication::CApplication() +{ +} + +CApplication::~CApplication() +{ +} + +std::string CApplication::GetDataFilePath(DataDir /* dataDir */, const std::string& subpath) +{ + return subpath; +} + + diff --git a/src/ui/test/stubs/engine_stub.cpp b/src/ui/test/stubs/engine_stub.cpp new file mode 100644 index 0000000..6ec6006 --- /dev/null +++ b/src/ui/test/stubs/engine_stub.cpp @@ -0,0 +1,79 @@ +#include "../../graphics/engine/engine.h" +#include "../../graphics/engine/text.h" +#include "../mocks/text_mock.h" + +namespace Gfx { + +CEngine::CEngine(CInstanceManager* iMan, CApplication* app) : + m_iMan(iMan), m_app(app) +{ + m_text = new CTextMock(m_iMan, this); + m_text->Create(); +} + +CEngine::~CEngine() +{ + delete m_text; + m_text = NULL; +} + +Math::Point CEngine::WindowToInterfaceSize(Math::IntPoint size) +{ + return Math::Point(size.x, size.y); +} + +void CEngine::SetState(int state, const Color& color) +{ + if (state == m_lastState && color == m_lastColor) + return; + + m_lastState = state; + m_lastColor = color; +} + +Math::IntPoint CEngine::GetWindowSize() +{ + return m_size; +} + +void CEngine::AddStatisticTriangle(int count) +{ + m_statisticTriangle += count; +} + +void CEngine::SetMouseType(EngineMouseType type) +{ + m_mouseType = type; +} + +bool CEngine::SetTexture(const std::string& /* name */, int /* stage */) +{ + return true; +} + +CText* CEngine::GetText() +{ + return m_text; +} + +CDevice* CEngine::GetDevice() +{ + return m_device; +} + +int CEngine::GetEditIndentValue() +{ + return m_editIndentValue; +} + +void CEngine::DeleteTexture(const std::string& /* texName */) +{ +} +Texture CEngine::LoadTexture(const std::string& /* name */) +{ + Texture texture; + return texture; +} + +} /* Gfx */ + diff --git a/src/ui/test/stubs/restext_stub.cpp b/src/ui/test/stubs/restext_stub.cpp new file mode 100644 index 0000000..c1986ca --- /dev/null +++ b/src/ui/test/stubs/restext_stub.cpp @@ -0,0 +1,11 @@ +#include "../../common/restext.h" +bool GetResource(ResType /* type */, int /* num */, char* /* text */) +{ + return true; +} + +bool SearchKey(const char * /* cmd */, InputSlot & /* key */) +{ + return true; +} + diff --git a/src/ui/test/stubs/robotmain_stub.cpp b/src/ui/test/stubs/robotmain_stub.cpp new file mode 100644 index 0000000..93e0e82 --- /dev/null +++ b/src/ui/test/stubs/robotmain_stub.cpp @@ -0,0 +1,17 @@ +#include "../../object/robotmain.h" + + +template<> CRobotMain* CSingleton::mInstance = nullptr; + +bool CRobotMain::GetGlint() +{ + return false; +} + +const InputBinding& CRobotMain::GetInputBinding(InputSlot slot) +{ + unsigned int index = static_cast(slot); + assert(index >= 0 && index < INPUT_SLOT_MAX); + return m_inputBindings[index]; +} + -- cgit v1.2.3-1-g7c22