From 1327ce6d736f15220b200ea1f1d3e0f7509ff6f7 Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 23 Mar 2014 14:47:30 -0500 Subject: [PATCH 1/2] Convert core parser tests to gtest --- test/core/parsertests.cpp | 83 ---------------------------- test/integration/handler_test.cpp | 89 +++++++++++++++++++++++++++++++ test/parsertests.h | 14 ----- test/tests.cpp | 4 -- 4 files changed, 89 insertions(+), 101 deletions(-) delete mode 100644 test/core/parsertests.cpp create mode 100644 test/integration/handler_test.cpp delete mode 100644 test/parsertests.h diff --git a/test/core/parsertests.cpp b/test/core/parsertests.cpp deleted file mode 100644 index 95eb168..0000000 --- a/test/core/parsertests.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include - -#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; -} -} diff --git a/test/integration/handler_test.cpp b/test/integration/handler_test.cpp new file mode 100644 index 0000000..8d0c78f --- /dev/null +++ b/test/integration/handler_test.cpp @@ -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 handler; + NiceMock 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"); +} +} +} diff --git a/test/parsertests.h b/test/parsertests.h deleted file mode 100644 index 27dbeb6..0000000 --- a/test/parsertests.h +++ /dev/null @@ -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 diff --git a/test/tests.cpp b/test/tests.cpp index 50dd8aa..7f6064c 100644 --- a/test/tests.cpp +++ b/test/tests.cpp @@ -4,7 +4,6 @@ #include #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"; } From 90d7562be346570ce4ba5e69f1228dbdf91454bf Mon Sep 17 00:00:00 2001 From: Jesse Beder Date: Sun, 23 Mar 2014 14:50:58 -0500 Subject: [PATCH 2/2] Fixed uninteresting mock --- test/integration/handler_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/handler_test.cpp b/test/integration/handler_test.cpp index 8d0c78f..4182340 100644 --- a/test/integration/handler_test.cpp +++ b/test/integration/handler_test.cpp @@ -62,7 +62,7 @@ class HandlerTest : public ::testing::Test { }; TEST_F(HandlerTest, NoEndOfMapFlow) { - EXPECT_THROW_PARSER_EXCEPTION(Parse("---{header: {id: 1"), + EXPECT_THROW_PARSER_EXCEPTION(IgnoreParse("---{header: {id: 1"), ErrorMsg::END_OF_MAP_FLOW); }