]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/program_options/test/unicode_test.cpp
1 // Copyright Vladimir Prus 2002-2004.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE_1_0.txt
4 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7 #include <boost/program_options/variables_map.hpp>
8 #include <boost/program_options/options_description.hpp>
9 #include <boost/program_options/parsers.hpp>
10 #include <boost/program_options/detail/utf8_codecvt_facet.hpp>
11 using namespace boost::program_options
;
12 // We'll use po::value everywhere to workaround vc6 bug.
13 namespace po
= boost::program_options
;
15 #include <boost/function.hpp>
16 using namespace boost
;
21 #include "minitest.hpp"
23 // Test that unicode input is forwarded to unicode option without
25 void test_unicode_to_unicode()
27 options_description desc
;
30 ("foo", po::wvalue
<wstring
>(), "unicode option")
34 args
.push_back(L
"--foo=\x044F");
37 basic_parsed_options
<wchar_t> parsed
=
38 wcommand_line_parser(args
).options(desc
).run();
41 BOOST_CHECK(vm
["foo"].as
<wstring
>() == L
"\x044F");
42 BOOST_CHECK(parsed
.options
[0].original_tokens
.size() == 1);
43 BOOST_CHECK(parsed
.options
[0].original_tokens
[0] == L
"--foo=\x044F");
46 // Test that unicode input is property converted into
47 // local 8 bit string. To test this, make local 8 bit encoding
49 void test_unicode_to_native()
51 std::codecvt
<wchar_t, char, mbstate_t>* facet
=
52 new boost::program_options::detail::utf8_codecvt_facet
;
53 locale::global(locale(locale(), facet
));
55 options_description desc
;
58 ("foo", po::value
<string
>(), "unicode option")
62 args
.push_back(L
"--foo=\x044F");
65 store(wcommand_line_parser(args
).options(desc
).run(), vm
);
67 BOOST_CHECK(vm
["foo"].as
<string
>() == "\xD1\x8F");
70 void test_native_to_unicode()
72 std::codecvt
<wchar_t, char, mbstate_t>* facet
=
73 new boost::program_options::detail::utf8_codecvt_facet
;
74 locale::global(locale(locale(), facet
));
76 options_description desc
;
79 ("foo", po::wvalue
<wstring
>(), "unicode option")
83 args
.push_back("--foo=\xD1\x8F");
86 store(command_line_parser(args
).options(desc
).run(), vm
);
88 BOOST_CHECK(vm
["foo"].as
<wstring
>() == L
"\x044F");
91 vector
<wstring
> sv(const wchar_t* array
[], unsigned size
)
94 for (unsigned i
= 0; i
< size
; ++i
)
95 r
.push_back(array
[i
]);
99 void check_value(const woption
& option
, const char* name
, const wchar_t* value
)
101 BOOST_CHECK(option
.string_key
== name
);
102 BOOST_REQUIRE(option
.value
.size() == 1);
103 BOOST_CHECK(option
.value
.front() == value
);
106 void test_command_line()
108 options_description desc
;
110 ("foo,f", new untyped_value(), "")
111 // Explicit qualification is a workaround for vc6
112 ("bar,b", po::value
<std::string
>(), "")
113 ("baz", new untyped_value())
114 ("plug*", new untyped_value())
117 const wchar_t* cmdline4_
[] = { L
"--foo=1\u0FF52", L
"-f4", L
"--bar=11",
118 L
"-b4", L
"--plug3=10"};
119 vector
<wstring
> cmdline4
= sv(cmdline4_
,
120 sizeof(cmdline4_
)/sizeof(cmdline4_
[0]));
122 wcommand_line_parser(cmdline4
).options(desc
).run().options
;
124 BOOST_REQUIRE(a4
.size() == 5);
126 check_value(a4
[0], "foo", L
"1\u0FF52");
127 check_value(a4
[1], "foo", L
"4");
128 check_value(a4
[2], "bar", L
"11");
131 // Since we've already tested conversion between parser encoding and
132 // option encoding, all we need to check for config file is that
133 // when reading wistream, it generates proper UTF8 data.
134 void test_config_file()
136 std::codecvt
<wchar_t, char, mbstate_t>* facet
=
137 new boost::program_options::detail::utf8_codecvt_facet
;
138 locale::global(locale(locale(), facet
));
140 options_description desc
;
143 ("foo", po::value
<string
>(), "unicode option")
146 std::wstringstream
stream(L
"foo = \x044F");
149 store(parse_config_file(stream
, desc
), vm
);
151 BOOST_CHECK(vm
["foo"].as
<string
>() == "\xD1\x8F");
154 int main(int, char* [])
156 test_unicode_to_unicode();
157 test_unicode_to_native();
158 test_native_to_unicode();