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_LINE_NUMBER,
18                   uint32_t column_number = LLDB_INVALID_COLUMN_NUMBER) {
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(),
42             static_cast<uint32_t>(LLDB_INVALID_LINE_NUMBER));
43   ASSERT_EQ(value.GetColumnNumber(),
44             static_cast<uint32_t>(LLDB_INVALID_COLUMN_NUMBER));
45   ASSERT_FALSE(value.GetFileSpec());
46 
47   // Make sure it is an error to pass a specifier with no line number:
48   CheckSetting("foo.c", false);
49 
50   // Now try with just a file & line:
51   CheckSetting("foo.c:12", true, "foo.c", 12);
52   CheckSetting("foo.c:12:20", true, "foo.c", 12, 20);
53   // Make sure a colon doesn't mess us up:
54   CheckSetting("foo:bar.c:12", true, "foo:bar.c", 12);
55   CheckSetting("foo:bar.c:12:20", true, "foo:bar.c", 12, 20);
56   // Try errors in the line number:
57   CheckSetting("foo.c:12c", false);
58   CheckSetting("foo.c:12:20c", false);
59 }
60