lexical_cast_filesystem_test.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Unit test for boost::lexical_cast.
  2. //
  3. // See http://www.boost.org for most recent version, including documentation.
  4. //
  5. // Copyright Antony Polukhin, 2013-2019.
  6. //
  7. // Distributed under the Boost
  8. // Software License, Version 1.0. (See accompanying file
  9. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt).
  10. //
  11. // Test lexical_cast usage with long filesystem::path. Bug 7704.
  12. #include <boost/config.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <boost/lexical_cast.hpp>
  15. #include <boost/filesystem/path.hpp>
  16. using namespace boost;
  17. void test_filesystem();
  18. unit_test::test_suite *init_unit_test_suite(int, char *[])
  19. {
  20. unit_test::test_suite *suite =
  21. BOOST_TEST_SUITE("lexical_cast+filesystem unit test");
  22. suite->add(BOOST_TEST_CASE(&test_filesystem));
  23. return suite;
  24. }
  25. void test_filesystem()
  26. {
  27. boost::filesystem::path p;
  28. std::string s1 = "aaaaaaaaaaaaaaaaaaaaaaa";
  29. p = boost::lexical_cast<boost::filesystem::path>(s1);
  30. BOOST_CHECK(!p.empty());
  31. BOOST_CHECK_EQUAL(p, s1);
  32. p.clear();
  33. const char ab[] = "aaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
  34. p = boost::lexical_cast<boost::filesystem::path>(ab);
  35. BOOST_CHECK(!p.empty());
  36. BOOST_CHECK_EQUAL(p, ab);
  37. // Tests for
  38. // https://github.com/boostorg/lexical_cast/issues/25
  39. const char quoted_path[] = "\"/home/my user\"";
  40. p = boost::lexical_cast<boost::filesystem::path>(quoted_path);
  41. BOOST_CHECK(!p.empty());
  42. const char unquoted_path[] = "/home/my user";
  43. BOOST_CHECK_EQUAL(p, boost::filesystem::path(unquoted_path));
  44. // Converting back to std::string gives the initial string
  45. BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(p), quoted_path);
  46. try {
  47. // Without quotes the path will have only `/home/my` in it.
  48. // `user` remains in the stream, so an exception must be thrown.
  49. p = boost::lexical_cast<boost::filesystem::path>(unquoted_path);
  50. BOOST_CHECK(false);
  51. } catch (const boost::bad_lexical_cast& ) {
  52. // Exception is expected
  53. }
  54. }