1 //===-- ArgsTest.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 "lldb/Interpreter/OptionValueFileColonLine.h"
10 #include "lldb/Utility/FileSpec.h"
11 #include "lldb/Utility/Status.h"
12 #include "gtest/gtest.h"
13 
14 using namespace lldb_private;
15 
16 void CheckSetting(const char *input, bool success, const char *path = nullptr,
17                   uint32_t line_number = LLDB_INVALID_ADDRESS,
18                   uint32_t column_number = 0) {
19 
20   OptionValueFileColonLine value;
21   Status error;
22   llvm::StringRef s_ref(input);
23   error = value.SetValueFromString(s_ref);
24   ASSERT_EQ(error.Success(), success);
25 
26   // If we were meant to fail, we don't need to do more checks:
27   if (!success)
28     return;
29 
30   ASSERT_EQ(value.GetLineNumber(), line_number);
31   ASSERT_EQ(value.GetColumnNumber(), column_number);
32   std::string value_path = value.GetFileSpec().GetPath();
33   ASSERT_STREQ(value_path.c_str(), path);
34 }
35 
36 TEST(OptionValueFileColonLine, setFromString) {
37   OptionValueFileColonLine value;
38   Status error;
39 
40   // Make sure a default constructed value is invalid:
41   ASSERT_EQ(value.GetLineNumber(), LLDB_INVALID_LINE_NUMBER);
42   ASSERT_EQ(value.GetColumnNumber(), 0);
43   ASSERT_FALSE(value.GetFileSpec());
44 
45   // Make sure it is an error to pass a specifier with no line number:
46   CheckSetting("foo.c", false);
47 
48   // Now try with just a file & line:
49   CheckSetting("foo.c:12", true, "foo.c", 12);
50   CheckSetting("foo.c:12:20", true, "foo.c", 12, 20);
51   // Make sure a colon doesn't mess us up:
52   CheckSetting("foo:bar.c:12", true, "foo:bar.c", 12);
53   CheckSetting("foo:bar.c:12:20", true, "foo:bar.c", 12, 20);
54   // Try errors in the line number:
55   CheckSetting("foo.c:12c", false);
56   CheckSetting("foo.c:12:20c", false);
57 }
58