<commit_before>"""
Test the lldb command line completion mechanism.
"""

from __future__ import print_function


import os
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbplatform
from lldbsuite.test import lldbutil


class CommandLineCompletionTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    NO_DEBUG_INFO_TESTCASE = True

    @classmethod
    def classCleanup(cls):
        """Cleanup the test byproducts."""
        try:
            os.remove("child_send.txt")
            os.remove("child_read.txt")
        except:
            pass

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_at(self):
        """Test that 'at' completes to 'attach '."""
        self.complete_from_to('at', 'attach ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_de(self):
        """Test that 'de' completes to 'detach '."""
        self.complete_from_to('de', 'detach ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_frame_variable(self):
        self.build()
        self.main_source = "main.cpp"
        self.main_source_spec = lldb.SBFileSpec(self.main_source)

        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
                                          '// Break here', self.main_source_spec)
        self.assertEquals(process.GetState(), lldb.eStateStopped)
        # FIXME: This pulls in the debug information to make the completions work,
        # but the completions should also work without.
        self.runCmd("frame variable fooo")

        self.complete_from_to('frame variable fo',
                              'frame variable fooo')
        self.complete_from_to('frame variable fooo.',
                              'frame variable fooo.')
        self.complete_from_to('frame variable fooo.dd',
                              'frame variable fooo.dd')

        self.complete_from_to('frame variable ptr_fooo->',
                              'frame variable ptr_fooo->')
        self.complete_from_to('frame variable ptr_fooo->dd',
                              'frame variable ptr_fooo->dd')

        self.complete_from_to('frame variable cont',
                              'frame variable container')
        self.complete_from_to('frame variable container.',
                              'frame variable container.MemberVar')
        self.complete_from_to('frame variable container.Mem',
                              'frame variable container.MemberVar')

        self.complete_from_to('frame variable ptr_cont',
                              'frame variable ptr_container')
        self.complete_from_to('frame variable ptr_container->',
                              'frame variable ptr_container->MemberVar')
        self.complete_from_to('frame variable ptr_container->Mem',
                              'frame variable ptr_container->MemberVar')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_process_attach_dash_dash_con(self):
        """Test that 'process attach --con' completes to 'process attach --continue '."""
        self.complete_from_to(
            'process attach --con',
            'process attach --continue ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_process_launch_arch(self):
        self.complete_from_to('process launch --arch ',
                              ['mips',
                               'arm64'])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_plugin_load(self):
        self.complete_from_to('plugin load ', [])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_quoted_command(self):
        self.complete_from_to('"set',
                              ['"settings" '])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_quoted_arg_with_quoted_command(self):
        self.complete_from_to('"settings" "repl',
                              ['"replace" '])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_quoted_arg_without_quoted_command(self):
        self.complete_from_to('settings "repl',
                              ['"replace" '])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_single_quote_command(self):
        self.complete_from_to("'set",
                              ["'settings' "])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_terminated_quote_command(self):
        # This should not crash, but we don't get any
        # reasonable completions from this.
        self.complete_from_to("'settings'", [])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_process_launch_arch_arm(self):
        self.complete_from_to('process launch --arch arm',
                              ['arm64'])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_target_symbols_add_shlib(self):
        # Doesn't seem to work, but at least it shouldn't crash.
        self.complete_from_to('target symbols add --shlib ', [])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_log_file(self):
        # Complete in our source directory which contains a 'main.cpp' file.
        src_dir =  os.path.dirname(os.path.realpath(__file__)) + '/'
        self.complete_from_to('log enable lldb expr -f ' + src_dir,
                              ['main.cpp'])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_log_file(self):
        # Complete in our source directory which contains a 'main.cpp' file.
        src_dir =  os.path.dirname(os.path.realpath(__file__))
        self.complete_from_to('log enable lldb expr -f ' + src_dir,
                              [src_dir + "/"])

    # <rdar://problem/11052829>
    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_infinite_loop_while_completing(self):
        """Test that 'process print hello\' completes to itself and does not infinite loop."""
        self.complete_from_to('process print hello\\', 'process print hello\\',
                              turn_off_re_match=True)

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_watchpoint_co(self):
        """Test that 'watchpoint co' completes to 'watchpoint command '."""
        self.complete_from_to('watchpoint co', 'watchpoint command ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_watchpoint_command_space(self):
        """Test that 'watchpoint command ' completes to ['add', 'delete', 'list']."""
        self.complete_from_to(<commit_msg>[lldb][NFC] Give added test method a unique name<commit_after>"""
Test the lldb command line completion mechanism.
"""

from __future__ import print_function


import os
import lldb
from lldbsuite.test.decorators import *
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbplatform
from lldbsuite.test import lldbutil


class CommandLineCompletionTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    NO_DEBUG_INFO_TESTCASE = True

    @classmethod
    def classCleanup(cls):
        """Cleanup the test byproducts."""
        try:
            os.remove("child_send.txt")
            os.remove("child_read.txt")
        except:
            pass

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_at(self):
        """Test that 'at' completes to 'attach '."""
        self.complete_from_to('at', 'attach ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_de(self):
        """Test that 'de' completes to 'detach '."""
        self.complete_from_to('de', 'detach ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_frame_variable(self):
        self.build()
        self.main_source = "main.cpp"
        self.main_source_spec = lldb.SBFileSpec(self.main_source)

        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
                                          '// Break here', self.main_source_spec)
        self.assertEquals(process.GetState(), lldb.eStateStopped)
        # FIXME: This pulls in the debug information to make the completions work,
        # but the completions should also work without.
        self.runCmd("frame variable fooo")

        self.complete_from_to('frame variable fo',
                              'frame variable fooo')
        self.complete_from_to('frame variable fooo.',
                              'frame variable fooo.')
        self.complete_from_to('frame variable fooo.dd',
                              'frame variable fooo.dd')

        self.complete_from_to('frame variable ptr_fooo->',
                              'frame variable ptr_fooo->')
        self.complete_from_to('frame variable ptr_fooo->dd',
                              'frame variable ptr_fooo->dd')

        self.complete_from_to('frame variable cont',
                              'frame variable container')
        self.complete_from_to('frame variable container.',
                              'frame variable container.MemberVar')
        self.complete_from_to('frame variable container.Mem',
                              'frame variable container.MemberVar')

        self.complete_from_to('frame variable ptr_cont',
                              'frame variable ptr_container')
        self.complete_from_to('frame variable ptr_container->',
                              'frame variable ptr_container->MemberVar')
        self.complete_from_to('frame variable ptr_container->Mem',
                              'frame variable ptr_container->MemberVar')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_process_attach_dash_dash_con(self):
        """Test that 'process attach --con' completes to 'process attach --continue '."""
        self.complete_from_to(
            'process attach --con',
            'process attach --continue ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_process_launch_arch(self):
        self.complete_from_to('process launch --arch ',
                              ['mips',
                               'arm64'])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_plugin_load(self):
        self.complete_from_to('plugin load ', [])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_quoted_command(self):
        self.complete_from_to('"set',
                              ['"settings" '])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_quoted_arg_with_quoted_command(self):
        self.complete_from_to('"settings" "repl',
                              ['"replace" '])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_quoted_arg_without_quoted_command(self):
        self.complete_from_to('settings "repl',
                              ['"replace" '])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_single_quote_command(self):
        self.complete_from_to("'set",
                              ["'settings' "])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_terminated_quote_command(self):
        # This should not crash, but we don't get any
        # reasonable completions from this.
        self.complete_from_to("'settings'", [])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_process_launch_arch_arm(self):
        self.complete_from_to('process launch --arch arm',
                              ['arm64'])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_target_symbols_add_shlib(self):
        # Doesn't seem to work, but at least it shouldn't crash.
        self.complete_from_to('target symbols add --shlib ', [])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_log_file(self):
        # Complete in our source directory which contains a 'main.cpp' file.
        src_dir =  os.path.dirname(os.path.realpath(__file__)) + '/'
        self.complete_from_to('log enable lldb expr -f ' + src_dir,
                              ['main.cpp'])

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_log_dir(self):
        # Complete our source directory.
        src_dir =  os.path.dirname(os.path.realpath(__file__))
        self.complete_from_to('log enable lldb expr -f ' + src_dir,
                              [src_dir + "/"])

    # <rdar://problem/11052829>
    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_infinite_loop_while_completing(self):
        """Test that 'process print hello\' completes to itself and does not infinite loop."""
        self.complete_from_to('process print hello\\', 'process print hello\\',
                              turn_off_re_match=True)

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_watchpoint_co(self):
        """Test that 'watchpoint co' completes to 'watchpoint command '."""
        self.complete_from_to('watchpoint co', 'watchpoint command ')

    @skipIfFreeBSD  # timing out on the FreeBSD buildbot
    def test_watchpoint_command_space(self):
        """Test that 'watchpoint command ' completes to ['add', 'delete', 'list']."""
        self.complete_from_to(
{'fraction_non_alphanumeric': 0.09297583081570997, 'fraction_numerical': 0.0018126888217522659, 'mean_word_length': 2.2718062762540154, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1920999', 'n_tokens_mistral': 3823, 'n_tokens_neox': 3648, 'n_words': 954}