diff options
author | Thiago Farina <tfarina@chromium.org> | 2011-09-09 02:51:53 (GMT) |
---|---|---|
committer | Thiago Farina <tfarina@chromium.org> | 2011-09-09 02:51:53 (GMT) |
commit | 50d9bd7a63820c220722e0d4be5e4320fdc088a4 (patch) | |
tree | b2d33c96d0e2cdd34d6ddc037c5c7a40e015d638 /src/eval_env_test.cc | |
parent | f782babf8f943725b68f0e26cfbeaedbabe77477 (diff) | |
download | Ninja-50d9bd7a63820c220722e0d4be5e4320fdc088a4.zip Ninja-50d9bd7a63820c220722e0d4be5e4320fdc088a4.tar.gz Ninja-50d9bd7a63820c220722e0d4be5e4320fdc088a4.tar.bz2 |
Move EvalString tests to eval_env_test.cc
Signed-off-by: Thiago Farina <tfarina@chromium.org>
Diffstat (limited to 'src/eval_env_test.cc')
-rw-r--r-- | src/eval_env_test.cc | 101 |
1 files changed, 101 insertions, 0 deletions
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 <gtest/gtest.h> + +#include <map> +#include <string> + +#include "eval_env.h" + +namespace { + +struct TestEnv : public Env { + virtual string LookupVariable(const string& var) { + return vars[var]; + } + map<string, string> 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 |