yaml-cpp

FORK: A YAML parser and emitter in C++
git clone https://git.neptards.moe/neptards/yaml-cpp.git
Log | Files | Refs | README | LICENSE

error_messages_test.cpp (2374B)


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