Sort the #include lines for unittest/...
[oota-llvm.git] / unittests / Support / CommandLineTest.cpp
1 //===- llvm/unittest/Support/CommandLineTest.cpp - CommandLine tests ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/CommandLine.h"
11 #include "llvm/Config/config.h"
12 #include "gtest/gtest.h"
13 #include <stdlib.h>
14 #include <string>
15
16 using namespace llvm;
17
18 namespace {
19
20 class TempEnvVar {
21  public:
22   TempEnvVar(const char *name, const char *value)
23       : name(name) {
24     const char *old_value = getenv(name);
25     EXPECT_EQ(NULL, old_value) << old_value;
26 #if HAVE_SETENV
27     setenv(name, value, true);
28 #else
29 #   define SKIP_ENVIRONMENT_TESTS
30 #endif
31   }
32
33   ~TempEnvVar() {
34 #if HAVE_SETENV
35     // Assume setenv and unsetenv come together.
36     unsetenv(name);
37 #endif
38   }
39
40  private:
41   const char *const name;
42 };
43
44 #ifndef SKIP_ENVIRONMENT_TESTS
45
46 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
47
48 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
49 TEST(CommandLineTest, ParseEnvironment) {
50   TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
51   EXPECT_EQ("", EnvironmentTestOption);
52   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
53   EXPECT_EQ("hello", EnvironmentTestOption);
54 }
55
56 // This test used to make valgrind complain
57 // ("Conditional jump or move depends on uninitialised value(s)")
58 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
59   // Put cl::opt on stack to check for proper initialization of fields.
60   cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
61   TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
62   EXPECT_EQ("", EnvironmentTestOptionLocal);
63   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
64   EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
65 }
66
67 #endif  // SKIP_ENVIRONMENT_TESTS
68
69 }  // anonymous namespace