"Fossies" - the Fresh Open Source Software Archive

Member "zim-0.70/tests/parsing.py" (14 Mar 2019, 7211 Bytes) of package /linux/privat/zim-0.70.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "parsing.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.69.1_vs_0.70.

    1 
    2 # Copyright 2009 Jaap Karssenberg <jaap.karssenberg@gmail.com>
    3 
    4 import tests
    5 
    6 from zim.parsing import *
    7 from zim.parser import *
    8 
    9 class TestParsing(tests.TestCase):
   10 
   11     def testSplitWords(self):
   12         '''Test parsing quoted strings'''
   13         string = r'''"foo bar", "\"foooo bar\"" dusss ja'''
   14         list = ['foo bar', ',', '"foooo bar"', 'dusss', 'ja']
   15         result = split_quoted_strings(string)
   16         self.assertEqual(result, list)
   17         list = ['"foo bar"', ',', r'"\"foooo bar\""', 'dusss', 'ja']
   18         result = split_quoted_strings(string, unescape=False)
   19         self.assertEqual(result, list)
   20 
   21         string = r'''"foo bar", False, True'''
   22         list = ['foo bar', ',', 'False', ',', 'True']
   23         result = split_quoted_strings(string)
   24         self.assertEqual(result, list)
   25 
   26         self.assertRaises(ValueError, split_quoted_strings, "If you don't mind me asking")
   27         string = "If you don't mind me asking"
   28         list = ["If", "you", "don", "'t", "mind", "me", "asking"]
   29         result = split_quoted_strings(string, strict=False)
   30         self.assertEqual(result, list)
   31 
   32     def testParseDate(self):
   33         '''Test parsing dates'''
   34         from datetime import date
   35         today = date.today()
   36         year = today.year
   37         if today.month > 6:
   38             year += 1 # Starting July next year January is closer
   39         self.assertEqual(parse_date('1/1'), (year, 1, 1))
   40         self.assertEqual(parse_date('1-1'), (year, 1, 1))
   41         self.assertEqual(parse_date('1:1'), (year, 1, 1))
   42         self.assertEqual(parse_date('11/11/99'), (1999, 11, 11))
   43         self.assertEqual(parse_date('11/11/11'), (2011, 11, 11))
   44         self.assertEqual(parse_date('1/11/2001'), (2001, 11, 1))
   45         self.assertEqual(parse_date('1-11-2001'), (2001, 11, 1))
   46         self.assertEqual(parse_date('1:11:2001'), (2001, 11, 1))
   47         self.assertEqual(parse_date('2001/11/1'), (2001, 11, 1))
   48 
   49     def testRe(self):
   50         '''Test parsing Re class'''
   51         string = 'foo bar baz'
   52         re = Re('f(oo)\s*(bar)')
   53         if re.match(string):
   54             self.assertEqual(len(re), 3)
   55             self.assertEqual(re[0], 'foo bar')
   56             self.assertEqual(re[1], 'oo')
   57             self.assertEqual(re[2], 'bar')
   58         else:
   59             assert False, 'fail'
   60 
   61     def testTextBuffer(self):
   62         '''Test parsing TextBuffer class'''
   63         buffer = TextBuffer()
   64         buffer += ['test 123\n test !', 'fooo bar\n', 'duss']
   65         self.assertEqual(
   66             buffer.get_lines(),
   67             ['test 123\n', ' test !fooo bar\n', 'duss\n'])
   68 
   69     def testURLEncoding(self):
   70         '''Test encoding and decoding urls'''
   71         for url, readable in (
   72             ('file:///foo/file%25%20%5D', 'file:///foo/file%25 %5D'),
   73             ('http://foo/bar%20monkey%E2%80%99s', 'http://foo/bar monkey\u2019s'), # Multibyte unicode char
   74 
   75             # from bug report lp:545712
   76             ('http://www.moneydj.com/e/newage/JAVA%B4%FA%B8%D5%B0%CF.htm',
   77                 'http://www.moneydj.com/e/newage/JAVA%B4%FA%B8%D5%B0%CF.htm'),
   78             ('http://www.moneydj.com/e/newage/JAVA%20%B4%FA%B8%D5%B0%CF.htm',
   79                 'http://www.moneydj.com/e/newage/JAVA %B4%FA%B8%D5%B0%CF.htm'),
   80         ):
   81             self.assertEqual(url_decode(url, mode=URL_ENCODE_READABLE), readable)
   82             self.assertEqual(url_decode(readable, mode=URL_ENCODE_READABLE), readable)
   83             self.assertEqual(url_encode(url, mode=URL_ENCODE_READABLE), url)
   84             self.assertEqual(url_encode(readable, mode=URL_ENCODE_READABLE), url)
   85 
   86         for path, encoded in (
   87             ('/foo/file% ]', '/foo/file%25%20%5D'),
   88             ('/foo/bar monkey\u2019s', '/foo/bar%20monkey%E2%80%99s'),
   89         ):
   90             self.assertEqual(url_encode(path, mode=URL_ENCODE_PATH), encoded)
   91             self.assertEqual(url_decode(encoded, mode=URL_ENCODE_PATH), path)
   92 
   93         self.assertEqual(url_encode('foo?bar/baz', mode=URL_ENCODE_DATA), 'foo%3Fbar%2Fbaz')
   94         self.assertEqual(url_decode('foo%3Fbar%2Fbaz', mode=URL_ENCODE_DATA), 'foo?bar/baz')
   95         # from bug report lp:545712
   96         self.assertEqual(url_decode('%B4%FA%B8%D5%B0%CF', mode=URL_ENCODE_DATA), '%B4%FA%B8%D5%B0%CF')
   97 
   98         ## test round trip for utf-8
   99         data = '\u0421\u0430\u0439'
  100         encoded = url_encode(data)
  101         decoded = url_decode(data)
  102         #~ print("DATA, ENCODED, DECODED:", (data, encoded, decoded))
  103         self.assertEqual(decoded, data)
  104         self.assertEqual(url_decode(encoded), data)
  105         self.assertEqual(url_encode(decoded), encoded)
  106 
  107     def testLinkType(self):
  108         '''Test link_type()'''
  109         for href, type in (
  110             ('zim+file://foo/bar?dus.txt', 'notebook'),
  111             ('file:///foo/bar', 'file'),
  112             ('http://foo/bar', 'http'),
  113             ('http://192.168.168.100', 'http'),
  114             ('file+ssh://foo/bar', 'file+ssh'),
  115             ('mailto:foo@bar.com', 'mailto'),
  116             ('mailto:foo.com', 'page'),
  117             ('foo@bar.com', 'mailto'),
  118             ('mailto:foo//bar@bar.com', 'mailto'), # is this a valid mailto uri ?
  119             ('mid:foo@bar.org', 'mid'),
  120             ('cid:foo@bar.org', 'cid'),
  121             ('./foo/bar', 'file'),
  122             ('/foo/bar', 'file'),
  123             ('~/foo', 'file'),
  124             ('C:\\foo', 'file'),
  125             ('wp?foo', 'interwiki'),
  126             ('http://foo?bar', 'http'),
  127             ('\\\\host\\foo\\bar', 'smb'),
  128             ('foo', 'page'),
  129             ('foo:bar', 'page'),
  130         ):
  131             #~ print('>>', href)
  132             self.assertEqual(link_type(href), type)
  133 
  134 
  135 class TestSimpleTreeBuilder(tests.TestCase):
  136 
  137     def runTest(self):
  138         E = SimpleTreeElement
  139 
  140         builder = SimpleTreeBuilder()
  141 
  142         builder.start('root', {})
  143         builder.text('foo')
  144         builder.text('bar')
  145         builder.append('dus', {}, 'ja')
  146         builder.text('foo')
  147         builder.text('bar')
  148         builder.append('br', {})
  149         builder.text('foo')
  150         builder.text('bar')
  151         builder.end('root')
  152 
  153         root = builder.get_root()
  154         self.assertEqual(root, [
  155             E('root', {}, [
  156                     'foo', 'bar',
  157                     E('dus', {}, ['ja']),
  158                     'foo', 'bar',
  159                     E('br', {}, []),
  160                     'foo', 'bar',
  161                 ]
  162             )
  163         ])
  164 
  165 
  166         realbuilder = SimpleTreeBuilder()
  167         builder = BuilderTextBuffer(realbuilder)
  168 
  169         builder.start('root', {})
  170         builder.text('foo')
  171         builder.text('bar')
  172         builder.append('dus', {}, 'ja')
  173         builder.text('foo')
  174         builder.text('bar')
  175         builder.append('br', {})
  176         builder.text('foo')
  177         builder.text('bar')
  178         builder.end('root')
  179 
  180         root = realbuilder.get_root()
  181         self.assertEqual(root, [
  182             E('root', {}, [
  183                     'foobar',
  184                     E('dus', {}, ['ja']),
  185                     'foobar',
  186                     E('br', {}, []),
  187                     'foobar',
  188                 ]
  189             )
  190         ])
  191 
  192 
  193 
  194 class TestBuilderTextBuffer(tests.TestCase):
  195 
  196     def runTest(self):
  197         builder = SimpleTreeBuilder()
  198         buffer = BuilderTextBuffer(builder)
  199 
  200         buffer.start('FOO')
  201         buffer.text('aaa\n')
  202         buffer.text('bbb\n')
  203         buffer.text('ccc\n')
  204         self.assertEqual(buffer.get_text(), 'aaa\nbbb\nccc\n')
  205 
  206         buffer.append('BAR')
  207         self.assertEqual(buffer.get_text(), '')
  208 
  209         buffer.text('qqq\n')
  210         self.assertEqual(buffer.get_text(), 'qqq\n')
  211         buffer.clear_text()
  212 
  213         buffer.text('qqq\n')
  214         self.assertEqual(buffer.get_text(), 'qqq\n')
  215         buffer.set_text('ddd\n')
  216         self.assertEqual(buffer.get_text(), 'ddd\n')
  217 
  218         buffer.text('')
  219         buffer.text('eee')
  220         buffer.end('FOO')
  221 
  222         E = SimpleTreeElement
  223         self.assertEqual(builder.get_root(), [
  224             E('FOO', None, [
  225                 'aaa\nbbb\nccc\n',
  226                 E('BAR', None, []),
  227                 'ddd\neee',
  228             ])
  229         ])
  230 
  231 
  232 
  233 class TestParser(tests.TestCase):
  234 
  235     def testFunctions(self):
  236         # Helper functions
  237 
  238         self.assertEqual(fix_line_end('foo'), 'foo\n')
  239         self.assertEqual(fix_line_end('foo\nbar'), 'foo\nbar\n')
  240 
  241         self.assertEqual(convert_space_to_tab('    foo\n\t     bar\n'), '\tfoo\n\t\t bar\n')
  242 
  243         text = 'foo\nbar\nbaz\n'
  244         for offset, wanted in (
  245             (0, (1, 0)),
  246             (3, (1, 3)),
  247             (4, (2, 0)),
  248             (8, (3, 0)),
  249             (9, (3, 1)),
  250         ):
  251             line = get_line_count(text, offset)
  252             self.assertEqual(line, wanted)
  253 
  254     ## TODO -- Parser test cases ##