Beispiel #1
0
def test_template_parser():
    ''' uses main parse function parse_block which determines what is contained in each line '''
    _in = [
        "template <typename T>", "template <class Name = std::ForwardIterator>",
        "template", "<typename TT, typename U, int X_size = 10>", "template",
        "<typename T=float,", "typename C=long long>", ""
    ]

    expected_result_hpp = [
        "\nusing T = change_this;\n", "using Name = std::ForwardIterator;\n",
        "using TT = change_this;\nusing U = change_this;\nconstexpr int X_size = 10;\n",
        "using T = float;\nusing C = long long;\n"
    ]

    expected_result_cpp = ["", "", "", "", "", "", ""]

    parser = Parser()
    h = HeaderDef()
    it = enumerate(_in)
    pos = next(it)[1]
    try:
        parser.parse_block(it, pos, h)
    except StopIteration:
        pass

    is_equal(h.format_hpp(), ''.join(expected_result_hpp))
    is_equal(h.format_cpp(), "")
Beispiel #2
0
def test_preproc():
    ''' ignores_new lines '''

    parser = Parser()

    parse_batch_run(
        _in, expected_result_hpp, expected_result_cpp,
        lambda i, pos, h: parser._parse_preprocessor_stuff(i, pos, h))
Beispiel #3
0
def test_class():
    ''' ignores_new lines '''
    _in = [
        "class Nutella", "{", "};", "class PeanutButter{", "};",
        "class PeanutButter{//something", "};"
    ]

    expected_result_hpp = [
        "class Nutella {}; ", "class PeanutButter {}; ",
        "class PeanutButter {//something}; "
    ]

    expected_result_cpp = ["", "", ""]

    parser = Parser()
    parse_batch_run(_in, expected_result_hpp, expected_result_cpp,
                    lambda i, txt, h: parser._parse_class_block(i, txt, h))
def parse_header(filename):
    parser = Parser()
    clang_formatter = Formatter()

    path_name = os.path.splitext(os.path.basename(filename))[0]

    text = clang_formatter.open_and_launch(filename)
    it = enumerate(text)

    header_root = HeaderDef()
    try:
        parser.parse_block(it, next(it)[1], header_root)
    except StopIteration:
        pass
    return [
        clang_formatter.launch_batch(header_root.get_lines_hpp()),
        clang_formatter.launch_batch(
            header_root.get_lines_cpp(path_name + ".hpp"))
    ]
Beispiel #5
0
def test_namespace():
    ''' ignores_new lines '''
    _in = [
        "namespace Nutella", "{", "}", "namespace PeanutButter{", "}",
        "namespace Cereal{ //something", "}"
    ]

    expected_result_hpp = [
        "namespace Nutella {}// namespace Nutella",
        "namespace PeanutButter {}// namespace PeanutButter",
        "namespace Cereal {//something}// namespace Cereal"
    ]
    expected_result_cpp = [
        "namespace Nutella {}// namespace Nutella",
        "namespace PeanutButter {}// namespace PeanutButter",
        "namespace Cereal {}// namespace Cereal"
    ]

    parser = Parser()
    parse_batch_run(_in, expected_result_hpp, expected_result_cpp,
                    lambda i, txt, h: parser._parse_namespace_block(i, txt, h))
Beispiel #6
0
def test_functions():
    ''' ignores_new lines '''
    _in = [
        'void func();', 'std::string get_key(int index_pos = 0);',
        'std::string get_key(int index_pos = 0)',
        '{return ptr->find_key(index_pos);\n', '}',
        'std::vector<int> get_vector(int min_number = 0, int max_number = 10);'
    ]

    expected_result_hpp = [
        'void func();', 'std::string get_key(int index_pos= 0);',
        'std::string get_key(int index_pos= 0);',
        'std::vector<int> get_vector(int min_number = 0, int max_number = 10);'
    ]

    expected_result_cpp = [
        'void func() {}', 'std::string get_key(int index_pos) {}',
        'std::string get_key(int index_pos){return ptr->find_key(index_pos);}',
        'std::vector<int> get_vector(int min_number, int max_number) {}'
    ]

    rg = Regex()
    parser = FunctionVariableParser(rg)

    parse_batch_run(_in, expected_result_hpp, expected_result_cpp,
                    lambda i, pos, h: parser.parse(i, pos, h))

    _class = ClassDef("class", "Base")
    _in = ["void do_things(){ do_other_things();}", ""]
    it = enumerate(_in)
    pos = next(it)[1]
    _parser = Parser()
    try:
        _parser.parse_block(it, pos, _class)
    except StopIteration:
        pass

    is_equal(
        _class.format_cpp(None),
        "void  Base::do_things(){ do_other_things();}")
Beispiel #7
0
def test_comment_parser():
    ''' uses main parse function parse_block which determines what is contained in each line
    '''
    _in = [
        "//comment is this\n", "/* comment is this as well */\n",
        "    //some comment\n", "/* single line comment */\n",
        "/* multiline comment\n", "\n", "\n", "\n", "\n", "\n*/", ''
    ]
    expected_result = [
        "\n//comment is this\n", "/* comment is this as well */\n",
        "//some comment\n", "/* single line comment */\n",
        "/* multiline comment\n", "\n", "\n", "\n", "\n", "\n*/\n"
    ]

    parser = Parser()
    h = HeaderDef()
    it = enumerate(_in)
    pos = next(it)
    try:
        parser.parse_block(it, pos[1], h)
    except StopIteration:
        pass

    is_equal(h.format_hpp(), ''.join(expected_result))
Beispiel #8
0
def test_preproc_parser():
    ''' uses main parse function parse_block which determines what is contained in each line 
    '''
    parser = Parser()
    parse_batch_run(_in, expected_result_hpp, expected_result_cpp,
                    lambda i, pos, h: parser.parse_block(i, pos, h))