Add unit tests for parser

This commit is contained in:
Andrew Cassidy 2021-04-25 02:16:19 -07:00
parent 358942c858
commit be78167b4b
6 changed files with 142 additions and 10 deletions

View File

@ -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

View File

@ -37,3 +37,6 @@ packages = find:
[options.entry_points]
console_scripts =
yaclog = yaclog.cli.__main__:cli
[options.packages.find]
exclude = tests.*

64
tests/Test-Changelog.md Normal file
View File

@ -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

0
tests/__init__.py Normal file
View File

62
tests/test_changelog.py Normal file
View File

@ -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()

View File

@ -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)