summaryrefslogtreecommitdiff
path: root/support/intex/lib/intex/test_paren_parser.py
blob: df5a620999b3f6729a69362d72bad5170f996b57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#! /usr/bin/python
# -*- coding: latin-1 -*-
# $Id$
"""
Copyright (C) 2007, 2008 by Martin Thorsen Ranang

This file is part of InTeX.

InTeX is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your
option) any later version.

InTeX is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with InTeX.  If not, see <http://www.gnu.org/licenses/>.
"""
__revision__ = "$Rev$"
__author__ = "Martin Thorsen Ranang <mtr@ranang.org>"

import paren_parser
import unittest


class SimpleParenParserTestCase(unittest.TestCase):
    matching_pairs = [
        ('(', ')'),
        ('[', ']'),
        ('{', '}'),
        ]

    closing = dict(matching_pairs)
    
    def setUp(self):
        self.parser = paren_parser.ParenParser()

    def get_all_indices(self, string):
        return list(self.parser.get_scope_spans(string))
        
class NonMatchingClosingTestCase(SimpleParenParserTestCase):
    def runTest(self):
        # Test all simple combinations of non-matching closing
        # parenthesis.  For example: '(}' and '(})'
        for left, right in self.matching_pairs:
            for ignore, token in self.matching_pairs:
                if token == right:
                    continue
                
                failing = '%(left)s%(token)s' % locals()
                self.assertRaises(paren_parser.UnexpectedClosingError,
                                  self.get_all_indices, failing)

                failing += right
                self.assertRaises(paren_parser.UnexpectedClosingError,
                                  self.get_all_indices, failing)

class NestedScopesTestCase(SimpleParenParserTestCase):
    def runTest(self):
        lefts = [left for left, right in self.matching_pairs]
        levels = len(lefts)
        
        for combo in paren_parser.cartesian(lefts, lefts, lefts):
            indices = []
            for i, opening in enumerate(reversed(combo)):
                combo.append(self.closing[opening])
                indices.append(((levels - (i + 1)), (levels + (i + 1))))
                
            self.assertEqual(indices,
                             list(self.get_all_indices(''.join(combo))))

class LaTeXTestCase(SimpleParenParserTestCase):
    def runTest(self):
        # Find all the scopes in each string, innermost scopes first.
        for string, scopes in [
            ('H2O@H$_{2}$O', ['{2}']),
            ('ABC@($a$, {$b$^$c$})', ['{$b$^$c$}', '($a$, {$b$^$c$})']),
            ('LaTeX@{\LaTeX}', ['{\LaTeX}']),
            ]:
            for k, (i, j) in enumerate(self.get_all_indices(string)):
                self.assertEqual(scopes[k], string[i:j])

class WhitespaceSplitTestCase(SimpleParenParserTestCase):
    def runTest(self):
        # Find all the scopes in each string, innermost scopes first.
        for separator, string, parts in [
            # Default whitespace-based splitting.
            (None, '', []),
            (None, 'abc def ghi', ['abc', 'def', 'ghi']),
            (None, '()', ['()']),
            (None, '(abc)', ['(abc)']),
            (None, '(abc) def', ['(abc)', 'def']),
            (None, 'abc(def)ghi (jkl) mno(pqr) stu',
             ['abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu']),
            (None, '(abc(def)ghi (jkl) mno(pqr) stu)',
             ['(abc(def)ghi (jkl) mno(pqr) stu)']),
            (None, '()abc(def)ghi (jkl) mno(pqr) stu',
             ['()abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu']),
            (None, 'abc(def)ghi (jkl) mno(pqr) stu()',
             ['abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu()']),
            (None, 'abc(def)ghi (jkl) mno(pqr) stu (vwx) ()',
             ['abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu', '(vwx)', '()']),
            ]:
            self.assertEqual(self.parser.split(string, separator), parts)

class TokenSplitTestCase(SimpleParenParserTestCase):
    def runTest(self):
        for separator, string, parts in [
            # '@'-based splitting.
            ('@', '', ['']),
            ('@', 'abc@def@ghi', ['abc', 'def', 'ghi']),
            ('@', '()', ['()']),
            ('@', '(abc)', ['(abc)']),
            ('@', '(abc)@def', ['(abc)', 'def']),
            ('@', 'abc(def)ghi@(jkl)@mno(pqr)@stu',
             ['abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu']),
            ('@', '(abc(def)ghi@(jkl)@mno(pqr)@stu)',
             ['(abc(def)ghi@(jkl)@mno(pqr)@stu)']),
            ('@', '()abc(def)ghi@(jkl)@mno(pqr)@stu',
             ['()abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu']),
            ('@', 'abc(def)ghi@(jkl)@mno(pqr)@stu()',
             ['abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu()']),
            ('@', 'abc(def)ghi@(jkl)@mno(pqr)@stu@(vwx)@()',
             ['abc(def)ghi', '(jkl)', 'mno(pqr)', 'stu', '(vwx)', '()']),
            ]:
            self.assertEqual(self.parser.split(string, separator), parts)
                
class ParenParserTestSuite(unittest.TestSuite):
    def __init__(self):
        unittest.TestSuite.__init__(self, [
            NonMatchingClosingTestCase(),
            NestedScopesTestCase(),
            LaTeXTestCase(),
            WhitespaceSplitTestCase(),
            TokenSplitTestCase(),
            ])
        
def main():
    """Module mainline (for standalone execution).
    """
    suite = ParenParserTestSuite()
    unittest.TextTestRunner(verbosity=2).run(suite)

if __name__ == "__main__":
    main()