Ninja
eval_env_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 <gtest/gtest.h>
00016 
00017 #include <map>
00018 #include <string>
00019 
00020 #include "eval_env.h"
00021 
00022 namespace {
00023 
00024 struct TestEnv : public Env {
00025   virtual string LookupVariable(const string& var) {
00026     return vars[var];
00027   }
00028   map<string, string> vars;
00029 };
00030 
00031 TEST(EvalString, PlainText) {
00032   EvalString str;
00033   string err;
00034   EXPECT_TRUE(str.Parse("plain text", &err));
00035   EXPECT_EQ("", err);
00036   EXPECT_EQ("plain text", str.Evaluate(NULL));
00037 }
00038 
00039 TEST(EvalString, OneVariable) {
00040   EvalString str;
00041   string err;
00042   EXPECT_TRUE(str.Parse("hi $var", &err));
00043   EXPECT_EQ("", err);
00044   EXPECT_EQ("hi $var", str.unparsed());
00045   TestEnv env;
00046   EXPECT_EQ("hi ", str.Evaluate(&env));
00047   env.vars["var"] = "there";
00048   EXPECT_EQ("hi there", str.Evaluate(&env));
00049 }
00050 
00051 TEST(EvalString, OneVariableUpperCase) {
00052   EvalString str;
00053   string err;
00054   EXPECT_TRUE(str.Parse("hi $VaR", &err));
00055   EXPECT_EQ("", err);
00056   EXPECT_EQ("hi $VaR", str.unparsed());
00057   TestEnv env;
00058   EXPECT_EQ("hi ", str.Evaluate(&env));
00059   env.vars["VaR"] = "there";
00060   EXPECT_EQ("hi there", str.Evaluate(&env));
00061 }
00062 
00063 TEST(EvalString, Error) {
00064   EvalString str;
00065   string err;
00066   size_t err_index;
00067   EXPECT_FALSE(str.Parse("bad $", &err, &err_index));
00068   EXPECT_EQ("expected variable after $", err);
00069   EXPECT_EQ(5u, err_index);
00070 }
00071 TEST(EvalString, CurlyError) {
00072   EvalString str;
00073   string err;
00074   size_t err_index;
00075   EXPECT_FALSE(str.Parse("bad ${bar", &err, &err_index));
00076   EXPECT_EQ("expected closing curly after ${", err);
00077   EXPECT_EQ(9u, err_index);
00078 }
00079 
00080 TEST(EvalString, Curlies) {
00081   EvalString str;
00082   string err;
00083   EXPECT_TRUE(str.Parse("foo ${var}baz", &err));
00084   EXPECT_EQ("", err);
00085   TestEnv env;
00086   EXPECT_EQ("foo baz", str.Evaluate(&env));
00087   env.vars["var"] = "barbar";
00088   EXPECT_EQ("foo barbarbaz", str.Evaluate(&env));
00089 }
00090 
00091 TEST(EvalString, Dollars) {
00092   EvalString str;
00093   string err;
00094   EXPECT_TRUE(str.Parse("foo$$bar$bar", &err));
00095   ASSERT_EQ("", err);
00096   TestEnv env;
00097   env.vars["bar"] = "baz";
00098   EXPECT_EQ("foo$barbaz", str.Evaluate(&env));
00099 }
00100 
00101 }  // namespace