• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  libs/io/test/quote_manip_test.cpp  -----------------------------------------------  //
2 
3 //  Copyright Beman Dawes 2010
4 
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7 
8 //  Library home page: http://www.boost.org/libs/io
9 
10 //  ----------------------------------------------------------------------------------  //
11 
12 #include <boost/io/detail/quoted_manip.hpp>
13 #include <boost/detail/lightweight_test.hpp>
14 #include <iostream>
15 #include <sstream>
16 
17 using boost::io::quoted;
18 using std::string;
19 using std::wstring;
20 
main()21 int main()
22 {
23 
24   std::wstringstream wss;
25 
26   string r; // test results
27 
28   const string s0("foo");
29   {
30     std::stringstream ss;
31     ss << quoted(s0);
32     ss >> r;
33     BOOST_TEST(r == "\"foo\"");
34   }
35   {
36     std::stringstream ss;
37     ss << quoted(s0);
38     ss >> quoted(r);
39     BOOST_TEST(r == "foo");
40   }
41 
42   const string s0s("foo bar");
43   {
44     std::stringstream ss;
45     ss << quoted(s0s);
46     ss >> r;
47     BOOST_TEST(r == "\"foo");
48   }
49   {
50     std::stringstream ss;
51     ss << quoted(s0s);
52     ss >> quoted(r);
53     BOOST_TEST(r == "foo bar");
54   }
55 
56   const string s1("foo\\bar, \" *");
57   {
58     std::stringstream ss;
59     ss << quoted(s1);
60     ss >> r;
61     BOOST_TEST(r == "\"foo\\\\bar,");
62   }
63   {
64     std::stringstream ss;
65     ss << quoted("foo\\bar, \" *");
66     ss >> r;
67     BOOST_TEST(r == "\"foo\\\\bar,");
68   }
69   {
70     std::stringstream ss;
71     ss << quoted(s1);
72     ss >> quoted(r);
73     BOOST_TEST(r == s1);
74   }
75   {
76     std::stringstream ss;
77     ss << quoted(s1.c_str());
78     ss >> quoted(r);
79     BOOST_TEST(r == s1);
80   }
81 
82   string s2("'Jack & Jill'");
83   {
84     std::stringstream ss;
85     ss << quoted(s2, '&', '\'');
86     ss >> quoted(r, '&', '\'');
87     BOOST_TEST(r == s2);
88   }
89 
90   wstring ws1(L"foo$bar, \" *");
91   wstring wr; // test results
92   {
93     std::wstringstream wss;
94     wss << quoted(ws1, L'$');
95     wss >> quoted(wr,  L'$');
96     BOOST_TEST(wr == ws1);
97   }
98 
99   const string s3("const string");
100   {
101     std::stringstream ss;
102     ss << quoted(s3);
103     ss >> quoted(r);
104     BOOST_TEST(r == s3);
105   }
106   {
107     //  missing end delimiter test
108     std::stringstream ss;
109     ss << "\"abc";      // load ss with faulty quoting
110     ss >> quoted(r);    // this loops if istream error/eof not detected
111     BOOST_TEST(r == "abc");
112   }
113   {
114     //  no initial delmiter test
115     std::stringstream ss;
116     ss << "abc";
117     ss >> quoted(r);
118     BOOST_TEST(r == "abc");
119   }
120   {
121     //  no initial delmiter, space in ss
122     std::stringstream ss;
123     ss << "abc def";
124     ss >> quoted(r);
125     BOOST_TEST(r == "abc");
126   }
127 
128   // these should fail to compile because the arguments are const:
129   //   ss >> quoted(s1);
130   //   ss >> quoted("foo");
131 
132   return boost::report_errors();
133 }
134