tests/test_treeparse.py
author Tero Marttila <terom@fixme.fi>
Mon, 16 Feb 2009 19:08:17 +0200
changeset 78 a46d2fc07951
parent 77 bef7196f7682
permissions -rw-r--r--
add test for tree_parse filesystem stuff
# :set encoding=utf8

"""
    Unit tests for qmsk.web.tree_parse
"""

import unittest, os
import tree_parse

class TestTreeParse (unittest.TestCase) :
    VALID = """\
foo
  bar
   quux

  asdf
        further
         still

  and back
""".split('\n')

    def test_read_lines_valid (self) :
        self.assertEquals(list(tree_parse._read_lines(self.VALID)), [
            (1, 0,  "foo"),
            (2, 2,  "bar"),
            (3, 3,  "quux"),
            (5, 2,  "asdf"),
            (6, 8,  "further"),
            (7, 9,  "still"),
            (9, 2,  "and back"),
        ])

    def _parse (self, *lines, **args) :
        return list(tree_parse._read_lines(lines, **args))
    
    def test_read_lines_decode (self) :
        data_unicode = u'föö'
        data_raw = 'föö'

        self.assertEquals(self._parse(data_unicode.encode('utf8'), charset='utf8'), [(1, 0, data_unicode)])
        self.assertEquals(self._parse(data_unicode, charset=None), [(1, 0, data_unicode)])
        self.assertEquals(self._parse(data_raw, charset=None), [(1, 0, data_raw)])
        self.assertRaises(UnicodeDecodeError, self._parse, data_raw, charset='ascii')
    
    def test_read_lines_stop (self) :
        self.assertEquals(self._parse(" : foo"), [(1, 1, ": foo")])
        self.assertEquals(self._parse("    : foo", stop_tokens=':'), [(1, 0, ": foo")])
    
    def test_read_lines_empty (self) :
        self.assertEquals(self._parse(*("foo\n\n  bar".split('\n'))), [(1, 0, "foo"), (3, 2, "bar")])

    def test_read_lines_strip (self) :
        self.assertEquals(self._parse(" foo "), [(1, 1, "foo")])
    
    def _do (self, *lines, **args) :
        return tree_parse.parse(lines, **args)

    def test_parse_valid (self) :
        self.assertEquals(self._do(*self.VALID), 
            (1, "foo", [
                (2, "bar", [
                    (3, "quux", []),
                ]),
                (5, "asdf", [
                    (6, "further", [
                        (7, "still", []),
                    ]),
                ]),
                (9, "and back", [])
            ])
        )
    
    def test_parse_invalid (self) :
        self.assertRaises(tree_parse.TreeParseError, self._do, "foo", "  bar", " quux")
    
    # XXX: don't test root, as that's known-broken still

class TestTreeParseFS (unittest.TestCase) :
    DATA = ["foo", " bar", " quux"]
    def setUp (self) :
        """
            Sets up a temporary file with the path in self.path, and self.DATA
        """
        
        # XXX: supress warnings
        self.path = os.tempnam()

        # write out DATA
        open(self.path, 'w').write('\n'.join(self.DATA))
    
    def tearDown (self) :
        """
            Removes the tempfile created earlier
        """

        os.remove(self.path)
    
    def test_simple (self) :
        self.assertEquals(tree_parse.parse(self.path), (1, "foo", [(2, "bar", []), (3, "quux", [])]))