1 """Test the go expression parser/interpreter."""
7 from lldbsuite.test.decorators import *
8 from lldbsuite.test.lldbtest import *
9 from lldbsuite.test import lldbutil
12 class TestGoUserExpression(TestBase):
14 mydir = TestBase.compute_mydir(__file__)
16 @add_test_categories(['pyapi'])
17 @skipIfRemote # Not remote test suit ready
18 @skipUnlessGoInstalled
19 def test_with_dsym_and_python_api(self):
20 """Test GoASTUserExpress."""
26 # Call super's setUp().
28 # Find the line numbers to break inside main().
29 self.main_source = "main.go"
30 self.break_line = line_number(
31 self.main_source, '// Set breakpoint here.')
33 def check_builtin(self, name, size=0, typeclass=lldb.eTypeClassBuiltin):
34 tl = self.target().FindTypes(name)
35 self.assertEqual(1, len(tl))
37 self.assertEqual(name, t.name)
38 self.assertEqual(typeclass, t.type)
40 self.assertEqual(size, t.size)
42 def launchProcess(self):
43 exe = os.path.join(os.getcwd(), "a.out")
45 target = self.dbg.CreateTarget(exe)
46 self.assertTrue(target, VALID_TARGET)
48 bpt = target.BreakpointCreateByLocation(
49 self.main_source, self.break_line)
50 self.assertTrue(bpt, VALID_BREAKPOINT)
52 # Now launch the process, and do not stop at entry point.
53 process = target.LaunchSimple(
54 None, None, self.get_process_working_directory())
56 self.assertTrue(process, PROCESS_IS_VALID)
58 # The stop reason of the thread should be breakpoint.
59 thread_list = lldbutil.get_threads_stopped_at_breakpoint(process, bpt)
61 # Make sure we stopped at the first breakpoint.
63 len(thread_list) != 0,
64 "No thread stopped at our breakpoint.")
65 self.assertTrue(len(thread_list) == 1,
66 "More than one thread stopped at our breakpoint.")
68 frame = thread_list[0].GetFrameAtIndex(0)
69 self.assertTrue(frame, "Got a valid frame 0 frame.")
71 def go_expressions(self):
73 v = frame.EvaluateExpression("1")
74 self.assertEqual(1, v.GetValueAsSigned())
75 x = frame.EvaluateExpression("x")
76 self.assertEqual(22, x.GetValueAsSigned())
78 a = frame.EvaluateExpression("a")
79 self.assertEqual(3, a.GetNumChildren())
80 a0 = a.GetChildAtIndex(0)
81 self.assertEqual(8, a0.GetValueAsSigned())
84 a0 = frame.EvaluateExpression("a[0]")
85 self.assertEqual(8, a0.GetValueAsSigned())
88 b1 = frame.EvaluateExpression("b[1]")
89 self.assertEqual(9, b1.GetValueAsSigned())
91 # Test global in this package
92 g = frame.EvaluateExpression("myGlobal")
93 self.assertEqual(17, g.GetValueAsSigned(), str(g))
95 # Global with package name
96 g = frame.EvaluateExpression("main.myGlobal")
97 self.assertEqual(17, g.GetValueAsSigned(), str(g))
99 # Global with quoted package name
100 g = frame.EvaluateExpression('"main".myGlobal')
101 self.assertEqual(17, g.GetValueAsSigned(), str(g))
103 # Casting with package local type
104 s = frame.EvaluateExpression("*(*myStruct)(i.data)")
105 sb = s.GetChildMemberWithName("a")
106 self.assertEqual(2, sb.GetValueAsSigned())
108 # casting with explicit package
109 s = frame.EvaluateExpression("*(*main.myStruct)(i.data)")
110 sb = s.GetChildMemberWithName("a")
111 self.assertEqual(2, sb.GetValueAsSigned())
113 # Casting quoted package
114 s = frame.EvaluateExpression('*(*"main".myStruct)(i.data)')
115 sb = s.GetChildMemberWithName("b")
116 self.assertEqual(-1, sb.GetValueAsSigned())
118 if __name__ == '__main__':
120 lldb.SBDebugger.Initialize()
121 atexit.register(lambda: lldb.SBDebugger.Terminate())