master_lexer_state_unittest.cc 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <dns/inputsource.h>
  15. #include <dns/master_lexer.h>
  16. #include <dns/master_lexer_state.h>
  17. #include <gtest/gtest.h>
  18. #include <sstream>
  19. using namespace isc::dns;
  20. using namespace master_lexer_internal;
  21. namespace {
  22. typedef MasterLexer::Token Token; // shortcut
  23. class MasterLexerStateTest : public ::testing::Test {
  24. protected:
  25. MasterLexerStateTest() : s_start(State::getInstance(State::Start)),
  26. s_crlf(State::getInstance(State::CRLF)),
  27. options(MasterLexer::NONE)
  28. {
  29. lexer.open(ss);
  30. }
  31. const State& s_start;
  32. const State& s_crlf;
  33. MasterLexer lexer;
  34. std::stringstream ss;
  35. MasterLexer::Options options;
  36. };
  37. // Common check for the end-of-file condition.
  38. // Token is set to END_OF_FILE, and the lexer was NOT last eol state.
  39. // Passed state can be any valid one; they are stateless, just providing the
  40. // interface for inspection.
  41. void
  42. eofCheck(const State& state, MasterLexer& lexer) {
  43. EXPECT_EQ(Token::END_OF_FILE, state.getToken(lexer).getType());
  44. EXPECT_FALSE(state.wasLastEOL(lexer));
  45. }
  46. TEST_F(MasterLexerStateTest, startAndEnd) {
  47. // A simple case: the input is empty, so we begin with start and
  48. // are immediately done.
  49. const State* s_next = s_start.handle(lexer, options);
  50. EXPECT_EQ(static_cast<const State*>(NULL), s_next);
  51. eofCheck(s_start, lexer);
  52. }
  53. TEST_F(MasterLexerStateTest, startToEOL) {
  54. ss << "\n";
  55. const State* s_next = s_start.handle(lexer, options);
  56. EXPECT_EQ(static_cast<const State*>(NULL), s_next);
  57. EXPECT_TRUE(s_start.wasLastEOL(lexer));
  58. EXPECT_EQ(Token::END_OF_LINE, s_start.getToken(lexer).getType());
  59. // The next lexer session will reach EOF. Same eof check should pass.
  60. s_start.handle(lexer, options);
  61. eofCheck(s_start, lexer);
  62. }
  63. TEST_F(MasterLexerStateTest, space) {
  64. // by default space characters and tabs will be ignored. We check this
  65. // twice; at the second iteration, it's a white space at the beginning
  66. // of line, but since we don't specify INITIAL_WS option, it's treated as
  67. // normal space and ignored.
  68. const State* s_next;
  69. for (size_t i = 0; i < 2; ++i) {
  70. ss << " \t\n";
  71. s_next = s_start.handle(lexer, options);
  72. EXPECT_EQ(static_cast<const State*>(NULL), s_next);
  73. EXPECT_TRUE(s_start.wasLastEOL(lexer));
  74. EXPECT_EQ(Token::END_OF_LINE, s_start.getToken(lexer).getType());
  75. }
  76. // Now we specify the INITIAL_WS option. It will be recognized and the
  77. // corresponding token will be returned.
  78. ss << " ";
  79. options = MasterLexer::INITIAL_WS;
  80. s_next = s_start.handle(lexer, options);
  81. EXPECT_EQ(static_cast<const State*>(NULL), s_next);
  82. EXPECT_FALSE(s_start.wasLastEOL(lexer));
  83. EXPECT_EQ(Token::INITIAL_WS, s_start.getToken(lexer).getType());
  84. }
  85. }