1"""Test the SBCommandInterpreter APIs."""
2
3from __future__ import print_function
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class CommandInterpreterAPICase(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number to break on inside main.cpp.
21        self.line = line_number('main.c', 'Hello world.')
22
23    @add_test_categories(['pyapi'])
24    def test_with_process_launch_api(self):
25        """Test the SBCommandInterpreter APIs."""
26        self.build()
27        exe = self.getBuildArtifact("a.out")
28
29        # Create a target by the debugger.
30        target = self.dbg.CreateTarget(exe)
31        self.assertTrue(target, VALID_TARGET)
32
33        # Retrieve the associated command interpreter from our debugger.
34        ci = self.dbg.GetCommandInterpreter()
35        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
36
37        # Exercise some APIs....
38
39        self.assertTrue(ci.HasCommands())
40        self.assertTrue(ci.HasAliases())
41        self.assertTrue(ci.HasAliasOptions())
42        self.assertTrue(ci.CommandExists("breakpoint"))
43        self.assertTrue(ci.CommandExists("target"))
44        self.assertTrue(ci.CommandExists("platform"))
45        self.assertTrue(ci.AliasExists("file"))
46        self.assertTrue(ci.AliasExists("run"))
47        self.assertTrue(ci.AliasExists("bt"))
48
49        res = lldb.SBCommandReturnObject()
50        ci.HandleCommand("breakpoint set -f main.c -l %d" % self.line, res)
51        self.assertTrue(res.Succeeded())
52        ci.HandleCommand("process launch", res)
53        self.assertTrue(res.Succeeded())
54
55        # Boundary conditions should not crash lldb!
56        self.assertFalse(ci.CommandExists(None))
57        self.assertFalse(ci.AliasExists(None))
58        ci.HandleCommand(None, res)
59        self.assertFalse(res.Succeeded())
60        res.AppendMessage("Just appended a message.")
61        res.AppendMessage(None)
62        if self.TraceOn():
63            print(res)
64
65        process = ci.GetProcess()
66        self.assertTrue(process)
67
68        import lldbsuite.test.lldbutil as lldbutil
69        if process.GetState() != lldb.eStateStopped:
70            self.fail("Process should be in the 'stopped' state, "
71                      "instead the actual state is: '%s'" %
72                      lldbutil.state_type_to_str(process.GetState()))
73
74        if self.TraceOn():
75            lldbutil.print_stacktraces(process)
76
77    @add_test_categories(['pyapi'])
78    def test_command_output(self):
79        """Test command output handling."""
80        ci = self.dbg.GetCommandInterpreter()
81        self.assertTrue(ci, VALID_COMMAND_INTERPRETER)
82
83        # Test that a command which produces no output returns "" instead of
84        # None.
85        res = lldb.SBCommandReturnObject()
86        ci.HandleCommand("settings set use-color false", res)
87        self.assertTrue(res.Succeeded())
88        self.assertIsNotNone(res.GetOutput())
89        self.assertEquals(res.GetOutput(), "")
90        self.assertIsNotNone(res.GetError())
91        self.assertEquals(res.GetError(), "")
92