1 | // |
2 | // Copyright (c) 2022 Alexander Grund |
3 | // |
4 | // Distributed under the Boost Software License, Version 1.0. |
5 | // https://www.boost.org/LICENSE_1_0.txt |
6 | |
7 | #include <boost/locale/formatting.hpp> |
8 | #include "boostLocale/test/tools.hpp" |
9 | #include "boostLocale/test/unit_test.hpp" |
10 | #include <locale> |
11 | #include <sstream> |
12 | #include <stdexcept> |
13 | |
14 | void test_member_methods() |
15 | { |
16 | std::stringstream ss; |
17 | { |
18 | // Test the default constructed instance |
19 | boost::locale::time_zone::global(new_tz: "Global TZ" ); |
20 | const auto& info = boost::locale::ios_info::get(ios&: ss); |
21 | TEST_EQ(info.display_flags(), 0u); |
22 | TEST_EQ(info.currency_flags(), 0u); |
23 | TEST_EQ(info.date_flags(), 0u); |
24 | TEST_EQ(info.time_flags(), 0u); |
25 | TEST_EQ(info.domain_id(), 0); |
26 | TEST_EQ(info.time_zone(), "Global TZ" ); |
27 | TEST_THROWS(info.date_time_pattern<char>(), std::bad_cast); |
28 | } |
29 | { |
30 | namespace flags = boost::locale::flags; |
31 | auto& info = boost::locale::ios_info::get(ios&: ss); |
32 | for(const auto flag : {flags::posix, |
33 | flags::number, |
34 | flags::currency, |
35 | flags::percent, |
36 | flags::date, |
37 | flags::time, |
38 | flags::datetime, |
39 | flags::strftime, |
40 | flags::spellout, |
41 | flags::ordinal}) |
42 | { |
43 | info.display_flags(flags: flag); |
44 | TEST_EQ(info.display_flags(), flag); |
45 | } |
46 | for(const auto flag : {flags::currency_default, flags::currency_iso, flags::currency_national}) { |
47 | info.currency_flags(flags: flag); |
48 | TEST_EQ(info.currency_flags(), flag); |
49 | } |
50 | for(const auto flag : |
51 | {flags::time_default, flags::time_short, flags::time_medium, flags::time_long, flags::time_full}) { |
52 | info.time_flags(flags: flag); |
53 | TEST_EQ(info.time_flags(), flag); |
54 | } |
55 | for(const auto flag : |
56 | {flags::date_default, flags::date_short, flags::date_medium, flags::date_long, flags::date_full}) { |
57 | info.date_flags(flags: flag); |
58 | TEST_EQ(info.date_flags(), flag); |
59 | } |
60 | // Should only change 1 setting |
61 | info.display_flags(flags: flags::number); |
62 | info.currency_flags(flags: flags::currency_iso); |
63 | info.time_flags(flags: flags::time_medium); |
64 | info.date_flags(flags: flags::date_short); |
65 | TEST_EQ(info.display_flags(), flags::number); |
66 | TEST_EQ(info.currency_flags(), flags::currency_iso); |
67 | TEST_EQ(info.time_flags(), flags::time_medium); |
68 | TEST_EQ(info.date_flags(), flags::date_short); |
69 | |
70 | info.display_flags(flags: flags::ordinal); |
71 | TEST_EQ(info.display_flags(), flags::ordinal); |
72 | TEST_EQ(info.currency_flags(), flags::currency_iso); |
73 | TEST_EQ(info.time_flags(), flags::time_medium); |
74 | TEST_EQ(info.date_flags(), flags::date_short); |
75 | |
76 | info.display_flags(flags: flags::number); |
77 | info.currency_flags(flags: flags::currency_national); |
78 | TEST_EQ(info.display_flags(), flags::number); |
79 | TEST_EQ(info.currency_flags(), flags::currency_national); |
80 | TEST_EQ(info.time_flags(), flags::time_medium); |
81 | TEST_EQ(info.date_flags(), flags::date_short); |
82 | |
83 | info.display_flags(flags: flags::number); |
84 | info.currency_flags(flags: flags::currency_iso); |
85 | info.time_flags(flags: flags::time_full); |
86 | TEST_EQ(info.display_flags(), flags::number); |
87 | TEST_EQ(info.currency_flags(), flags::currency_iso); |
88 | TEST_EQ(info.time_flags(), flags::time_full); |
89 | TEST_EQ(info.date_flags(), flags::date_short); |
90 | |
91 | info.display_flags(flags: flags::number); |
92 | info.currency_flags(flags: flags::currency_iso); |
93 | info.time_flags(flags: flags::time_medium); |
94 | info.date_flags(flags: flags::date_full); |
95 | TEST_EQ(info.display_flags(), flags::number); |
96 | TEST_EQ(info.currency_flags(), flags::currency_iso); |
97 | TEST_EQ(info.time_flags(), flags::time_medium); |
98 | TEST_EQ(info.date_flags(), flags::date_full); |
99 | |
100 | info.domain_id(42); |
101 | TEST_EQ(info.domain_id(), 42); |
102 | |
103 | info.time_zone("Test-TZ" ); |
104 | TEST_EQ(info.time_zone(), "Test-TZ" ); |
105 | |
106 | info.date_time_pattern(str: std::string("Pattern" )); |
107 | TEST_EQ(info.date_time_pattern<char>(), "Pattern" ); |
108 | } |
109 | } |
110 | |
111 | void test_manipulators() |
112 | { |
113 | std::stringstream ss; |
114 | const auto& info = boost::locale::ios_info::get(ios&: ss); |
115 | namespace as = boost::locale::as; |
116 | namespace flags = boost::locale::flags; |
117 | ss << as::number << as::currency_iso << as::time_short << as::date_short << as::time_zone(id: "FooTZ" ); |
118 | TEST_EQ(info.display_flags(), flags::number); |
119 | TEST_EQ(info.currency_flags(), flags::currency_iso); |
120 | TEST_EQ(info.time_flags(), flags::time_short); |
121 | TEST_EQ(info.date_flags(), flags::date_short); |
122 | TEST_EQ(info.time_zone(), "FooTZ" ); |
123 | TEST_THROWS(info.date_time_pattern<char>(), std::bad_cast); |
124 | |
125 | #define TEST_MODIFIER(modifier) \ |
126 | ss << as::modifier; \ |
127 | TEST_EQ(info.display_flags(), flags::modifier); |
128 | |
129 | TEST_MODIFIER(posix); |
130 | TEST_MODIFIER(currency); |
131 | TEST_MODIFIER(percent); |
132 | TEST_MODIFIER(date); |
133 | TEST_MODIFIER(time); |
134 | TEST_MODIFIER(strftime); |
135 | TEST_MODIFIER(spellout); |
136 | TEST_MODIFIER(ordinal); |
137 | #undef TEST_MODIFIER |
138 | |
139 | #define TEST_MODIFIER(modifier) \ |
140 | ss << as::modifier; \ |
141 | TEST_EQ(info.currency_flags(), flags::modifier); |
142 | |
143 | TEST_MODIFIER(currency_default); |
144 | TEST_MODIFIER(currency_iso); |
145 | TEST_MODIFIER(currency_national); |
146 | #undef TEST_MODIFIER |
147 | |
148 | #define TEST_MODIFIER(modifier) \ |
149 | ss << as::modifier; \ |
150 | TEST_EQ(info.currency_flags(), flags::modifier); |
151 | |
152 | TEST_MODIFIER(currency_default); |
153 | TEST_MODIFIER(currency_iso); |
154 | TEST_MODIFIER(currency_national); |
155 | #undef TEST_MODIFIER |
156 | |
157 | #define TEST_MODIFIER(modifier) \ |
158 | ss << as::modifier; \ |
159 | TEST_EQ(info.time_flags(), flags::modifier); |
160 | |
161 | TEST_MODIFIER(time_default); |
162 | TEST_MODIFIER(time_short); |
163 | TEST_MODIFIER(time_medium); |
164 | TEST_MODIFIER(time_long); |
165 | TEST_MODIFIER(time_full); |
166 | #undef TEST_MODIFIER |
167 | |
168 | #define TEST_MODIFIER(modifier) \ |
169 | ss << as::modifier; \ |
170 | TEST_EQ(info.date_flags(), flags::modifier); |
171 | |
172 | TEST_MODIFIER(date_default); |
173 | TEST_MODIFIER(date_short); |
174 | TEST_MODIFIER(date_medium); |
175 | TEST_MODIFIER(date_long); |
176 | TEST_MODIFIER(date_full); |
177 | #undef TEST_MODIFIER |
178 | |
179 | ss << as::number << as::ftime(format: "Test format" ); |
180 | TEST_EQ(info.display_flags(), flags::strftime); // Auto changed |
181 | TEST_EQ(info.date_time_pattern<char>(), "Test format" ); |
182 | ss >> as::number >> as::ftime(format: "Test format2" ); // Also as input |
183 | TEST_EQ(info.display_flags(), flags::strftime); // Auto changed |
184 | TEST_EQ(info.date_time_pattern<char>(), "Test format2" ); |
185 | |
186 | ss << as::gmt; |
187 | TEST_EQ(info.time_zone(), "GMT" ); |
188 | boost::locale::time_zone::global(new_tz: "Global TZ" ); |
189 | ss << as::local_time; |
190 | TEST_EQ(info.time_zone(), "Global TZ" ); |
191 | ss << as::time_zone(id: "Foo TZ" ); |
192 | TEST_EQ(info.time_zone(), "Foo TZ" ); |
193 | ss >> as::time_zone(id: "Bar TZ" ); // Also as input |
194 | TEST_EQ(info.time_zone(), "Bar TZ" ); |
195 | |
196 | std::wistringstream ss2; |
197 | ss2 >> as::ftime(format: L"My TZ" ); |
198 | const auto& info2 = boost::locale::ios_info::get(ios&: ss2); |
199 | TEST_EQ(info2.display_flags(), flags::strftime); |
200 | TEST_EQ(info2.date_time_pattern<wchar_t>(), L"My TZ" ); |
201 | } |
202 | |
203 | void test_any_string() |
204 | { |
205 | boost::locale::detail::any_string s; |
206 | TEST_THROWS(s.get<char>(), std::bad_cast); |
207 | TEST_THROWS(s.get<wchar_t>(), std::bad_cast); |
208 | |
209 | s.set<char>("Char Pattern" ); |
210 | TEST_EQ(s.get<char>(), "Char Pattern" ); |
211 | TEST_THROWS(s.get<wchar_t>(), std::bad_cast); |
212 | |
213 | s.set<wchar_t>(ascii_to<wchar_t>(str: "WChar Pattern" )); |
214 | TEST_EQ(s.get<wchar_t>(), ascii_to<wchar_t>("WChar Pattern" )); |
215 | TEST_THROWS(s.get<char>(), std::bad_cast); |
216 | |
217 | s.set<char16_t>(ascii_to<char16_t>(str: "Char16 Pattern" )); |
218 | TEST_EQ(s.get<char16_t>(), ascii_to<char16_t>("Char16 Pattern" )); |
219 | TEST_THROWS(s.get<char>(), std::bad_cast); |
220 | |
221 | s.set<char32_t>(ascii_to<char32_t>(str: "Char32 Pattern" )); |
222 | TEST_EQ(s.get<char32_t>(), ascii_to<char32_t>("Char32 Pattern" )); |
223 | TEST_THROWS(s.get<char16_t>(), std::bad_cast); |
224 | |
225 | #ifndef BOOST_LOCALE_NO_CXX20_STRING8 |
226 | s.set<char8_t>(ascii_to<char8_t>("Char8 Pattern" )); |
227 | TEST_EQ(s.get<char8_t>(), ascii_to<char8_t>("Char8 Pattern" )); |
228 | TEST_THROWS(s.get<char32_t>(), std::bad_cast); |
229 | #endif |
230 | |
231 | boost::locale::detail::any_string s1, s2, empty; |
232 | s1.set<char>("Char" ); |
233 | s2.set<wchar_t>(ascii_to<wchar_t>(str: "WChar" )); |
234 | // Copy ctor |
235 | boost::locale::detail::any_string s3(s1); |
236 | TEST_EQ(s3.get<char>(), "Char" ); |
237 | TEST_EQ(s1.get<char>(), "Char" ); |
238 | // Ensure deep copy |
239 | s3.set<char>("Foo" ); |
240 | TEST_EQ(s3.get<char>(), "Foo" ); |
241 | TEST_EQ(s1.get<char>(), "Char" ); |
242 | // Copy assign |
243 | s3 = s2; |
244 | TEST_EQ(s3.get<wchar_t>(), ascii_to<wchar_t>("WChar" )); |
245 | TEST_EQ(s2.get<wchar_t>(), ascii_to<wchar_t>("WChar" )); |
246 | // Move assign |
247 | s3 = std::move(s1); |
248 | TEST_EQ(s3.get<char>(), "Char" ); |
249 | // From empty |
250 | s3 = empty; |
251 | TEST_THROWS(s3.get<char>(), std::bad_cast); |
252 | } |
253 | |
254 | void test_main(int /*argc*/, char** /*argv*/) |
255 | { |
256 | test_any_string(); |
257 | test_member_methods(); |
258 | test_manipulators(); |
259 | } |
260 | |