]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
[2375] Add a test a token is actually produced
authorMichal 'vorner' Vaner <michal.vaner@nic.cz>
Tue, 13 Nov 2012 18:04:16 +0000 (19:04 +0100)
committerMichal 'vorner' Vaner <michal.vaner@nic.cz>
Mon, 19 Nov 2012 15:54:04 +0000 (16:54 +0100)
This is a debug test. It checks the state machine produced a token.
There are no tests for this test, since such condition would be a
programmer error, not something expected.

src/lib/dns/master_lexer.cc
src/lib/dns/master_lexer.h
src/lib/dns/tests/master_lexer_token_unittest.cc

index 30273963e4aa585d1f4a57b510406f4123170fb7..55678335600f237703934443e696adf98391b8dd 100644 (file)
@@ -154,6 +154,9 @@ MasterLexer::getSourceLine() const {
 
 MasterLexer::Token
 MasterLexer::getNextToken(Options options) {
+    // Reset the token now. This is to check a token was actually produced.
+    // This is debugging aid.
+    impl_->token_ = Token(Token::NO_TOKEN_PRODUCED);
     if (impl_->source_ == NULL) {
         isc_throw(isc::InvalidOperation, "No source to read tokens from");
     }
@@ -161,7 +164,10 @@ MasterLexer::getNextToken(Options options) {
          state = state->handle(*this)) {
         // Do nothing here. All is handled in the for cycle header itself.
     }
-    // TODO load the token
+    // Make sure a token was produced. Since this Can Not Happen, we assert
+    // here instead of throwing.
+    assert(impl_->token_.getType() != Token::ERROR ||
+           impl_->token_.getErrorCode() != Token::NO_TOKEN_PRODUCED);
     return (impl_->token_);
 }
 
@@ -180,7 +186,8 @@ const char* const error_text[] = {
     "lexer not started",        // NOT_STARTED
     "unbalanced parentheses",   // UNBALANCED_PAREN
     "unexpected end of input",  // UNEXPECTED_END
-    "unbalanced quotes"         // UNBALANCED_QUOTES
+    "unbalanced quotes",        // UNBALANCED_QUOTES
+    "no token produced"
 };
 const size_t error_text_max_count = sizeof(error_text) / sizeof(error_text[0]);
 }
index c977050e3b4abae5ffc069f7c3a78195629471a9..78bcfb6ce8017640c6c9e64b5d7cc7144258dbee 100644 (file)
@@ -277,8 +277,10 @@ public:
         NOT_STARTED, ///< The lexer is just initialized and has no token
         UNBALANCED_PAREN,       ///< Unbalanced parentheses detected
         UNEXPECTED_END, ///< The lexer reaches the end of line or file
-                       /// unexpectedly
+                        /// unexpectedly
         UNBALANCED_QUOTES,      ///< Unbalanced quotations detected
+        NO_TOKEN_PRODUCED, ///< No token was produced. This means programmer
+                           /// error and should never get out of the lexer.
         MAX_ERROR_CODE ///< Max integer corresponding to valid error codes.
                        /// (excluding this one). Mainly for internal use.
     };
index e3b5183c635b4583509c9f82d8409263d4fdcc3c..5f887f9df7e8cd94e0be9d0f37c63a68d2ddc12b 100644 (file)
@@ -142,15 +142,18 @@ TEST_F(MasterLexerTokenTest, errors) {
     EXPECT_EQ("unbalanced quotes",
               MasterLexer::Token(MasterLexer::Token::UNBALANCED_QUOTES).
               getErrorText());
+    EXPECT_EQ("no token produced",
+              MasterLexer::Token(MasterLexer::Token::NO_TOKEN_PRODUCED).
+              getErrorText());
 
     // getErrorCode/Text() isn't allowed for non number types
     EXPECT_THROW(token_num.getErrorCode(), isc::InvalidOperation);
     EXPECT_THROW(token_num.getErrorText(), isc::InvalidOperation);
 
-    // Only the pre-defined error code is accepted.  Hardcoding '4' (max code
+    // Only the pre-defined error code is accepted.  Hardcoding '5' (max code
     // + 1) is intentional; it'd be actually better if we notice it when we
     // update the enum list (which shouldn't happen too often).
-    EXPECT_THROW(MasterLexer::Token(MasterLexer::Token::ErrorCode(4)),
+    EXPECT_THROW(MasterLexer::Token(MasterLexer::Token::ErrorCode(5)),
                  isc::InvalidParameter);
 
     // Check the coexistence of "from number" and "from error-code"