1"""
2Test lldb data formatter subsystem.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class GlobalsDataFormatterTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "data-formatter", "data-formatter-globals")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @dsym_test
17    def test_with_dsym_and_run_command(self):
18        """Test data formatter commands."""
19        self.buildDsym()
20        self.data_formatter_commands()
21
22    @dwarf_test
23    def test_with_dwarf_and_run_command(self):
24        """Test data formatter commands."""
25        self.buildDwarf()
26        self.data_formatter_commands()
27
28    def setUp(self):
29        # Call super's setUp().
30        TestBase.setUp(self)
31        # Find the line number to break at.
32        self.line = line_number('main.cpp', '// Set break point at this line.')
33
34    def data_formatter_commands(self):
35        """Test that that file and class static variables display correctly."""
36        self.runCmd("file a.out", CURRENT_EXECUTABLE_SET)
37
38        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True)
39
40        # This is the function to remove the custom formats in order to have a
41        # clean slate for the next test case.
42        def cleanup():
43            self.runCmd('type format clear', check=False)
44            self.runCmd('type summary clear', check=False)
45
46        # Execute the cleanup function during test case tear down.
47        self.addTearDownHook(cleanup)
48
49        self.runCmd("type summary add --summary-string \"JustATest\" Point")
50
51        # Simply check we can get at global variables
52        self.expect("target variable g_point",
53            substrs = ['JustATest'])
54
55        self.expect("target variable g_point_pointer",
56            substrs = ['(Point *) g_point_pointer ='])
57
58        # Print some information about the variables
59        # (we ignore the actual values)
60        self.runCmd("type summary add --summary-string \"(x=${var.x},y=${var.y})\" Point")
61
62        self.expect("target variable g_point",
63                    substrs = ['x=',
64                               'y='])
65
66        self.expect("target variable g_point_pointer",
67                    substrs = ['(Point *) g_point_pointer ='])
68
69        # Test Python code on resulting SBValue
70        self.runCmd("type summary add --python-script \"return 'x=' + str(valobj.GetChildMemberWithName('x').GetValue());\" Point")
71
72        self.expect("target variable g_point",
73                    substrs = ['x='])
74
75        self.expect("target variable g_point_pointer",
76                    substrs = ['(Point *) g_point_pointer ='])
77
78
79if __name__ == '__main__':
80    import atexit
81    lldb.SBDebugger.Initialize()
82    atexit.register(lambda: lldb.SBDebugger.Terminate())
83    unittest2.main()
84