import re
import textwrap


def compileTestView(tmpdir_factory, initCMakeProject, generateSourceView):
    """Fixture to set up the CMake project and compile the C++ code for testing
    the functions in GDBKokkos for examining Kokkos::View


    Args:
        tmpdir_factory : Fixture for generating temporary directory
        initCMakeProject : Fixture for setting up CMake project
        generateSourceView : Fixture for generating C++ code for test cases
    Returns: tuple of: LocalPath of the CMake project directory, pd.DataFrame of
    the test cases
    """
    p = tmpdir_factory.mktemp(f"testGDBKokkosView")
    proj = initCMakeProject(p)
    next(proj)
    cppSources = generateSourceView
    fcpps = []
    for _, row in cppSources.iterrows():
        cpp = row["cpp"]
        layout = row["layout"]
        arrayType = row["arrayType"]
        shape = row["shape"]
        fnShape = "-".join(map(str, shape))
        fnLayout = re.sub(r"::", "", layout)
        fnArrayType = re.sub(r"\s+", "", arrayType)
        fnArrayType = re.sub(r"[*]", "x", fnArrayType)
        fnArrayType = re.sub(r"\[", "", fnArrayType)
        fnArrayType = re.sub(r"\]", "-", fnArrayType)
        fcpp = p.join(f"testView{fnArrayType}{fnLayout}{fnShape}.cpp")
        fcpp.write(textwrap.dedent(cpp))
        fcpps.append(fcpp)
    cppSources.insert(0, "fcpp", fcpps)
    cppSources = proj.send(cppSources)
    return p, cppSources