--- 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
-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 process(self, line):
-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
- 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)
- self.assertTrue(self.parser.started)
- self.assertTrue(self.parser.finished)
- self.assertEqual(parsed, lines)
- """ 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
- 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)
- 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
- 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
- self._test_counts(True, 'whitespace_mixed.txt', 0, 0)