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) |
---|
5 | |
---|
6 | |
---|
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; |
---|
14 | |
---|
15 | #include <boost/function.hpp> |
---|
16 | using namespace boost; |
---|
17 | |
---|
18 | #define BOOST_INCLUDE_MAIN // for testing, include rather than link |
---|
19 | #include <boost/test/test_tools.hpp> |
---|
20 | |
---|
21 | #include <sstream> |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | // Test that unicode input is forwarded to unicode option without |
---|
25 | // problems. |
---|
26 | void test_unicode_to_unicode() |
---|
27 | { |
---|
28 | options_description desc; |
---|
29 | |
---|
30 | desc.add_options() |
---|
31 | ("foo", po::wvalue<wstring>(), "unicode option") |
---|
32 | ; |
---|
33 | |
---|
34 | vector<wstring> args; |
---|
35 | args.push_back(L"--foo=\x044F"); |
---|
36 | |
---|
37 | variables_map vm; |
---|
38 | store(wcommand_line_parser(args).options(desc).run(), vm); |
---|
39 | |
---|
40 | BOOST_CHECK(vm["foo"].as<wstring>() == L"\x044F"); |
---|
41 | } |
---|
42 | |
---|
43 | // Test that unicode input is property converted into |
---|
44 | // local 8 bit string. To test this, make local 8 bit encoding |
---|
45 | // be utf8. |
---|
46 | void test_unicode_to_native() |
---|
47 | { |
---|
48 | std::codecvt<wchar_t, char, mbstate_t>* facet = |
---|
49 | new boost::program_options::detail::utf8_codecvt_facet; |
---|
50 | locale::global(locale(locale(), facet)); |
---|
51 | |
---|
52 | options_description desc; |
---|
53 | |
---|
54 | desc.add_options() |
---|
55 | ("foo", po::value<string>(), "unicode option") |
---|
56 | ; |
---|
57 | |
---|
58 | vector<wstring> args; |
---|
59 | args.push_back(L"--foo=\x044F"); |
---|
60 | |
---|
61 | variables_map vm; |
---|
62 | store(wcommand_line_parser(args).options(desc).run(), vm); |
---|
63 | |
---|
64 | BOOST_CHECK(vm["foo"].as<string>() == "\xD1\x8F"); |
---|
65 | } |
---|
66 | |
---|
67 | void test_native_to_unicode() |
---|
68 | { |
---|
69 | std::codecvt<wchar_t, char, mbstate_t>* facet = |
---|
70 | new boost::program_options::detail::utf8_codecvt_facet; |
---|
71 | locale::global(locale(locale(), facet)); |
---|
72 | |
---|
73 | options_description desc; |
---|
74 | |
---|
75 | desc.add_options() |
---|
76 | ("foo", po::wvalue<wstring>(), "unicode option") |
---|
77 | ; |
---|
78 | |
---|
79 | vector<string> args; |
---|
80 | args.push_back("--foo=\xD1\x8F"); |
---|
81 | |
---|
82 | variables_map vm; |
---|
83 | store(command_line_parser(args).options(desc).run(), vm); |
---|
84 | |
---|
85 | BOOST_CHECK(vm["foo"].as<wstring>() == L"\x044F"); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | vector<wstring> sv(wchar_t* array[], unsigned size) |
---|
90 | { |
---|
91 | vector<wstring> r; |
---|
92 | for (unsigned i = 0; i < size; ++i) |
---|
93 | r.push_back(array[i]); |
---|
94 | return r; |
---|
95 | } |
---|
96 | |
---|
97 | void check_value(const woption& option, const char* name, const wchar_t* value) |
---|
98 | { |
---|
99 | BOOST_CHECK(option.string_key == name); |
---|
100 | BOOST_REQUIRE(option.value.size() == 1); |
---|
101 | BOOST_CHECK(option.value.front() == value); |
---|
102 | } |
---|
103 | |
---|
104 | void test_command_line() |
---|
105 | { |
---|
106 | options_description desc; |
---|
107 | desc.add_options() |
---|
108 | ("foo,f", new untyped_value(), "") |
---|
109 | // Explicit qualification is a workaround for vc6 |
---|
110 | ("bar,b", po::value<std::string>(), "") |
---|
111 | ("baz", new untyped_value()) |
---|
112 | ("plug*", new untyped_value()) |
---|
113 | ; |
---|
114 | |
---|
115 | wchar_t* cmdline4_[] = { L"--foo=1\u0FF52", L"-f4", L"--bar=11", |
---|
116 | L"-b4", L"--plug3=10"}; |
---|
117 | vector<wstring> cmdline4 = sv(cmdline4_, |
---|
118 | sizeof(cmdline4_)/sizeof(cmdline4_[0])); |
---|
119 | vector<woption> a4 = |
---|
120 | wcommand_line_parser(cmdline4).options(desc).run().options; |
---|
121 | |
---|
122 | BOOST_REQUIRE(a4.size() == 5); |
---|
123 | |
---|
124 | check_value(a4[0], "foo", L"1\u0FF52"); |
---|
125 | check_value(a4[1], "foo", L"4"); |
---|
126 | check_value(a4[2], "bar", L"11"); |
---|
127 | } |
---|
128 | |
---|
129 | // Since we've already tested conversion between parser encoding and |
---|
130 | // option encoding, all we need to check for config file is that |
---|
131 | // when reading wistream, it generates proper UTF8 data. |
---|
132 | void test_config_file() |
---|
133 | { |
---|
134 | std::codecvt<wchar_t, char, mbstate_t>* facet = |
---|
135 | new boost::program_options::detail::utf8_codecvt_facet; |
---|
136 | locale::global(locale(locale(), facet)); |
---|
137 | |
---|
138 | options_description desc; |
---|
139 | |
---|
140 | desc.add_options() |
---|
141 | ("foo", po::value<string>(), "unicode option") |
---|
142 | ; |
---|
143 | |
---|
144 | std::wstringstream stream(L"foo = \x044F"); |
---|
145 | |
---|
146 | variables_map vm; |
---|
147 | store(parse_config_file(stream, desc), vm); |
---|
148 | |
---|
149 | BOOST_CHECK(vm["foo"].as<string>() == "\xD1\x8F"); |
---|
150 | } |
---|
151 | |
---|
152 | int test_main(int, char* []) |
---|
153 | { |
---|
154 | test_unicode_to_unicode(); |
---|
155 | test_unicode_to_native(); |
---|
156 | test_native_to_unicode(); |
---|
157 | test_command_line(); |
---|
158 | test_config_file(); |
---|
159 | return 0; |
---|
160 | } |
---|
161 | |
---|