summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/support/empty.py0
-rw-r--r--tests/support/html_formatter.py5
-rw-r--r--tests/support/python_lexer.py15
-rw-r--r--tests/test_cmdline.py54
4 files changed, 74 insertions, 0 deletions
diff --git a/tests/support/empty.py b/tests/support/empty.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/support/empty.py
diff --git a/tests/support/html_formatter.py b/tests/support/html_formatter.py
new file mode 100644
index 00000000..7f5581ed
--- /dev/null
+++ b/tests/support/html_formatter.py
@@ -0,0 +1,5 @@
+
+from pygments.formatters import HtmlFormatter
+
+class HtmlFormatterWrapper(HtmlFormatter):
+ name = 'HtmlWrapper'
diff --git a/tests/support/python_lexer.py b/tests/support/python_lexer.py
new file mode 100644
index 00000000..ad34d31b
--- /dev/null
+++ b/tests/support/python_lexer.py
@@ -0,0 +1,15 @@
+# -*- coding: utf-8 -*-
+"""
+ pygments.lexers.python (as CustomLexer)
+ ~~~~~~~~~~~~~~~~~~~~~~
+
+ For test_cmdline.py
+"""
+
+from pygments.lexers import PythonLexer
+
+class CustomLexer(PythonLexer):
+ name = 'PythonLexerWrapper'
+
+class LexerWrapper(CustomLexer):
+ name="PythonLexerWrapperWrapper"
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index a37fb498..1500c875 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -110,6 +110,32 @@ class CmdLineTest(unittest.TestCase):
finally:
os.unlink(name)
+ def test_load_from_file(self):
+ lexer_file = os.path.join(TESTDIR, 'support', 'python_lexer.py')
+ formatter_file = os.path.join(TESTDIR, 'support', 'html_formatter.py')
+
+ # By default, use CustomLexer
+ o = self.check_success('-l', lexer_file, '-f', 'html',
+ '-x', stdin=TESTCODE)
+ o = re.sub('<[^>]*>', '', o)
+ # rstrip is necessary since HTML inserts a \n after the last </div>
+ self.assertEqual(o.rstrip(), TESTCODE.rstrip())
+
+ # If user specifies a name, use it
+ o = self.check_success('-f', 'html', '-x', '-l',
+ lexer_file + ':LexerWrapper', stdin=TESTCODE)
+ o = re.sub('<[^>]*>', '', o)
+ # rstrip is necessary since HTML inserts a \n after the last </div>
+ self.assertEqual(o.rstrip(), TESTCODE.rstrip())
+
+ # Should also work for formatters
+ o = self.check_success('-lpython', '-f',
+ formatter_file + ':HtmlFormatterWrapper',
+ '-x', stdin=TESTCODE)
+ o = re.sub('<[^>]*>', '', o)
+ # rstrip is necessary since HTML inserts a \n after the last </div>
+ self.assertEqual(o.rstrip(), TESTCODE.rstrip())
+
def test_stream_opt(self):
o = self.check_success('-lpython', '-s', '-fterminal', stdin=TESTCODE)
o = re.sub(r'\x1b\[.*?m', '', o)
@@ -211,6 +237,20 @@ class CmdLineTest(unittest.TestCase):
e = self.check_failure('-lfooo', TESTFILE)
self.assertTrue('Error: no lexer for alias' in e)
+ # cannot load .py file without load_from_file flag
+ e = self.check_failure('-l', 'nonexistent.py', TESTFILE)
+ self.assertTrue('Error: no lexer for alias' in e)
+
+ # lexer file is missing/unreadable
+ e = self.check_failure('-l', 'nonexistent.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: cannot read' in e)
+
+ # lexer file is malformed
+ e = self.check_failure('-l', 'support/empty.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: no valid CustomLexer class found' in e)
+
# formatter not found
e = self.check_failure('-lpython', '-ffoo', TESTFILE)
self.assertTrue('Error: no formatter found for name' in e)
@@ -219,6 +259,20 @@ class CmdLineTest(unittest.TestCase):
e = self.check_failure('-ofoo.foo', TESTFILE)
self.assertTrue('Error: no formatter found for file name' in e)
+ # cannot load .py file without load_from_file flag
+ e = self.check_failure('-f', 'nonexistent.py', TESTFILE)
+ self.assertTrue('Error: no formatter found for name' in e)
+
+ # formatter file is missing/unreadable
+ e = self.check_failure('-f', 'nonexistent.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: cannot read' in e)
+
+ # formatter file is malformed
+ e = self.check_failure('-f', 'support/empty.py',
+ '-x', TESTFILE)
+ self.assertTrue('Error: no valid CustomFormatter class found' in e)
+
# output file not writable
e = self.check_failure('-o', os.path.join('nonexistent', 'dir', 'out.html'),
'-lpython', TESTFILE)