You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
2.3 KiB

2 months ago
  1. #include "yaml-cpp/yaml.h" // IWYU pragma: keep
  2. #include "gtest/gtest.h"
  3. #define EXPECT_THROW_EXCEPTION(exception_type, statement, message) \
  4. ASSERT_THROW(statement, exception_type); \
  5. try { \
  6. statement; \
  7. } catch (const exception_type& e) { \
  8. EXPECT_EQ(e.msg, message); \
  9. }
  10. namespace YAML {
  11. namespace {
  12. TEST(ErrorMessageTest, BadSubscriptErrorMessage) {
  13. const char *example_yaml = "first:\n"
  14. " second: 1\n"
  15. " third: 2\n";
  16. Node doc = Load(example_yaml);
  17. // Test that printable key is part of error message
  18. EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"]["fourth"],
  19. "operator[] call on a scalar (key: \"fourth\")");
  20. EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][37],
  21. "operator[] call on a scalar (key: \"37\")");
  22. // Non-printable key is not included in error message
  23. EXPECT_THROW_EXCEPTION(YAML::BadSubscript,
  24. doc["first"]["second"][std::vector<int>()],
  25. "operator[] call on a scalar");
  26. EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][Node()],
  27. "operator[] call on a scalar");
  28. }
  29. TEST(ErrorMessageTest, Ex9_1_InvalidNodeErrorMessage) {
  30. const char *example_yaml = "first:\n"
  31. " second: 1\n"
  32. " third: 2\n";
  33. const Node doc = Load(example_yaml);
  34. // Test that printable key is part of error message
  35. EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"]["fourth"].as<int>(),
  36. "invalid node; first invalid key: \"fourth\"");
  37. EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"][37].as<int>(),
  38. "invalid node; first invalid key: \"37\"");
  39. // Non-printable key is not included in error message
  40. EXPECT_THROW_EXCEPTION(YAML::InvalidNode,
  41. doc["first"][std::vector<int>()].as<int>(),
  42. "invalid node; this may result from using a map "
  43. "iterator as a sequence iterator, or vice-versa");
  44. }
  45. }
  46. }