Browse Source

[2371] supported close() method

JINMEI Tatuya 12 years ago
parent
commit
1f1131d4f9

+ 10 - 0
src/lib/dns/master_lexer.cc

@@ -12,6 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
+#include <exceptions/exceptions.h>
+
 #include <dns/master_lexer.h>
 #include <dns/master_lexer.h>
 
 
 #include <boost/shared_ptr.hpp>
 #include <boost/shared_ptr.hpp>
@@ -72,6 +74,14 @@ MasterLexer::open(std::istream& input) {
     impl_->sources_.push_back(InputSourcePtr(new InputSource(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
 std::string
 MasterLexer::getSourceName() const {
 MasterLexer::getSourceName() const {
     if (impl_->sources_.empty()) {
     if (impl_->sources_.empty()) {

+ 1 - 0
src/lib/dns/master_lexer.h

@@ -33,6 +33,7 @@ public:
     ~MasterLexer();
     ~MasterLexer();
     void open(const char* filename);
     void open(const char* filename);
     void open(std::istream& input);
     void open(std::istream& input);
+    void close();
     std::string getSourceName() const;
     std::string getSourceName() const;
     size_t getSourceLine() const;
     size_t getSourceLine() const;
 
 

+ 22 - 2
src/lib/dns/tests/master_lexer_unittest.cc

@@ -12,6 +12,8 @@
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 // PERFORMANCE OF THIS SOFTWARE.
 // PERFORMANCE OF THIS SOFTWARE.
 
 
+#include <exceptions/exceptions.h>
+
 #include <dns/master_lexer.h>
 #include <dns/master_lexer.h>
 
 
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
@@ -36,12 +38,18 @@ protected:
     stringstream ss;
     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_TRUE(lexer.getSourceName().empty());
     EXPECT_EQ(0, lexer.getSourceLine());
     EXPECT_EQ(0, lexer.getSourceLine());
 }
 }
 
 
+TEST_F(MasterLexerTest, preOpen) {
+    // Initially sources stack is empty.
+    checkEmptySource(lexer);
+}
+
 TEST_F(MasterLexerTest, openStream) {
 TEST_F(MasterLexerTest, openStream) {
     lexer.open(ss);
     lexer.open(ss);
     EXPECT_EQ(string("stream-") + lexical_cast<string>(&ss),
     EXPECT_EQ(string("stream-") + lexical_cast<string>(&ss),
@@ -51,6 +59,10 @@ TEST_F(MasterLexerTest, openStream) {
     // indirectly) getSourceLine calls InputSource::getCurrentLine.  It should
     // indirectly) getSourceLine calls InputSource::getCurrentLine.  It should
     // return 1 initially.
     // return 1 initially.
     EXPECT_EQ(1, lexer.getSourceLine());
     EXPECT_EQ(1, lexer.getSourceLine());
+
+    // By closing it the stack will be empty again.
+    lexer.close();
+    checkEmptySource(lexer);
 }
 }
 
 
 TEST_F(MasterLexerTest, openFile) {
 TEST_F(MasterLexerTest, openFile) {
@@ -59,6 +71,14 @@ TEST_F(MasterLexerTest, openFile) {
     lexer.open(TEST_DATA_SRCDIR "/masterload.txt");
     lexer.open(TEST_DATA_SRCDIR "/masterload.txt");
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
     EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
     EXPECT_EQ(1, lexer.getSourceLine());
     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);
 }
 }
 
 
 }
 }