grim/pyparser

Parents 84f1badf6503
Children 6f2bab600b84
moved tests/__init__.py to tests/parser.py so the tests will actually get run
--- a/tests/__init__.py Sun Mar 24 22:59:17 2013 -0500
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,174 +0,0 @@
-"""
-Unittests for the main pyparser.Parser class
-"""
-
-import os
-import unittest
-
-from pyparser import Parser
-
-
-def _get_filename(filename):
- return os.path.join(os.path.dirname(__file__), 'data', filename)
-
-
-class CountingParser(Parser):
- """
- A simple parser that counts the number of lines it parsed as well as
- whether or not start and finish have been called.
- """
-
- def __init__(self):
- Parser.__init__(self)
-
- self.started = False
- self.finished = False
-
- self.data = 0
-
-
- def process(self, line):
- self.data += 1
-
-
- def start(self):
- self.started = True
-
-
- def finish(self):
- self.finished = True
-
-
-class TestCountingParser(unittest.TestCase): # pylint:disable-msg=R0904
- """
- Just test counting of lines via a parser
- """
-
- def setUp(self): # pylint:disable-msg=C0103
- self.parser = CountingParser()
-
-
- def tearDown(self): # pylint:disable-msg=C0103
- self.parser = None
-
-
- def _parse_file(self, filename, lines):
- """
- The main method that does all of the testing
- """
-
- self.assertFalse(self.parser.started)
- self.assertFalse(self.parser.finished)
-
- ifp = open(_get_filename(filename))
- parsed = self.parser.parse(ifp)
- ifp.close()
-
- self.assertTrue(self.parser.started)
- self.assertTrue(self.parser.finished)
-
- self.assertEqual(parsed, lines)
-
-
- def test_empty(self):
- """ Test an empty file """
-
- self._parse_file('empty.txt', 0)
-
-
- def test_single_line(self):
- """ Test a file with a single line """
-
- self._parse_file('basic_single_line.txt', 1)
-
-
- def test_multiple_lines(self):
- """ Test a file with multiple lines """
-
- self._parse_file('basic_multiple_lines.txt', 5)
-
-
-class WhiteSpaceParser(Parser):
- """
- A simple parser that counts how many lines have leading and trailing
- whitespace.
- """
-
- def __init__(self, strip):
- Parser.__init__(self, strip=strip)
-
- self.data = {'leading': 0, 'trailing': 0}
-
-
- def process(self, line):
- if line.lstrip() != line:
- self.data['leading'] += 1
-
- if line.rstrip() != line:
- self.data['trailing'] += 1
-
-
-class TestWhiteSpaceParser(unittest.TestCase): # pylint:disable-msg=R0904
- """
- Tests that the strip function of the parser works correctly
- """
-
- def _test_counts(self, strip, filename, leading, trailing):
- """
- The main testing method that will check that the WhiteSpaceParser found
- the correct number of lines with leading and trailing whitespace.
- """
-
- ifp = open(_get_filename(filename))
- data = WhiteSpaceParser(strip).parse(ifp)
- ifp.close()
-
- self.assertEqual(data['leading'], leading)
- self.assertEqual(data['trailing'], trailing)
-
-
- def test_no_strip_empty(self):
- """ Test the WhiteSpaceParser without stripping on an empty file """
-
- self._test_counts(False, 'empty.txt', 0, 0)
-
-
- def test_no_strip_single(self):
- """
- Test the WhiteSpaceParser without stripping on a file with one line
- """
-
- self._test_counts(False, 'whitespace_single.txt', 1, 1)
-
-
- def test_no_strip_mixed(self):
- """
- Test the WhiteSpaceParser without stripping on a file with mixed
- whitespace
- """
-
- self._test_counts(False, 'whitespace_mixed.txt', 3, 3)
-
-
- def test_strip_empty(self):
- """ Test the WhiteSpaceParser with stripping on an empty file """
-
- self._test_counts(True, 'empty.txt', 0, 0)
-
-
- def test_strip_single(self):
- """
- Test the WhiteSpaceParser with stripping on a file with one line
- """
-
- self._test_counts(True, 'whitespace_single.txt', 0, 0)
-
-
- def test_strip_mixed(self):
- """
- Test the WhiteSpaceParser with stripping on a file with mixed
- whitespace
- """
-
- self._test_counts(True, 'whitespace_mixed.txt', 0, 0)
-
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/parser.py Fri Apr 19 02:08:37 2013 -0500
@@ -0,0 +1,174 @@
+"""
+Unittests for the main pyparser.Parser class
+"""
+
+import os
+import unittest
+
+from pyparser import Parser
+
+
+def _get_filename(filename):
+ return os.path.join(os.path.dirname(__file__), 'data', filename)
+
+
+class CountingParser(Parser):
+ """
+ A simple parser that counts the number of lines it parsed as well as
+ whether or not start and finish have been called.
+ """
+
+ def __init__(self):
+ Parser.__init__(self)
+
+ self.started = False
+ self.finished = False
+
+ self.data = 0
+
+
+ def process(self, line):
+ self.data += 1
+
+
+ def start(self):
+ self.started = True
+
+
+ def finish(self):
+ self.finished = True
+
+
+class TestCountingParser(unittest.TestCase): # pylint:disable-msg=R0904
+ """
+ Just test counting of lines via a parser
+ """
+
+ def setUp(self): # pylint:disable-msg=C0103
+ self.parser = CountingParser()
+
+
+ def tearDown(self): # pylint:disable-msg=C0103
+ self.parser = None
+
+
+ def _parse_file(self, filename, lines):
+ """
+ The main method that does all of the testing
+ """
+
+ self.assertFalse(self.parser.started)
+ self.assertFalse(self.parser.finished)
+
+ ifp = open(_get_filename(filename))
+ parsed = self.parser.parse(ifp)
+ ifp.close()
+
+ self.assertTrue(self.parser.started)
+ self.assertTrue(self.parser.finished)
+
+ self.assertEqual(parsed, lines)
+
+
+ def test_empty(self):
+ """ Test an empty file """
+
+ self._parse_file('empty.txt', 0)
+
+
+ def test_single_line(self):
+ """ Test a file with a single line """
+
+ self._parse_file('basic_single_line.txt', 1)
+
+
+ def test_multiple_lines(self):
+ """ Test a file with multiple lines """
+
+ self._parse_file('basic_multiple_lines.txt', 5)
+
+
+class WhiteSpaceParser(Parser):
+ """
+ A simple parser that counts how many lines have leading and trailing
+ whitespace.
+ """
+
+ def __init__(self, strip):
+ Parser.__init__(self, strip=strip)
+
+ self.data = {'leading': 0, 'trailing': 0}
+
+
+ def process(self, line):
+ if line.lstrip() != line:
+ self.data['leading'] += 1
+
+ if line.rstrip() != line:
+ self.data['trailing'] += 1
+
+
+class TestWhiteSpaceParser(unittest.TestCase): # pylint:disable-msg=R0904
+ """
+ Tests that the strip function of the parser works correctly
+ """
+
+ def _test_counts(self, strip, filename, leading, trailing):
+ """
+ The main testing method that will check that the WhiteSpaceParser found
+ the correct number of lines with leading and trailing whitespace.
+ """
+
+ ifp = open(_get_filename(filename))
+ data = WhiteSpaceParser(strip).parse(ifp)
+ ifp.close()
+
+ self.assertEqual(data['leading'], leading)
+ self.assertEqual(data['trailing'], trailing)
+
+
+ def test_no_strip_empty(self):
+ """ Test the WhiteSpaceParser without stripping on an empty file """
+
+ self._test_counts(False, 'empty.txt', 0, 0)
+
+
+ def test_no_strip_single(self):
+ """
+ Test the WhiteSpaceParser without stripping on a file with one line
+ """
+
+ self._test_counts(False, 'whitespace_single.txt', 1, 1)
+
+
+ def test_no_strip_mixed(self):
+ """
+ Test the WhiteSpaceParser without stripping on a file with mixed
+ whitespace
+ """
+
+ self._test_counts(False, 'whitespace_mixed.txt', 3, 3)
+
+
+ def test_strip_empty(self):
+ """ Test the WhiteSpaceParser with stripping on an empty file """
+
+ self._test_counts(True, 'empty.txt', 0, 0)
+
+
+ def test_strip_single(self):
+ """
+ Test the WhiteSpaceParser with stripping on a file with one line
+ """
+
+ self._test_counts(True, 'whitespace_single.txt', 0, 0)
+
+
+ def test_strip_mixed(self):
+ """
+ Test the WhiteSpaceParser with stripping on a file with mixed
+ whitespace
+ """
+
+ self._test_counts(True, 'whitespace_mixed.txt', 0, 0)
+