| 1 | /*============================================================================= |
| 2 | Copyright (c) 2001-2015 Joel de Guzman |
| 3 | |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | =============================================================================*/ |
| 7 | #include <boost/spirit/home/x3.hpp> |
| 8 | |
| 9 | #include <boost/fusion/include/std_pair.hpp> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "test.hpp" |
| 13 | |
| 14 | int main() |
| 15 | { |
| 16 | using spirit_test::test_attr; |
| 17 | using boost::spirit::x3::attr; |
| 18 | using boost::spirit::x3::int_; |
| 19 | |
| 20 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(attr); |
| 21 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(attr(1)); |
| 22 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(attr("asd" )); |
| 23 | { |
| 24 | constexpr char s[] = "asd" ; |
| 25 | BOOST_SPIRIT_ASSERT_CONSTEXPR_CTORS(attr(s)); |
| 26 | } |
| 27 | |
| 28 | { |
| 29 | int d = 0; |
| 30 | BOOST_TEST(test_attr("" , attr(1), d) && d == 1); |
| 31 | |
| 32 | int d1 = 1; |
| 33 | BOOST_TEST(test_attr("" , attr(d1), d) && d == 1); |
| 34 | |
| 35 | std::pair<int, int> p; |
| 36 | BOOST_TEST(test_attr("1" , int_ >> attr(1), p) && |
| 37 | p.first == 1 && p.second == 1); |
| 38 | |
| 39 | char c = '\0'; |
| 40 | BOOST_TEST(test_attr("" , attr('a'), c) && c == 'a'); |
| 41 | |
| 42 | // $$$ Needs some special is_convertible support, or |
| 43 | // str ends up with an explicit null-terminator... $$$ |
| 44 | std::string str; |
| 45 | //~ BOOST_TEST(test_attr("", attr("test"), str) && str == "test"); |
| 46 | |
| 47 | str.clear(); |
| 48 | BOOST_TEST(test_attr("" , attr(std::string("test" )), str)) |
| 49 | && BOOST_TEST_EQ(str, "test" ); |
| 50 | |
| 51 | int array[] = {0, 1, 2}; |
| 52 | std::vector<int> vec; |
| 53 | BOOST_TEST(test_attr("" , attr(array), vec) && vec.size() == 3 && |
| 54 | vec[0] == 0 && vec[1] == 1 && vec[2] == 2); |
| 55 | } |
| 56 | |
| 57 | { |
| 58 | std::string s; |
| 59 | BOOST_TEST(test_attr("s" , "s" >> attr(std::string("123" )), s) && |
| 60 | s == "123" ); |
| 61 | |
| 62 | s.clear(); |
| 63 | BOOST_TEST(test_attr("" , attr(std::string("123" )) >> attr(std::string("456" )), s)) |
| 64 | && BOOST_TEST_EQ(s, "123456" ); |
| 65 | } |
| 66 | |
| 67 | return boost::report_errors(); |
| 68 | } |
| 69 | |