• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "lexer.h"
16 
17 #include "eval_env.h"
18 #include "test.h"
19 
20 using namespace std;
21 
TEST(Lexer,ReadVarValue)22 TEST(Lexer, ReadVarValue) {
23   Lexer lexer("plain text $var $VaR ${x}\n");
24   EvalString eval;
25   string err;
26   EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
27   EXPECT_EQ("", err);
28   EXPECT_EQ("[plain text ][$var][ ][$VaR][ ][$x]",
29             eval.Serialize());
30 }
31 
TEST(Lexer,ReadEvalStringEscapes)32 TEST(Lexer, ReadEvalStringEscapes) {
33   Lexer lexer("$ $$ab c$: $\ncde\n");
34   EvalString eval;
35   string err;
36   EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
37   EXPECT_EQ("", err);
38   EXPECT_EQ("[ $ab c: cde]",
39             eval.Serialize());
40 }
41 
TEST(Lexer,ReadIdent)42 TEST(Lexer, ReadIdent) {
43   Lexer lexer("foo baR baz_123 foo-bar");
44   string ident;
45   EXPECT_TRUE(lexer.ReadIdent(&ident));
46   EXPECT_EQ("foo", ident);
47   EXPECT_TRUE(lexer.ReadIdent(&ident));
48   EXPECT_EQ("baR", ident);
49   EXPECT_TRUE(lexer.ReadIdent(&ident));
50   EXPECT_EQ("baz_123", ident);
51   EXPECT_TRUE(lexer.ReadIdent(&ident));
52   EXPECT_EQ("foo-bar", ident);
53 }
54 
TEST(Lexer,ReadIdentCurlies)55 TEST(Lexer, ReadIdentCurlies) {
56   // Verify that ReadIdent includes dots in the name,
57   // but in an expansion $bar.dots stops at the dot.
58   Lexer lexer("foo.dots $bar.dots ${bar.dots}\n");
59   string ident;
60   EXPECT_TRUE(lexer.ReadIdent(&ident));
61   EXPECT_EQ("foo.dots", ident);
62 
63   EvalString eval;
64   string err;
65   EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
66   EXPECT_EQ("", err);
67   EXPECT_EQ("[$bar][.dots ][$bar.dots]",
68             eval.Serialize());
69 }
70 
TEST(Lexer,Error)71 TEST(Lexer, Error) {
72   Lexer lexer("foo$\nbad $");
73   EvalString eval;
74   string err;
75   ASSERT_FALSE(lexer.ReadVarValue(&eval, &err));
76   EXPECT_EQ("input:2: bad $-escape (literal $ must be written as $$)\n"
77             "bad $\n"
78             "    ^ near here"
79             , err);
80 }
81 
TEST(Lexer,CommentEOF)82 TEST(Lexer, CommentEOF) {
83   // Verify we don't run off the end of the string when the EOF is
84   // mid-comment.
85   Lexer lexer("# foo");
86   Lexer::Token token = lexer.ReadToken();
87   EXPECT_EQ(Lexer::ERROR, token);
88 }
89 
TEST(Lexer,Tabs)90 TEST(Lexer, Tabs) {
91   // Verify we print a useful error on a disallowed character.
92   Lexer lexer("   \tfoobar");
93   Lexer::Token token = lexer.ReadToken();
94   EXPECT_EQ(Lexer::INDENT, token);
95   token = lexer.ReadToken();
96   EXPECT_EQ(Lexer::ERROR, token);
97   EXPECT_EQ("tabs are not allowed, use spaces", lexer.DescribeLastError());
98 }
99