1 //===-- OptionsWithRawTest.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/Args.h"
12 #include "lldb/Utility/StringList.h"
13 
14 using namespace lldb_private;
15 
TEST(OptionsWithRawTest,EmptyInput)16 TEST(OptionsWithRawTest, EmptyInput) {
17   // An empty string is just an empty suffix without any arguments.
18   OptionsWithRaw args("");
19   ASSERT_FALSE(args.HasArgs());
20   ASSERT_STREQ(args.GetRawPart().c_str(), "");
21 }
22 
TEST(OptionsWithRawTest,SingleWhitespaceInput)23 TEST(OptionsWithRawTest, SingleWhitespaceInput) {
24   // Only whitespace is just a suffix.
25   OptionsWithRaw args(" ");
26   ASSERT_FALSE(args.HasArgs());
27   ASSERT_STREQ(args.GetRawPart().c_str(), " ");
28 }
29 
TEST(OptionsWithRawTest,WhitespaceInput)30 TEST(OptionsWithRawTest, WhitespaceInput) {
31   // Only whitespace is just a suffix.
32   OptionsWithRaw args("  ");
33   ASSERT_FALSE(args.HasArgs());
34   ASSERT_STREQ(args.GetRawPart().c_str(), "  ");
35 }
36 
TEST(OptionsWithRawTest,ArgsButNoDelimiter)37 TEST(OptionsWithRawTest, ArgsButNoDelimiter) {
38   // This counts as a suffix because there is no -- at the end.
39   OptionsWithRaw args("-foo bar");
40   ASSERT_FALSE(args.HasArgs());
41   ASSERT_STREQ(args.GetRawPart().c_str(), "-foo bar");
42 }
43 
TEST(OptionsWithRawTest,ArgsButNoLeadingDash)44 TEST(OptionsWithRawTest, ArgsButNoLeadingDash) {
45   // No leading dash means we have no arguments.
46   OptionsWithRaw args("foo bar --");
47   ASSERT_FALSE(args.HasArgs());
48   ASSERT_STREQ(args.GetRawPart().c_str(), "foo bar --");
49 }
50 
TEST(OptionsWithRawTest,QuotedSuffix)51 TEST(OptionsWithRawTest, QuotedSuffix) {
52   // We need to have a way to escape the -- to make it usable as an argument.
53   OptionsWithRaw args("-foo \"--\" bar");
54   ASSERT_FALSE(args.HasArgs());
55   ASSERT_STREQ(args.GetRawPart().c_str(), "-foo \"--\" bar");
56 }
57 
TEST(OptionsWithRawTest,EmptySuffix)58 TEST(OptionsWithRawTest, EmptySuffix) {
59   // An empty suffix with arguments.
60   OptionsWithRaw args("-foo --");
61   ASSERT_TRUE(args.HasArgs());
62   ASSERT_EQ(args.GetArgString(), "-foo ");
63   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo --");
64 
65   auto ref = args.GetArgs().GetArgumentArrayRef();
66   ASSERT_EQ(1u, ref.size());
67   EXPECT_STREQ("-foo", ref[0]);
68 
69   ASSERT_STREQ(args.GetRawPart().c_str(), "");
70 }
71 
TEST(OptionsWithRawTest,EmptySuffixSingleWhitespace)72 TEST(OptionsWithRawTest, EmptySuffixSingleWhitespace) {
73   // A single whitespace also countas as an empty suffix (because that usually
74   // separates the suffix from the double dash.
75   OptionsWithRaw args("-foo -- ");
76   ASSERT_TRUE(args.HasArgs());
77   ASSERT_EQ(args.GetArgString(), "-foo ");
78   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo -- ");
79 
80   auto ref = args.GetArgs().GetArgumentArrayRef();
81   ASSERT_EQ(1u, ref.size());
82   EXPECT_STREQ("-foo", ref[0]);
83 
84   ASSERT_STREQ(args.GetRawPart().c_str(), "");
85 }
86 
TEST(OptionsWithRawTest,WhitespaceSuffix)87 TEST(OptionsWithRawTest, WhitespaceSuffix) {
88   // A single whtiespace character as a suffix.
89   OptionsWithRaw args("-foo --  ");
90   ASSERT_TRUE(args.HasArgs());
91   ASSERT_EQ(args.GetArgString(), "-foo ");
92   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo -- ");
93 
94   auto ref = args.GetArgs().GetArgumentArrayRef();
95   ASSERT_EQ(1u, ref.size());
96   EXPECT_STREQ("-foo", ref[0]);
97 
98   ASSERT_STREQ(args.GetRawPart().c_str(), " ");
99 }
100 
TEST(OptionsWithRawTest,LeadingSpaceArgs)101 TEST(OptionsWithRawTest, LeadingSpaceArgs) {
102   // Whitespace before the first dash needs to be ignored.
103   OptionsWithRaw args(" -foo -- bar");
104   ASSERT_TRUE(args.HasArgs());
105   ASSERT_EQ(args.GetArgString(), " -foo ");
106   ASSERT_EQ(args.GetArgStringWithDelimiter(), " -foo -- ");
107 
108   auto ref = args.GetArgs().GetArgumentArrayRef();
109   ASSERT_EQ(1u, ref.size());
110   EXPECT_STREQ("-foo", ref[0]);
111 
112   ASSERT_STREQ(args.GetRawPart().c_str(), "bar");
113 }
114 
TEST(OptionsWithRawTest,SingleWordSuffix)115 TEST(OptionsWithRawTest, SingleWordSuffix) {
116   // A single word as a suffix.
117   OptionsWithRaw args("-foo -- bar");
118   ASSERT_TRUE(args.HasArgs());
119   ASSERT_EQ(args.GetArgString(), "-foo ");
120   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo -- ");
121 
122   auto ref = args.GetArgs().GetArgumentArrayRef();
123   ASSERT_EQ(1u, ref.size());
124   EXPECT_STREQ("-foo", ref[0]);
125 
126   ASSERT_STREQ(args.GetRawPart().c_str(), "bar");
127 }
128 
TEST(OptionsWithRawTest,MultiWordSuffix)129 TEST(OptionsWithRawTest, MultiWordSuffix) {
130   // Multiple words as a suffix.
131   OptionsWithRaw args("-foo -- bar baz");
132   ASSERT_TRUE(args.HasArgs());
133   ASSERT_EQ(args.GetArgString(), "-foo ");
134   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo -- ");
135 
136   auto ref = args.GetArgs().GetArgumentArrayRef();
137   ASSERT_EQ(1u, ref.size());
138   EXPECT_STREQ("-foo", ref[0]);
139 
140   ASSERT_STREQ(args.GetRawPart().c_str(), "bar baz");
141 }
142 
TEST(OptionsWithRawTest,UnterminatedQuote)143 TEST(OptionsWithRawTest, UnterminatedQuote) {
144   // A quote character in the suffix shouldn't influence the parsing.
145   OptionsWithRaw args("-foo -- bar \" ");
146   ASSERT_TRUE(args.HasArgs());
147   ASSERT_EQ(args.GetArgString(), "-foo ");
148   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo -- ");
149 
150   auto ref = args.GetArgs().GetArgumentArrayRef();
151   ASSERT_EQ(1u, ref.size());
152   EXPECT_STREQ("-foo", ref[0]);
153 
154   ASSERT_STREQ(args.GetRawPart().c_str(), "bar \" ");
155 }
156 
TEST(OptionsWithRawTest,TerminatedQuote)157 TEST(OptionsWithRawTest, TerminatedQuote) {
158   // A part of the suffix is quoted, which shouldn't influence the parsing.
159   OptionsWithRaw args("-foo -- bar \"a\" ");
160   ASSERT_TRUE(args.HasArgs());
161   ASSERT_EQ(args.GetArgString(), "-foo ");
162   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-foo -- ");
163 
164   auto ref = args.GetArgs().GetArgumentArrayRef();
165   ASSERT_EQ(1u, ref.size());
166   EXPECT_STREQ("-foo", ref[0]);
167 
168   ASSERT_STREQ(args.GetRawPart().c_str(), "bar \"a\" ");
169 }
170 
TEST(OptionsWithRawTest,EmptyArgsOnlySuffix)171 TEST(OptionsWithRawTest, EmptyArgsOnlySuffix) {
172   // Empty argument list, but we have a suffix.
173   OptionsWithRaw args("-- bar");
174   ASSERT_TRUE(args.HasArgs());
175   ASSERT_EQ(args.GetArgString(), "");
176   ASSERT_EQ(args.GetArgStringWithDelimiter(), "-- ");
177 
178   auto ref = args.GetArgs().GetArgumentArrayRef();
179   ASSERT_EQ(0u, ref.size());
180 
181   ASSERT_STREQ(args.GetRawPart().c_str(), "bar");
182 }
183