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 
17 #include <gtest/gtest.h>
18 
19 #include <sstream>
20 
21 #include "sexp/lexer.hpp"
22 
TEST(LexerTest,simple_tokens)23 TEST(LexerTest, simple_tokens)
24 {
25   std::istringstream is("(foo . bar #())");
26   sexp::Lexer lexer(is);
27   ASSERT_EQ(sexp::Lexer::TOKEN_OPEN_PAREN, lexer.get_next_token());
28   ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
29   ASSERT_EQ("foo", lexer.get_string());
30   ASSERT_EQ(sexp::Lexer::TOKEN_DOT, lexer.get_next_token());
31   ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
32   ASSERT_EQ("bar", lexer.get_string());
33   ASSERT_EQ(sexp::Lexer::TOKEN_ARRAY_START, lexer.get_next_token());
34   ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
35   ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
36   ASSERT_EQ(sexp::Lexer::TOKEN_EOF, lexer.get_next_token());
37 }
38 
TEST(LexerTest,long_tokens)39 TEST(LexerTest, long_tokens)
40 {
41   std::string long_token(32768, 'X');
42   std::istringstream is("(" + long_token + ")");
43   sexp::Lexer lexer(is);
44   ASSERT_EQ(sexp::Lexer::TOKEN_OPEN_PAREN, lexer.get_next_token());
45   ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
46   ASSERT_EQ(long_token, lexer.get_string());
47   ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
48   ASSERT_EQ(sexp::Lexer::TOKEN_EOF, lexer.get_next_token());
49 }
50 
TEST(LexerTest,comment)51 TEST(LexerTest, comment)
52 {
53   std::istringstream is(";comment\n(foo ;comment\n;comment\n bar);EOF");
54   sexp::Lexer lexer(is);
55   ASSERT_EQ(sexp::Lexer::TOKEN_OPEN_PAREN, lexer.get_next_token());
56   ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
57   ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
58   ASSERT_EQ(sexp::Lexer::TOKEN_CLOSE_PAREN, lexer.get_next_token());
59 }
60 
TEST(LexerTest,token_dot)61 TEST(LexerTest, token_dot)
62 {
63   std::vector<std::string> texts = {
64     "."
65   };
66 
67   for(const auto& text : texts)
68   {
69     std::istringstream is(text);
70     sexp::Lexer lexer(is);
71     ASSERT_EQ(sexp::Lexer::TOKEN_DOT, lexer.get_next_token());
72     ASSERT_EQ(text, lexer.get_string());
73   }
74 }
75 
TEST(LexerTest,token_symbol)76 TEST(LexerTest, token_symbol)
77 {
78   std::vector<std::string> texts = {
79     "SymbolTest",
80     "foo-bar",
81     "1.2.3",
82     "e50",
83   };
84 
85   for(const auto& text : texts)
86   {
87     std::istringstream is(text);
88     sexp::Lexer lexer(is);
89     ASSERT_EQ(sexp::Lexer::TOKEN_SYMBOL, lexer.get_next_token());
90     ASSERT_EQ(text, lexer.get_string());
91   }
92 }
93 
TEST(LexerTest,token_string)94 TEST(LexerTest, token_string)
95 {
96   std::istringstream is("\"StringTest\"");
97   sexp::Lexer lexer(is);
98   ASSERT_EQ(sexp::Lexer::TOKEN_STRING, lexer.get_next_token());
99   ASSERT_EQ("StringTest", lexer.get_string());
100 }
101 
TEST(LexerTest,token_integer)102 TEST(LexerTest, token_integer)
103 {
104   std::vector<std::string> texts = {
105     "123456789",
106     "-123456789"
107   };
108 
109   for(const auto& text : texts)
110   {
111     std::istringstream is(text);
112     sexp::Lexer lexer(is);
113     ASSERT_EQ(sexp::Lexer::TOKEN_INTEGER, lexer.get_next_token());
114     ASSERT_EQ(text, lexer.get_string());
115   }
116 }
117 
TEST(LexerTest,token_real)118 TEST(LexerTest, token_real)
119 {
120   std::vector<std::string> texts = {
121     ".1234",
122     ".1234e15",
123     "1234.6789",
124     "1234.",
125     "1234.5678",
126     "1234.5678e15",
127     "-1234.5678e15",
128     "1234.5678e15",
129     "1234.5678E+15",
130     "-1234.5678E-15",
131   };
132 
133   for(const auto& text : texts)
134   {
135     std::istringstream is(text);
136     sexp::Lexer lexer(is);
137     ASSERT_EQ(sexp::Lexer::TOKEN_REAL, lexer.get_next_token());
138     ASSERT_EQ(text, lexer.get_string());
139   }
140 }
141 
142 /* EOF */
143