Commit 1f1131d4 authored by JINMEI Tatuya's avatar JINMEI Tatuya
Browse files

[2371] supported close() method

parent b237d108
......@@ -12,6 +12,8 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <exceptions/exceptions.h>
#include <dns/master_lexer.h>
#include <boost/shared_ptr.hpp>
......@@ -72,6 +74,14 @@ MasterLexer::open(std::istream& input) {
impl_->sources_.push_back(InputSourcePtr(new InputSource(input)));
}
void
MasterLexer::close() {
if (impl_->sources_.empty()) {
isc_throw(InvalidOperation, "MasterLexer::close on an empty source");
}
impl_->sources_.pop_back();
}
std::string
MasterLexer::getSourceName() const {
if (impl_->sources_.empty()) {
......
......@@ -33,6 +33,7 @@ public:
~MasterLexer();
void open(const char* filename);
void open(std::istream& input);
void close();
std::string getSourceName() const;
size_t getSourceLine() const;
......
......@@ -12,6 +12,8 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <exceptions/exceptions.h>
#include <dns/master_lexer.h>
#include <gtest/gtest.h>
......@@ -36,12 +38,18 @@ protected:
stringstream ss;
};
TEST_F(MasterLexerTest, preOPen) {
// Initially sources stack is empty, and getXXX() returns accordingly.
// Commonly used check case where the input sources stack is empty.
void
checkEmptySource(const MasterLexer& lexer) {
EXPECT_TRUE(lexer.getSourceName().empty());
EXPECT_EQ(0, lexer.getSourceLine());
}
TEST_F(MasterLexerTest, preOpen) {
// Initially sources stack is empty.
checkEmptySource(lexer);
}
TEST_F(MasterLexerTest, openStream) {
lexer.open(ss);
EXPECT_EQ(string("stream-") + lexical_cast<string>(&ss),
......@@ -51,6 +59,10 @@ TEST_F(MasterLexerTest, openStream) {
// indirectly) getSourceLine calls InputSource::getCurrentLine. It should
// return 1 initially.
EXPECT_EQ(1, lexer.getSourceLine());
// By closing it the stack will be empty again.
lexer.close();
checkEmptySource(lexer);
}
TEST_F(MasterLexerTest, openFile) {
......@@ -59,6 +71,14 @@ TEST_F(MasterLexerTest, openFile) {
lexer.open(TEST_DATA_SRCDIR "/masterload.txt");
EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
EXPECT_EQ(1, lexer.getSourceLine());
lexer.close();
checkEmptySource(lexer);
}
TEST_F(MasterLexerTest, invalidClose) {
// close() cannot be called if the sources stack is empty.
EXPECT_THROW(lexer.close(), isc::InvalidOperation);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment