optional.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright Abel Sinkovics (abel@sinkovics.hu) 2015.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/metaparse/optional.hpp>
  6. #include <boost/metaparse/string.hpp>
  7. #include <boost/metaparse/lit_c.hpp>
  8. #include <boost/metaparse/get_result.hpp>
  9. #include <boost/metaparse/start.hpp>
  10. #include <boost/mpl/assert.hpp>
  11. #include <boost/mpl/equal_to.hpp>
  12. #include <boost/mpl/char.hpp>
  13. #include <boost/type_traits/is_same.hpp>
  14. #include "test_case.hpp"
  15. BOOST_METAPARSE_TEST_CASE(optional)
  16. {
  17. using boost::metaparse::optional;
  18. using boost::metaparse::lit_c;
  19. using boost::metaparse::string;
  20. using boost::metaparse::get_result;
  21. using boost::metaparse::start;
  22. using boost::mpl::equal_to;
  23. using boost::is_same;
  24. typedef optional<lit_c<'x'>, double> optional_x;
  25. // test_optional_parser_succeeds
  26. BOOST_MPL_ASSERT((
  27. equal_to<
  28. boost::mpl::char_<'x'>,
  29. get_result<optional_x::apply<string<'x'>, start> >::type
  30. >
  31. ));
  32. // test_optional_parser_fails
  33. BOOST_MPL_ASSERT((
  34. is_same<double, get_result<optional_x::apply<string<'y'>, start> >::type>
  35. ));
  36. // test_optional_parser_default_value
  37. BOOST_MPL_ASSERT((
  38. is_same<
  39. void,
  40. get_result<optional<lit_c<'x'> >::apply<string<'y'>, start> >::type
  41. >
  42. ));
  43. }