lexer_test.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SExp - A S-Expression Parser for C++
  2. // Copyright (C) 2015 Ingo Ruhnke <grumbel@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #include <gtest/gtest.h>
  17. #include <sstream>
  18. #include "sexp/lexer.hpp"
  19. TEST(LexerTest, simple_tokens)
  20. {
  21. std::istringstream is("(foo . bar #())");
  22. sexp::Lexer lexer(is);
  23. ASSERT_EQ(sexp::Lexer::TOKEN_OPEN_PAREN, lexer.get_next_token());
  24. ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
  25. ASSERT_EQ("foo", lexer.get_string());
  26. ASSERT_EQ(sexp::Lexer::TOKEN_DOT, lexer.get_next_token());
  27. ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
  28. ASSERT_EQ("bar", lexer.get_string());
  29. ASSERT_EQ(sexp::Lexer::TOKEN_ARRAY_START, lexer.get_next_token());
  30. ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
  31. ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
  32. ASSERT_EQ(sexp::Lexer::TOKEN_EOF, lexer.get_next_token());
  33. }
  34. TEST(LexerTest, long_tokens)
  35. {
  36. std::string long_token(32768, 'X');
  37. std::istringstream is("(" + long_token + ")");
  38. sexp::Lexer lexer(is);
  39. ASSERT_EQ(sexp::Lexer::TOKEN_OPEN_PAREN, lexer.get_next_token());
  40. ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
  41. ASSERT_EQ(long_token, lexer.get_string());
  42. ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
  43. ASSERT_EQ(sexp::Lexer::TOKEN_EOF, lexer.get_next_token());
  44. }
  45. TEST(LexerTest, comment)
  46. {
  47. std::istringstream is(";comment\n(foo ;comment\n;comment\n bar);EOF");
  48. sexp::Lexer lexer(is);
  49. ASSERT_EQ(sexp::Lexer::TOKEN_OPEN_PAREN, lexer.get_next_token());
  50. ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
  51. ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
  52. ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
  53. }
  54. TEST(LexerTest, token_dot)
  55. {
  56. std::vector<std::string> texts = {
  57. "."
  58. };
  59. for(const auto& text : texts)
  60. {
  61. std::istringstream is(text);
  62. sexp::Lexer lexer(is);
  63. ASSERT_EQ(sexp::Lexer::TOKEN_DOT, lexer.get_next_token());
  64. ASSERT_EQ(text, lexer.get_string());
  65. }
  66. }
  67. TEST(LexerTest, token_symbol)
  68. {
  69. std::vector<std::string> texts = {
  70. "SymbolTest",
  71. "foo-bar",
  72. "1.2.3",
  73. "e50",
  74. };
  75. for(const auto& text : texts)
  76. {
  77. std::istringstream is(text);
  78. sexp::Lexer lexer(is);
  79. ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
  80. ASSERT_EQ(text, lexer.get_string());
  81. }
  82. }
  83. TEST(LexerTest, token_string)
  84. {
  85. std::istringstream is("\"StringTest\"");
  86. sexp::Lexer lexer(is);
  87. ASSERT_EQ(sexp::Lexer::TOKEN_STRING, lexer.get_next_token());
  88. ASSERT_EQ("StringTest", lexer.get_string());
  89. }
  90. TEST(LexerTest, token_integer)
  91. {
  92. std::vector<std::string> texts = {
  93. "123456789",
  94. "-123456789"
  95. };
  96. for(const auto& text : texts)
  97. {
  98. std::istringstream is(text);
  99. sexp::Lexer lexer(is);
  100. ASSERT_EQ(sexp::Lexer::TOKEN_INTEGER, lexer.get_next_token());
  101. ASSERT_EQ(text, lexer.get_string());
  102. }
  103. }
  104. TEST(LexerTest, token_real)
  105. {
  106. std::vector<std::string> texts = {
  107. ".1234",
  108. ".1234e15",
  109. "1234.6789",
  110. "1234.",
  111. "1234.5678",
  112. "1234.5678e15",
  113. "-1234.5678e15",
  114. "1234.5678e15",
  115. "1234.5678E+15",
  116. "-1234.5678E-15",
  117. };
  118. for(const auto& text : texts)
  119. {
  120. std::istringstream is(text);
  121. sexp::Lexer lexer(is);
  122. ASSERT_EQ(sexp::Lexer::TOKEN_REAL, lexer.get_next_token());
  123. ASSERT_EQ(text, lexer.get_string());
  124. }
  125. }
  126. /* EOF */