Beispiel #1
0
def test_template_evaluator_if_stmt_proper_complex():
    file_content = [
        '// {% if directive.a && true then %}\n',
        '  cout << /* {% directive.A %} */ <<endl;\n',
        '  cout << /* {% directive.B %} */ <<endl;\n',
        '  cout << /* {% directive.C %} */ <<endl;\n',
        '// {% elif directive.b || false then  %}\n',
        '  cout << /* {% directive.B %} */ <<endl;\n',
        '// {% else %}\n',
        '  cout << /* {% directive.C %} */ <<endl;\n',
        '// {% endif %}\n',
    ]

    # Case 1
    known_symb = {'a': True, 'b': True, 'A': 1, 'B': 2, 'C': 3}
    result = template_transformer(file_content, known_symb)
    assert template_evaluator(result) == [
        '  cout << 1 <<endl;\n',
        '  cout << 2 <<endl;\n',
        '  cout << 3 <<endl;\n',
    ]

    # Case 2
    known_symb = {'a': False, 'b': True, 'A': 1, 'B': 2, 'C': 3}
    result = template_transformer(file_content, known_symb)
    assert template_evaluator(result) == [
        '  cout << 2 <<endl;\n',
    ]

    # Case 3
    known_symb = {'a': False, 'b': False, 'A': 1, 'B': 2, 'C': 3}
    result = template_transformer(file_content, known_symb)
    assert template_evaluator(result) == [
        '  cout << 3 <<endl;\n',
    ]
Beispiel #2
0
def test_template_evaluator_for_stmt_proper():
    file_content = [
        '// {% for key, val in directive.b->items: %}\n',
        '  cout << /* {% key %} */ <<endl;\n',
        '  // {% for v in val %}',
        '    cout << /* {% v %} */ <<endl;\n',
        '  // {% endfor %}\n',
        '  cout << "stuff" <<endl;\n',
        '// {% endfor %}\n',
    ]
    result = template_transformer(
        file_content, {'b': {
            'a': [1, 2],
            'b': [3, 4, 5],
            'c': [6]
        }}, False)
    assert template_evaluator(result) == [
        '  cout << a <<endl;\n',
        '    cout << 1 <<endl;\n',
        '    cout << 2 <<endl;\n',
        '  cout << "stuff" <<endl;\n',
        '  cout << b <<endl;\n',
        '    cout << 3 <<endl;\n',
        '    cout << 4 <<endl;\n',
        '    cout << 5 <<endl;\n',
        '  cout << "stuff" <<endl;\n',
        '  cout << c <<endl;\n',
        '    cout << 6 <<endl;\n',
        '  cout << "stuff" <<endl;\n',
    ]
Beispiel #3
0
def test_template_evaluator_for_stmt_simple():
    file_content = [
        '// {% for i in directive.b %}\n',
        'cout << "Random stuff";\n',
        'cout << /* {% i %} */ ;\n',
    ]
    result = template_transformer(file_content, {'b': [1, 2, 3]}, False)
    assert template_evaluator(result.parent) == [
        'cout << "Random stuff";\n', 'cout << 1 ;\n',
        'cout << "Random stuff";\n', 'cout << 2 ;\n',
        'cout << "Random stuff";\n', 'cout << 3 ;\n'
    ]
Beispiel #4
0
def test_template_evaluator_for_stmt_multi_idx():
    file_content = [
        '// {% for key, val in directive.b->items: %}\n',
        '  cout << /* {% format: "{} = {}", key, val %} */ <<endl;\n'
    ]
    result = template_transformer(file_content,
                                  {'b': {
                                      'a': 1,
                                      'b': 2,
                                      'c': 3
                                  }}, False)
    assert template_evaluator(result.parent) == [
        '  cout << a = 1 <<endl;\n',
        '  cout << b = 2 <<endl;\n',
        '  cout << c = 3 <<endl;\n',
    ]
Beispiel #5
0
def test_template_evaluator_for_stmt_nested():
    file_content = [
        '// {% for i in directive.b %}\n', 'cout << "Random stuff";\n',
        '// {% for j in i.stuff %}\n', '  cout << /* {% j %} */ ;\n',
        '// {% endfor %}\n', '// {% endfor %}\n'
    ]
    result = template_transformer(
        file_content,
        {'b': [{
            'stuff': [1, 2]
        }, {
            'stuff': [3]
        }, {
            'stuff': [4, 5, 6]
        }]})
    assert template_evaluator(result) == [
        'cout << "Random stuff";\n', '  cout << 1 ;\n', '  cout << 2 ;\n',
        'cout << "Random stuff";\n', '  cout << 3 ;\n',
        'cout << "Random stuff";\n', '  cout << 4 ;\n', '  cout << 5 ;\n',
        '  cout << 6 ;\n'
    ]
Beispiel #6
0
    def gen(self, filename, literals={}, blocked_trees=[], debug=False):
        """
        Generate C++ file based on inputs.
        """
        parsed_config = self.read(self.config_filename)
        dumped_ntuple, tree_relations = self.dump_ntuples(blocked_trees)
        directive = self.directive_gen(parsed_config, dumped_ntuple, literals,
                                       debug)

        # Adding ntuple info to the directive
        directive['ntuple'] = self.ntuple_filename
        directive['friends'] = self.friend_filenames
        directive['tree_relations'] = tree_relations

        with open(self.template_filename) as tmpl:
            macros = template_transformer(tmpl, directive)

        output_cpp = template_evaluator(macros)

        with open(filename, 'w') as f:
            f.write(''.join(output_cpp))
        if self.use_reformatter:
            self.reformat(filename)
Beispiel #7
0
def test_template_evaluator_inline_simple():
    file_content = ['int a = 1;\n', '    if(/* {% directive.b %}  */  )\n']
    result = template_transformer(file_content, {'b': 1})
    assert template_evaluator(result) == ['int a = 1;\n', '    if(1  )\n']
Beispiel #8
0
def test_template_evaluator_full_line_simple():
    file_content = ['int a = 1;\n', '    // {% directive.b %}\n']
    result = template_transformer(file_content, {'b': 1})
    assert template_evaluator(result) == ['int a = 1;\n', '    1\n']
Beispiel #9
0
def test_template_transformer_trivial_line():
    file_content = ['int a = 1;\n']
    result = template_transformer(file_content, {})
    assert template_evaluator(result) == ['int a = 1;\n']