1 /*****************************************************************************
2 * Copyright (c) 2014-2018 OpenRCT2 developers
3 *
4 * For a complete list of all authors, please refer to contributors.md
5 * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
6 *
7 * OpenRCT2 is licensed under the GNU General Public License version 3.
8 *****************************************************************************/
9
10 #include "openrct2/localisation/LanguagePack.h"
11
12 #include "openrct2/localisation/Language.h"
13 #include "openrct2/localisation/StringIds.h"
14
15 #include <gtest/gtest.h>
16
17 class LanguagePackTest : public testing::Test
18 {
19 protected:
20 static const utf8* LanguageEnGB;
21 static const unsigned char LanguageZhTW[];
22 };
23
TEST_F(LanguagePackTest,create_empty)24 TEST_F(LanguagePackTest, create_empty)
25 {
26 auto empty = LanguagePackFactory::FromText(0, "");
27 ASSERT_EQ(empty->GetId(), 0);
28 ASSERT_EQ(empty->GetCount(), 0U);
29 }
30
TEST_F(LanguagePackTest,create_mutable_id_1)31 TEST_F(LanguagePackTest, create_mutable_id_1)
32 {
33 auto lang = LanguagePackFactory::FromText(1, "STR_0000:\n");
34 ASSERT_EQ(lang->GetId(), 1);
35 ASSERT_EQ(lang->GetCount(), 1U);
36 ASSERT_STREQ(lang->GetString(0), nullptr);
37 lang->SetString(0, "xx");
38 ASSERT_EQ(lang->GetCount(), 1U);
39 ASSERT_STREQ(lang->GetString(0), "xx");
40 }
41
TEST_F(LanguagePackTest,language_pack_simple)42 TEST_F(LanguagePackTest, language_pack_simple)
43 {
44 auto lang = LanguagePackFactory::FromText(0, LanguageEnGB);
45 ASSERT_EQ(lang->GetId(), 0);
46 ASSERT_EQ(lang->GetCount(), 4U);
47 ASSERT_STREQ(lang->GetString(2), "Spiral Roller Coaster");
48 ASSERT_EQ(lang->GetScenarioOverrideStringId("Arid Heights", 0), 0x7000);
49 ASSERT_STREQ(lang->GetString(0x7000), "Arid Heights scenario string");
50 ASSERT_EQ(lang->GetObjectOverrideStringId("CONDORRD", 0), 0x6000);
51 ASSERT_STREQ(lang->GetString(0x6000), "my test ride");
52 // Test some negatives too
53 ASSERT_EQ(lang->GetString(1000), nullptr);
54 ASSERT_EQ(lang->GetScenarioOverrideStringId("No such park", 0), STR_NONE);
55 ASSERT_EQ(lang->GetObjectOverrideStringId(" ", 0), STR_NONE);
56 }
57
TEST_F(LanguagePackTest,language_pack_multibyte)58 TEST_F(LanguagePackTest, language_pack_multibyte)
59 {
60 auto lang = LanguagePackFactory::FromText(0, (const utf8*)LanguageZhTW);
61 ASSERT_EQ(lang->GetId(), 0);
62 ASSERT_EQ(lang->GetCount(), 4U);
63 ASSERT_STREQ(lang->GetString(2), u8"懸吊式雲霄飛車");
64 ASSERT_EQ(lang->GetScenarioOverrideStringId("Forest Frontiers", 0), 0x7000);
65 ASSERT_EQ(lang->GetScenarioOverrideStringId("Forest Frontiers", 2), 0x7002);
66 ASSERT_STREQ(lang->GetString(0x7000), "Forest Frontiers");
67 ASSERT_STREQ(lang->GetString(0x7002), u8"在隱藏於森林深處的清空範圍中, 建造一個很受歡迎的樂園");
68 ASSERT_EQ(lang->GetObjectOverrideStringId("CONDORRD", 0), 0x6000);
69 ASSERT_STREQ(lang->GetString(0x6000), u8"神鷹暢遊");
70 }
71
72 const utf8* LanguagePackTest::LanguageEnGB = "# STR_XXXX part is read and XXXX becomes the string id number.\n"
73 "# Everything after the colon and before the new line will be saved as the "
74 "string.\n"
75 "# Use # at the beginning of a line to leave a comment.\n"
76 "STR_0000 :\n"
77 "STR_0001 :{STRINGID} {COMMA16}\n"
78 "STR_0002 :Spiral Roller Coaster\n"
79 "STR_0003 :Stand-up Roller Coaster\n"
80 "<Arid Heights>\n"
81 "STR_SCNR :Arid Heights scenario string\n"
82 "STR_PARK :Arid Heights park string\n"
83 "STR_DTLS :Free of any financial limits, your challenge is to develop "
84 "this desert park while keeping the guests happy\n"
85 "[CONDORRD]\n"
86 "STR_NAME :my test ride\n"
87 "STR_DESC :ride description\n"
88 "STR_CPTY :ride capacity\n";
89
90 // This includes a few entries extracted from zh-TW localisation.
91 // It has to be declared as `unsigned char`, or else the values overflow signed byte.
92 const unsigned char LanguagePackTest::LanguageZhTW[] = {
93 0x53, 0x54, 0x52, 0x5f, 0x30, 0x30, 0x30, 0x30, 0x20, 0x20, 0x20, 0x20, 0x3a, 0xe8, 0x9e, 0xba, 0xe6, 0x97, 0x8b, 0xe5,
94 0xbc, 0x8f, 0xe9, 0x9b, 0xb2, 0xe9, 0x9c, 0x84, 0xe9, 0xa3, 0x9b, 0xe8, 0xbb, 0x8a, 0x0a, 0x53, 0x54, 0x52, 0x5f, 0x30,
95 0x30, 0x30, 0x31, 0x20, 0x20, 0x20, 0x20, 0x3a, 0xe7, 0xab, 0x99, 0xe7, 0xab, 0x8b, 0xe5, 0xbc, 0x8f, 0xe9, 0x9b, 0xb2,
96 0xe9, 0x9c, 0x84, 0xe9, 0xa3, 0x9b, 0xe8, 0xbb, 0x8a, 0x0a, 0x53, 0x54, 0x52, 0x5f, 0x30, 0x30, 0x30, 0x32, 0x20, 0x20,
97 0x20, 0x20, 0x3a, 0xe6, 0x87, 0xb8, 0xe5, 0x90, 0x8a, 0xe5, 0xbc, 0x8f, 0xe9, 0x9b, 0xb2, 0xe9, 0x9c, 0x84, 0xe9, 0xa3,
98 0x9b, 0xe8, 0xbb, 0x8a, 0x0a, 0x53, 0x54, 0x52, 0x5f, 0x30, 0x30, 0x30, 0x33, 0x20, 0x20, 0x20, 0x20, 0x3a, 0xe5, 0x8f,
99 0x8d, 0xe8, 0xbd, 0x89, 0xe5, 0xbc, 0x8f, 0xe9, 0x9b, 0xb2, 0xe9, 0x9c, 0x84, 0xe9, 0xa3, 0x9b, 0xe8, 0xbb, 0x8a, 0x0a,
100 0x3c, 0x46, 0x6f, 0x72, 0x65, 0x73, 0x74, 0x20, 0x46, 0x72, 0x6f, 0x6e, 0x74, 0x69, 0x65, 0x72, 0x73, 0x3e, 0x0a, 0x53,
101 0x54, 0x52, 0x5f, 0x53, 0x43, 0x4e, 0x52, 0x20, 0x20, 0x20, 0x20, 0x3a, 0x46, 0x6f, 0x72, 0x65, 0x73, 0x74, 0x20, 0x46,
102 0x72, 0x6f, 0x6e, 0x74, 0x69, 0x65, 0x72, 0x73, 0x0a, 0x53, 0x54, 0x52, 0x5f, 0x50, 0x41, 0x52, 0x4b, 0x20, 0x20, 0x20,
103 0x20, 0x3a, 0x46, 0x6f, 0x72, 0x65, 0x73, 0x74, 0x20, 0x46, 0x72, 0x6f, 0x6e, 0x74, 0x69, 0x65, 0x72, 0x73, 0x0a, 0x53,
104 0x54, 0x52, 0x5f, 0x44, 0x54, 0x4c, 0x53, 0x20, 0x20, 0x20, 0x20, 0x3a, 0xe5, 0x9c, 0xa8, 0xe9, 0x9a, 0xb1, 0xe8, 0x97,
105 0x8f, 0xe6, 0x96, 0xbc, 0xe6, 0xa3, 0xae, 0xe6, 0x9e, 0x97, 0xe6, 0xb7, 0xb1, 0xe8, 0x99, 0x95, 0xe7, 0x9a, 0x84, 0xe6,
106 0xb8, 0x85, 0xe7, 0xa9, 0xba, 0xe7, 0xaf, 0x84, 0xe5, 0x9c, 0x8d, 0xe4, 0xb8, 0xad, 0x2c, 0x20, 0xe5, 0xbb, 0xba, 0xe9,
107 0x80, 0xa0, 0xe4, 0xb8, 0x80, 0xe5, 0x80, 0x8b, 0xe5, 0xbe, 0x88, 0xe5, 0x8f, 0x97, 0xe6, 0xad, 0xa1, 0xe8, 0xbf, 0x8e,
108 0xe7, 0x9a, 0x84, 0xe6, 0xa8, 0x82, 0xe5, 0x9c, 0x92, 0x0a, 0x5b, 0x43, 0x4f, 0x4e, 0x44, 0x4f, 0x52, 0x52, 0x44, 0x5d,
109 0x0a, 0x53, 0x54, 0x52, 0x5f, 0x4e, 0x41, 0x4d, 0x45, 0x20, 0x20, 0x20, 0x20, 0x3a, 0xe7, 0xa5, 0x9e, 0xe9, 0xb7, 0xb9,
110 0xe6, 0x9a, 0xa2, 0xe9, 0x81, 0x8a, 0x0a, 0x53, 0x54, 0x52, 0x5f, 0x44, 0x45, 0x53, 0x43, 0x20, 0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