File: error_messages_test.cpp

package info (click to toggle)
yaml-cpp 0.7.0%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,400 kB
  • sloc: cpp: 23,299; ansic: 931; python: 196; makefile: 50
file content (61 lines) | stat: -rw-r--r-- 2,374 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "yaml-cpp/yaml.h"  // IWYU pragma: keep

#include "gtest/gtest.h"

#define EXPECT_THROW_EXCEPTION(exception_type, statement, message) \
  ASSERT_THROW(statement, exception_type);                         \
  try {                                                            \
    statement;                                                     \
  } catch (const exception_type& e) {                              \
    EXPECT_EQ(e.msg, message);                                     \
  }

namespace YAML {
namespace {

TEST(ErrorMessageTest, BadSubscriptErrorMessage) {
  const char *example_yaml = "first:\n"
                             "   second: 1\n"
                             "   third: 2\n";

  Node doc = Load(example_yaml);

  // Test that printable key is part of error message
  EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"]["fourth"],
                         "operator[] call on a scalar (key: \"fourth\")");
  
  EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][37],
                         "operator[] call on a scalar (key: \"37\")");


  // Non-printable key is not included in error message
  EXPECT_THROW_EXCEPTION(YAML::BadSubscript,
                         doc["first"]["second"][std::vector<int>()],
                         "operator[] call on a scalar");

  EXPECT_THROW_EXCEPTION(YAML::BadSubscript, doc["first"]["second"][Node()],
                         "operator[] call on a scalar");
}

TEST(ErrorMessageTest, Ex9_1_InvalidNodeErrorMessage) {
  const char *example_yaml = "first:\n"
                             "   second: 1\n"
                             "   third: 2\n";

  const Node doc = Load(example_yaml);

  // Test that printable key is part of error message
  EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"]["fourth"].as<int>(),
                         "invalid node; first invalid key: \"fourth\"");
  
  EXPECT_THROW_EXCEPTION(YAML::InvalidNode, doc["first"][37].as<int>(),
                         "invalid node; first invalid key: \"37\"");
 
  // Non-printable key is not included in error message
  EXPECT_THROW_EXCEPTION(YAML::InvalidNode,
                         doc["first"][std::vector<int>()].as<int>(),
                         "invalid node; this may result from using a map "
                         "iterator as a sequence iterator, or vice-versa");
}
}   
}