#include <dns/master_lexer_inputsource.h>
#include <dns/master_lexer_state.h>
+#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lexical_cast.hpp>
return (impl_->sources_.back()->getCurrentLine());
}
+size_t
+MasterLexer::getTotalSourceSize() const {
+ size_t total_size = 0;
+ BOOST_FOREACH(InputSourcePtr& src, impl_->sources_) {
+ total_size += src->getSize();
+ }
+ return (total_size);
+}
+
const MasterToken&
MasterLexer::getNextToken(Options options) {
if (impl_->source_ == NULL) {
/// \return The current line number of the source (see the description)
size_t getSourceLine() const;
+ /// \brief Return the total size of pushed sources.
+ ///
+ /// This method returns the sum of the size of sources that have been
+ /// pushed to the lexer by the time of the call. It would give the
+ /// caller of some hint about the amount of data the lexer is working on.
+ ///
+ /// The size of a normal file is equal to the file size at the time of
+ /// the source is pushed. The size of other type of input stream is
+ /// the size of the data available in the stream at the time of the
+ /// source is pushed.
+ ///
+ /// If there is no source pushed in the lexer, it returns 0.
+ ///
+ /// \throw None
+ size_t getTotalSourceSize() const;
+
/// \brief Parse and return another token from the input.
///
/// It reads a bit of the last opened source and produces another token
/// \brief Returns the size of the input source in bytes.
///
- /// If the input source is a normal file, the return value should be
- /// equal to the file size at the time of the source is created.
- /// If the input source is other type of input stream, its the size of
- /// the data available in the stream at the time of the construction of
- /// the source.
+ /// See \c MasterLexer::getTotalSourceSize() for the definition of
+ /// the size of sources.
///
/// \throw None
size_t getSize() const { return (input_size_); }
}
TEST_F(InputSourceTest, getPosition) {
- // Initially the position is set to 0.
+ // Initially the position is set to 0. Other cases are tested in tests
+ // for get and unget.
EXPECT_EQ(0, source_.getPosition());
EXPECT_EQ(0, InputSource(TEST_DATA_SRCDIR "/masterload.txt").getPosition());
}
checkEmptySource(const MasterLexer& lexer) {
EXPECT_TRUE(lexer.getSourceName().empty());
EXPECT_EQ(0, lexer.getSourceLine());
+ EXPECT_EQ(0, lexer.getTotalSourceSize());
}
TEST_F(MasterLexerTest, preOpen) {
TEST_F(MasterLexerTest, pushStream) {
EXPECT_EQ(0, lexer.getSourceCount());
+ ss << "test";
lexer.pushSource(ss);
EXPECT_EQ(expected_stream_name, lexer.getSourceName());
EXPECT_EQ(1, lexer.getSourceCount());
+ EXPECT_EQ(4, lexer.getTotalSourceSize()); // 4 = len("test")
// From the point of view of this test, we only have to check (though
// indirectly) getSourceLine calls InputSource::getCurrentLine. It should
EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
EXPECT_EQ(1, lexer.getSourceLine());
+ // 143 = size of the test zone file. hardcode it assuming it won't change
+ // too often.
+ EXPECT_EQ(143, lexer.getTotalSourceSize());
+
lexer.popSource();
checkEmptySource(lexer);
EXPECT_EQ(0, lexer.getSourceCount());
}
TEST_F(MasterLexerTest, nestedPush) {
+ ss << "test";
lexer.pushSource(ss);
EXPECT_EQ(expected_stream_name, lexer.getSourceName());
// We can push another source without popping the previous one.
lexer.pushSource(TEST_DATA_SRCDIR "/masterload.txt");
EXPECT_EQ(TEST_DATA_SRCDIR "/masterload.txt", lexer.getSourceName());
+ EXPECT_EQ(143 + 4, lexer.getTotalSourceSize()); // see above for magic nums
// popSource() works on the "topmost" (last-pushed) source
lexer.popSource();
EXPECT_EQ(expected_stream_name, lexer.getSourceName());
+ EXPECT_EQ(4, lexer.getTotalSourceSize());
lexer.popSource();
EXPECT_TRUE(lexer.getSourceName().empty());