Merge branch 'daemon-ext'
[libi2ncommon] / test / test_i18n.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /**
21    i18n unit tests
22
23    @copyright Intra2net AG
24 */
25
26 #define BOOST_TEST_DYN_LINK
27 #include <boost/test/unit_test.hpp>
28
29 #include "i18n.h"
30 #include <vector>
31 #include <string>
32
33 using namespace std;
34
35 BOOST_AUTO_TEST_SUITE(TestI18n)
36
37 BOOST_AUTO_TEST_CASE(GetString)
38 {
39     vector<string> data;
40     data.push_back("dummy");
41
42     string result = i18n_get_string("$0 test string", data);
43
44     BOOST_CHECK_EQUAL("dummy test string", result);
45 }
46
47 BOOST_AUTO_TEST_CASE(MissingParameter)
48 {
49     vector<string> data;
50     string result = i18n_get_string("$0 test string", data);
51
52     BOOST_CHECK_EQUAL("i18n parameter error: missing variable ->$0<- in string ->$0 test string<-", result);
53 }
54
55 BOOST_AUTO_TEST_CASE(MissingNumberAfterDollar)
56 {
57     vector<string> data;
58     string result = i18n_get_string("$ test string", data);
59
60     BOOST_CHECK_EQUAL("i18n syntax error: $ without number at pos ->0<- in string ->$ test string<-", result);
61 }
62
63 BOOST_AUTO_TEST_CASE(MissingNumberAfterDollar2)
64 {
65     vector<string> data;
66     string result = i18n_get_string("$x test string", data);
67
68     BOOST_CHECK_EQUAL("i18n syntax error: $ without number at pos ->0<- in string ->$x test string<-", result);
69 }
70
71 BOOST_AUTO_TEST_CASE(BrokenNumberAfterDollar)
72 {
73     vector<string> data;
74     string result = i18n_get_string("$x test string", data);
75
76     BOOST_CHECK_EQUAL("i18n syntax error: $ without number at pos ->0<- in string ->$x test string<-", result);
77 }
78
79 BOOST_AUTO_TEST_CASE(KeepEscapedDollarSign)
80 {
81     vector<string> data;
82     data.push_back("dummy");
83
84     string result = i18n_get_string("$0 $$ test string", data);
85
86     BOOST_CHECK_EQUAL("dummy $ test string", result);
87 }
88
89 BOOST_AUTO_TEST_CASE(StdStringInput)
90 {
91     vector<string> data;
92     data.push_back("dummy");
93
94     const string input = "$0 test string";
95     string result = i18n_get_string(input, data);
96
97     BOOST_CHECK_EQUAL("dummy test string", result);
98 }
99
100 BOOST_AUTO_TEST_CASE(PluralString0)
101 {
102     // Zero elements get the plural message (in the default "plural" configuration)
103     BOOST_CHECK_EQUAL("xxx new messages", i18n_plural("One new message", "xxx new messages", 0));
104 }
105
106 BOOST_AUTO_TEST_CASE(PluralString1)
107 {
108     BOOST_CHECK_EQUAL("One new message", i18n_plural("One new message", "xxx new messages", 1));
109 }
110
111 BOOST_AUTO_TEST_CASE(PluralString2)
112 {
113     BOOST_CHECK_EQUAL("xxx new messages", i18n_plural("One new message", "xxx new messages", 2));
114 }
115
116 BOOST_AUTO_TEST_CASE(PluralStringWithParamter1)
117 {
118     vector<string> data;
119     data.push_back("1");
120
121     BOOST_CHECK_EQUAL("1 new message", i18n_get_string(i18n_plural("$0 new message", "$0 new messages", 1), data));
122 }
123
124 BOOST_AUTO_TEST_CASE(PluralStringWithParamter2)
125 {
126     vector<string> data;
127     data.push_back("500");
128
129     BOOST_CHECK_EQUAL("500 new messages", i18n_get_string(i18n_plural("$0 new message", "$0 new messages", 500), data));
130 }
131
132 BOOST_AUTO_TEST_CASE(Convenience0)
133 {
134     string result = i18n_get_string("$0!", "Energize");
135     BOOST_CHECK_EQUAL("Energize!", result);
136 }
137
138 BOOST_AUTO_TEST_CASE(Convenience1)
139 {
140     string result = i18n_get_string("Make $0 $1!", "it", "so");
141     BOOST_CHECK_EQUAL("Make it so!", result);
142 }
143
144 BOOST_AUTO_TEST_CASE(Convenience2)
145 {
146     string result = i18n_get_string("$0, $1, $2!", "Tea", "Earl Grey", "hot");
147     BOOST_CHECK_EQUAL("Tea, Earl Grey, hot!", result);
148 }
149
150 BOOST_AUTO_TEST_CASE(Noop)
151 {
152     BOOST_CHECK_EQUAL(i18n_noop("Test"), "Test");
153     BOOST_CHECK_EQUAL(i18n_noop(" Test "), " Test ");
154 }
155
156 BOOST_AUTO_TEST_SUITE_END()