Small code improvements in Parser + test

This commit is contained in:
hyperxor 2019-11-23 11:01:43 +03:00
parent a8ba6a8dca
commit 2510d8cfa4
3 changed files with 28 additions and 13 deletions

View File

@ -18,7 +18,7 @@ Parser::Parser(std::istream& in) : Parser() { Load(in); }
Parser::~Parser() = default; Parser::~Parser() = default;
Parser::operator bool() const { Parser::operator bool() const {
return m_pScanner.get() && !m_pScanner->empty(); return m_pScanner && !m_pScanner->empty();
} }
void Parser::Load(std::istream& in) { void Parser::Load(std::istream& in) {
@ -27,7 +27,7 @@ void Parser::Load(std::istream& in) {
} }
bool Parser::HandleNextDocument(EventHandler& eventHandler) { bool Parser::HandleNextDocument(EventHandler& eventHandler) {
if (!m_pScanner.get()) if (!m_pScanner)
return false; return false;
ParseDirectives(); ParseDirectives();
@ -43,11 +43,7 @@ bool Parser::HandleNextDocument(EventHandler& eventHandler) {
void Parser::ParseDirectives() { void Parser::ParseDirectives() {
bool readDirective = false; bool readDirective = false;
while (1) { while (!m_pScanner->empty()) {
if (m_pScanner->empty()) {
break;
}
Token& token = m_pScanner->peek(); Token& token = m_pScanner->peek();
if (token.type != Token::DIRECTIVE) { if (token.type != Token::DIRECTIVE) {
break; break;
@ -113,15 +109,11 @@ void Parser::HandleTagDirective(const Token& token) {
} }
void Parser::PrintTokens(std::ostream& out) { void Parser::PrintTokens(std::ostream& out) {
if (!m_pScanner.get()) { if (!m_pScanner) {
return; return;
} }
while (1) { while (!m_pScanner->empty()) {
if (m_pScanner->empty()) {
break;
}
out << m_pScanner->peek() << "\n"; out << m_pScanner->peek() << "\n";
m_pScanner->pop(); m_pScanner->pop();
} }

View File

@ -1,8 +1,11 @@
#include "yaml-cpp/emitterstyle.h" #include "yaml-cpp/emitterstyle.h"
#include "yaml-cpp/eventhandler.h" #include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/mark.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include <string>
namespace YAML { namespace YAML {
class MockEventHandler : public EventHandler { class MockEventHandler : public EventHandler {

20
test/parser_test.cpp Normal file
View File

@ -0,0 +1,20 @@
#include "yaml-cpp/parser.h"
#include "mock_event_handler.h"
#include "gtest/gtest.h"
using YAML::Parser;
using YAML::MockEventHandler;
TEST(ParserTest, Empty) {
Parser parser;
EXPECT_FALSE(parser);
MockEventHandler handler;
EXPECT_FALSE(parser.HandleNextDocument(handler));
std::ostringstream oss;
parser.PrintTokens(oss);
EXPECT_EQ(oss.str(), "");
}