From 95661918ce5da32cce84055e80fcc15343591638 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Fri, 20 Dec 2013 20:18:30 +0100 Subject: [PATCH] Some further refactoring and test corrections --- src/ui/displayinfo.cpp | 38 ++++++++++++++++++----------- src/ui/studio.cpp | 2 +- test/unit/ui/edit_test.cpp | 3 ++- test/unit/ui/mocks/text_mock.h | 16 +++++++++--- test/unit/ui/stubs/restext_stub.cpp | 2 +- 5 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/ui/displayinfo.cpp b/src/ui/displayinfo.cpp index bd20452b..79eb38a0 100644 --- a/src/ui/displayinfo.cpp +++ b/src/ui/displayinfo.cpp @@ -23,6 +23,7 @@ #include "common/iman.h" #include "common/misc.h" #include "common/restext.h" +#include "common/stringutils.h" #include "graphics/core/light.h" #include "graphics/engine/engine.h" @@ -971,32 +972,41 @@ void ObjectAdd(ObjectList list[], ObjectType type) void ObjectWrite(FILE* file, ObjectList list[], int i) { - char line[100]; - char* p; + std::string line; if ( list[i].total < 10 ) { - sprintf(line, "\\c; %dx \\n;\\l;", list[i].total); + line = StrUtils::Format("\\c; %dx \\n;\\l;", list[i].total); } else { - sprintf(line, "\\c;%dx \\n;\\l;", list[i].total); + line = StrUtils::Format("\\c;%dx \\n;\\l;", list[i].total); } std::string res; GetResource(RES_OBJECT, list[i].type, res); - if (res.empty()) return; - strcat(line, res.c_str()); + if (res.empty()) + return; + + line += res; + + line += "\\u "; - strcat(line, "\\u "); std::string helpFilename = GetHelpFilename(list[i].type); - p = const_cast(helpFilename.c_str()); - if ( p[0] == 0 ) return; - strcat(line, p+7); // skip "help\?\" - p = strstr(line, ".txt"); - if ( p != 0 ) *p = 0; - strcat(line, ";\n"); - fputs(line, file); + if (helpFilename.empty()) + return; + + line += helpFilename.substr(7); // skip "help\?\" + + auto pos = line.find(".txt"); + if (pos != std::string::npos) + { + line = line.substr(0, pos); + } + + line += ";\n"; + + fputs(line.c_str(), file); } // Creates the file containing the list of objects. diff --git a/src/ui/studio.cpp b/src/ui/studio.cpp index ba28a0aa..fb4dd3d1 100644 --- a/src/ui/studio.cpp +++ b/src/ui/studio.cpp @@ -1497,7 +1497,7 @@ void CStudio::UpdateDialogPublic() pl = static_cast< CLabel* >(pw->SearchControl(EVENT_DIALOG_LABEL1)); if ( pl != 0 ) { - //? GetResource(RES_TEXT, RT_IO_LIST, name); + // GetResource(RES_TEXT, RT_IO_LIST, name); // TODO: unused? pl->SetName(SearchDirectory(false).c_str(), false); } } diff --git a/test/unit/ui/edit_test.cpp b/test/unit/ui/edit_test.cpp index 21af00f1..4a4063ec 100644 --- a/test/unit/ui/edit_test.cpp +++ b/test/unit/ui/edit_test.cpp @@ -51,6 +51,7 @@ protected: }; using ::testing::_; +using ::testing::An; using ::testing::Return; TEST_F(CEditTest, WriteTest) @@ -58,7 +59,7 @@ 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)); + EXPECT_CALL(*text, GetStringWidth(An(), _, _, _)).WillOnce(Return(1.0f)); std::string filename = "test.file"; m_edit->SetMaxChar(Ui::EDITSTUDIOMAX); m_edit->SetAutoIndent(true); diff --git a/test/unit/ui/mocks/text_mock.h b/test/unit/ui/mocks/text_mock.h index f38b9772..b9af6d31 100644 --- a/test/unit/ui/mocks/text_mock.h +++ b/test/unit/ui/mocks/text_mock.h @@ -15,11 +15,21 @@ public: { }; - MOCK_METHOD4(GetCharWidth, float(Gfx::UTF8Char, Gfx::FontType, float, float)); + MOCK_METHOD4(GetCharWidth, float(Gfx::UTF8Char ch, + Gfx::FontType type, + float size, + float offset)); MOCK_METHOD4(GetStringWidth, float(const std::string &text, std::vector::iterator format, - std::vector::iterator end, float size)); - MOCK_METHOD3(GetStringWidth, float(const std::string &, Gfx::FontType, float)); + std::vector::iterator end, + float size)); + MOCK_METHOD3(GetStringWidth, float(std::string text, + Gfx::FontType font, + float size)); + MOCK_METHOD4(GetStringWidth, float(Gfx::UTF8Char ch, + Gfx::FontType font, + float size, + float offset)); }; diff --git a/test/unit/ui/stubs/restext_stub.cpp b/test/unit/ui/stubs/restext_stub.cpp index 004da19a..fa47da6e 100644 --- a/test/unit/ui/stubs/restext_stub.cpp +++ b/test/unit/ui/stubs/restext_stub.cpp @@ -1,6 +1,6 @@ #include "common/restext.h" -bool GetResource(ResType /* type */, int /* num */, char* /* text */) +bool GetResource(ResType /* type */, int /* num */, std::string& /* text */) { return true; }