From 50d9bd7a63820c220722e0d4be5e4320fdc088a4 Mon Sep 17 00:00:00 2001 From: Thiago Farina Date: Thu, 8 Sep 2011 23:51:53 -0300 Subject: Move EvalString tests to eval_env_test.cc Signed-off-by: Thiago Farina --- configure.py | 4 +- src/eval_env_test.cc | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ninja_test.cc | 73 ------------------------------------- 3 files changed, 103 insertions(+), 75 deletions(-) create mode 100644 src/eval_env_test.cc diff --git a/configure.py b/configure.py index b680a8c..8d9caca 100755 --- a/configure.py +++ b/configure.py @@ -173,8 +173,8 @@ else: objs = [] for name in ['build_test', 'build_log_test', 'disk_interface_test', - 'graph_test', 'ninja_test', 'parsers_test', 'subprocess_test', - 'util_test', 'clean_test', 'test']: + 'eval_env_test', 'graph_test', 'ninja_test', 'parsers_test', + 'subprocess_test', 'util_test', 'clean_test', 'test']: objs += cxx(name, variables=[('cflags', test_cflags)]) if platform != 'mingw': diff --git a/src/eval_env_test.cc b/src/eval_env_test.cc new file mode 100644 index 0000000..4836e24 --- /dev/null +++ b/src/eval_env_test.cc @@ -0,0 +1,101 @@ +// Copyright 2011 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include + +#include +#include + +#include "eval_env.h" + +namespace { + +struct TestEnv : public Env { + virtual string LookupVariable(const string& var) { + return vars[var]; + } + map vars; +}; + +TEST(EvalString, PlainText) { + EvalString str; + string err; + EXPECT_TRUE(str.Parse("plain text", &err)); + EXPECT_EQ("", err); + EXPECT_EQ("plain text", str.Evaluate(NULL)); +} + +TEST(EvalString, OneVariable) { + EvalString str; + string err; + EXPECT_TRUE(str.Parse("hi $var", &err)); + EXPECT_EQ("", err); + EXPECT_EQ("hi $var", str.unparsed()); + TestEnv env; + EXPECT_EQ("hi ", str.Evaluate(&env)); + env.vars["var"] = "there"; + EXPECT_EQ("hi there", str.Evaluate(&env)); +} + +TEST(EvalString, OneVariableUpperCase) { + EvalString str; + string err; + EXPECT_TRUE(str.Parse("hi $VaR", &err)); + EXPECT_EQ("", err); + EXPECT_EQ("hi $VaR", str.unparsed()); + TestEnv env; + EXPECT_EQ("hi ", str.Evaluate(&env)); + env.vars["VaR"] = "there"; + EXPECT_EQ("hi there", str.Evaluate(&env)); +} + +TEST(EvalString, Error) { + EvalString str; + string err; + size_t err_index; + EXPECT_FALSE(str.Parse("bad $", &err, &err_index)); + EXPECT_EQ("expected variable after $", err); + EXPECT_EQ(5u, err_index); +} +TEST(EvalString, CurlyError) { + EvalString str; + string err; + size_t err_index; + EXPECT_FALSE(str.Parse("bad ${bar", &err, &err_index)); + EXPECT_EQ("expected closing curly after ${", err); + EXPECT_EQ(9u, err_index); +} + +TEST(EvalString, Curlies) { + EvalString str; + string err; + EXPECT_TRUE(str.Parse("foo ${var}baz", &err)); + EXPECT_EQ("", err); + TestEnv env; + EXPECT_EQ("foo baz", str.Evaluate(&env)); + env.vars["var"] = "barbar"; + EXPECT_EQ("foo barbarbaz", str.Evaluate(&env)); +} + +TEST(EvalString, Dollars) { + EvalString str; + string err; + EXPECT_TRUE(str.Parse("foo$$bar$bar", &err)); + ASSERT_EQ("", err); + TestEnv env; + env.vars["bar"] = "baz"; + EXPECT_EQ("foo$barbaz", str.Evaluate(&env)); +} + +} // namespace diff --git a/src/ninja_test.cc b/src/ninja_test.cc index c1999d9..7337606 100644 --- a/src/ninja_test.cc +++ b/src/ninja_test.cc @@ -43,77 +43,6 @@ TEST(State, Basic) { EXPECT_FALSE(state.GetNode("out")->dirty()); } -struct TestEnv : public Env { - virtual string LookupVariable(const string& var) { - return vars[var]; - } - map vars; -}; -TEST(EvalString, PlainText) { - EvalString str; - string err; - EXPECT_TRUE(str.Parse("plain text", &err)); - EXPECT_EQ("", err); - EXPECT_EQ("plain text", str.Evaluate(NULL)); -} -TEST(EvalString, OneVariable) { - EvalString str; - string err; - EXPECT_TRUE(str.Parse("hi $var", &err)); - EXPECT_EQ("", err); - EXPECT_EQ("hi $var", str.unparsed()); - TestEnv env; - EXPECT_EQ("hi ", str.Evaluate(&env)); - env.vars["var"] = "there"; - EXPECT_EQ("hi there", str.Evaluate(&env)); -} -TEST(EvalString, OneVariableUpperCase) { - EvalString str; - string err; - EXPECT_TRUE(str.Parse("hi $VaR", &err)); - EXPECT_EQ("", err); - EXPECT_EQ("hi $VaR", str.unparsed()); - TestEnv env; - EXPECT_EQ("hi ", str.Evaluate(&env)); - env.vars["VaR"] = "there"; - EXPECT_EQ("hi there", str.Evaluate(&env)); -} -TEST(EvalString, Error) { - EvalString str; - string err; - size_t err_index; - EXPECT_FALSE(str.Parse("bad $", &err, &err_index)); - EXPECT_EQ("expected variable after $", err); - EXPECT_EQ(5u, err_index); -} -TEST(EvalString, CurlyError) { - EvalString str; - string err; - size_t err_index; - EXPECT_FALSE(str.Parse("bad ${bar", &err, &err_index)); - EXPECT_EQ("expected closing curly after ${", err); - EXPECT_EQ(9u, err_index); -} -TEST(EvalString, Curlies) { - EvalString str; - string err; - EXPECT_TRUE(str.Parse("foo ${var}baz", &err)); - EXPECT_EQ("", err); - TestEnv env; - EXPECT_EQ("foo baz", str.Evaluate(&env)); - env.vars["var"] = "barbar"; - EXPECT_EQ("foo barbarbaz", str.Evaluate(&env)); -} -TEST(EvalString, Dollars) { - EvalString str; - string err; - EXPECT_TRUE(str.Parse("foo$$bar$bar", &err)); - ASSERT_EQ("", err); - TestEnv env; - env.vars["bar"] = "baz"; - EXPECT_EQ("foo$barbaz", str.Evaluate(&env)); -} - struct StatTest : public StateTestWithBuiltinRules, public DiskInterface { // DiskInterface implementation. @@ -210,5 +139,3 @@ TEST_F(StatTest, Middle) { ASSERT_TRUE(GetNode("mid")->dirty_); ASSERT_TRUE(GetNode("out")->dirty_); } - - -- cgit v0.12