Browse Source

[2506] supported NUMBER in getNextToken() with expected type

JINMEI Tatuya 12 years ago
parent
commit
d52186367e
2 changed files with 39 additions and 6 deletions
  1. 18 6
      src/lib/dns/master_lexer.cc
  2. 21 0
      src/lib/dns/tests/master_lexer_unittest.cc

+ 18 - 6
src/lib/dns/master_lexer.cc

@@ -192,14 +192,26 @@ MasterLexer::getNextToken(Options options) {
     return (impl_->token_);
 }
 
-const MasterToken&
-MasterLexer::getNextToken(MasterToken::Type expect, bool eol_ok) {
-    Options options = NONE;
-    if (expect == MasterToken::QSTRING) {
-        options = options | QSTRING;
+namespace {
+inline MasterLexer::Options
+optionsForTokenType(MasterToken::Type expect) {
+    switch (expect) {
+    case MasterToken::STRING:
+        return (MasterLexer::NONE);
+    case MasterToken::QSTRING:
+        return (MasterLexer::QSTRING);
+    case MasterToken::NUMBER:
+        return (MasterLexer::NUMBER);
+    default:
+        assert(false);
     }
+}
+}
 
-    getNextToken(options);      // the result should be set in impl_->token_
+const MasterToken&
+MasterLexer::getNextToken(MasterToken::Type expect, bool eol_ok) {
+    // the result should be set in impl_->token_
+    getNextToken(optionsForTokenType(expect));
 
     const bool is_eol_like =
         (impl_->token_.getType() == MasterToken::END_OF_LINE ||

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

@@ -356,4 +356,25 @@ TEST_F(MasterLexerTest, getNextTokenQString) {
     eofCheck(lexer, MasterToken::QSTRING);
 }
 
+TEST_F(MasterLexerTest, getNextTokenNumber) {
+    ss << "3600\n";
+    ss << "\n";
+    ss << "86400";
+    lexer.pushSource(ss);
+
+    // Expecting a number string and get one.
+    EXPECT_EQ(3600,
+              lexer.getNextToken(MasterToken::NUMBER).getNumber());
+    eolCheck(lexer, MasterToken::NUMBER);
+
+    // Skip the 2nd '\n'
+    EXPECT_EQ(MasterToken::END_OF_LINE, lexer.getNextToken().getType());
+
+    // Unless we specify NUMBER, decimal number string should be recognized
+    // as a string.
+    EXPECT_EQ("86400",
+              lexer.getNextToken(MasterToken::STRING).getString());
+    eofCheck(lexer, MasterToken::NUMBER);
+}
+
 }