parsertests.cpp 1.69 KB
Newer Older
1
2
3
4
5
#include "parsertests.h"
#include "handlermacros.h"
#include "yaml-cpp/yaml.h"
#include <iostream>

Jesse Beder's avatar
Jesse Beder committed
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
namespace Test {
namespace Parser {
TEST NoEndOfMapFlow() {
  try {
    HANDLE("---{header: {id: 1");
  }
  catch (const YAML::ParserException& e) {
    YAML_ASSERT(e.msg == std::string(YAML::ErrorMsg::END_OF_MAP_FLOW));
    return true;
  }
  return "  no exception caught";
}

TEST PlainScalarStartingWithQuestionMark() {
  HANDLE("foo: ?bar");
  EXPECT_DOC_START();
  EXPECT_MAP_START("?", 0);
  EXPECT_SCALAR("?", 0, "foo");
  EXPECT_SCALAR("?", 0, "?bar");
  EXPECT_MAP_END();
  EXPECT_DOC_END();
  DONE();
}

TEST NullStringScalar() {
  HANDLE("foo: null");
  EXPECT_DOC_START();
  EXPECT_MAP_START("?", 0);
  EXPECT_SCALAR("?", 0, "foo");
  EXPECT_NULL(0);
  EXPECT_MAP_END();
  EXPECT_DOC_END();
  DONE();
}
}

namespace {
void RunParserTest(TEST (*test)(), const std::string& name, int& passed,
                   int& total) {
  TEST ret;
  try {
    ret = test();
  }
  catch (const YAML::Exception& e) {
    ret.ok = false;
    ret.error = std::string("  Exception caught: ") + e.what();
  }
53

Jesse Beder's avatar
Jesse Beder committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  if (!ret.ok) {
    std::cout << "Parser test failed: " << name << "\n";
    std::cout << ret.error << "\n";
  }

  if (ret.ok)
    passed++;
  total++;
}
}

bool RunParserTests() {
  int passed = 0;
  int total = 0;
  RunParserTest(&Parser::NoEndOfMapFlow, "No end of map flow", passed, total);
  RunParserTest(&Parser::PlainScalarStartingWithQuestionMark,
                "Plain scalar starting with question mark", passed, total);
  RunParserTest(&Parser::NullStringScalar, "Null string scalar", passed, total);

  std::cout << "Parser tests: " << passed << "/" << total << " passed\n";
  return passed == total;
}
76
}