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