File: test_examplefiles.py

package info (click to toggle)
pygments 0.5.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,776 kB
  • ctags: 3,400
  • sloc: ansic: 10,354; python: 8,398; xml: 7,407; ruby: 6,789; cpp: 4,263; perl: 4,164; pascal: 2,174; java: 1,741; php: 248; cs: 225; makefile: 83
file content (49 lines) | stat: -rw-r--r-- 1,312 bytes parent folder | download
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
# -*- coding: utf-8 -*-
"""
    Pygments tests with example files
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    :copyright: 2006 by Georg Brandl.
    :license: GNU GPL, see LICENSE for more details.
"""

import unittest
import os

from pygments import highlight
from pygments.lexers import get_lexer_for_filename, get_lexer_by_name
from pygments.token import Error


class ExampleFileTest(unittest.TestCase):
    pass

lfd = 0

# generate methods
for fn in os.listdir(os.path.join(testdir, 'examplefiles')):
    absfn = os.path.join(testdir, 'examplefiles', fn)
    if not os.path.isfile(absfn):
        continue

    try:
        lx = get_lexer_for_filename(absfn)
    except ValueError:
        try:
            name, rest = fn.split("_", 1)
            lx = get_lexer_by_name(name)
        except ValueError:
            raise AssertionError('no lexer found for file %r' % fn)

    def test(self, lx=lx, absfn=absfn):
        text = file(absfn, 'U').read()
        text = text.strip('\n') + '\n'
        ntext = ''
        for type, val in lx.get_tokens(text):
            ntext += val
            self.failIf(type == Error, 'lexer generated error token for '+absfn)
        if ntext != text:
            self.fail('round trip failed for '+absfn)

    setattr(ExampleFileTest, 'test_%i' % lfd, test)
    lfd += 1