From be78167b4b5212baedea832920f51fa9ca5126c8 Mon Sep 17 00:00:00 2001 From: drewcassidy Date: Sun, 25 Apr 2021 02:16:19 -0700 Subject: [PATCH] Add unit tests for parser --- CHANGELOG.md | 6 ++++ setup.cfg | 3 ++ tests/Test-Changelog.md | 64 +++++++++++++++++++++++++++++++++++++++++ tests/__init__.py | 0 tests/test_changelog.py | 62 +++++++++++++++++++++++++++++++++++++++ yaclog/changelog.py | 17 +++++------ 6 files changed, 142 insertions(+), 10 deletions(-) create mode 100644 tests/Test-Changelog.md create mode 100644 tests/__init__.py create mode 100644 tests/test_changelog.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 71d132b..c3ceb40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,12 @@ All notable changes to this project will be documented in this file +## Unreleased + +### Added + +- Unit tests in the `tests` folder + ## 0.3.2 - 2021-04-24 ### Added diff --git a/setup.cfg b/setup.cfg index e615652..913c222 100644 --- a/setup.cfg +++ b/setup.cfg @@ -37,3 +37,6 @@ packages = find: [options.entry_points] console_scripts = yaclog = yaclog.cli.__main__:cli + +[options.packages.find] +exclude = tests.* \ No newline at end of file diff --git a/tests/Test-Changelog.md b/tests/Test-Changelog.md new file mode 100644 index 0000000..acbc5ae --- /dev/null +++ b/tests/Test-Changelog.md @@ -0,0 +1,64 @@ +# Changelog + +This changelog is for testing the parser, and has many things in it that might trip it up. + +## [Unreleased] + +- bullet point with no section + +### Added + +- bullet point dash +* bullet point star ++ bullet point plus + - sub point 1 + - sub point 2 + - sub point 3 + +### Fixed ## + +- this is a bullet point + it spans many lines + +This is +a paragraph +it spans many lines + +this line has an [id] link + +#### This is an H4 + +##### This is an H5 + +###### This is an H6 + +```markdown +this is some example code +it spans many lines +``` + +> this is a block quote +it spans many lines + +[1.1.0] - 1969-07-20 [PRERELEASE] +--- + +### Fixed + +- Nyan Cat now has correct music + +### Added + +- Multiplayer + +## Not a version number + +### Removed + +- Herobrine + +[unreleased]: http://beesbeesbees.com + +[1.1.0]: http://endless.horse + +[id]: http://www.koalastothemax.com \ No newline at end of file diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_changelog.py b/tests/test_changelog.py new file mode 100644 index 0000000..6ad8655 --- /dev/null +++ b/tests/test_changelog.py @@ -0,0 +1,62 @@ +import unittest +import yaclog +import os.path +import datetime + +test_dir = os.path.dirname(os.path.realpath(__file__)) + + +class TestParser(unittest.TestCase): + def setUp(self): + self.log = yaclog.read(os.path.join(test_dir, 'Test-Changelog.md')) + + def test_header(self): + self.assertEqual(self.log.header, + '# Changelog\n\n' + 'This changelog is for testing the parser, and has many things in it that might trip it up.') + + def test_path(self): + self.assertEqual(self.log.path, os.path.join(test_dir, 'Test-Changelog.md')) + + def test_links(self): + self.assertEqual(self.log.links, {'unreleased': 'http://beesbeesbees.com', + '1.1.0': 'http://endless.horse', + 'id': 'http://www.koalastothemax.com'}) + + def test_versions(self): + v = self.log.versions + self.assertEqual(v[0].name, 'Unreleased') + self.assertEqual(v[0].link, 'http://beesbeesbees.com') + self.assertEqual(v[0].date, None) + self.assertEqual(v[0].tags, []) + + self.assertEqual(v[1].name, '1.1.0') + self.assertEqual(v[1].link, 'http://endless.horse') + self.assertEqual(v[1].date, datetime.date.fromisoformat('1969-07-20')) + self.assertEqual(v[1].tags, ['PRERELEASE']) + + self.assertEqual(v[2].name, 'Not a version number') + self.assertEqual(v[2].link, None) + self.assertEqual(v[2].date, None) + self.assertEqual(v[2].tags, []) + + def test_unreleased(self): + v = self.log.versions[0] + + self.assertEqual(v.sections[''], ['- bullet point with no section']) + self.assertEqual(v.sections['Added'], + ['- bullet point dash', '* bullet point star', + '+ bullet point plus\n - sub point 1\n - sub point 2\n - sub point 3']) + self.assertEqual(v.sections['Fixed'], + ['- this is a bullet point\n it spans many lines', + 'This is\na paragraph\nit spans many lines', + 'this line has an [id] link', + '#### This is an H4', + '##### This is an H5', + '###### This is an H6', + '```markdown\nthis is some example code\nit spans many lines\n```', + '> this is a block quote\nit spans many lines']) + + +if __name__ == '__main__': + unittest.main() diff --git a/yaclog/changelog.py b/yaclog/changelog.py index f4e96da..e5f16e9 100644 --- a/yaclog/changelog.py +++ b/yaclog/changelog.py @@ -66,8 +66,8 @@ class VersionEntry: self.name: str = 'Unreleased' self.date: Optional[datetime.date] = None self.tags: List[str] = [] - self.link: str = '' - self.link_id: str = '' + self.link: Optional[str] = None + self.link_id: Optional[str] = None self.line_no: int = -1 def body(self, md: bool = True) -> str: @@ -203,7 +203,7 @@ class Changelog: version.name = slug version.line_no = segment[0] tags = [] - date = [] + date = None for word in split[1:]: if match := re.match(r'\d{4}-\d{2}-\d{2}', word): @@ -246,13 +246,13 @@ class Changelog: # ref-matched link link_id = match[1].lower() if link_id in self.links: - version.link = self.links.pop(link_id) + version.link = self.links[link_id] version.link_id = None version.name = match[1] elif version.link_id in self.links: # id-matched link - version.link = self.links.pop(version.link_id) + version.link = self.links[version.link_id] # strip whitespace from header self.header = _join_markdown(header_segments) @@ -261,10 +261,8 @@ class Changelog: if path is None: path = self.path - v_links = {} - v_links.update(self.links) - segments = [self.header] + v_links = {**self.links} for version in self.versions: if version.link: @@ -272,8 +270,7 @@ class Changelog: segments.append(version.text()) - for link_id, link in v_links.items(): - segments.append(f'[{link_id.lower()}]: {link}') + segments += [f'[{link_id.lower()}]: {link}' for link_id, link in v_links.items()] text = _join_markdown(segments)