"""
Test the printing of anonymous and named namespace variables.
"""



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


class NamespaceLookupTestCase(TestBase):

    mydir = TestBase.compute_mydir(__file__)

    def setUp(self):
        # Call super's setUp().
        TestBase.setUp(self)
        # Break inside different scopes and evaluate value
        self.line_break_global_scope = line_number(
            'ns.cpp', '// BP_global_scope')
        self.line_break_file_scope = line_number('ns2.cpp', '// BP_file_scope')
        self.line_break_ns_scope = line_number('ns2.cpp', '// BP_ns_scope')
        self.line_break_nested_ns_scope = line_number(
            'ns2.cpp', '// BP_nested_ns_scope')
        self.line_break_nested_ns_scope_after_using = line_number(
            'ns2.cpp', '// BP_nested_ns_scope_after_using')
        self.line_break_before_using_directive = line_number(
            'ns3.cpp', '// BP_before_using_directive')
        self.line_break_after_using_directive = line_number(
            'ns3.cpp', '// BP_after_using_directive')

    def runToBkpt(self, command):
        self.runCmd(command, RUN_SUCCEEDED)
        # The stop reason of the thread should be breakpoint.
        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
                    substrs=['stopped',
                             'stop reason = breakpoint'])

    @expectedFailureAll(
        oslist=["freebsd"],
        bugnumber="llvm.org/pr25819")
    @skipIfWindows # This is flakey on Windows: llvm.org/pr38373
    def test_scope_lookup_with_run_command(self):
        """Test scope lookup of functions in lldb."""
        self.build()
        self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)

        lldbutil.run_break_set_by_file_and_line(
            self,
            "ns.cpp",
            self.line_break_global_scope,
            num_expected_locations=1,
            loc_exact=False)
        lldbutil.run_break_set_by_file_and_line(
            self,
            "ns2.cpp",
            self.line_break_ns_scope,
            num_expected_locations=1,
            loc_exact=False)
        lldbutil.run_break_set_by_file_and_line(
            self,
            "ns2.cpp",
            self.line_break_nested_ns_scope,
            num_expected_locations=1,
            loc_exact=False)