02b8162aad656a5dc12f251f95baf6cbe0e6502c
[openbsd] /
1 """
2 Tests expression evaluation in context of an object.
3 """
4
5 import lldb
6 import lldbsuite.test.lldbutil as lldbutil
7 from lldbsuite.test.lldbtest import *
8
9 class ContextObjectTestCase(TestBase):
10
11     mydir = TestBase.compute_mydir(__file__)
12
13     def test_context_object(self):
14         """Tests expression evaluation in context of an object."""
15         self.build()
16
17         (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, '// Break here', self.main_source_spec)
18         frame = thread.GetFrameAtIndex(0)
19
20         #
21         # Test C++ struct variable
22         #
23
24         obj_val = frame.FindVariable("cpp_struct")
25         self.assertTrue(obj_val.IsValid())
26
27         # Test an empty expression evaluation
28         value = obj_val.EvaluateExpression("")
29         self.assertFalse(value.IsValid())
30         self.assertFalse(value.GetError().Success())
31
32         # Test retrieveing of a field (not a local with the same name)
33         value = obj_val.EvaluateExpression("field")
34         self.assertTrue(value.IsValid())
35         self.assertTrue(value.GetError().Success())
36         self.assertEqual(value.GetValueAsSigned(), 1111)
37
38         # Test functions evaluation
39         value = obj_val.EvaluateExpression("function()")
40         self.assertTrue(value.IsValid())
41         self.assertTrue(value.GetError().Success())
42         self.assertEqual(value.GetValueAsSigned(), 2222)
43
44         # Test that we retrieve the right global
45         value = obj_val.EvaluateExpression("global.field")
46         self.assertTrue(value.IsValid())
47         self.assertTrue(value.GetError().Success())
48         self.assertEqual(value.GetValueAsSigned(), 1111)
49
50         #
51         # Test C++ union variable
52         #
53
54         obj_val = frame.FindVariable("cpp_union")
55         self.assertTrue(obj_val.IsValid())
56
57         # Test retrieveing of a field
58         value = obj_val.EvaluateExpression("field_int")
59         self.assertTrue(value.IsValid())
60         self.assertTrue(value.GetError().Success())
61         self.assertEqual(value.GetValueAsSigned(), 5555)
62
63         #
64         # Test C++ scalar
65         #
66
67         obj_val = frame.FindVariable("cpp_scalar")
68         self.assertTrue(obj_val.IsValid())
69
70         # Test an expression evaluation
71         value = obj_val.EvaluateExpression("1")
72         self.assertFalse(value.IsValid())
73         self.assertFalse(value.GetError().Success())
74
75         #
76         # Test C++ array
77         #
78
79         obj_val = frame.FindVariable("cpp_array")
80         self.assertTrue(obj_val.IsValid())
81
82         # Test an expression evaluation
83         value = obj_val.EvaluateExpression("1")
84         self.assertFalse(value.IsValid())
85         self.assertFalse(value.GetError().Success())
86
87         # Test retrieveing of an element's field
88         value = obj_val.GetValueForExpressionPath("[7]").EvaluateExpression("field")
89         self.assertTrue(value.IsValid())
90         self.assertTrue(value.GetError().Success())
91         self.assertEqual(value.GetValueAsSigned(), 1111)
92
93         #
94         # Test C++ pointer
95         #
96
97         obj_val = frame.FindVariable("cpp_pointer")
98         self.assertTrue(obj_val.IsValid())
99
100         # Test an expression evaluation
101         value = obj_val.EvaluateExpression("1")
102         self.assertFalse(value.IsValid())
103         self.assertFalse(value.GetError().Success())
104
105         # Test retrieveing of a dereferenced object's field
106         value = obj_val.Dereference().EvaluateExpression("field")
107         self.assertTrue(value.IsValid())
108         self.assertTrue(value.GetError().Success())
109         self.assertEqual(value.GetValueAsSigned(), 1111)
110
111         #
112         # Test C++ computation result
113         #
114
115         obj_val = frame.EvaluateExpression("cpp_namespace::GetCppStruct()")
116         self.assertTrue(obj_val.IsValid())
117
118         # Test an expression evaluation
119         value = obj_val.EvaluateExpression("1")
120         self.assertTrue(value.IsValid())
121         self.assertFalse(value.GetError().Success())
122
123         #
124         # Test C++ computation result located in debuggee memory
125         #
126
127         obj_val = frame.EvaluateExpression("cpp_namespace::GetCppStructPtr()")
128         self.assertTrue(obj_val.IsValid())
129
130         # Test an expression evaluation
131         value = obj_val.EvaluateExpression("1")
132         self.assertFalse(value.IsValid())
133         self.assertFalse(value.GetError().Success())
134
135         # Test retrieveing of a dereferenced object's field
136         value = obj_val.Dereference().EvaluateExpression("field")
137         self.assertTrue(value.IsValid())
138         self.assertTrue(value.GetError().Success())
139         self.assertEqual(value.GetValueAsSigned(), 1111)
140
141     def setUp(self):
142         TestBase.setUp(self)
143
144         self.main_source = "main.cpp"
145         self.main_source_spec = lldb.SBFileSpec(self.main_source)