$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r58107 - in trunk/libs/spirit: doc test/karma test/qi
From: hartmut.kaiser_at_[hidden]
Date: 2009-12-02 22:18:42
Author: hkaiser
Date: 2009-12-02 22:18:41 EST (Wed, 02 Dec 2009)
New Revision: 58107
URL: http://svn.boost.org/trac/boost/changeset/58107
Log:
Spirit: added tests for new API functions
Text files modified: 
   trunk/libs/spirit/doc/what_s_new.qbk          |     7 ++++                                    
   trunk/libs/spirit/test/karma/auto.cpp         |    57 ++++++++++++++++++++++++++++++++++++++++
   trunk/libs/spirit/test/karma/format_manip.cpp |    32 +++++++++++++++++++--                   
   trunk/libs/spirit/test/qi/auto.cpp            |    49 ++++++++++++++++++++++++++++++++++      
   trunk/libs/spirit/test/qi/match_manip.cpp     |    40 +++++++++++++++++++++++++--             
   5 files changed, 176 insertions(+), 9 deletions(-)
Modified: trunk/libs/spirit/doc/what_s_new.qbk
==============================================================================
--- trunk/libs/spirit/doc/what_s_new.qbk	(original)
+++ trunk/libs/spirit/doc/what_s_new.qbk	2009-12-02 22:18:41 EST (Wed, 02 Dec 2009)
@@ -12,7 +12,12 @@
 
 [heading What's changed in __qi__ and __karma__ from V2.1 (Boost V1.41.0) to 2.2 (Boost V1.42.0)]
 
-* add here what's new...
+* Fixed karma::alternatives to work with embedded containers of hold_any (i.e.
+  constructs like `*stream | "empty"` (that fixes the Karma example 
+  basic_facilities.cpp).
+* Added `auto_` component and API functions `qi::create_parser` and 
+  `karma::create_generator`.
+* Added `auto_` based overloads for all API functions taking no attributes.
 
 [heading Spirit V2.1] 
 
Modified: trunk/libs/spirit/test/karma/auto.cpp
==============================================================================
--- trunk/libs/spirit/test/karma/auto.cpp	(original)
+++ trunk/libs/spirit/test/karma/auto.cpp	2009-12-02 22:18:41 EST (Wed, 02 Dec 2009)
@@ -36,6 +36,19 @@
     return result && generated == expected;
 }
 
+template <typename Char, typename T>
+bool test_create_generator_auto(Char const *expected, T const& t)
+{
+    std::basic_string<Char> generated;
+    std::back_insert_iterator<std::basic_string<Char> > sink(generated);
+
+    BOOST_TEST((traits::meta_create_exists<karma::domain, T>::value));
+    bool result = karma::generate(sink, t);
+
+    spirit_test::print_if_failed("test_create_generator (auto)", result, generated, expected);
+    return result && generated == expected;
+}
+
 template <typename Char, typename Attribute>
 bool test_rule(Char const *expected, Attribute const& attr)
 {
@@ -112,6 +125,50 @@
     }
 
     {
+        // test primitive types
+//         BOOST_TEST(test_create_generator_auto("true", true));
+//         BOOST_TEST(test_create_generator_auto("1", 1));
+//         BOOST_TEST(test_create_generator_auto("1.1", 1.1));
+//         BOOST_TEST(test_create_generator_auto("test", std::string("test")));
+//         BOOST_TEST(test_create_generator_auto("a", 'a'));
+//         BOOST_TEST(test_create_generator_auto(L"a", L'a'));
+
+        // test containers
+        std::vector<int> v;
+        v.push_back(0);
+        v.push_back(1);
+        v.push_back(2);
+        BOOST_TEST(test_create_generator_auto("012", v));
+
+        std::list<int> l;
+        l.push_back(0);
+        l.push_back(1);
+        l.push_back(2);
+        BOOST_TEST(test_create_generator_auto("012", l));
+
+        // test optional
+        boost::optional<int> o;
+        BOOST_TEST(test_create_generator_auto("", o));
+        o = 1;
+        BOOST_TEST(test_create_generator_auto("1", o));
+
+        // test alternative
+        boost::variant<int, double, float, std::string> vv;
+        vv = 1;
+        BOOST_TEST(test_create_generator_auto("1", vv));
+        vv = 1.0;
+        BOOST_TEST(test_create_generator_auto("1.0", vv));
+        vv = 1.0f;
+        BOOST_TEST(test_create_generator_auto("1.0", vv));
+        vv = "some string";
+        BOOST_TEST(test_create_generator_auto("some string", vv));
+
+        // test fusion sequence
+        std::pair<int, double> p (1, 2.0);
+        BOOST_TEST(test_create_generator_auto("12.0", p));
+    }
+
+    {
         using karma::auto_;
         using karma::upper;
         using spirit_test::test;
Modified: trunk/libs/spirit/test/karma/format_manip.cpp
==============================================================================
--- trunk/libs/spirit/test/karma/format_manip.cpp	(original)
+++ trunk/libs/spirit/test/karma/format_manip.cpp	2009-12-02 22:18:41 EST (Wed, 02 Dec 2009)
@@ -3,9 +3,11 @@
 //  Distributed under the Boost Software License, Version 1.0. (See accompanying 
 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 
+#include <boost/mpl/print.hpp>
 #include <boost/config/warning_disable.hpp>
 #include <boost/spirit/include/karma.hpp>
 #include <boost/spirit/include/karma_format.hpp>
+#include <boost/spirit/include/karma_format_auto.hpp>
 #include <boost/spirit/include/karma_stream.hpp>
 #include <boost/spirit/include/phoenix_core.hpp>
 #include <boost/spirit/include/phoenix_operator.hpp>
@@ -34,10 +36,11 @@
     return ostrm.good() && ostrm.str() == expected;
 }
 
-template <typename Char, typename Expr, typename Copy, typename Delimiter
-  , typename Attribute>
+template <typename Char, typename Expr, typename CopyExpr, typename CopyAttr
+  , typename Delimiter, typename Attribute>
 bool test(Char const *expected, 
-    boost::spirit::karma::detail::format_manip<Expr, Copy, Delimiter, Attribute> const& fm)
+    boost::spirit::karma::detail::format_manip<
+        Expr, CopyExpr, CopyAttr, Delimiter, Attribute> const& fm)
 {
     std::ostringstream ostrm;
     ostrm << fm;
@@ -94,6 +97,13 @@
         BOOST_TEST(test( "a b ", 
             karma::format_delimited(char_ << char_, space, t) 
         ));
+
+        BOOST_TEST(test( "ab", 
+            karma::format(t) 
+        ));
+        BOOST_TEST(test( "a b ", 
+            karma::format_delimited(t, space) 
+        ));
     }
 
     {
@@ -131,7 +141,7 @@
             karma::format_delimited(char_ << int_, space, t) 
         ));
     }
-    
+
     using namespace boost::assign;
 
     {
@@ -149,6 +159,13 @@
             karma::format_delimited(*char_, space, v)
         ));
 
+        BOOST_TEST(test( "abc", 
+            karma::format(v)
+        ));
+        BOOST_TEST(test( "a b c ", 
+            karma::format_delimited(v, space)
+        ));
+
         // output a comma separated list of vector elements
         BOOST_TEST(test( "a, b, c", 
             (char_ % lit(", "))[_0 = fusion::make_single_view(v)] 
@@ -185,6 +202,13 @@
         BOOST_TEST(test( "a b c ", 
             karma::format_delimited(*char_, space, l)
         ));
+
+        BOOST_TEST(test( "abc", 
+            karma::format(l)
+        ));
+        BOOST_TEST(test( "a b c ", 
+            karma::format_delimited(l, space)
+        ));
     }
 
     return boost::report_errors();
Modified: trunk/libs/spirit/test/qi/auto.cpp
==============================================================================
--- trunk/libs/spirit/test/qi/auto.cpp	(original)
+++ trunk/libs/spirit/test/qi/auto.cpp	2009-12-02 22:18:41 EST (Wed, 02 Dec 2009)
@@ -35,6 +35,17 @@
     return qi::phrase_parse(in, last, qi::create_parser<T>(), qi::space, t);
 }
 
+template <typename Char, typename T>
+bool test_create_parser_auto(Char const *in, T& t)
+{
+    Char const* last = in;
+    while (*last)
+        last++;
+
+    BOOST_TEST((traits::meta_create_exists<qi::domain, T>::value));
+    return qi::phrase_parse(in, last, t, qi::space);
+}
+
 template <typename Char, typename Attribute>
 bool test_rule(Char const* in, Attribute const& expected)
 {
@@ -108,6 +119,44 @@
     }
 
     {
+        // test primitive types
+        bool b = false;
+        BOOST_TEST(test_create_parser_auto("true", b) && b == true);
+        int i = 0;
+        BOOST_TEST(test_create_parser_auto("1", i) && i == 1);
+        double d = 0;
+        BOOST_TEST(test_create_parser_auto("1.1", d) && d == 1.1);
+
+        // test containers
+        std::vector<int> v;
+        BOOST_TEST(test_create_parser_auto("0 1 2", v) && v.size() == 3 && 
+            v[0] == 0 && v[1] == 1 && v[2] == 2);
+
+        std::list<int> l;
+        BOOST_TEST(test_create_parser_auto("0 1 2", l) && l.size() == 3 &&
+            *l.begin() == 0 && *(++l.begin()) == 1 && *(++ ++l.begin()) == 2);
+
+        // test optional
+        boost::optional<int> o;
+        BOOST_TEST(test_create_parser_auto("", o) && !o);
+        BOOST_TEST(test_create_parser_auto("1", o) && !!o && boost::get<int>(o) == 1);
+
+        // test alternative
+        boost::variant<double, bool, std::vector<char> > vv;
+        BOOST_TEST(test_create_parser_auto("true", vv) && vv.which() == 1 &&
+            boost::get<bool>(vv) == true);
+        BOOST_TEST(test_create_parser_auto("1.0", vv) && vv.which() == 0 &&
+            boost::get<double>(vv) == 1.0);
+        BOOST_TEST(test_create_parser_auto("some_string", vv) && vv.which() == 2 &&
+            boost::equals(boost::get<std::vector<char> >(vv), "some_string"));
+
+        // test fusion sequence
+        std::pair<int, double> p;
+        BOOST_TEST(test_create_parser_auto("1 2.0", p) && 
+            p.first == 1 && p.second == 2.0);
+    }
+
+    {
         using qi::auto_;
         using qi::no_case;
         using spirit_test::test_attr;
Modified: trunk/libs/spirit/test/qi/match_manip.cpp
==============================================================================
--- trunk/libs/spirit/test/qi/match_manip.cpp	(original)
+++ trunk/libs/spirit/test/qi/match_manip.cpp	2009-12-02 22:18:41 EST (Wed, 02 Dec 2009)
@@ -15,6 +15,7 @@
 #include <boost/spirit/include/qi_operator.hpp>
 #include <boost/spirit/include/qi_stream.hpp>
 #include <boost/spirit/include/qi_match.hpp>
+#include <boost/spirit/include/qi_match_auto.hpp>
 #include <boost/spirit/include/phoenix_core.hpp>
 #include <boost/spirit/include/phoenix_operator.hpp>
 #include <boost/spirit/include/phoenix_statement.hpp>
@@ -38,10 +39,11 @@
     return istrm.good() || istrm.eof();
 }
 
-template <typename Char, typename Expr, typename Copy, typename Skipper
-  , typename Attribute>
+template <typename Char, typename Expr, typename CopyExpr, typename CopyAttr
+  , typename Skipper, typename Attribute>
 bool test(Char const *toparse,
-    boost::spirit::qi::detail::match_manip<Expr, Copy, Skipper, Attribute> const& mm)
+    boost::spirit::qi::detail::match_manip<
+        Expr, CopyExpr, CopyAttr, Skipper, Attribute> const& mm)
 {
     std::istringstream istrm(toparse);
     istrm >> mm;
@@ -222,6 +224,16 @@
         BOOST_TEST(test( " a b c",
             phrase_match(char_ >> char_ >> char_, space, t)
         ) && fusion::at_c<0>(t) == 'a' && fusion::at_c<1>(t) == 'b' && fusion::at_c<2>(t) == 'c');
+
+        t = fusion::vector<char, char, char>();
+        BOOST_TEST(test( "abc",
+            match(t)
+        ) && fusion::at_c<0>(t) == 'a' && fusion::at_c<1>(t) == 'b' && fusion::at_c<2>(t) == 'c');
+
+        t = fusion::vector<char, char, char>();
+        BOOST_TEST(test( " a b c",
+            phrase_match(t, space)
+        ) && fusion::at_c<0>(t) == 'a' && fusion::at_c<1>(t) == 'b' && fusion::at_c<2>(t) == 'c');
     }
 
     {
@@ -250,7 +262,7 @@
     }
 
     {
-        // output all elements of a vector
+        // parse elements of a vector
         std::vector<char> v;
         BOOST_TEST(test( "abc",
             (*char_)[phx::ref(v) = _1]
@@ -266,6 +278,16 @@
             phrase_match(*char_, space, v)
         ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
 
+        v.clear();
+        BOOST_TEST(test( "abc",
+            match(v)
+        ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
+
+        v.clear();
+        BOOST_TEST(test( " a b c",
+            phrase_match(v, space)
+        ) && 3 == v.size() && v[0] == 'a' && v[1] == 'b' && v[2] == 'c');
+
         // parse a comma separated list of vector elements
         v.clear();
         BOOST_TEST(test( "a,b,c",
@@ -287,6 +309,16 @@
         BOOST_TEST(test( " a b c",
             phrase_match(*char_, space, l)
         ) && 3 == l.size() && is_list_ok(l));
+
+        l.clear();
+        BOOST_TEST(test( "abc",
+            match(l)
+        ) && 3 == l.size() && is_list_ok(l));
+
+        l.clear();
+        BOOST_TEST(test( " a b c",
+            phrase_match(l, space)
+        ) && 3 == l.size() && is_list_ok(l));
     }
 
     return boost::report_errors();