1 | //===-- VariablesTest.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 "Variables.h" |
10 | #include "lldb/API/SBValue.h" |
11 | #include "lldb/API/SBValueList.h" |
12 | #include "gtest/gtest.h" |
13 | |
14 | using namespace lldb_dap; |
15 | |
16 | class VariablesTest : public ::testing::Test { |
17 | protected: |
18 | enum : bool { Permanent = true, Temporary = false }; |
19 | Variables vars; |
20 | }; |
21 | |
22 | TEST_F(VariablesTest, GetNewVariableReference_UniqueAndRanges) { |
23 | const int64_t temp1 = vars.GetNewVariableReference(is_permanent: Temporary); |
24 | const int64_t temp2 = vars.GetNewVariableReference(is_permanent: Temporary); |
25 | const int64_t perm1 = vars.GetNewVariableReference(is_permanent: Permanent); |
26 | const int64_t perm2 = vars.GetNewVariableReference(is_permanent: Permanent); |
27 | |
28 | EXPECT_NE(temp1, temp2); |
29 | EXPECT_NE(perm1, perm2); |
30 | EXPECT_LT(temp1, perm1); |
31 | EXPECT_LT(temp2, perm1); |
32 | } |
33 | |
34 | TEST_F(VariablesTest, InsertAndGetVariable_Temporary) { |
35 | lldb::SBValue dummy; |
36 | const int64_t ref = vars.InsertVariable(variable: dummy, is_permanent: Temporary); |
37 | lldb::SBValue out = vars.GetVariable(var_ref: ref); |
38 | |
39 | EXPECT_EQ(out.IsValid(), dummy.IsValid()); |
40 | } |
41 | |
42 | TEST_F(VariablesTest, InsertAndGetVariable_Permanent) { |
43 | lldb::SBValue dummy; |
44 | const int64_t ref = vars.InsertVariable(variable: dummy, is_permanent: Permanent); |
45 | lldb::SBValue out = vars.GetVariable(var_ref: ref); |
46 | |
47 | EXPECT_EQ(out.IsValid(), dummy.IsValid()); |
48 | } |
49 | |
50 | TEST_F(VariablesTest, IsPermanentVariableReference) { |
51 | const int64_t perm = vars.GetNewVariableReference(is_permanent: Permanent); |
52 | const int64_t temp = vars.GetNewVariableReference(is_permanent: Temporary); |
53 | |
54 | EXPECT_TRUE(Variables::IsPermanentVariableReference(perm)); |
55 | EXPECT_FALSE(Variables::IsPermanentVariableReference(temp)); |
56 | } |
57 | |
58 | TEST_F(VariablesTest, Clear_RemovesTemporaryKeepsPermanent) { |
59 | lldb::SBValue dummy; |
60 | const int64_t temp = vars.InsertVariable(variable: dummy, is_permanent: Temporary); |
61 | const int64_t perm = vars.InsertVariable(variable: dummy, is_permanent: Permanent); |
62 | vars.Clear(); |
63 | |
64 | EXPECT_FALSE(vars.GetVariable(temp).IsValid()); |
65 | EXPECT_EQ(vars.GetVariable(perm).IsValid(), dummy.IsValid()); |
66 | } |
67 | |
68 | TEST_F(VariablesTest, GetTopLevelScope_ReturnsCorrectScope) { |
69 | vars.locals.Append(val_obj: lldb::SBValue()); |
70 | vars.globals.Append(val_obj: lldb::SBValue()); |
71 | vars.registers.Append(val_obj: lldb::SBValue()); |
72 | |
73 | EXPECT_EQ(vars.GetTopLevelScope(VARREF_LOCALS), &vars.locals); |
74 | EXPECT_EQ(vars.GetTopLevelScope(VARREF_GLOBALS), &vars.globals); |
75 | EXPECT_EQ(vars.GetTopLevelScope(VARREF_REGS), &vars.registers); |
76 | EXPECT_EQ(vars.GetTopLevelScope(9999), nullptr); |
77 | } |
78 | |
79 | TEST_F(VariablesTest, FindVariable_LocalsByName) { |
80 | lldb::SBValue dummy; |
81 | vars.locals.Append(val_obj: dummy); |
82 | lldb::SBValue found = vars.FindVariable(VARREF_LOCALS, name: "" ); |
83 | |
84 | EXPECT_EQ(found.IsValid(), dummy.IsValid()); |
85 | } |
86 | |