tests/test_treeparse.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 22:40:35 +0200
changeset 86 6ff1140586d6
parent 78 a46d2fc07951
permissions -rw-r--r--
some tests for wsgi.Application
77
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     1
# :set encoding=utf8
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     2
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     3
"""
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     4
    Unit tests for qmsk.web.tree_parse
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     5
"""
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     6
78
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
     7
import unittest, os
77
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     8
import tree_parse
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
     9
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    10
class TestTreeParse (unittest.TestCase) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    11
    VALID = """\
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    12
foo
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    13
  bar
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    14
   quux
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    15
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    16
  asdf
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    17
        further
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    18
         still
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    19
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    20
  and back
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    21
""".split('\n')
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    22
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    23
    def test_read_lines_valid (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    24
        self.assertEquals(list(tree_parse._read_lines(self.VALID)), [
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    25
            (1, 0,  "foo"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    26
            (2, 2,  "bar"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    27
            (3, 3,  "quux"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    28
            (5, 2,  "asdf"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    29
            (6, 8,  "further"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    30
            (7, 9,  "still"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    31
            (9, 2,  "and back"),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    32
        ])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    33
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    34
    def _parse (self, *lines, **args) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    35
        return list(tree_parse._read_lines(lines, **args))
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    36
    
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    37
    def test_read_lines_decode (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    38
        data_unicode = u'föö'
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    39
        data_raw = 'föö'
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    40
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    41
        self.assertEquals(self._parse(data_unicode.encode('utf8'), charset='utf8'), [(1, 0, data_unicode)])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    42
        self.assertEquals(self._parse(data_unicode, charset=None), [(1, 0, data_unicode)])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    43
        self.assertEquals(self._parse(data_raw, charset=None), [(1, 0, data_raw)])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    44
        self.assertRaises(UnicodeDecodeError, self._parse, data_raw, charset='ascii')
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    45
    
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    46
    def test_read_lines_stop (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    47
        self.assertEquals(self._parse(" : foo"), [(1, 1, ": foo")])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    48
        self.assertEquals(self._parse("    : foo", stop_tokens=':'), [(1, 0, ": foo")])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    49
    
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    50
    def test_read_lines_empty (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    51
        self.assertEquals(self._parse(*("foo\n\n  bar".split('\n'))), [(1, 0, "foo"), (3, 2, "bar")])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    52
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    53
    def test_read_lines_strip (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    54
        self.assertEquals(self._parse(" foo "), [(1, 1, "foo")])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    55
    
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    56
    def _do (self, *lines, **args) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    57
        return tree_parse.parse(lines, **args)
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    58
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    59
    def test_parse_valid (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    60
        self.assertEquals(self._do(*self.VALID), 
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    61
            (1, "foo", [
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    62
                (2, "bar", [
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    63
                    (3, "quux", []),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    64
                ]),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    65
                (5, "asdf", [
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    66
                    (6, "further", [
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    67
                        (7, "still", []),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    68
                    ]),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    69
                ]),
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    70
                (9, "and back", [])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    71
            ])
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    72
        )
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    73
    
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    74
    def test_parse_invalid (self) :
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    75
        self.assertRaises(tree_parse.TreeParseError, self._do, "foo", "  bar", " quux")
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    76
    
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    77
    # XXX: don't test root, as that's known-broken still
bef7196f7682 add tree_parse test and fix treeparse to handle other than filesystem paths
Tero Marttila <terom@fixme.fi>
parents:
diff changeset
    78
78
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    79
class TestTreeParseFS (unittest.TestCase) :
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    80
    DATA = ["foo", " bar", " quux"]
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    81
    def setUp (self) :
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    82
        """
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    83
            Sets up a temporary file with the path in self.path, and self.DATA
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    84
        """
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    85
        
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    86
        # XXX: supress warnings
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    87
        self.path = os.tempnam()
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    88
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    89
        # write out DATA
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    90
        open(self.path, 'w').write('\n'.join(self.DATA))
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    91
    
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    92
    def tearDown (self) :
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    93
        """
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    94
            Removes the tempfile created earlier
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    95
        """
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    96
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    97
        os.remove(self.path)
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    98
    
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
    99
    def test_simple (self) :
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   100
        self.assertEquals(tree_parse.parse(self.path), (1, "foo", [(2, "bar", []), (3, "quux", [])]))
a46d2fc07951 add test for tree_parse filesystem stuff
Tero Marttila <terom@fixme.fi>
parents: 77
diff changeset
   101