Remove the 'N' modifier from llvm-ar.
[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 cl::OptionCategory TestCategory("Test Options", "Description");
45 cl::opt<int> TestOption("test-option", cl::desc("old description"));
46 TEST(CommandLineTest, ModifyExisitingOption) {
47   const char Description[] = "New description";
48   const char ArgString[] = "new-test-option";
49   const char ValueString[] = "Integer";
50
51   StringMap<cl::Option*> Map;
52   cl::getRegisteredOptions(Map);
53
54   ASSERT_TRUE(Map.count("test-option") == 1) <<
55     "Could not find option in map.";
56
57   cl::Option *Retrieved = Map["test-option"];
58   ASSERT_EQ(&TestOption, Retrieved) << "Retrieved wrong option.";
59
60   ASSERT_EQ(&cl::GeneralCategory,Retrieved->Category) <<
61     "Incorrect default option category.";
62
63   Retrieved->setCategory(TestCategory);
64   ASSERT_EQ(&TestCategory,Retrieved->Category) <<
65     "Failed to modify option's option category.";
66
67   Retrieved->setDescription(Description);
68   ASSERT_STREQ(Retrieved->HelpStr, Description) <<
69     "Changing option description failed.";
70
71   Retrieved->setArgStr(ArgString);
72   ASSERT_STREQ(ArgString, Retrieved->ArgStr) <<
73     "Failed to modify option's Argument string.";
74
75   Retrieved->setValueStr(ValueString);
76   ASSERT_STREQ(Retrieved->ValueStr, ValueString) <<
77     "Failed to modify option's Value string.";
78
79   Retrieved->setHiddenFlag(cl::Hidden);
80   ASSERT_EQ(cl::Hidden, TestOption.getOptionHiddenFlag()) <<
81     "Failed to modify option's hidden flag.";
82 }
83 #ifndef SKIP_ENVIRONMENT_TESTS
84
85 const char test_env_var[] = "LLVM_TEST_COMMAND_LINE_FLAGS";
86
87 cl::opt<std::string> EnvironmentTestOption("env-test-opt");
88 TEST(CommandLineTest, ParseEnvironment) {
89   TempEnvVar TEV(test_env_var, "-env-test-opt=hello");
90   EXPECT_EQ("", EnvironmentTestOption);
91   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
92   EXPECT_EQ("hello", EnvironmentTestOption);
93 }
94
95 // This test used to make valgrind complain
96 // ("Conditional jump or move depends on uninitialised value(s)")
97 //
98 // Warning: Do not run any tests after this one that try to gain access to
99 // registered command line options because this will likely result in a
100 // SEGFAULT. This can occur because the cl::opt in the test below is declared
101 // on the stack which will be destroyed after the test completes but the
102 // command line system will still hold a pointer to a deallocated cl::Option.
103 TEST(CommandLineTest, ParseEnvironmentToLocalVar) {
104   // Put cl::opt on stack to check for proper initialization of fields.
105   cl::opt<std::string> EnvironmentTestOptionLocal("env-test-opt-local");
106   TempEnvVar TEV(test_env_var, "-env-test-opt-local=hello-local");
107   EXPECT_EQ("", EnvironmentTestOptionLocal);
108   cl::ParseEnvironmentOptions("CommandLineTest", test_env_var);
109   EXPECT_EQ("hello-local", EnvironmentTestOptionLocal);
110 }
111
112 #endif  // SKIP_ENVIRONMENT_TESTS
113
114 TEST(CommandLineTest, UseOptionCategory) {
115   cl::opt<int> TestOption2("test-option", cl::cat(TestCategory));
116
117   ASSERT_EQ(&TestCategory,TestOption2.Category) << "Failed to assign Option "
118                                                   "Category.";
119 }
120
121 }  // anonymous namespace