Skip to content

Commit

Permalink
add test for po file parser
Browse files Browse the repository at this point in the history
  • Loading branch information
y5nw committed Jul 8, 2024
1 parent 0295886 commit ecf8857
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/translation.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Translations
static inline bool isTranslationFile(const std::string &filename) {
return getFileLanguage(filename) != "";
}
// for testing
inline size_t size() { return m_translations.size() + m_plural_translations.size()/2; }

private:
std::unordered_map<std::wstring, std::wstring> m_translations;
Expand Down
25 changes: 25 additions & 0 deletions src/unittest/test_translations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/

#include "translation.h"
#include "filesys.h"
#include "content/subgames.h"
#include "catch.h"

#define TEXTDOMAIN_PO L"translation_po"
#define TEST_PO_NAME "translation_po.de.po"

TEST_CASE("test translations")
{
SECTION("Plural-Forms function for translations")
Expand All @@ -30,4 +35,24 @@ TEST_CASE("test translations")
CHECK((*form)(1) == 0);
CHECK((*form)(2) == 1);
}

SECTION("PO file parser")
{
Translations translations;
auto gamespec = findSubgame("devtest");
CHECK(gamespec.isValid());
auto popath = gamespec.gamemods_path + (DIR_DELIM "testtranslations" DIR_DELIM "locale" DIR_DELIM TEST_PO_NAME);
std::string content;
CHECK(fs::ReadFile(popath, content));
translations.loadTranslation(TEST_PO_NAME, content);

CHECK(translations.size() == 3);
CHECK(translations.getTranslation(TEXTDOMAIN_PO, L"foo") == L"bar");
CHECK(translations.getTranslation(TEXTDOMAIN_PO, L"Untranslated") == L"Untranslated");
CHECK(translations.getTranslation(TEXTDOMAIN_PO, L"Fuzzy") == L"Fuzzy");
CHECK(translations.getTranslation(TEXTDOMAIN_PO, L"Multi\\line\nstring") == L"Multi\\\"li\\ne\nresult");
CHECK(translations.getTranslation(TEXTDOMAIN_PO, L"Wrong order") == L"Wrong order");
CHECK(translations.getPluralTranslation(TEXTDOMAIN_PO, L"Plural form", 1) == L"Singular result");
CHECK(translations.getPluralTranslation(TEXTDOMAIN_PO, L"Singular form", 0) == L"Plural result");
}
}

0 comments on commit ecf8857

Please sign in to comment.