From: JINMEI Tatuya Date: Tue, 30 Oct 2012 00:10:26 +0000 (-0700) Subject: [2371] implemented initial framework of MasterLexer and some methods X-Git-Tag: trac2487_base~1^2~21^2~30^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=24e75ceec06dd94df77bec50b61a19d93962c10b;p=thirdparty%2Fkea.git [2371] implemented initial framework of MasterLexer and some methods --- diff --git a/src/lib/dns/master_lexer.cc b/src/lib/dns/master_lexer.cc index 2a5c886d8f..1d8b3c2a4f 100644 --- a/src/lib/dns/master_lexer.cc +++ b/src/lib/dns/master_lexer.cc @@ -14,8 +14,71 @@ #include +#include + #include #include +#include +#include + +namespace isc { +namespace dns { + +namespace master_lexer_internal { +std::string +createStreamName(std::istream& input_stream) { + std::stringstream ss; + ss << "stream-" << &input_stream; + return (ss.str()); +} + +class InputSource { +public: + InputSource(std::istream& input_stream) : + name_(createStreamName(input_stream)) + {} + const std::string& getName() const { return (name_); } + size_t getCurrentLine() const { return (1); } + +private: + const std::string name_; +}; + +typedef boost::shared_ptr InputSourcePtr; +} +using namespace master_lexer_internal; + +struct MasterLexer::MasterLexerImpl { + std::vector sources_; +}; + +MasterLexer::MasterLexer() : impl_(new MasterLexerImpl) { +} + +MasterLexer::~MasterLexer() { + delete impl_; +} + +void +MasterLexer::open(std::istream& input) { + impl_->sources_.push_back(InputSourcePtr(new InputSource(input))); +} + +std::string +MasterLexer::getSourceName() const { + if (impl_->sources_.empty()) { + return (std::string()); + } + return (impl_->sources_.back()->getName()); +} + +size_t +MasterLexer::getSourceLine() const { + if (impl_->sources_.empty()) { + return (0); + } + return (impl_->sources_.back()->getCurrentLine()); +} namespace { const char* const error_text[] = { @@ -27,9 +90,6 @@ const char* const error_text[] = { const size_t error_text_max_count = sizeof(error_text) / sizeof(error_text[0]); } -namespace isc { -namespace dns { - std::string MasterLexer::Token::getErrorText() const { if (type_ != ERROR) { @@ -42,6 +102,5 @@ MasterLexer::Token::getErrorText() const { return (error_text[val_.error_code_]); } - } // end of namespace dns } // end of namespace isc diff --git a/src/lib/dns/master_lexer.h b/src/lib/dns/master_lexer.h index bd86a04dc8..f07528e8c6 100644 --- a/src/lib/dns/master_lexer.h +++ b/src/lib/dns/master_lexer.h @@ -17,6 +17,7 @@ #include +#include #include #include @@ -27,6 +28,16 @@ namespace dns { class MasterLexer { public: class Token; // we define it separately for better readability + + MasterLexer(); + ~MasterLexer(); + void open(std::istream& input); + std::string getSourceName() const; + size_t getSourceLine() const; + +private: + struct MasterLexerImpl; + MasterLexerImpl* impl_; }; /// \brief Tokens for \c MasterLexer diff --git a/src/lib/dns/tests/Makefile.am b/src/lib/dns/tests/Makefile.am index 6df0e624d4..99d877f96a 100644 --- a/src/lib/dns/tests/Makefile.am +++ b/src/lib/dns/tests/Makefile.am @@ -25,6 +25,7 @@ run_unittests_SOURCES += edns_unittest.cc run_unittests_SOURCES += labelsequence_unittest.cc run_unittests_SOURCES += messagerenderer_unittest.cc run_unittests_SOURCES += master_lexer_token_unittest.cc +run_unittests_SOURCES += master_lexer_unittest.cc run_unittests_SOURCES += name_unittest.cc run_unittests_SOURCES += nsec3hash_unittest.cc run_unittests_SOURCES += rrclass_unittest.cc rrtype_unittest.cc diff --git a/src/lib/dns/tests/master_lexer_unittest.cc b/src/lib/dns/tests/master_lexer_unittest.cc new file mode 100644 index 0000000000..8994c786e3 --- /dev/null +++ b/src/lib/dns/tests/master_lexer_unittest.cc @@ -0,0 +1,56 @@ +// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC") +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice appear in all copies. +// +// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH +// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, +// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE +// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +// PERFORMANCE OF THIS SOFTWARE. + +#include + +#include + +#include + +#include +#include + +using namespace isc::dns; +using std::string; +using std::stringstream; +using boost::lexical_cast; + +namespace { + +class MasterLexerTest : public ::testing::Test { +protected: + MasterLexerTest() {} + + MasterLexer lexer; + stringstream ss; +}; + +TEST_F(MasterLexerTest, preOPen) { + // Initially sources stack is empty, and getXXX() returns accordingly. + EXPECT_TRUE(lexer.getSourceName().empty()); + EXPECT_EQ(0, lexer.getSourceLine()); +} + +TEST_F(MasterLexerTest, openStream) { + lexer.open(ss); + EXPECT_EQ(string("stream-") + lexical_cast(&ss), + lexer.getSourceName()); + + // From the point of view of this test, we only have to check (though + // indirectly) getSourceLine calls InputSource::getCurrentLine. It should + // return 1 initially. + EXPECT_EQ(1, lexer.getSourceLine()); +} + +}