|
13 | 13 | #include "llvm/Config/config.h"
|
14 | 14 | #include "llvm/Support/FileSystem.h"
|
15 | 15 | #include "llvm/Support/InitLLVM.h"
|
| 16 | +#include "llvm/Support/MemoryBuffer.h" |
16 | 17 | #include "llvm/Support/Path.h"
|
17 | 18 | #include "llvm/Support/Program.h"
|
18 | 19 | #include "llvm/Support/StringSaver.h"
|
@@ -839,4 +840,137 @@ TEST(CommandLineTest, GetCommandLineArguments) {
|
839 | 840 | }
|
840 | 841 | #endif
|
841 | 842 |
|
| 843 | +class OutputRedirector { |
| 844 | +public: |
| 845 | + OutputRedirector(int RedirectFD) |
| 846 | + : RedirectFD(RedirectFD), OldFD(dup(RedirectFD)) { |
| 847 | + if (OldFD == -1 || |
| 848 | + sys::fs::createTemporaryFile("unittest-redirect", "", NewFD, |
| 849 | + FilePath) || |
| 850 | + dup2(NewFD, RedirectFD) == -1) |
| 851 | + Valid = false; |
| 852 | + } |
| 853 | + |
| 854 | + ~OutputRedirector() { |
| 855 | + dup2(OldFD, RedirectFD); |
| 856 | + close(OldFD); |
| 857 | + close(NewFD); |
| 858 | + } |
| 859 | + |
| 860 | + SmallVector<char, 128> FilePath; |
| 861 | + bool Valid = true; |
| 862 | + |
| 863 | +private: |
| 864 | + int RedirectFD; |
| 865 | + int OldFD; |
| 866 | + int NewFD; |
| 867 | +}; |
| 868 | + |
| 869 | +struct AutoDeleteFile { |
| 870 | + SmallVector<char, 128> FilePath; |
| 871 | + ~AutoDeleteFile() { |
| 872 | + if (!FilePath.empty()) |
| 873 | + sys::fs::remove(std::string(FilePath.data(), FilePath.size())); |
| 874 | + } |
| 875 | +}; |
| 876 | + |
| 877 | +class PrintOptionInfoTest : public ::testing::Test { |
| 878 | +public: |
| 879 | + // Return std::string because the output of a failing EXPECT check is |
| 880 | + // unreadable for StringRef. It also avoids any lifetime issues. |
| 881 | + template <typename... Ts> std::string runTest(Ts... OptionAttributes) { |
| 882 | + AutoDeleteFile File; |
| 883 | + { |
| 884 | + OutputRedirector Stdout(fileno(stdout)); |
| 885 | + if (!Stdout.Valid) |
| 886 | + return ""; |
| 887 | + File.FilePath = Stdout.FilePath; |
| 888 | + |
| 889 | + StackOption<OptionValue> TestOption(Opt, cl::desc(HelpText), |
| 890 | + OptionAttributes...); |
| 891 | + printOptionInfo(TestOption, 25); |
| 892 | + outs().flush(); |
| 893 | + } |
| 894 | + auto Buffer = MemoryBuffer::getFile(File.FilePath); |
| 895 | + if (!Buffer) |
| 896 | + return ""; |
| 897 | + return Buffer->get()->getBuffer().str(); |
| 898 | + } |
| 899 | + |
| 900 | + enum class OptionValue { Val }; |
| 901 | + const StringRef Opt = "some-option"; |
| 902 | + const StringRef HelpText = "some help"; |
| 903 | + |
| 904 | +private: |
| 905 | + // This is a workaround for cl::Option sub-classes having their |
| 906 | + // printOptionInfo functions private. |
| 907 | + void printOptionInfo(const cl::Option &O, size_t Width) { |
| 908 | + O.printOptionInfo(Width); |
| 909 | + } |
| 910 | +}; |
| 911 | + |
| 912 | +TEST_F(PrintOptionInfoTest, PrintOptionInfoValueOptionalWithoutSentinel) { |
| 913 | + std::string Output = |
| 914 | + runTest(cl::ValueOptional, |
| 915 | + cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"))); |
| 916 | + |
| 917 | + // clang-format off |
| 918 | + EXPECT_EQ(Output, (" -" + Opt + "=<value> - " + HelpText + "\n" |
| 919 | + " =v1 - desc1\n") |
| 920 | + .str()); |
| 921 | + // clang-format on |
| 922 | +} |
| 923 | + |
| 924 | +TEST_F(PrintOptionInfoTest, PrintOptionInfoValueOptionalWithSentinel) { |
| 925 | + std::string Output = runTest( |
| 926 | + cl::ValueOptional, cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"), |
| 927 | + clEnumValN(OptionValue::Val, "", ""))); |
| 928 | + |
| 929 | + // clang-format off |
| 930 | + EXPECT_EQ(Output, |
| 931 | + (" -" + Opt + " - " + HelpText + "\n" |
| 932 | + " -" + Opt + "=<value> - " + HelpText + "\n" |
| 933 | + " =v1 - desc1\n") |
| 934 | + .str()); |
| 935 | + // clang-format on |
| 936 | +} |
| 937 | + |
| 938 | +TEST_F(PrintOptionInfoTest, PrintOptionInfoValueOptionalWithSentinelWithHelp) { |
| 939 | + std::string Output = runTest( |
| 940 | + cl::ValueOptional, cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"), |
| 941 | + clEnumValN(OptionValue::Val, "", "desc2"))); |
| 942 | + |
| 943 | + // clang-format off |
| 944 | + EXPECT_EQ(Output, (" -" + Opt + " - " + HelpText + "\n" |
| 945 | + " -" + Opt + "=<value> - " + HelpText + "\n" |
| 946 | + " =v1 - desc1\n" |
| 947 | + " =<empty> - desc2\n") |
| 948 | + .str()); |
| 949 | + // clang-format on |
| 950 | +} |
| 951 | + |
| 952 | +TEST_F(PrintOptionInfoTest, PrintOptionInfoValueRequiredWithEmptyValueName) { |
| 953 | + std::string Output = runTest( |
| 954 | + cl::ValueRequired, cl::values(clEnumValN(OptionValue::Val, "v1", "desc1"), |
| 955 | + clEnumValN(OptionValue::Val, "", ""))); |
| 956 | + |
| 957 | + // clang-format off |
| 958 | + EXPECT_EQ(Output, (" -" + Opt + "=<value> - " + HelpText + "\n" |
| 959 | + " =v1 - desc1\n" |
| 960 | + " =<empty>\n") |
| 961 | + .str()); |
| 962 | + // clang-format on |
| 963 | +} |
| 964 | + |
| 965 | +TEST_F(PrintOptionInfoTest, PrintOptionInfoEmptyValueDescription) { |
| 966 | + std::string Output = runTest( |
| 967 | + cl::ValueRequired, cl::values(clEnumValN(OptionValue::Val, "v1", ""))); |
| 968 | + |
| 969 | + // clang-format off |
| 970 | + EXPECT_EQ(Output, |
| 971 | + (" -" + Opt + "=<value> - " + HelpText + "\n" |
| 972 | + " =v1\n").str()); |
| 973 | + // clang-format on |
| 974 | +} |
| 975 | + |
842 | 976 | } // anonymous namespace
|
0 commit comments