1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 TEST(Lexer, ReadVarValue) {
21 Lexer lexer("plain text $var $VaR ${x}\n");
24 EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
26 EXPECT_EQ("[plain text ][$var][ ][$VaR][ ][$x]",
30 TEST(Lexer, ReadEvalStringEscapes) {
31 Lexer lexer("$ $$ab c$: $\ncde\n");
34 EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
36 EXPECT_EQ("[ $ab c: cde]",
40 TEST(Lexer, ReadIdent) {
41 Lexer lexer("foo baR baz_123 foo-bar");
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);
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");
58 EXPECT_TRUE(lexer.ReadIdent(&ident));
59 EXPECT_EQ("foo.dots", ident);
63 EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
65 EXPECT_EQ("[$bar][.dots ][$bar.dots]",
70 Lexer lexer("foo$\nbad $");
73 ASSERT_FALSE(lexer.ReadVarValue(&eval, &err));
74 EXPECT_EQ("input:2: bad $-escape (literal $ must be written as $$)\n"
80 TEST(Lexer, CommentEOF) {
81 // Verify we don't run off the end of the string when the EOF is
84 Lexer::Token token = lexer.ReadToken();
85 EXPECT_EQ(Lexer::ERROR, token);
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());