Ninja
lexer_test.cc
Go to the documentation of this file.
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 <gtest/gtest.h>
18 
19 #include "eval_env.h"
20 
21 TEST(Lexer, ReadVarValue) {
22  Lexer lexer("plain text $var $VaR ${x}\n");
23  EvalString eval;
24  string err;
25  EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
26  EXPECT_EQ("", err);
27  EXPECT_EQ("[plain text ][$var][ ][$VaR][ ][$x]",
28  eval.Serialize());
29 }
30 
31 TEST(Lexer, ReadEvalStringEscapes) {
32  Lexer lexer("$ $$ab c$: $\ncde\n");
33  EvalString eval;
34  string err;
35  EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
36  EXPECT_EQ("", err);
37  EXPECT_EQ("[ $ab c: cde]",
38  eval.Serialize());
39 }
40 
41 TEST(Lexer, ReadIdent) {
42  Lexer lexer("foo baR baz_123 foo-bar");
43  string ident;
44  EXPECT_TRUE(lexer.ReadIdent(&ident));
45  EXPECT_EQ("foo", ident);
46  EXPECT_TRUE(lexer.ReadIdent(&ident));
47  EXPECT_EQ("baR", ident);
48  EXPECT_TRUE(lexer.ReadIdent(&ident));
49  EXPECT_EQ("baz_123", ident);
50  EXPECT_TRUE(lexer.ReadIdent(&ident));
51  EXPECT_EQ("foo-bar", ident);
52 }
53 
54 TEST(Lexer, ReadIdentCurlies) {
55  // Verify that ReadIdent includes dots in the name,
56  // but in an expansion $bar.dots stops at the dot.
57  Lexer lexer("foo.dots $bar.dots ${bar.dots}\n");
58  string ident;
59  EXPECT_TRUE(lexer.ReadIdent(&ident));
60  EXPECT_EQ("foo.dots", ident);
61 
62  EvalString eval;
63  string err;
64  EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
65  EXPECT_EQ("", err);
66  EXPECT_EQ("[$bar][.dots ][$bar.dots]",
67  eval.Serialize());
68 }
69 
71  Lexer lexer("foo$\nbad $");
72  EvalString eval;
73  string err;
74  ASSERT_FALSE(lexer.ReadVarValue(&eval, &err));
75  EXPECT_EQ("input:2: bad $-escape (literal $ must be written as $$)\n"
76  "bad $\n"
77  " ^ near here"
78  , err);
79 }
80 
81 TEST(Lexer, CommentEOF) {
82  // Verify we don't run off the end of the string when the EOF is
83  // mid-comment.
84  Lexer lexer("# foo");
85  Lexer::Token token = lexer.ReadToken();
86  EXPECT_EQ(Lexer::ERROR, token);
87 }
88 
89 TEST(Lexer, Tabs) {
90  // Verify we print a useful error on a disallowed character.
91  Lexer lexer(" \tfoobar");
92  Lexer::Token token = lexer.ReadToken();
93  EXPECT_EQ(Lexer::INDENT, token);
94  token = lexer.ReadToken();
95  EXPECT_EQ(Lexer::ERROR, token);
96  EXPECT_EQ("tabs are not allowed, use spaces", lexer.DescribeLastError());
97 }
Definition: lexer.h:27
TEST(Lexer, ReadVarValue)
Definition: lexer_test.cc:21
Token ReadToken()
Read a Token from the Token enum.
Definition: lexer.cc:119
string Serialize() const
Construct a human-readable representation of the parsed state, for use in tests.
Definition: eval_env.cc:69
string DescribeLastError()
If the last token read was an ERROR token, provide more info or the empty string. ...
Definition: lexer.cc:103
Token
Definition: lexer.h:32
bool ReadIdent(string *out)
Read a simple identifier (a rule or variable name).
Definition: lexer.cc:509
bool ReadVarValue(EvalString *value, string *err)
Read the value side of a var = value line (complete with $escapes).
Definition: lexer.h:85
A tokenized string that contains variable references.
Definition: eval_env.h:59
void Error(const char *msg,...)
Log an error message.
Definition: util.cc:78