Imported Upstream version 1.7.1
[platform/upstream/ninja.git] / src / lexer_test.cc
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
69 TEST(Lexer, Error) {
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 }