Browse Source

[2375] A fake state class

It is for tests, but in the main code for technical reasons.
Michal 'vorner' Vaner 12 years ago
parent
commit
1c14515cc6

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

@@ -390,6 +390,52 @@ QString::handle(MasterLexer& lexer) const {
     }
 }
 
+namespace {
+
+// A fake state that just eats something from the input, pushes
+// a given token and calls a callback if it is set. It refers to
+// another state to return.
+class FakeState : public State {
+public:
+    FakeState(const State* next, size_t eat_chars,
+              MasterLexer::Token* token = NULL,
+              const boost::function<void ()>& callback =
+              boost::function<void ()>()) :
+        next_(next),
+        eat_chars_(eat_chars),
+        token_(token),
+        callback_(callback)
+    {}
+    virtual const State* handle(MasterLexer& lexer) const {
+        for (size_t i = 0; i < eat_chars_; ++i) {
+            getLexerImpl(lexer)->source_->getChar();
+        }
+        if (token_ != NULL) {
+            getLexerImpl(lexer)->token_ = *token_;
+        }
+        if (!callback_.empty()) {
+            callback_();
+        }
+        return (next_);
+    }
+private:
+    const State* const next_;
+    size_t eat_chars_;
+    MasterLexer::Token* const token_;
+    const boost::function<void ()> callback_;
+};
+
+}
+
+State*
+State::getFakeState(const State* next, size_t eat_chars,
+                    MasterLexer::Token* token,
+                    const boost::function<void ()>& callback)
+{
+    // Just allocate new FakeState with the parameters.
+    return (new FakeState(next, eat_chars, token, callback));
+}
+
 } // namespace master_lexer_internal
 
 } // end of namespace dns

+ 17 - 0
src/lib/dns/master_lexer_state.h

@@ -17,6 +17,8 @@
 
 #include <dns/master_lexer.h>
 
+#include <boost/function.hpp>
+
 namespace isc {
 namespace dns {
 
@@ -109,6 +111,21 @@ public:
     /// need this method.
     static const State& getInstance(ID state_id);
 
+    /// \brief Returns a fake State instance.
+    ///
+    /// The returned State will eat eat_chars from the input source,
+    /// it'll set the given token if not NULL, call the given callback
+    /// and return the next state when its handle() is called.
+    ///
+    /// This is provided only for testing purposes. MasterLexer shouldn't
+    /// need this method.
+    ///
+    /// The caller is responsible for deleting the State.
+    static State* getFakeState(const State* next, size_t eat_chars,
+                               MasterLexer::Token* token = NULL,
+                               const boost::function<void ()>& callback =
+                               boost::function<void ()>());
+
     /// \name Read-only accessors for testing purposes.
     ///
     /// These allow tests to inspect some selected portion of the internal

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

@@ -15,10 +15,12 @@
 #include <exceptions/exceptions.h>
 
 #include <dns/master_lexer.h>
+#include <dns/master_lexer_state.h>
 
 #include <gtest/gtest.h>
 
 #include <boost/lexical_cast.hpp>
+#include <boost/function.hpp>
 
 #include <string>
 #include <sstream>