diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | pygments/util.py | 14 | ||||
-rw-r--r-- | tests/run.py | 5 | ||||
-rw-r--r-- | tests/test_util.py | 89 |
4 files changed, 106 insertions, 7 deletions
@@ -13,7 +13,7 @@ PYTHON ?= python export PYTHONPATH = $(shell echo "$$PYTHONPATH"):$(shell python -c 'import os; print ":".join(os.path.abspath(line.strip()) for line in file("PYTHONPATH"))' 2>/dev/null) .PHONY: all apidocs check clean clean-pyc codetags docs epydoc mapfiles \ - pylint reindent test + pylint reindent test test-coverage all: clean-pyc check test @@ -68,3 +68,6 @@ reindent: test: @$(PYTHON) tests/run.py $(TESTS) + +test-coverage: + @$(PYTHON) tests/run.py -C $(TESTS) diff --git a/pygments/util.py b/pygments/util.py index 79fa250e..3331400a 100644 --- a/pygments/util.py +++ b/pygments/util.py @@ -18,7 +18,7 @@ doctype_lookup_re = re.compile(r'''(?smx) [a-zA-Z_][a-zA-Z0-9]*\s+ [a-zA-Z_][a-zA-Z0-9]*\s+ "[^"]*") - [^>]+> + [^>]*> ''') tag_re = re.compile(r'<(.+?)(\s.*?)?>.*?</\1>(?uism)') @@ -37,6 +37,12 @@ def get_bool_opt(options, optname, default=None): string = options.get(optname, default) if isinstance(string, bool): return string + elif isinstance(string, int): + return bool(string) + elif not isinstance(string, basestring): + raise OptionError('Invalid type %r for option %s; use ' + '1/0, yes/no, true/false, on/off' % ( + string, optname)) elif string.lower() in ('1', 'yes', 'true', 'on'): return True elif string.lower() in ('0', 'no', 'false', 'off'): @@ -51,6 +57,10 @@ def get_int_opt(options, optname, default=None): string = options.get(optname, default) try: return int(string) + except TypeError: + raise OptionError('Invalid type %r for option %s; you ' + 'must give an integer value' % ( + string, optname)) except ValueError: raise OptionError('Invalid value %r for option %s; you ' 'must give an integer value' % ( @@ -64,7 +74,7 @@ def get_list_opt(options, optname, default=None): elif isinstance(val, (list, tuple)): return list(val) else: - raise OptionError('Invalid value %r for option %s; you ' + raise OptionError('Invalid type %r for option %s; you ' 'must give a list value' % ( val, optname)) diff --git a/tests/run.py b/tests/run.py index debaaf7a..457a97a1 100644 --- a/tests/run.py +++ b/tests/run.py @@ -66,8 +66,6 @@ def run_tests(with_coverage=False): # needed to avoid confusion involving atexit handlers import logging - #orig_modules = sys.modules.keys() - if sys.argv[1:]: # test only files given on cmdline files = [entry + '.py' for entry in sys.argv[1:] if entry.startswith('test_')] @@ -104,8 +102,7 @@ def run_tests(with_coverage=False): for name, thing in globs.iteritems(): if name.endswith('Test'): tests.append((name, unittest.makeSuite(thing))) - # PY24: use key keyword arg - tests.sort(lambda x, y: cmp(x[0], y[0])) + tests.sort() suite = unittest.TestSuite() suite.addTests([x[1] for x in tests]) runner.run(suite) diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 00000000..737a40f4 --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,89 @@ +# -*- coding: utf-8 -*- +""" + Test suite for the util module + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + :copyright: 2006-2007 by Georg Brandl. + :license: BSD, see LICENSE for more details. +""" + +import unittest +import os + +from pygments import util + + +class UtilTest(unittest.TestCase): + + def test_getoptions(self): + raises = self.assertRaises + equals = self.assertEquals + + equals(util.get_bool_opt({}, 'a', True), True) + equals(util.get_bool_opt({}, 'a', 1), True) + equals(util.get_bool_opt({}, 'a', 'true'), True) + equals(util.get_bool_opt({}, 'a', 'no'), False) + raises(util.OptionError, util.get_bool_opt, {}, 'a', []) + raises(util.OptionError, util.get_bool_opt, {}, 'a', 'foo') + + equals(util.get_int_opt({}, 'a', 1), 1) + raises(util.OptionError, util.get_int_opt, {}, 'a', []) + raises(util.OptionError, util.get_int_opt, {}, 'a', 'bar') + + equals(util.get_list_opt({}, 'a', [1]), [1]) + equals(util.get_list_opt({}, 'a', '1 2'), ['1', '2']) + raises(util.OptionError, util.get_list_opt, {}, 'a', 1) + + + def test_docstring_headline(self): + def f1(): + """ + docstring headline + + other text + """ + def f2(): + """ + docstring + headline + + other text + """ + + self.assertEquals(util.docstring_headline(f1), "docstring headline") + self.assertEquals(util.docstring_headline(f2), "docstring headline") + + def test_analysator(self): + class X(object): + def analyse(text): + return 0.5 + analyse = util.make_analysator(analyse) + self.assertEquals(X.analyse(''), 0.5) + + def test_shebang_matches(self): + self.assert_(util.shebang_matches('#!/usr/bin/env python', r'python(2\.\d)?')) + self.assert_(util.shebang_matches('#!/usr/bin/python2.4', r'python(2\.\d)?')) + self.assert_(util.shebang_matches('#!/usr/bin/startsomethingwith python', + r'python(2\.\d)?')) + self.assert_(util.shebang_matches('#!C:\\Python2.4\\Python.exe', r'python(2\.\d)?')) + + self.failIf(util.shebang_matches('#!/usr/bin/python-ruby', r'python(2\.\d)?')) + self.failIf(util.shebang_matches('#!/usr/bin/python/ruby', r'python(2\.\d)?')) + self.failIf(util.shebang_matches('#!', r'python')) + + def test_doctype_matches(self): + self.assert_(util.doctype_matches('<!DOCTYPE html PUBLIC "a"> <html>', + 'html.*')) + self.failIf(util.doctype_matches('<?xml ?> <DOCTYPE html PUBLIC "a"> <html>', + 'html.*')) + self.assert_(util.html_doctype_matches( + '<?xml ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">')) + + def test_xml(self): + self.assert_(util.looks_like_xml( + '<?xml ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">')) + self.assert_(util.looks_like_xml('<html xmlns>abc</html>')) + self.failIf(util.looks_like_xml('<html>')) + +if __name__ == '__main__': + unittest.main() |