199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest lldb data formatter subsystem.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprechtimport lldb
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtimport lldbsuite.test.lldbutil as lldbutil
999451b44SJordan Rupprecht
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprechtclass ScriptDataFormatterTestCase(TestBase):
1299451b44SJordan Rupprecht    def test_with_run_command(self):
1399451b44SJordan Rupprecht        """Test data formatter commands."""
1499451b44SJordan Rupprecht        self.build()
1599451b44SJordan Rupprecht        self.data_formatter_commands()
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht    def setUp(self):
1899451b44SJordan Rupprecht        # Call super's setUp().
1999451b44SJordan Rupprecht        TestBase.setUp(self)
2099451b44SJordan Rupprecht        # Find the line number to break at.
21*2238dcc3SJonas Devlieghere        self.line = line_number("main.cpp", "// Set break point at this line.")
2299451b44SJordan Rupprecht
2399451b44SJordan Rupprecht    def data_formatter_commands(self):
2499451b44SJordan Rupprecht        """Test that that file and class static variables display correctly."""
2599451b44SJordan Rupprecht        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)
2699451b44SJordan Rupprecht
2799451b44SJordan Rupprecht        lldbutil.run_break_set_by_file_and_line(
28*2238dcc3SJonas Devlieghere            self, "main.cpp", self.line, num_expected_locations=1, loc_exact=True
29*2238dcc3SJonas Devlieghere        )
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht        self.runCmd("run", RUN_SUCCEEDED)
3299451b44SJordan Rupprecht
3399451b44SJordan Rupprecht        # The stop reason of the thread should be breakpoint.
34*2238dcc3SJonas Devlieghere        self.expect(
35*2238dcc3SJonas Devlieghere            "thread list",
36*2238dcc3SJonas Devlieghere            STOPPED_DUE_TO_BREAKPOINT,
37*2238dcc3SJonas Devlieghere            substrs=["stopped", "stop reason = breakpoint"],
38*2238dcc3SJonas Devlieghere        )
3999451b44SJordan Rupprecht
4099451b44SJordan Rupprecht        # This is the function to remove the custom formats in order to have a
4199451b44SJordan Rupprecht        # clean slate for the next test case.
4299451b44SJordan Rupprecht        def cleanup():
43*2238dcc3SJonas Devlieghere            self.runCmd("type format clear", check=False)
44*2238dcc3SJonas Devlieghere            self.runCmd("type summary clear", check=False)
4599451b44SJordan Rupprecht
4699451b44SJordan Rupprecht        # Execute the cleanup function during test case tear down.
4799451b44SJordan Rupprecht        self.addTearDownHook(cleanup)
4899451b44SJordan Rupprecht
4999451b44SJordan Rupprecht        # Set the script here to ease the formatting
50*2238dcc3SJonas Devlieghere        script = "a = valobj.GetChildMemberWithName('integer'); a_val = a.GetValue(); str = 'Hello from Python, ' + a_val + ' time'; return str + ('!' if a_val == '1' else 's!');"
5199451b44SJordan Rupprecht
52*2238dcc3SJonas Devlieghere        self.runCmd('type summary add i_am_cool --python-script "%s"' % script)
53*2238dcc3SJonas Devlieghere        self.expect("type summary list i_am_cool", substrs=[script])
5499451b44SJordan Rupprecht
55*2238dcc3SJonas Devlieghere        self.expect("frame variable one", substrs=["Hello from Python", "1 time!"])
5699451b44SJordan Rupprecht
57*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["Hello from Python", "4 times!"])
5899451b44SJordan Rupprecht
5999451b44SJordan Rupprecht        self.runCmd("n")  # skip ahead to make values change
6099451b44SJordan Rupprecht
61*2238dcc3SJonas Devlieghere        self.expect(
62*2238dcc3SJonas Devlieghere            "frame variable three",
63*2238dcc3SJonas Devlieghere            substrs=["Hello from Python, 10 times!", "Hello from Python, 4 times!"],
64*2238dcc3SJonas Devlieghere        )
6599451b44SJordan Rupprecht
6699451b44SJordan Rupprecht        self.runCmd("n")  # skip ahead to make values change
6799451b44SJordan Rupprecht
68*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["Hello from Python", "1 time!"])
6999451b44SJordan Rupprecht
70*2238dcc3SJonas Devlieghere        script = "a = valobj.GetChildMemberWithName('integer'); a_val = a.GetValue(); str = 'int says ' + a_val; return str;"
7199451b44SJordan Rupprecht
7299451b44SJordan Rupprecht        # Check that changes in the script are immediately reflected
73*2238dcc3SJonas Devlieghere        self.runCmd('type summary add i_am_cool --python-script "%s"' % script)
7499451b44SJordan Rupprecht
75*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["int says 1"])
7699451b44SJordan Rupprecht
77*2238dcc3SJonas Devlieghere        self.expect("frame variable twoptr", substrs=["int says 1"])
7899451b44SJordan Rupprecht
7999451b44SJordan Rupprecht        # Change the summary
8099451b44SJordan Rupprecht        self.runCmd(
81*2238dcc3SJonas Devlieghere            'type summary add --summary-string "int says ${var.integer}, and float says ${var.floating}" i_am_cool'
82*2238dcc3SJonas Devlieghere        )
8399451b44SJordan Rupprecht
84*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["int says 1", "and float says 2.71"])
8599451b44SJordan Rupprecht        # Try it for pointers
86*2238dcc3SJonas Devlieghere        self.expect(
87*2238dcc3SJonas Devlieghere            "frame variable twoptr", substrs=["int says 1", "and float says 2.71"]
88*2238dcc3SJonas Devlieghere        )
8999451b44SJordan Rupprecht
9099451b44SJordan Rupprecht        # Force a failure for pointers
91*2238dcc3SJonas Devlieghere        self.runCmd('type summary add i_am_cool -p --python-script "%s"' % script)
9299451b44SJordan Rupprecht
93*2238dcc3SJonas Devlieghere        self.expect(
94*2238dcc3SJonas Devlieghere            "frame variable twoptr", matching=False, substrs=["and float says 2.71"]
95*2238dcc3SJonas Devlieghere        )
9699451b44SJordan Rupprecht
97*2238dcc3SJonas Devlieghere        script = "return 'Python summary'"
9899451b44SJordan Rupprecht
9999451b44SJordan Rupprecht        self.runCmd(
100*2238dcc3SJonas Devlieghere            'type summary add --name test_summary --python-script "%s"' % script
101*2238dcc3SJonas Devlieghere        )
10299451b44SJordan Rupprecht
10399451b44SJordan Rupprecht        # attach the Python named summary to someone
104*2238dcc3SJonas Devlieghere        self.expect(
105*2238dcc3SJonas Devlieghere            "frame variable one --summary test_summary", substrs=["Python summary"]
106*2238dcc3SJonas Devlieghere        )
10799451b44SJordan Rupprecht
10899451b44SJordan Rupprecht        # should not bind to the type
109*2238dcc3SJonas Devlieghere        self.expect("frame variable two", matching=False, substrs=["Python summary"])
11099451b44SJordan Rupprecht
11199451b44SJordan Rupprecht        # and should not stick to the variable
112*2238dcc3SJonas Devlieghere        self.expect("frame variable one", matching=False, substrs=["Python summary"])
11399451b44SJordan Rupprecht
114*2238dcc3SJonas Devlieghere        self.runCmd('type summary add i_am_cool --summary-string "Text summary"')
11599451b44SJordan Rupprecht
11699451b44SJordan Rupprecht        # should be temporary only
117*2238dcc3SJonas Devlieghere        self.expect("frame variable one", matching=False, substrs=["Python summary"])
11899451b44SJordan Rupprecht
11999451b44SJordan Rupprecht        # use the type summary
120*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["Text summary"])
12199451b44SJordan Rupprecht
12299451b44SJordan Rupprecht        self.runCmd("n")  # skip ahead to make values change
12399451b44SJordan Rupprecht
12499451b44SJordan Rupprecht        # both should use the type summary now
125*2238dcc3SJonas Devlieghere        self.expect("frame variable one", substrs=["Text summary"])
12699451b44SJordan Rupprecht
127*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["Text summary"])
12899451b44SJordan Rupprecht
12999451b44SJordan Rupprecht        # disable type summary for pointers, and make a Python regex summary
130*2238dcc3SJonas Devlieghere        self.runCmd('type summary add i_am_cool -p --summary-string "Text summary"')
131*2238dcc3SJonas Devlieghere        self.runCmd('type summary add -x cool --python-script "%s"' % script)
13299451b44SJordan Rupprecht
13399451b44SJordan Rupprecht        # variables should stick to the type summary
134*2238dcc3SJonas Devlieghere        self.expect("frame variable one", substrs=["Text summary"])
13599451b44SJordan Rupprecht
136*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["Text summary"])
13799451b44SJordan Rupprecht
13899451b44SJordan Rupprecht        # array and pointer should match the Python one
139*2238dcc3SJonas Devlieghere        self.expect("frame variable twoptr", substrs=["Python summary"])
14099451b44SJordan Rupprecht
141*2238dcc3SJonas Devlieghere        self.expect("frame variable array", substrs=["Python summary"])
14299451b44SJordan Rupprecht
14399451b44SJordan Rupprecht        # return pointers to the type summary
144*2238dcc3SJonas Devlieghere        self.runCmd('type summary add i_am_cool --summary-string "Text summary"')
14599451b44SJordan Rupprecht
146*2238dcc3SJonas Devlieghere        self.expect("frame variable one", substrs=["Text summary"])
14799451b44SJordan Rupprecht
148*2238dcc3SJonas Devlieghere        self.expect("frame variable two", substrs=["Text summary"])
14999451b44SJordan Rupprecht
150*2238dcc3SJonas Devlieghere        self.expect("frame variable twoptr", substrs=["Text summary"])
15199451b44SJordan Rupprecht
152*2238dcc3SJonas Devlieghere        self.expect("frame variable array", substrs=["Python summary"])
153