Ninja
lexer_test.cc
Go to the documentation of this file.
00001 // Copyright 2011 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //     http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "lexer.h"
00016 
00017 #include <gtest/gtest.h>
00018 
00019 #include "eval_env.h"
00020 
00021 TEST(Lexer, ReadVarValue) {
00022   Lexer lexer("plain text $var $VaR ${x}\n");
00023   EvalString eval;
00024   string err;
00025   EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
00026   EXPECT_EQ("", err);
00027   EXPECT_EQ("[plain text ][$var][ ][$VaR][ ][$x]",
00028             eval.Serialize());
00029 }
00030 
00031 TEST(Lexer, ReadEvalStringEscapes) {
00032   Lexer lexer("$ $$ab c$: $\ncde\n");
00033   EvalString eval;
00034   string err;
00035   EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
00036   EXPECT_EQ("", err);
00037   EXPECT_EQ("[ $ab c: cde]",
00038             eval.Serialize());
00039 }
00040 
00041 TEST(Lexer, ReadIdent) {
00042   Lexer lexer("foo baR baz_123 foo-bar");
00043   string ident;
00044   EXPECT_TRUE(lexer.ReadIdent(&ident));
00045   EXPECT_EQ("foo", ident);
00046   EXPECT_TRUE(lexer.ReadIdent(&ident));
00047   EXPECT_EQ("baR", ident);
00048   EXPECT_TRUE(lexer.ReadIdent(&ident));
00049   EXPECT_EQ("baz_123", ident);
00050   EXPECT_TRUE(lexer.ReadIdent(&ident));
00051   EXPECT_EQ("foo-bar", ident);
00052 }
00053 
00054 TEST(Lexer, ReadIdentCurlies) {
00055   // Verify that ReadIdent includes dots in the name,
00056   // but in an expansion $bar.dots stops at the dot.
00057   Lexer lexer("foo.dots $bar.dots ${bar.dots}\n");
00058   string ident;
00059   EXPECT_TRUE(lexer.ReadIdent(&ident));
00060   EXPECT_EQ("foo.dots", ident);
00061 
00062   EvalString eval;
00063   string err;
00064   EXPECT_TRUE(lexer.ReadVarValue(&eval, &err));
00065   EXPECT_EQ("", err);
00066   EXPECT_EQ("[$bar][.dots ][$bar.dots]",
00067             eval.Serialize());
00068 }
00069 
00070 TEST(Lexer, Error) {
00071   Lexer lexer("foo$\nbad $");
00072   EvalString eval;
00073   string err;
00074   ASSERT_FALSE(lexer.ReadVarValue(&eval, &err));
00075   EXPECT_EQ("input:2: bad $-escape (literal $ must be written as $$)\n"
00076             "bad $\n"
00077             "    ^ near here"
00078             , err);
00079 }
00080 
00081 TEST(Lexer, CommentEOF) {
00082   // Verify we don't run off the end of the string when the EOF is
00083   // mid-comment.
00084   Lexer lexer("# foo");
00085   Lexer::Token token = lexer.ReadToken();
00086   EXPECT_EQ(Lexer::ERROR, token);
00087 }
00088 
00089 TEST(Lexer, Tabs) {
00090   // Verify we print a useful error on a disallowed character.
00091   Lexer lexer("   \tfoobar");
00092   Lexer::Token token = lexer.ReadToken();
00093   EXPECT_EQ(Lexer::INDENT, token);
00094   token = lexer.ReadToken();
00095   EXPECT_EQ(Lexer::ERROR, token);
00096   EXPECT_EQ("tabs are not allowed, use spaces", lexer.DescribeLastError());
00097 }