Ejemplo n.º 1
0
    def test_complex_nested_blocks(self):
        b = self._build_Model([("if a", FragmentType.IfStatement),
                               ("a body", FragmentType.Body),
                               ("elif b", FragmentType.ElIfStatement),
                               ("b body", FragmentType.Body),
                               ("if aa", FragmentType.IfStatement),
                               ("if aaa", FragmentType.IfStatement),
                               ("aaa body", FragmentType.Body),
                               ("endif aaa", FragmentType.EndIfStatement),
                               ("else", FragmentType.ElseStatement),
                               ("else aa body", FragmentType.Body),
                               ("endif aa", FragmentType.EndIfStatement),
                               ("b body2", FragmentType.Body),
                               ("endif b", FragmentType.EndIfStatement)])

        self._check_Model(b, [
            Block([
                Branch(Fragment(FragmentType.IfStatement, "if a"), ["a body"]),
                Branch(Fragment(FragmentType.ElIfStatement, "elif b"), [
                    "b body",
                    Block([
                        Branch(Fragment(FragmentType.IfStatement, "if aa"), [
                            Block([
                                Branch(
                                    Fragment(FragmentType.IfStatement,
                                             "if aaa"), ["aaa body"])
                            ], "endif aaa"),
                        ]),
                        Branch(Fragment(FragmentType.ElseStatement, "else"),
                               ["else aa body"])
                    ], "endif aa"), "b body2"
                ])
            ], "endif b")
        ])
Ejemplo n.º 2
0
    def test_single_if_else_block(self):
        b = self._build_Model([("#if a", FragmentType.IfStatement),
                               ("a body", FragmentType.Body),
                               ("#else", FragmentType.ElseStatement),
                               ("else body", FragmentType.Body),
                               ("#endif", FragmentType.EndIfStatement)])

        self._check_Model(b, [
            Block([
                Branch(Fragment(FragmentType.IfStatement, "#if a"),
                       ["a body"]),
                Branch(Fragment(FragmentType.ElseStatement, "#else"),
                       ["else body"])
            ], "#endif")
        ])
Ejemplo n.º 3
0
def _normalize(model):
    res = []
    body_text = None
    for fragment in model:
        if fragment.type == FragmentType.Body:
            prev_text = body_text if body_text is not None else ''
            body_text = prev_text + fragment.text
        else:
            if body_text is not None:
                res.append(Fragment(FragmentType.Body, body_text))
                body_text = None
            res.append(fragment)
    if body_text is not None:
        res.append(Fragment(FragmentType.Body, body_text))
    return res
Ejemplo n.º 4
0
def split_recursive(file, patterns):
    res = [Fragment(FragmentType.Body, file)]
    left_count = len(patterns)
    if left_count is not 0:
        res = []
        pattern = patterns[0]
        split_result = re.split(str.format("({0})", pattern[0]), file)

        for fragment in split_result:
            if len(fragment) == 0:
                continue
            if re.match(pattern[0], fragment) is None:
                res.extend(split_recursive(fragment, patterns[1:]))
            else:
                res.append(Fragment(pattern[1], fragment))
    return res
Ejemplo n.º 5
0
    def test_nested_single_if_block(self):
        b = self._build_Model([("#if a", FragmentType.IfStatement),
                               ("#if b", FragmentType.IfStatement),
                               ("b body", FragmentType.Body),
                               ("#endif", FragmentType.EndIfStatement),
                               ("#endif", FragmentType.EndIfStatement)])

        self._check_Model(b, [
            Block([
                Branch(Fragment(FragmentType.IfStatement, "#if a"), [
                    Block([
                        Branch(Fragment(FragmentType.IfStatement, "#if b"),
                               ["b body"])
                    ], "#endif")
                ])
            ], "#endif")
        ])
Ejemplo n.º 6
0
    def test_single_empty_if_block(self):
        b = self._build_Model([("#if a", FragmentType.IfStatement),
                               ("#endif", FragmentType.EndIfStatement)])

        self._check_Model(b, [
            Block([Branch(Fragment(FragmentType.IfStatement, "#if a"), [])],
                  "#endif")
        ])
Ejemplo n.º 7
0
    def test_single_if_block_with_context(self):
        b = self._build_Model([
            ("before", FragmentType.Body),
            ("#if a", FragmentType.IfStatement),
            ("a body", FragmentType.Body),
            ("#endif", FragmentType.EndIfStatement),
            ("after", FragmentType.Body),
        ])

        self._check_Model(b, [
            "before",
            Block([
                Branch(Fragment(FragmentType.IfStatement, "#if a"), ["a body"])
            ], "#endif"), "after"
        ])
Ejemplo n.º 8
0
 def _check_Model(self, act, exp):
     self.assertEqual(len(act), len(exp))
     exp = list(Fragment(t[1], t[0]) for t in exp)
     for i in range(0, len(exp)):
         check_fragments(self, act[i], exp[i])
Ejemplo n.º 9
0
 def _build_Model(tuples):
     fs = (Fragment(t[1], t[0]) for t in tuples)
     return list(build(fs))