/*============================================================================= Copyright (c) 2001-2015 Joel de Guzman Copyright (c) 2025 Nana Sakisaka 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 "test.hpp" #include #include #include #include #include #include #include #include #include #include TEST_CASE("attr") { using namespace std::string_literals; using namespace std::string_view_literals; using x4::attr; using x4::int_; { [[maybe_unused]] constexpr auto attr_p = attr(1); STATIC_CHECK(std::same_as, x4::attr_parser>); } { [[maybe_unused]] constexpr auto attr_p = attr(3.14); STATIC_CHECK(std::same_as, x4::attr_parser>); } { constexpr auto attr_p = attr("foo"); STATIC_REQUIRE(std::same_as, x4::attr_parser, std::basic_string_view>>); // Make sure `attr(std::string_view)` is parsable into std::string { constexpr auto result = [&](std::string_view expected_str) consteval { std::string str; std::string_view const input; auto it = input.begin(); auto const se = input.end(); bool const ok = attr_p.parse(it, se, unused, str); return std::make_pair(ok, str == expected_str); }("foo"); STATIC_REQUIRE(result.first == true); STATIC_CHECK(result.second == true); } { std::string str; std::string_view const input; auto it = input.begin(); auto const se = input.end(); REQUIRE(attr_p.parse(it, se, unused, str)); CHECK(str == "foo"); } } { [[maybe_unused]] /*constexpr*/ auto attr_p = attr("foo"s); STATIC_CHECK(std::same_as, x4::attr_parser>>); } { [[maybe_unused]] constexpr auto attr_p = attr("foo"sv); STATIC_CHECK(std::same_as, x4::attr_parser>>); } { [[maybe_unused]] constexpr auto attr_p = attr(U"foo"); STATIC_CHECK(std::same_as, x4::attr_parser, std::basic_string_view>>); } { [[maybe_unused]] /*constexpr*/ auto attr_p = attr(U"foo"s); STATIC_CHECK(std::same_as, x4::attr_parser>>); } { [[maybe_unused]] constexpr auto attr_p = attr(U"foo"sv); STATIC_CHECK(std::same_as, x4::attr_parser>>); } BOOST_SPIRIT_X4_ASSERT_CONSTEXPR_CTORS(attr(1)); BOOST_SPIRIT_X4_ASSERT_CONSTEXPR_CTORS(attr("asd")); { constexpr char s[] = "asd"; BOOST_SPIRIT_X4_ASSERT_CONSTEXPR_CTORS(attr(s)); } { int d = 0; REQUIRE(parse("", attr(1), d)); CHECK(d == 1); } { int d = 0; int d1 = 1; REQUIRE(parse("", attr(d1), d)); CHECK(d == 1); } { std::pair p; REQUIRE(parse("1", int_ >> attr(2), p)); CHECK(p.first == 1); CHECK(p.second == 2); } { char c = '\0'; REQUIRE(parse("", attr('a'), c)); CHECK(c == 'a'); } { std::string str; REQUIRE(parse("", attr("test"), str)); CHECK(str == "test"); } { std::string str; REQUIRE(parse("", attr(std::string("test")), str)); CHECK(str == "test"); } { std::vector array = {0, 1, 2}; std::vector vec; REQUIRE(parse("", attr(array), vec)); REQUIRE(vec.size() == 3); CHECK(vec[0] == 0); CHECK(vec[1] == 1); CHECK(vec[2] == 2); } { std::string s; REQUIRE(parse("s", "s" >> attr(std::string("123")), s)); CHECK(s == "123"); } { std::vector strs; REQUIRE(parse("", attr(std::string("123")) >> attr(std::string("456")), strs)); CHECK(strs == std::vector{"123", "456"}); } { std::string s; REQUIRE(parse("", attr(std::string("123")) >> attr(std::string("456")), s)); CHECK(s == "123456"); } { std::vector ints; REQUIRE(parse("", attr(std::vector{1, 2, 3}) >> attr(std::vector{4, 5, 6}), ints)); CHECK(ints == std::vector{1, 2, 3, 4, 5, 6}); } { std::vector ints; REQUIRE(parse("", (attr(std::vector{1, 2, 3}) >> attr(std::vector{4, 5, 6})) >> (attr(std::vector{7, 8, 9}) >> attr(std::vector{0, 1, 2})), ints )); CHECK(ints == std::vector{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2}); } }