Convert core parser tests to gtest

This commit is contained in:
Jesse Beder 2014-03-23 14:47:30 -05:00
parent dfa32c7f44
commit 1327ce6d73
4 changed files with 89 additions and 101 deletions

View File

@ -1,83 +0,0 @@
#include <iostream>
#include "handlermacros.h"
#include "parsertests.h" // IWYU pragma: keep
#include "teststruct.h"
#include "yaml-cpp/parser.h"
namespace YAML {
class Exception;
class ParserException;
} // namespace YAML
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();
}
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;
}
}

View File

@ -0,0 +1,89 @@
#include "specexamples.h" // IWYU pragma: keep
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/yaml.h" // IWYU pragma: keep
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using ::testing::_;
using ::testing::InSequence;
using ::testing::NiceMock;
using ::testing::StrictMock;
#define EXPECT_THROW_PARSER_EXCEPTION(statement, message) \
ASSERT_THROW(statement, ParserException); \
try { \
statement; \
} \
catch (const ParserException& e) { \
EXPECT_EQ(e.msg, message); \
}
namespace YAML {
namespace {
class MockEventHandler : public EventHandler {
public:
MOCK_METHOD1(OnDocumentStart, void(const Mark&));
MOCK_METHOD0(OnDocumentEnd, void());
MOCK_METHOD2(OnNull, void(const Mark&, anchor_t));
MOCK_METHOD2(OnAlias, void(const Mark&, anchor_t));
MOCK_METHOD4(OnScalar, void(const Mark&, const std::string&, anchor_t,
const std::string&));
MOCK_METHOD3(OnSequenceStart,
void(const Mark&, const std::string&, anchor_t));
MOCK_METHOD0(OnSequenceEnd, void());
MOCK_METHOD3(OnMapStart, void(const Mark&, const std::string&, anchor_t));
MOCK_METHOD0(OnMapEnd, void());
};
class HandlerTest : public ::testing::Test {
protected:
void Parse(const std::string& example) {
std::stringstream stream(example);
Parser parser(stream);
while (parser.HandleNextDocument(handler)) {
}
}
void IgnoreParse(const std::string& example) {
std::stringstream stream(example);
Parser parser(stream);
while (parser.HandleNextDocument(nice_handler)) {
}
}
InSequence sequence;
StrictMock<MockEventHandler> handler;
NiceMock<MockEventHandler> nice_handler;
};
TEST_F(HandlerTest, NoEndOfMapFlow) {
EXPECT_THROW_PARSER_EXCEPTION(Parse("---{header: {id: 1"),
ErrorMsg::END_OF_MAP_FLOW);
}
TEST_F(HandlerTest, PlainScalarStartingWithQuestionMark) {
EXPECT_CALL(handler, OnDocumentStart(_));
EXPECT_CALL(handler, OnMapStart(_, "?", 0));
EXPECT_CALL(handler, OnScalar(_, "?", 0, "foo"));
EXPECT_CALL(handler, OnScalar(_, "?", 0, "?bar"));
EXPECT_CALL(handler, OnMapEnd());
EXPECT_CALL(handler, OnDocumentEnd());
Parse("foo: ?bar");
}
TEST_F(HandlerTest, NullStringScalar) {
EXPECT_CALL(handler, OnDocumentStart(_));
EXPECT_CALL(handler, OnMapStart(_, "?", 0));
EXPECT_CALL(handler, OnScalar(_, "?", 0, "foo"));
EXPECT_CALL(handler, OnNull(_, 0));
EXPECT_CALL(handler, OnMapEnd());
EXPECT_CALL(handler, OnDocumentEnd());
Parse("foo: null");
}
}
}

View File

@ -1,14 +0,0 @@
#ifndef PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#define PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
#if defined(_MSC_VER) || \
(defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
(__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
#pragma once
#endif
namespace Test {
bool RunParserTests();
}
#endif // PARSERTESTS_H_62B23520_7C8E_11DE_8A39_0800200C9A66

View File

@ -4,7 +4,6 @@
#include <vector>
#include "emittertests.h"
#include "parsertests.h"
#include "tests.h"
namespace Test {
@ -13,9 +12,6 @@ void RunAll() {
if (!RunEmitterTests())
passed = false;
if (!RunParserTests())
passed = false;
if (passed)
std::cout << "All tests passed!\n";
}