environment_unittest.cc revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/environment.h"
6#include "base/scoped_ptr.h"
7#include "testing/gtest/include/gtest/gtest.h"
8#include "testing/platform_test.h"
9
10typedef PlatformTest EnvironmentTest;
11
12TEST_F(EnvironmentTest, GetVar) {
13  // Every setup should have non-empty PATH...
14  scoped_ptr<base::Environment> env(base::Environment::Create());
15  std::string env_value;
16  EXPECT_TRUE(env->GetVar("PATH", &env_value));
17  EXPECT_NE(env_value, "");
18}
19
20TEST_F(EnvironmentTest, GetVarReverse) {
21  scoped_ptr<base::Environment> env(base::Environment::Create());
22  const char* kFooUpper = "FOO";
23  const char* kFooLower = "foo";
24
25  // Set a variable in UPPER case.
26  EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
27
28  // And then try to get this variable passing the lower case.
29  std::string env_value;
30  EXPECT_TRUE(env->GetVar(kFooLower, &env_value));
31
32  EXPECT_STREQ(env_value.c_str(), kFooLower);
33
34  EXPECT_TRUE(env->UnSetVar(kFooUpper));
35
36  const char* kBar = "bar";
37  // Now do the opposite, set the variable in the lower case.
38  EXPECT_TRUE(env->SetVar(kFooLower, kBar));
39
40  // And then try to get this variable passing the UPPER case.
41  EXPECT_TRUE(env->GetVar(kFooUpper, &env_value));
42
43  EXPECT_STREQ(env_value.c_str(), kBar);
44
45  EXPECT_TRUE(env->UnSetVar(kFooLower));
46}
47
48TEST_F(EnvironmentTest, HasVar) {
49  // Every setup should have PATH...
50  scoped_ptr<base::Environment> env(base::Environment::Create());
51  EXPECT_TRUE(env->HasVar("PATH"));
52}
53
54TEST_F(EnvironmentTest, SetVar) {
55  scoped_ptr<base::Environment> env(base::Environment::Create());
56
57  const char* kFooUpper = "FOO";
58  const char* kFooLower = "foo";
59  EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
60
61  // Now verify that the environment has the new variable.
62  EXPECT_TRUE(env->HasVar(kFooUpper));
63
64  std::string var_value;
65  EXPECT_TRUE(env->GetVar(kFooUpper, &var_value));
66  EXPECT_EQ(var_value, kFooLower);
67}
68
69TEST_F(EnvironmentTest, UnSetVar) {
70  scoped_ptr<base::Environment> env(base::Environment::Create());
71
72  const char* kFooUpper = "FOO";
73  const char* kFooLower = "foo";
74  // First set some environment variable.
75  EXPECT_TRUE(env->SetVar(kFooUpper, kFooLower));
76
77  // Now verify that the environment has the new variable.
78  EXPECT_TRUE(env->HasVar(kFooUpper));
79
80  // Finally verify that the environment variable was erased.
81  EXPECT_TRUE(env->UnSetVar(kFooUpper));
82
83  // And check that the variable has been unset.
84  EXPECT_FALSE(env->HasVar(kFooUpper));
85}
86