1 | //===-- EnvironmentTest.cpp -----------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "gtest/gtest.h" |
10 | |
11 | #include "lldb/Utility/Environment.h" |
12 | |
13 | using namespace lldb_private; |
14 | |
15 | TEST(EnvironmentTest, EnvpConstruction) { |
16 | const char **Envp1 = nullptr; |
17 | EXPECT_EQ(0u, Environment(Envp1).size()); |
18 | |
19 | const char *Envp2[] = {"FOO=BAR" , nullptr}; |
20 | EXPECT_EQ("BAR" , Environment(Envp2).lookup("FOO" )); |
21 | |
22 | const char *Envp3[] = {"FOO=BAR" , "FOO=BAZ" , nullptr}; |
23 | EXPECT_EQ("BAR" , Environment(Envp3).lookup("FOO" )); |
24 | |
25 | const char *Envp4[] = {"FOO=" , "BAR" , nullptr}; |
26 | Environment Env4(Envp4); |
27 | ASSERT_EQ(2u, Env4.size()); |
28 | EXPECT_EQ("" , Environment(Envp4).find("FOO" )->second); |
29 | EXPECT_EQ("" , Environment(Envp4).find("BAR" )->second); |
30 | |
31 | const char *Envp5[] = {"FOO=BAR=BAZ" , nullptr}; |
32 | EXPECT_EQ("BAR=BAZ" , Environment(Envp5).lookup("FOO" )); |
33 | } |
34 | |
35 | TEST(EnvironmentTest, EnvpConversion) { |
36 | std::string FOO_EQ_BAR("FOO=BAR" ); |
37 | std::string BAR_EQ_BAZ("BAR=BAZ" ); |
38 | |
39 | Environment Env; |
40 | Env.insert(KeyEqValue: FOO_EQ_BAR); |
41 | Env.insert(KeyEqValue: BAR_EQ_BAZ); |
42 | Environment::Envp Envp = Env.getEnvp(); |
43 | const char *const *Envp_ = Envp; |
44 | |
45 | EXPECT_TRUE(FOO_EQ_BAR == Envp_[0] || FOO_EQ_BAR == Envp_[1]); |
46 | EXPECT_TRUE(BAR_EQ_BAZ == Envp_[0] || BAR_EQ_BAZ == Envp_[1]); |
47 | EXPECT_EQ(nullptr, Envp_[2]); |
48 | } |
49 | |